From 93e6d490a0c727a7fac6ff52936f2c453b8c62cd Mon Sep 17 00:00:00 2001 From: ElementG9 Date: Mon, 24 Aug 2020 21:16:39 -0600 Subject: [PATCH] Add booleans --- src/interpreter.rs | 1 + src/parser.rs | 10 ++++++++++ src/tokenizer.rs | 7 ++++++- test.pvt | 4 ++++ 4 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/interpreter.rs b/src/interpreter.rs index 02cc0a2..59ff13d 100644 --- a/src/interpreter.rs +++ b/src/interpreter.rs @@ -18,6 +18,7 @@ pub fn interpret(ast: Program) { Literal::FloatLiteral(f) => { args.push(format!("{}.{}f", f.trunc(), f.fract())) } + Literal::BooleanLiteral(b) => args.push(format!("{}", b)), }, Expression::Null => { args.push("null".to_owned()); diff --git a/src/parser.rs b/src/parser.rs index bba55fb..1a0d5bc 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -90,6 +90,15 @@ fn parse_expression(tokens: &Vec, current: &mut usize) -> Expression { let out = Expression::Literal(Literal::FloatLiteral(val)); *current += 1; out + } else if tokens[*current].kind == TokenKind::BooleanLiteral { + let mut val = false; + if tokens[*current].value == "true" { + val = true; + } else if tokens[*current].value == "false" { + val = false; + } + *current += 1; + Expression::Literal(Literal::BooleanLiteral(val)) } else { Expression::Null } @@ -114,4 +123,5 @@ pub enum Literal { StringLiteral(String), IntLiteral(i32), FloatLiteral(f32), + BooleanLiteral(bool), } diff --git a/src/tokenizer.rs b/src/tokenizer.rs index 69740a3..e6b2e1c 100644 --- a/src/tokenizer.rs +++ b/src/tokenizer.rs @@ -4,6 +4,7 @@ pub enum TokenKind { StringLiteral, IntLiteral, FloatLiteral, + BooleanLiteral, LeftParen, RightParen, Semicolon, @@ -77,8 +78,12 @@ fn read_identifier(chars: &Vec, current: &mut usize, tokens: &mut Vec