yield and sleep
This commit is contained in:
@@ -111,7 +111,7 @@ fn test_while_loop() -> anyhow::Result<()> {
|
||||
fn test_loop_continue() -> anyhow::Result<()> {
|
||||
let compiled = compile! {
|
||||
debug
|
||||
"
|
||||
r#"
|
||||
let a = 0;
|
||||
loop {
|
||||
a = a + 1;
|
||||
@@ -120,7 +120,7 @@ fn test_loop_continue() -> anyhow::Result<()> {
|
||||
}
|
||||
break;
|
||||
}
|
||||
"
|
||||
"#
|
||||
};
|
||||
|
||||
// Labels: L1 (start), L2 (end), L3 (if end)
|
||||
|
||||
@@ -47,3 +47,4 @@ mod declaration_literal;
|
||||
mod function_declaration;
|
||||
mod logic_expression;
|
||||
mod loops;
|
||||
mod syscall;
|
||||
|
||||
53
libs/compiler/src/test/syscall.rs
Normal file
53
libs/compiler/src/test/syscall.rs
Normal file
@@ -0,0 +1,53 @@
|
||||
use crate::compile;
|
||||
use indoc::indoc;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
#[test]
|
||||
fn test_yield_syscall() -> anyhow::Result<()> {
|
||||
let compiled = compile! {
|
||||
debug
|
||||
"
|
||||
yield();
|
||||
"
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
compiled,
|
||||
indoc! {
|
||||
"
|
||||
j main
|
||||
main:
|
||||
yield
|
||||
"
|
||||
}
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sleep_syscall() -> anyhow::Result<()> {
|
||||
let compiled = compile! {
|
||||
debug
|
||||
"
|
||||
sleep(3);
|
||||
let sleepAmount = 15;
|
||||
sleep(sleepAmount);
|
||||
"
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
compiled,
|
||||
indoc! {
|
||||
"
|
||||
j main
|
||||
main:
|
||||
sleep 3
|
||||
move r8 15 #sleepAmount
|
||||
sleep r8
|
||||
"
|
||||
}
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -1,10 +1,11 @@
|
||||
use crate::variable_manager::{self, LocationRequest, VariableLocation, VariableScope};
|
||||
use parser::{
|
||||
Parser as ASTParser,
|
||||
sys_call::{SysCall, System},
|
||||
tree_node::{
|
||||
AssignmentExpression, BinaryExpression, BlockExpression, DeviceDeclarationExpression,
|
||||
Expression, FunctionExpression, IfExpression, InvocationExpression, Literal,
|
||||
LogicalExpression, LoopExpression, WhileExpression,
|
||||
LiteralOrVariable, LogicalExpression, LoopExpression, WhileExpression,
|
||||
},
|
||||
};
|
||||
use quick_error::quick_error;
|
||||
@@ -152,6 +153,13 @@ impl<'a, W: std::io::Write> Compiler<'a, W> {
|
||||
self.expression_loop(expr_loop, scope)?;
|
||||
Ok(None)
|
||||
}
|
||||
Expression::Syscall(SysCall::System(system_syscall)) => {
|
||||
let res = self.expression_syscall_system(system_syscall, scope)?;
|
||||
Ok(res.map(|l| CompilationResult {
|
||||
location: l,
|
||||
temp_name: None,
|
||||
}))
|
||||
}
|
||||
Expression::While(expr_while) => {
|
||||
self.expression_while(expr_while, scope)?;
|
||||
Ok(None)
|
||||
@@ -740,6 +748,18 @@ impl<'a, W: std::io::Write> Compiler<'a, W> {
|
||||
}
|
||||
}
|
||||
|
||||
fn compile_literal_or_variable(
|
||||
&mut self,
|
||||
val: LiteralOrVariable,
|
||||
scope: &mut VariableScope,
|
||||
) -> Result<(String, Option<String>), Error> {
|
||||
let expr = match val {
|
||||
LiteralOrVariable::Literal(l) => Expression::Literal(l),
|
||||
LiteralOrVariable::Variable(v) => Expression::Variable(v),
|
||||
};
|
||||
self.compile_operand(expr, scope)
|
||||
}
|
||||
|
||||
fn expression_binary<'v>(
|
||||
&mut self,
|
||||
expr: BinaryExpression,
|
||||
@@ -978,6 +998,31 @@ impl<'a, W: std::io::Write> Compiler<'a, W> {
|
||||
Ok(VariableLocation::Persistant(VariableScope::RETURN_REGISTER))
|
||||
}
|
||||
|
||||
fn expression_syscall_system<'v>(
|
||||
&mut self,
|
||||
expr: System,
|
||||
scope: &mut VariableScope<'v>,
|
||||
) -> Result<Option<VariableLocation>, Error> {
|
||||
match expr {
|
||||
System::Yield => {
|
||||
self.write_output("yield")?;
|
||||
Ok(None)
|
||||
}
|
||||
System::Sleep(amt) => {
|
||||
let (var, cleanup) = self.compile_literal_or_variable(amt, scope)?;
|
||||
self.write_output(format!("sleep {var}"))?;
|
||||
if let Some(temp) = cleanup {
|
||||
scope.free_temp(temp)?;
|
||||
}
|
||||
|
||||
Ok(None)
|
||||
}
|
||||
_ => {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Compile a function declaration.
|
||||
/// Calees are responsible for backing up any registers they wish to use.
|
||||
fn expression_function<'v>(
|
||||
@@ -1092,4 +1137,3 @@ impl<'a, W: std::io::Write> Compiler<'a, W> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user