Add booleans

This commit is contained in:
ElementG9 2020-08-24 21:16:39 -06:00
parent ceb1615aea
commit 93e6d490a0
4 changed files with 21 additions and 1 deletions

View File

@ -18,6 +18,7 @@ pub fn interpret(ast: Program) {
Literal::FloatLiteral(f) => { Literal::FloatLiteral(f) => {
args.push(format!("{}.{}f", f.trunc(), f.fract())) args.push(format!("{}.{}f", f.trunc(), f.fract()))
} }
Literal::BooleanLiteral(b) => args.push(format!("{}", b)),
}, },
Expression::Null => { Expression::Null => {
args.push("null".to_owned()); args.push("null".to_owned());

View File

@ -90,6 +90,15 @@ fn parse_expression(tokens: &Vec<Token>, current: &mut usize) -> Expression {
let out = Expression::Literal(Literal::FloatLiteral(val)); let out = Expression::Literal(Literal::FloatLiteral(val));
*current += 1; *current += 1;
out 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 { } else {
Expression::Null Expression::Null
} }
@ -114,4 +123,5 @@ pub enum Literal {
StringLiteral(String), StringLiteral(String),
IntLiteral(i32), IntLiteral(i32),
FloatLiteral(f32), FloatLiteral(f32),
BooleanLiteral(bool),
} }

View File

@ -4,6 +4,7 @@ pub enum TokenKind {
StringLiteral, StringLiteral,
IntLiteral, IntLiteral,
FloatLiteral, FloatLiteral,
BooleanLiteral,
LeftParen, LeftParen,
RightParen, RightParen,
Semicolon, Semicolon,
@ -77,8 +78,12 @@ fn read_identifier(chars: &Vec<char>, current: &mut usize, tokens: &mut Vec<Toke
break; break;
} }
} }
let mut kind = TokenKind::Identifier;
if identifier == "true" || identifier == "false" {
kind = TokenKind::BooleanLiteral;
}
tokens.push(Token { tokens.push(Token {
kind: TokenKind::Identifier, kind: kind,
value: identifier, value: identifier,
index: original_current, index: original_current,
}); });

View File

@ -1,3 +1,7 @@
log(2);
log(5i); log(5i);
log(3f); log(3f);
log(2.5); log(2.5);
log(true, false);
log("Hello world!");
log('Goodbye cruel world!')