From 2ff43643cfabc8378d5e001a9c43a739ef755106 Mon Sep 17 00:00:00 2001 From: ghostly_zsh Date: Tue, 13 Aug 2024 02:54:57 -0500 Subject: [PATCH] silly error, opcodes were wrong in opcodes.rs --- kabel/src/codegen.rs | 30 +++++++++++++++++++++++++----- kabel/src/main.rs | 10 +++++----- kabel/src/opcodes.rs | 14 +++++++------- kabel/src/vm.rs | 4 +++- 4 files changed, 40 insertions(+), 18 deletions(-) diff --git a/kabel/src/codegen.rs b/kabel/src/codegen.rs index 0610aa47febcc1a16cdd6e5aac5fa92ad6a690df..47a1f39ff0d045f21cf4c1a85c564aa90e0d6ba8 100644 --- a/kabel/src/codegen.rs +++ b/kabel/src/codegen.rs @@ -10,7 +10,7 @@ impl Codegen { pub fn new(text: String) -> Self { Codegen { vm: VM::new(Vec::new(), Vec::new(), - Vec::new(), HashMap::new(), text.lines().map(|s| s.to_string()).collect()), + Vec::new(), HashMap::new(), text), } } pub fn visit(&mut self, ast: AST) { @@ -30,8 +30,8 @@ impl Codegen { Decl(ref name, ref expr) => { self.visit_decl(&ast, name.clone(), *expr.clone()); } - Expr(expr) => { - self.visit_expr_stmt(*expr); + Expr(ref expr) => { + self.visit_expr_stmt(&ast, *expr.clone()); } // REMOVE LATER Print(ref expr) => { @@ -50,11 +50,16 @@ impl Codegen { } } pub fn visit_if(&mut self, condition: AST, block: AST, else_expr: Option) { - self.visit(condition); + self.visit(condition.clone()); self.vm.chunk.push(OpCode::IF_NE.into()); self.vm.chunk.push(0xFF); // placeholder self.vm.chunk.push(0xFF); // placeholder let start_jmp_loc = self.vm.chunk.len()-2; + if self.vm.lines.last().unwrap().0 != condition.end_line { + self.vm.lines.push((condition.end_line, 3)); + } else { + self.vm.lines.last_mut().unwrap().1 += 3; + } self.visit(block); if let Some(ast) = else_expr { match ast.ast_type { @@ -63,6 +68,11 @@ impl Codegen { self.vm.chunk.push(0xFF); // placeholder self.vm.chunk.push(0xFF); // placeholder let end_jmp_loc = self.vm.chunk.len()-2; + if self.vm.lines.last().unwrap().0 != ast.end_line { + self.vm.lines.push((ast.end_line, 3)); + } else { + self.vm.lines.last_mut().unwrap().1 += 3; + } self.patch_jump(start_jmp_loc); self.visit(ast); @@ -73,6 +83,11 @@ impl Codegen { self.vm.chunk.push(OpCode::JMP.into()); self.vm.chunk.push(0xFF); // placeholder self.vm.chunk.push(0xFF); // placeholder + if self.vm.lines.last().unwrap().0 != ast.end_line { + self.vm.lines.push((ast.end_line, 3)); + } else { + self.vm.lines.last_mut().unwrap().1 += 3; + } let end_jmp_loc = self.vm.chunk.len()-2; self.patch_jump(start_jmp_loc); // jmp to else @@ -97,9 +112,14 @@ impl Codegen { self.vm.local.insert(name.name, ptr as u8); } } - pub fn visit_expr_stmt(&mut self, expr: AST) { + pub fn visit_expr_stmt(&mut self, ast: &AST, expr: AST) { self.visit(expr); self.vm.chunk.push(OpCode::POP.into()); + if self.vm.lines.last().unwrap().0 != ast.end_line { + self.vm.lines.push((ast.end_line, 1)); + } else { + self.vm.lines.last_mut().unwrap().1 += 1; + } } // REMOVE LATER pub fn visit_print(&mut self, ast: &AST, expr: AST) { diff --git a/kabel/src/main.rs b/kabel/src/main.rs index 43fe9fb59da5cbaea034c1250736049bb44611ee..b8200ad9d0a23f9be2302791a18d01c433658976 100644 --- a/kabel/src/main.rs +++ b/kabel/src/main.rs @@ -5,11 +5,11 @@ use std::{env, fs}; use kabel::{debug::{debug_ast, debug_bytecode, debug_stack, debug_token_array}, run_codegen, run_lexer, run_parser, run_semantic_analysis}; fn main() { - /*let args: Vec = env::args().collect(); + let args: Vec = env::args().collect(); let program = - fs::read_to_string(args[1].clone()).unwrap();*/ + fs::read_to_string(args[1].clone()).unwrap(); - let program = + /*let program = "var a = 2; var b = 3; print a+b; @@ -18,7 +18,7 @@ print a+b; var c = 4; print c; } -".to_string(); +".to_string();*/ let mut output = "".to_string(); @@ -59,7 +59,7 @@ print a+b; } //output += &format!("{:#?}", ast); - let codegen = run_codegen(program, ast); + let codegen = run_codegen(program.to_string(), ast); let mut vm = codegen.vm; output += &debug_bytecode(&vm.chunk); diff --git a/kabel/src/opcodes.rs b/kabel/src/opcodes.rs index 727fb060f47d678bbb93a7824b50d8a0d8baacd1..a2a1018782edd510f1925a67fb0fc88b81afc5b1 100644 --- a/kabel/src/opcodes.rs +++ b/kabel/src/opcodes.rs @@ -47,13 +47,13 @@ impl From for u8 { BITAND => 0x07, BITXOR => 0x08, BITOR => 0x09, - EQ => 0x010, - NE => 0x0A, - GR => 0x0B, - GE => 0x0C, - LS => 0x0D, - LE => 0x0E, - OR => 0x0F, + EQ => 0x0A, + NE => 0x0B, + GR => 0x0C, + GE => 0x0D, + LS => 0x0E, + LE => 0x0F, + OR => 0x10, AND => 0x11, NOT => 0x12, diff --git a/kabel/src/vm.rs b/kabel/src/vm.rs index d5939b9d5a81787735294cf4099c8b0a6dfaf415..56f1d92df6b07283ff3274388cf39ac65dc8c694 100644 --- a/kabel/src/vm.rs +++ b/kabel/src/vm.rs @@ -15,6 +15,7 @@ pub struct VM { impl VM { pub fn new(bytecode: Vec, lines: Vec<(usize, usize)>, pool: Vec, local: HashMap, text: String) -> Self { + println!("{:#?}", text); Self { ip: 0, chunk: bytecode, @@ -22,11 +23,12 @@ impl VM { pool, local, lines, - text: text.lines().map(|s| s.to_string()).collect(), + text: text.lines().map(|s| s.to_string()).collect::>(), } } pub fn run(&mut self, output: &mut String) -> Result<(), KabelRuntimeError> { use Value::*; + println!("{:#?}", self.text); while self.ip < self.chunk.len() { match self.read() { 0x00 => { // LOAD