diff --git a/compiler/src/lib.rs b/compiler/src/lib.rs index bcb51e9..64d4274 100644 --- a/compiler/src/lib.rs +++ b/compiler/src/lib.rs @@ -1,7 +1,7 @@ use std::rc::Rc; use anyhow::Error; -use lexer::token::TokenType; +use lexer::token::{TokenType, Token}; use opcode::{Instructions, Opcode}; use parser::ast::{ BlockStatement, BooleanLiteral, Expression, IntegerLiteral, Literal, Node, Statement, @@ -257,10 +257,10 @@ impl Compiler { &mut self, left: &Box, right: &Box, - operator: &str, + operator: Token, ) -> Result<(), Error> { - match operator { - "<" => { + match operator.token_type { + TokenType::Lt => { self.compile_expression(right)?; self.compile_expression(left)?; } @@ -369,17 +369,11 @@ impl Compiler { Ok(()) } Expression::Infix(infix_expression) => { - if infix_expression.operator.token_type == TokenType::Lt { - self.compile_expression(&infix_expression.right)?; - self.compile_expression(&infix_expression.left)?; - - self.emit(opcode::Opcode::OpGreaterThan, vec![]); - - return Ok(()); - } - - self.compile_expression(&infix_expression.left)?; - self.compile_expression(&infix_expression.right)?; + self.compile_operands( + &infix_expression.left, + &infix_expression.right, + infix_expression.operator.clone() + )?; match infix_expression.operator.token_type { TokenType::Plus => self.emit(opcode::Opcode::OpAdd, vec![]), diff --git a/compiler/tests/symbol_table_tests.rs b/compiler/tests/symbol_table_tests.rs index f410c3f..dc836cf 100644 --- a/compiler/tests/symbol_table_tests.rs +++ b/compiler/tests/symbol_table_tests.rs @@ -26,18 +26,35 @@ fn test_define() -> Result<(), Error> { "c".to_string(), Symbol { name: "c".to_string(), - scope: SymbolScope::Global, - index: 2, + scope: SymbolScope::Local, + index: 0, }, ), ( "d".to_string(), Symbol { name: "d".to_string(), - scope: SymbolScope::Global, - index: 3, + scope: SymbolScope::Local, + index: 1, }, ), + ( + "e".to_string(), + Symbol { + name: "e".to_string(), + scope: SymbolScope::Local, + index: 0, + }, + ), + ( + "f".to_string(), + Symbol { + name: "f".to_string(), + scope: SymbolScope::Local, + index: 1, + }, + ), + ]); let mut global = SymbolTable::new(); @@ -54,6 +71,34 @@ fn test_define() -> Result<(), Error> { panic!("b.name not 'b'. got={}", b.name); } + let mut first_local = SymbolTable::new_enclosed(global.clone()); + + let c = first_local.define("c"); + + if c.name != "c" { + panic!("c.name not 'c'. got={}", c.name); + } + + let d = first_local.define("d"); + + if d.name != "d" { + panic!("d.name not 'd'. got={}", d.name); + } + + let mut second_local = SymbolTable::new_enclosed(first_local.clone()); + + let e = second_local.define("e"); + + if e.name != "e" { + panic!("e.name not 'e'. got={}", e.name); + } + + let f = second_local.define("f"); + + if f.name != "f" { + panic!("f.name not 'f'. got={}", f.name); + } + Ok(()) } diff --git a/interpreter/src/repl.rs b/interpreter/src/repl.rs index 8f2d0cd..8fbfacf 100644 --- a/interpreter/src/repl.rs +++ b/interpreter/src/repl.rs @@ -48,7 +48,6 @@ pub fn init_repl() -> Result<(), Error> { match vm.run() { Ok(_) => { let last_popped = vm.last_popped_stack_elem(); - println!("{}", last_popped); } Err(err) => { println!("Error: {}", err);