From 68e56af987e08f292316c83001ba3ba49b61f5f5 Mon Sep 17 00:00:00 2001 From: Devin Bidwell Date: Tue, 25 Nov 2025 02:27:28 -0700 Subject: [PATCH] yield and sleep --- libs/compiler/src/test/loops.rs | 4 +-- libs/compiler/src/test/mod.rs | 1 + libs/compiler/src/test/syscall.rs | 53 +++++++++++++++++++++++++++++++ libs/compiler/src/v1.rs | 48 ++++++++++++++++++++++++++-- 4 files changed, 102 insertions(+), 4 deletions(-) create mode 100644 libs/compiler/src/test/syscall.rs diff --git a/libs/compiler/src/test/loops.rs b/libs/compiler/src/test/loops.rs index c421a99..b9c38e3 100644 --- a/libs/compiler/src/test/loops.rs +++ b/libs/compiler/src/test/loops.rs @@ -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) diff --git a/libs/compiler/src/test/mod.rs b/libs/compiler/src/test/mod.rs index 47893fc..6a4ba07 100644 --- a/libs/compiler/src/test/mod.rs +++ b/libs/compiler/src/test/mod.rs @@ -47,3 +47,4 @@ mod declaration_literal; mod function_declaration; mod logic_expression; mod loops; +mod syscall; diff --git a/libs/compiler/src/test/syscall.rs b/libs/compiler/src/test/syscall.rs new file mode 100644 index 0000000..46eb7dd --- /dev/null +++ b/libs/compiler/src/test/syscall.rs @@ -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(()) +} diff --git a/libs/compiler/src/v1.rs b/libs/compiler/src/v1.rs index 7329091..59475be 100644 --- a/libs/compiler/src/v1.rs +++ b/libs/compiler/src/v1.rs @@ -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), 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, 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(()) } } -