yield and sleep

This commit is contained in:
2025-11-25 02:27:28 -07:00
parent 9bc5cac2c2
commit 68e56af987
4 changed files with 102 additions and 4 deletions

View File

@@ -111,7 +111,7 @@ fn test_while_loop() -> anyhow::Result<()> {
fn test_loop_continue() -> anyhow::Result<()> { fn test_loop_continue() -> anyhow::Result<()> {
let compiled = compile! { let compiled = compile! {
debug debug
" r#"
let a = 0; let a = 0;
loop { loop {
a = a + 1; a = a + 1;
@@ -120,7 +120,7 @@ fn test_loop_continue() -> anyhow::Result<()> {
} }
break; break;
} }
" "#
}; };
// Labels: L1 (start), L2 (end), L3 (if end) // Labels: L1 (start), L2 (end), L3 (if end)

View File

@@ -47,3 +47,4 @@ mod declaration_literal;
mod function_declaration; mod function_declaration;
mod logic_expression; mod logic_expression;
mod loops; mod loops;
mod syscall;

View 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(())
}

View File

@@ -1,10 +1,11 @@
use crate::variable_manager::{self, LocationRequest, VariableLocation, VariableScope}; use crate::variable_manager::{self, LocationRequest, VariableLocation, VariableScope};
use parser::{ use parser::{
Parser as ASTParser, Parser as ASTParser,
sys_call::{SysCall, System},
tree_node::{ tree_node::{
AssignmentExpression, BinaryExpression, BlockExpression, DeviceDeclarationExpression, AssignmentExpression, BinaryExpression, BlockExpression, DeviceDeclarationExpression,
Expression, FunctionExpression, IfExpression, InvocationExpression, Literal, Expression, FunctionExpression, IfExpression, InvocationExpression, Literal,
LogicalExpression, LoopExpression, WhileExpression, LiteralOrVariable, LogicalExpression, LoopExpression, WhileExpression,
}, },
}; };
use quick_error::quick_error; use quick_error::quick_error;
@@ -152,6 +153,13 @@ impl<'a, W: std::io::Write> Compiler<'a, W> {
self.expression_loop(expr_loop, scope)?; self.expression_loop(expr_loop, scope)?;
Ok(None) 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) => { Expression::While(expr_while) => {
self.expression_while(expr_while, scope)?; self.expression_while(expr_while, scope)?;
Ok(None) 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>( fn expression_binary<'v>(
&mut self, &mut self,
expr: BinaryExpression, expr: BinaryExpression,
@@ -978,6 +998,31 @@ impl<'a, W: std::io::Write> Compiler<'a, W> {
Ok(VariableLocation::Persistant(VariableScope::RETURN_REGISTER)) 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. /// Compile a function declaration.
/// Calees are responsible for backing up any registers they wish to use. /// Calees are responsible for backing up any registers they wish to use.
fn expression_function<'v>( fn expression_function<'v>(
@@ -1092,4 +1137,3 @@ impl<'a, W: std::io::Write> Compiler<'a, W> {
Ok(()) Ok(())
} }
} }