M kabel/src/codegen.rs => kabel/src/codegen.rs +25 -5
@@ 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<AST>) {
- 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) {
M kabel/src/main.rs => kabel/src/main.rs +5 -5
@@ 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<String> = env::args().collect();
+ let args: Vec<String> = 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);
M kabel/src/opcodes.rs => kabel/src/opcodes.rs +7 -7
@@ 47,13 47,13 @@ impl From<OpCode> 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,
M kabel/src/vm.rs => kabel/src/vm.rs +3 -1
@@ 15,6 15,7 @@ pub struct VM {
impl VM {
pub fn new(bytecode: Vec<u8>, lines: Vec<(usize, usize)>, pool: Vec<Value>,
local: HashMap<String, u8>, 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::<Vec<String>>(),
}
}
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