From 3902b5ca76bc7846c4d70719d230c49dd23d439b Mon Sep 17 00:00:00 2001 From: Devin Bidwell Date: Sun, 1 Dec 2024 18:45:48 -0700 Subject: [PATCH] wip --- output.ic10 | 14 +++++++++++++ src/compiler/mod.rs | 50 ++++++++++++++++++++++++++++++++------------- tests/file.stlg | 9 ++------ 3 files changed, 52 insertions(+), 21 deletions(-) create mode 100644 output.ic10 diff --git a/output.ic10 b/output.ic10 new file mode 100644 index 0000000..2be2f72 --- /dev/null +++ b/output.ic10 @@ -0,0 +1,14 @@ +j main +sub sp sp 1 +pop ra +j ra +main: +push 25 +push 2 +pop r1 +pop r0 +mul r0 r0 r1 +push r0 +push 14 +push r0 +j 1 diff --git a/src/compiler/mod.rs b/src/compiler/mod.rs index 28a3b3a..2b446b8 100644 --- a/src/compiler/mod.rs +++ b/src/compiler/mod.rs @@ -57,12 +57,19 @@ impl<'a> Compiler<'a> { fn get_variable_index(&self, var_name: &str) -> Result { let mut offset = 0; + println!("Variable Scope: {:?}", self.variable_scope); + for scope in &self.variable_scope { + let scope_size = scope.len() as i32; if let Some(index) = scope.get(var_name) { - return Ok(*index + offset); + let index = (scope_size - *index) + offset; + + println!("Found variable {} at index {}", var_name, index); + + return Ok(index); } - offset += scope.len() as i32; + offset += scope_size; } Err(CompileError::VariableNotFound(var_name.to_owned())) @@ -168,34 +175,48 @@ impl<'a> Compiler<'a> { match left { Expression::Literal(Literal::Number(num)) => { compiler.write_output(format!("push {num}"))?; + compiler.push_stack(&format!("{op}ExpressionLeft"))?; } Expression::Variable(var_name) => { let var_offset = compiler.get_variable_index(&var_name)?; - compiler.write_output(format!("sub sp sp {var_offset}"))?; - compiler.write_output("peek r0")?; - compiler.write_output(format!("add sp sp {var_offset}"))?; - compiler.write_output("push r0")?; + compiler.write_output(format!("sub r15 sp {var_offset}"))?; + compiler.write_output("get r15 db r15")?; + compiler.write_output("push r15")?; + compiler.push_stack(&format!("{op}ExpressionLeft"))?; } Expression::BinaryExpression(expr) => { compiler.binary_expression(expr)?; } + Expression::PriorityExpression(expr) => match *expr { + Expression::BinaryExpression(expr) => { + compiler.binary_expression(expr)?; + } + _ => todo!(), + }, _ => todo!(), }; match right { Expression::Literal(Literal::Number(num)) => { compiler.write_output(format!("push {num}"))?; + compiler.push_stack(&format!("{op}ExpressionRight"))?; } Expression::Variable(var_name) => { let var_offset = compiler.get_variable_index(&var_name)?; - compiler.write_output(format!("sub sp sp {}", var_offset + 1))?; - compiler.write_output("peek r0")?; - compiler.write_output(format!("add sp sp {}", var_offset + 1))?; - compiler.write_output("push r0")?; + compiler.write_output(format!("sub r15 sp {}", var_offset))?; + compiler.write_output("get r15 db r15")?; + compiler.write_output("push r15")?; + compiler.push_stack(&format!("{op}ExpressionRight"))?; } Expression::BinaryExpression(expr) => { compiler.binary_expression(expr)?; } + Expression::PriorityExpression(expr) => match *expr { + Expression::BinaryExpression(expr) => { + compiler.binary_expression(expr)?; + } + _ => todo!(), + }, _ => todo!(), }; @@ -238,6 +259,8 @@ impl<'a> Compiler<'a> { let mut to_write = String::new(); + self.push_stack(&format!("{function_name}ReturnAddress"))?; + let mut iter_index = 0; for arg in expr.arguments { match arg { @@ -247,10 +270,9 @@ impl<'a> Compiler<'a> { Expression::Variable(var_name) => { let index = self.get_variable_index(&var_name)?; - to_write.push_str(&format!("sub sp sp {index}\n")); - to_write.push_str("peek r0\n"); - to_write.push_str(&format!("add sp sp {index}\n")); - to_write.push_str("push r0\n"); + to_write.push_str(&format!("sub r15 sp {index}\n")); + to_write.push_str("get r15 db r15\n"); + to_write.push_str("push r15\n"); } Expression::BinaryExpression(expr) => { self.binary_expression(expr)?; diff --git a/tests/file.stlg b/tests/file.stlg index 82e1567..341aa7a 100644 --- a/tests/file.stlg +++ b/tests/file.stlg @@ -1,10 +1,5 @@ -device self = "db"; +fn doStuff(i) { -fn doStuff(x, y, z) { - let i = x + y + z; }; -let i = 25; -let j = i + 25; - -doStuff(i, j, 100); \ No newline at end of file +doStuff(25 * 2); \ No newline at end of file