This commit is contained in:
2025-06-17 10:06:55 -07:00
parent 94dfd5ec83
commit 006e3cdf02
4 changed files with 31 additions and 2 deletions

View File

@@ -16,7 +16,7 @@ name = "slang"
path = "src/main.rs" path = "src/main.rs"
[lib] [lib]
name = "slanglib" name = "slang"
path = "src/lib.rs" path = "src/lib.rs"
crate-type = ["staticlib"] crate-type = ["staticlib"]

View File

@@ -1,3 +1,6 @@
#[cfg(test)]
mod test;
use parser::Parser as ASTParser; use parser::Parser as ASTParser;
use parser::sys_call::SysCall; use parser::sys_call::SysCall;
use parser::tree_node::*; use parser::tree_node::*;
@@ -104,6 +107,19 @@ impl<'a> Compiler<'a> {
Ok(()) 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<i32, CompileError> {
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<String>) -> Result<(), CompileError> { fn write_output(&mut self, output: impl Into<String>) -> Result<(), CompileError> {
self.output.write_all(output.into().as_bytes())?; self.output.write_all(output.into().as_bytes())?;
self.output.write_all(b"\n")?; self.output.write_all(b"\n")?;
@@ -281,6 +297,7 @@ impl<'a> Compiler<'a> {
fn invocation_expression(&mut self, expr: InvocationExpression) -> Result<(), CompileError> { fn invocation_expression(&mut self, expr: InvocationExpression) -> Result<(), CompileError> {
let function_name = expr.name; let function_name = expr.name;
let args_count = expr.arguments.len();
let function_line = *self let function_line = *self
.function_locations .function_locations
@@ -307,7 +324,7 @@ impl<'a> Compiler<'a> {
self.binary_expression(expr)?; self.binary_expression(expr)?;
to_write.push_str("push r0\n"); 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}"))?; 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.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(()) Ok(())
} }

View File

View File

@@ -0,0 +1,7 @@
fn addTemperatures(temp1, temp2) {
let toReturn = temp1 + temp2;
};
addTemperatures(15c, 120c);
addTemperatures(1500f, 20c);