From 006e3cdf02db45b3d6dfd944ed6a01ce3eee9128 Mon Sep 17 00:00:00 2001 From: Devin Bidwell Date: Tue, 17 Jun 2025 10:06:55 -0700 Subject: [PATCH] wip --- Cargo.toml | 2 +- libs/compiler/src/lib.rs | 24 +++++++++++++++++++++++- libs/compiler/src/test/mod.rs | 0 libs/compiler/test_files/math.slang | 7 +++++++ 4 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 libs/compiler/src/test/mod.rs create mode 100644 libs/compiler/test_files/math.slang diff --git a/Cargo.toml b/Cargo.toml index f5275d2..2898524 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,7 +16,7 @@ name = "slang" path = "src/main.rs" [lib] -name = "slanglib" +name = "slang" path = "src/lib.rs" crate-type = ["staticlib"] diff --git a/libs/compiler/src/lib.rs b/libs/compiler/src/lib.rs index 95df7c0..de54a6d 100644 --- a/libs/compiler/src/lib.rs +++ b/libs/compiler/src/lib.rs @@ -1,3 +1,6 @@ +#[cfg(test)] +mod test; + use parser::Parser as ASTParser; use parser::sys_call::SysCall; use parser::tree_node::*; @@ -104,6 +107,19 @@ impl<'a> Compiler<'a> { Ok(()) } + /// Pop the given variable from the current stack. Errors if the variable is not found in the + /// current scope. + fn pop_current(&mut self, var_name: &str) -> Result { + let last_scope = self + .variable_scope + .last_mut() + .ok_or(CompileError::ScopeError)?; + + last_scope + .remove(var_name) + .ok_or(CompileError::VariableNotFound(var_name.to_string())) + } + fn write_output(&mut self, output: impl Into) -> Result<(), CompileError> { self.output.write_all(output.into().as_bytes())?; self.output.write_all(b"\n")?; @@ -281,6 +297,7 @@ impl<'a> Compiler<'a> { fn invocation_expression(&mut self, expr: InvocationExpression) -> Result<(), CompileError> { let function_name = expr.name; + let args_count = expr.arguments.len(); let function_line = *self .function_locations @@ -307,7 +324,7 @@ impl<'a> Compiler<'a> { self.binary_expression(expr)?; to_write.push_str("push r0\n"); } - _ => todo!("something is up with the arguments"), + _ => todo!("something is up with the arguments: {arg:?}"), } self.push_stack(&format!("{function_name}Invocation{iter_index}"))?; } @@ -320,6 +337,11 @@ impl<'a> Compiler<'a> { self.write_output(format!("j {function_line}"))?; + self.pop_current(&format!("{function_name}ReturnAddress"))?; + for i in 0..args_count { + self.pop_current(&format!("{function_name}Invocation{i}"))?; + } + Ok(()) } diff --git a/libs/compiler/src/test/mod.rs b/libs/compiler/src/test/mod.rs new file mode 100644 index 0000000..e69de29 diff --git a/libs/compiler/test_files/math.slang b/libs/compiler/test_files/math.slang new file mode 100644 index 0000000..2fa5adc --- /dev/null +++ b/libs/compiler/test_files/math.slang @@ -0,0 +1,7 @@ +fn addTemperatures(temp1, temp2) { + let toReturn = temp1 + temp2; +}; + + +addTemperatures(15c, 120c); +addTemperatures(1500f, 20c);