Implement parsing and evaluation of boolean expressions
This commit is contained in:
parent
692f1145cd
commit
5d01f1caf9
@ -124,7 +124,7 @@ impl<'a> Lexer<'a> {
|
||||
self.source[self.position..].chars().nth(1)
|
||||
}
|
||||
|
||||
fn peek_until_whitespace(&self) -> Option<&str> {
|
||||
fn _peek_until_whitespace(&self) -> Option<&str> {
|
||||
let start = self.position;
|
||||
let end = self.source[self.position..]
|
||||
.find(char::is_whitespace)
|
||||
|
@ -105,6 +105,14 @@ impl<'src> Parser<'src> {
|
||||
|
||||
fn parse_primary(&mut self) -> Result<Node, ParseError> {
|
||||
match self.current.clone() {
|
||||
(Token::Boolean(boolean), span) => {
|
||||
self.next_token()?;
|
||||
|
||||
Ok(Node::new(
|
||||
Statement::Constant(Value::boolean(boolean)),
|
||||
span,
|
||||
))
|
||||
}
|
||||
(Token::Float(float), span) => {
|
||||
self.next_token()?;
|
||||
|
||||
@ -213,6 +221,16 @@ mod tests {
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn boolean() {
|
||||
let input = "true";
|
||||
|
||||
assert_eq!(
|
||||
parse(input),
|
||||
Ok([Node::new(Statement::Constant(Value::boolean(true)), (0, 4))].into())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn list_access() {
|
||||
let input = "[1, 2, 3].0";
|
||||
|
@ -144,16 +144,6 @@ impl Vm {
|
||||
});
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
return Err(VmError::ExpectedDifferentReservedIdentifier {
|
||||
position: right_span,
|
||||
expected: vec![
|
||||
ReservedIdentifier::IsEven,
|
||||
ReservedIdentifier::IsOdd,
|
||||
ReservedIdentifier::Length,
|
||||
],
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -181,25 +171,11 @@ pub enum VmError {
|
||||
|
||||
// Anaylsis Failures
|
||||
// These should be prevented by running the analyzer before the VM
|
||||
ExpectedIdentifier {
|
||||
position: Span,
|
||||
},
|
||||
ExpectedIdentifierOrInteger {
|
||||
position: Span,
|
||||
},
|
||||
ExpectedInteger {
|
||||
position: Span,
|
||||
},
|
||||
ExpectedList {
|
||||
position: Span,
|
||||
},
|
||||
ExpectedDifferentReservedIdentifier {
|
||||
position: Span,
|
||||
expected: Vec<ReservedIdentifier>,
|
||||
},
|
||||
ExpectedValue {
|
||||
position: Span,
|
||||
},
|
||||
ExpectedIdentifier { position: Span },
|
||||
ExpectedIdentifierOrInteger { position: Span },
|
||||
ExpectedInteger { position: Span },
|
||||
ExpectedList { position: Span },
|
||||
ExpectedValue { position: Span },
|
||||
}
|
||||
|
||||
impl From<ParseError> for VmError {
|
||||
@ -218,6 +194,16 @@ impl From<ValueError> for VmError {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn boolean() {
|
||||
let input = "true";
|
||||
|
||||
assert_eq!(
|
||||
run(input, &mut HashMap::new()),
|
||||
Ok(Some(Value::boolean(true)))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn is_even() {
|
||||
let input = "42.is_even";
|
||||
|
Loading…
Reference in New Issue
Block a user