Skip to content

Commit

Permalink
chore: extended symbol table define test
Browse files Browse the repository at this point in the history
  • Loading branch information
ZakFarmer committed Oct 29, 2023
1 parent fedf3f1 commit f9edc09
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 20 deletions.
24 changes: 9 additions & 15 deletions compiler/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -257,10 +257,10 @@ impl Compiler {
&mut self,
left: &Box<Expression>,
right: &Box<Expression>,
operator: &str,
operator: Token,
) -> Result<(), Error> {
match operator {
"<" => {
match operator.token_type {
TokenType::Lt => {
self.compile_expression(right)?;
self.compile_expression(left)?;
}
Expand Down Expand Up @@ -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![]),
Expand Down
53 changes: 49 additions & 4 deletions compiler/tests/symbol_table_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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(())
}

Expand Down
1 change: 0 additions & 1 deletion interpreter/src/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit f9edc09

Please sign in to comment.