Skip to content

Commit

Permalink
fix prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
maxmindlin committed Jun 25, 2024
1 parent 35cbd67 commit 1dd49f3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions scout-interpreter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ fn eval_expression<'a>(
ExprKind::Infix(lhs, op, rhs) => {
let l_obj = eval_expression(lhs, crawler, env.clone(), results.clone()).await?;
let r_obj = eval_expression(rhs, crawler, env.clone(), results.clone()).await?;
let res = eval_op(l_obj.clone(), op, r_obj.clone())?;
let res = eval_infix(l_obj.clone(), op, r_obj.clone())?;
Ok(res)
}
ExprKind::Boolean(val) => Ok(Arc::new(Object::Boolean(*val))),
Expand All @@ -601,7 +601,7 @@ fn eval_expression<'a>(
.boxed()
}

fn eval_op(lhs: Arc<Object>, op: &TokenKind, rhs: Arc<Object>) -> EvalResult {
fn eval_infix(lhs: Arc<Object>, op: &TokenKind, rhs: Arc<Object>) -> EvalResult {
match op {
TokenKind::EQ => Ok(Arc::new(Object::Boolean(lhs == rhs))),
TokenKind::NEQ => Ok(Arc::new(Object::Boolean(lhs != rhs))),
Expand Down
12 changes: 12 additions & 0 deletions scout-parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ fn map_prefix_fn(kind: &TokenKind) -> Option<PrefixParseFn> {
LBracket => Some(Parser::parse_list_literal),
SelectAll => Some(Parser::parse_select_all),
Select => Some(Parser::parse_select),
Bang => Some(Parser::parse_prefix),
_ => None,
}
}
Expand Down Expand Up @@ -270,6 +271,13 @@ impl Parser {
Ok(IfLiteral { cond, block })
}

fn parse_prefix(&mut self) -> ParseResult<ExprKind> {
let op = self.curr.kind;
self.next_token();
let expr = self.parse_expr(Precedence::Lowest)?;
Ok(ExprKind::Prefix(Box::new(expr), op))
}

fn parse_block(&mut self, finalizers: Vec<TokenKind>) -> ParseResult<Block> {
let mut stmts = Vec::new();
while !finalizers.contains(&self.curr.kind) {
Expand Down Expand Up @@ -753,6 +761,10 @@ mod tests {
)
); "crawl stmt with bindings"
)]
#[test_case(
"!true",
StmtKind::Expr(ExprKind::Prefix(Box::new(ExprKind::Boolean(true)), TokenKind::Bang,)); "bang prefix"
)]
fn test_single_stmt(input: &str, exp: StmtKind) {
let stmt = extract_first_stmt(input);
assert_eq!(stmt, exp);
Expand Down

0 comments on commit 1dd49f3

Please sign in to comment.