Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
agrawal-d committed Apr 11, 2024
1 parent f5536f8 commit 333c3dd
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 19 deletions.
10 changes: 5 additions & 5 deletions compiler/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ fn get_rules() -> &'static HashMap<TokenType, ParseRule> {
add_rule!(map, Error, None, None, Precedence::None);
add_rule!(map, EOF, None, None, Precedence::None);

return map;
map
})
}

Expand Down Expand Up @@ -204,7 +204,7 @@ impl Compiler {
compiler.expression();
compiler.parser.consume(TokenType::EOF, "Expect end of expression.");
compiler.end();
return Ok(compiler.compiling_chunk);
Ok(compiler.compiling_chunk)
}

#[cfg(not(feature = "print_code"))]
Expand Down Expand Up @@ -236,7 +236,7 @@ impl Compiler {
TokenType::GreaterEqual => self.emit_bytes(Opcode::Less as u8, Opcode::Not as u8),
TokenType::Less => self.emit_byte(Opcode::Less as u8),
TokenType::LessEqual => self.emit_bytes(Opcode::Greater as u8, Opcode::Not as u8),
_ => return,
_ => (),
}
}

Expand All @@ -245,7 +245,7 @@ impl Compiler {
TokenType::False => self.emit_byte(Opcode::False as u8),
TokenType::Nil => self.emit_byte(Opcode::Nil as u8),
TokenType::True => self.emit_byte(Opcode::True as u8),
_ => return,
_ => (),
}
}

Expand Down Expand Up @@ -276,7 +276,7 @@ impl Compiler {
match operator_type {
TokenType::Minus => self.emit_byte(Opcode::Negate as u8),
TokenType::Bang => self.emit_byte(Opcode::Not as u8),
_ => return,
_ => (),
}
}

Expand Down
4 changes: 2 additions & 2 deletions compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ static LOGGER: OnceLock<Logger> = OnceLock::new();
#[macro_export]
macro_rules! xprint {
($($arg:tt)*) => {
(crate::LOGGER.get().expect("Compiler not initialized").print_fn)(format!($($arg)*))
($crate::LOGGER.get().expect("Compiler not initialized").print_fn)(format!($($arg)*))
}
}

#[macro_export]
macro_rules! xprintln {
($($arg:tt)*) => {
(crate::LOGGER.get().expect("Compiler not initialized").println_fn)(format!($($arg)*))
($crate::LOGGER.get().expect("Compiler not initialized").println_fn)(format!($($arg)*))
}
}

Expand Down
6 changes: 6 additions & 0 deletions compiler/src/scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ pub struct Token {
pub line: usize,
}

impl Default for Token {
fn default() -> Self {
Self::new()
}
}

impl Token {
pub fn new() -> Token {
Token {
Expand Down
16 changes: 9 additions & 7 deletions compiler/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ pub fn print_value(&self, value: Value) {

use Value::*;

pub fn values_equal(a: Value, b: Value) -> bool {
match (a, b) {
(Number(a), Number(b)) => a == b,
(Bool(a), Bool(b)) => a == b,
(XString(a), XString(b)) => a.as_ref() == b.as_ref(),
(Nil, Nil) => true,
_ => false,
impl PartialEq<Value> for Value {
fn eq(&self, other: &Value) -> bool {
match (self, other) {
(Number(a), Number(b)) => a == b,
(Bool(a), Bool(b)) => a == b,
(XString(a), XString(b)) => a.as_ref() == b.as_ref(),
(Nil, Nil) => true,
_ => false,
}
}
}
7 changes: 2 additions & 5 deletions compiler/src/vm.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use crate::{
chunk::Chunk,
common::Opcode,
value::{
values_equal,
Value::{self, *},
},
value::Value::{self, *},
xprintln,
};
use anyhow::*;
Expand Down Expand Up @@ -147,7 +144,7 @@ impl Vm {
Opcode::Equal => {
let a = vm.pop()?;
let b = vm.pop()?;
vm.stack.push(Bool(values_equal(a, b)))
vm.stack.push(Bool(a == b))
}
Opcode::Greater => binop!(vm, Bool, >),
Opcode::Less => binop!(vm, Bool, <),
Expand Down

0 comments on commit 333c3dd

Please sign in to comment.