Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ pub enum BinaryOperation {
NotEqual,
And,
Or,
GreaterThanEquals,
LessThanEquals
}

#[derive(Debug, Clone)]
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ pub enum Instruction {
Divide,
Negate,
Greater,
GreaterThanEquals,
LessThanEquals,
Less,
Equal,
NotEqual,
Expand Down Expand Up @@ -92,6 +94,8 @@ fn compile_expr(instructions: &mut Vec<Instruction>, expr: &Expression) {
instructions[jump_instructions_index] =
Instruction::JumpIfTrue(instructions.len());
}
BinaryOperation::GreaterThanEquals => instructions.push(Instruction::GreaterThanEquals),
BinaryOperation::LessThanEquals => instructions.push(Instruction::LessThanEquals),
}
}
Expression::Unary { op, expr } => {
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ fn main() {
let filename = &run_args[2];
let source = std::fs::read_to_string(filename).expect("Failed to read source file");
let tokens = tokenize(&source);
// println!("{:?}", tokens);
let mut parser = Parser::new(tokens);
let ast = parser.parse();
// println!("Parsed AST: {:#?}", ast);
Expand Down
77 changes: 48 additions & 29 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,6 @@ fn is_arithmetic_operator(tkn: &Token) -> Option<BinaryOperation> {
}
}

fn is_compare_operator(tkn: &Token) -> Option<BinaryOperation> {
match tkn {
Token::Greater => Some(BinaryOperation::Greater),
Token::Less => Some(BinaryOperation::Less),
Token::EqualEqual => Some(BinaryOperation::Equal),
Token::NotEqual => Some(BinaryOperation::NotEqual),
_ => None,
}
}

impl Parser {
pub fn new(tokens: Vec<Token>) -> Self {
Self { tokens, pos: 0 }
Expand All @@ -51,6 +41,54 @@ impl Parser {
statements
}

fn parse_comparison(&mut self) -> Expression {
let mut left = self.parse_expression();

loop {
let op = match self.peek() {
Some(Token::Greater) => {
self.advance();
if let Some(Token::Equals) = self.peek() {
self.advance();
Some(BinaryOperation::GreaterThanEquals)
} else {
Some(BinaryOperation::Greater)
}
}
Some(Token::Less) => {
self.advance();
if let Some(Token::Equals) = self.peek() {
self.advance();
Some(BinaryOperation::LessThanEquals)
} else {
Some(BinaryOperation::Less)
}
}
Some(Token::EqualEqual) => {
self.advance();
Some(BinaryOperation::Equal)
}
Some(Token::NotEqual) => {
self.advance();
Some(BinaryOperation::NotEqual)
}
_ => None,
};

if let Some(opr) = op {
let right = self.parse_expression();
left = Expression::Binary {
left: Box::new(left),
op: opr,
right: Box::new(right),
};
} else {
break;
}
}
left
}

fn parse_statement(&mut self) -> Statement {
match self.peek() {
Some(Token::Let) => self.parse_var_decl(),
Expand Down Expand Up @@ -280,25 +318,6 @@ impl Parser {
left
}

fn parse_comparison(&mut self) -> Expression {
let mut left = self.parse_expression();

while let Some(token) = self.peek() {
if let Some(opr) = is_compare_operator(token) {
self.advance();
let right = self.parse_expression();
left = Expression::Binary {
left: Box::new(left),
op: opr,
right: Box::new(right),
};
} else {
break;
}
}
left
}

fn parse_expression(&mut self) -> Expression {
let mut left = self.parse_term();

Expand Down
26 changes: 26 additions & 0 deletions src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,26 @@ impl Runtime {
panic!("Greater comparison is only supported between values of the same type");
}
}
BinaryOperation::GreaterThanEquals => {
if matches!(
(&value1, &value2),
(Value::Number(_), Value::Number(_)) | (Value::String(_), Value::String(_))
) {
value2 >= value1
} else {
panic!("Greater comparison is only supported between values of the same type");
}
}
BinaryOperation::LessThanEquals => {
if matches!(
(&value1, &value2),
(Value::Number(_), Value::Number(_)) | (Value::String(_), Value::String(_))
) {
value2 <= value1
} else {
panic!("Less comparison is only supported between values of the same type");
}
}
BinaryOperation::Less => {
if matches!(
(&value1, &value2),
Expand Down Expand Up @@ -246,6 +266,12 @@ pub fn execute(program: Program, runtime: &mut Runtime) {
Instruction::Greater => {
runtime.compare_opr(BinaryOperation::Greater);
}
Instruction::GreaterThanEquals => {
runtime.compare_opr(BinaryOperation::GreaterThanEquals);
}
Instruction::LessThanEquals => {
runtime.compare_opr(BinaryOperation::LessThanEquals);
}
Instruction::Less => {
runtime.compare_opr(BinaryOperation::Less);
}
Expand Down
28 changes: 28 additions & 0 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1358,3 +1358,31 @@ print obj.inner["val"];
let out = run_rts(code);
assert_eq!(out, "5");
}

#[test]
fn test_greater_than_equals() {
let code = r#"
let a = 10;
let b = 10;
let c = 9;
print a >= b;
print a >= c;
print c >= a;
"#;
let out = run_rts(code);
assert_eq!(out, "1\n1\n0");
}

#[test]
fn test_less_than_equals() {
let code = r#"
let a = 10;
let b = 10;
let c = 11;
print a <= b;
print a <= c;
print c <= a;
"#;
let out = run_rts(code);
assert_eq!(out, "1\n1\n0");
}
Loading