Add booleans
This commit is contained in:
parent
ceb1615aea
commit
93e6d490a0
@ -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());
|
||||
|
@ -90,6 +90,15 @@ fn parse_expression(tokens: &Vec<Token>, 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),
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ pub enum TokenKind {
|
||||
StringLiteral,
|
||||
IntLiteral,
|
||||
FloatLiteral,
|
||||
BooleanLiteral,
|
||||
LeftParen,
|
||||
RightParen,
|
||||
Semicolon,
|
||||
@ -77,8 +78,12 @@ fn read_identifier(chars: &Vec<char>, current: &mut usize, tokens: &mut Vec<Toke
|
||||
break;
|
||||
}
|
||||
}
|
||||
let mut kind = TokenKind::Identifier;
|
||||
if identifier == "true" || identifier == "false" {
|
||||
kind = TokenKind::BooleanLiteral;
|
||||
}
|
||||
tokens.push(Token {
|
||||
kind: TokenKind::Identifier,
|
||||
kind: kind,
|
||||
value: identifier,
|
||||
index: original_current,
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user