Skip to content

Commit

Permalink
fix: fixed improper parsing of terms
Browse files Browse the repository at this point in the history
  • Loading branch information
Hirevo committed Jun 7, 2023
1 parent da7789b commit eef4502
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 96 deletions.
3 changes: 0 additions & 3 deletions som-core/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ pub struct Body {
/// "exit operation" ^counter
/// "literal" 'foo'
/// "block" [ :value | counter incrementBy: value ]
/// "term" ( counter increment )
/// ```
#[derive(Debug, Clone, PartialEq)]
pub enum Expression {
Expand All @@ -139,8 +138,6 @@ pub enum Expression {
Literal(Literal),
/// A block (eg. `[ :value | counter incrementBy: value ]`).
Block(Block),
/// A term (eg. `( counter increment )`).
Term(Term),
}

/// Represents a message send.
Expand Down
1 change: 0 additions & 1 deletion som-interpreter-ast/src/evaluate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ impl Evaluate for ast::Expression {
universe.unknown_global(self_value, name.as_str())
})
.unwrap_or_else(|| Return::Exception(format!("variable '{}' not found", name))),
Self::Term(term) => term.evaluate(universe),
Self::Message(msg) => msg.evaluate(universe),
}
}
Expand Down
5 changes: 0 additions & 5 deletions som-interpreter-bc/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,11 +329,6 @@ impl MethodCodegen for ast::Expression {
ctxt.push_instr(Bytecode::PushBlock(idx as u8));
Some(())
}
ast::Expression::Term(term) => term
.body
.exprs
.iter()
.try_for_each(|expr| expr.codegen(ctxt)),
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions som-parser-symbols/src/lang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,11 @@ pub fn block<'a>() -> impl Parser<Expression, &'a [Token]> {
}

pub fn term<'a>() -> impl Parser<Expression, &'a [Token]> {
between(exact(Token::NewTerm), body(), exact(Token::EndTerm))
.map(|body| Expression::Term(Term { body }))
between(
exact(Token::NewTerm),
assignment().or(expression()),
exact(Token::EndTerm),
)
}

pub fn exit<'a>() -> impl Parser<Expression, &'a [Token]> {
Expand Down
56 changes: 16 additions & 40 deletions som-parser-symbols/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,10 @@ fn expression_test_2() {
assert_eq!(
expression,
Expression::Message(Message {
receiver: Box::new(Expression::Term(Term {
body: Body {
exprs: vec![Expression::BinaryOp(BinaryOp {
op: String::from("=="),
lhs: Box::new(Expression::Literal(Literal::Integer(3))),
rhs: Box::new(Expression::Literal(Literal::Integer(3))),
})],
full_stopped: false,
}
receiver: Box::new(Expression::BinaryOp(BinaryOp {
op: String::from("=="),
lhs: Box::new(Expression::Literal(Literal::Integer(3))),
rhs: Box::new(Expression::Literal(Literal::Integer(3))),
})),
signature: String::from("ifTrue:ifFalse:"),
values: vec![
Expand Down Expand Up @@ -173,38 +168,19 @@ fn primary_test() {
signature: String::from("fib:"),
values: vec![Expression::BinaryOp(BinaryOp {
op: String::from("+"),
lhs: Box::new(Expression::Term(Term {
body: Body {
exprs: vec![Expression::BinaryOp(BinaryOp {
op: String::from("-"),
lhs: Box::new(Expression::Reference(String::from("n"))),
rhs: Box::new(Expression::Literal(Literal::Integer(1))),
})],
full_stopped: false,
}
lhs: Box::new(Expression::BinaryOp(BinaryOp {
op: String::from("-"),
lhs: Box::new(Expression::Reference(String::from("n"))),
rhs: Box::new(Expression::Literal(Literal::Integer(1))),
})),
rhs: Box::new(Expression::Term(Term {
body: Body {
exprs: vec![Expression::Message(Message {
receiver: Box::new(Expression::Reference(String::from("self"))),
signature: String::from("fib:"),
values: vec![Expression::Term(Term {
body: Body {
exprs: vec![Expression::BinaryOp(BinaryOp {
op: String::from("-"),
lhs: Box::new(Expression::Reference(String::from(
"n"
))),
rhs: Box::new(Expression::Literal(
Literal::Integer(2)
)),
})],
full_stopped: false,
}
})],
})],
full_stopped: false,
}
rhs: Box::new(Expression::Message(Message {
receiver: Box::new(Expression::Reference(String::from("self"))),
signature: String::from("fib:"),
values: vec![Expression::BinaryOp(BinaryOp {
op: String::from("-"),
lhs: Box::new(Expression::Reference(String::from("n"))),
rhs: Box::new(Expression::Literal(Literal::Integer(2))),
})],
}))
})],
})],
Expand Down
7 changes: 2 additions & 5 deletions som-parser-text/src/lang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,13 +374,10 @@ pub fn term<'a>() -> impl Parser<Expression, &'a [char]> {
move |input: &'a [char]| {
let (_, input) = exact('(').parse(input)?;
let (_, input) = many(spacing()).parse(input)?;
let (body, input) = body().parse(input)?;
let (expr, input) = assignment().or(expression()).parse(input)?;
let (_, input) = many(spacing()).parse(input)?;
let (_, input) = exact(')').parse(input)?;

let term = Term { body };
let term = Expression::Term(term);
Some((term, input))
Some((expr, input))
}
}

Expand Down
56 changes: 16 additions & 40 deletions som-parser-text/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,10 @@ fn expression_test_2() {
assert_eq!(
expression,
Expression::Message(Message {
receiver: Box::new(Expression::Term(Term {
body: Body {
exprs: vec![Expression::BinaryOp(BinaryOp {
op: String::from("=="),
lhs: Box::new(Expression::Literal(Literal::Integer(3))),
rhs: Box::new(Expression::Literal(Literal::Integer(3))),
})],
full_stopped: false,
}
receiver: Box::new(Expression::BinaryOp(BinaryOp {
op: String::from("=="),
lhs: Box::new(Expression::Literal(Literal::Integer(3))),
rhs: Box::new(Expression::Literal(Literal::Integer(3))),
})),
signature: String::from("ifTrue:ifFalse:"),
values: vec![
Expand Down Expand Up @@ -166,38 +161,19 @@ fn primary_test() {
signature: String::from("fib:"),
values: vec![Expression::BinaryOp(BinaryOp {
op: String::from("+"),
lhs: Box::new(Expression::Term(Term {
body: Body {
exprs: vec![Expression::BinaryOp(BinaryOp {
op: String::from("-"),
lhs: Box::new(Expression::Reference(String::from("n"))),
rhs: Box::new(Expression::Literal(Literal::Integer(1))),
})],
full_stopped: false,
}
lhs: Box::new(Expression::BinaryOp(BinaryOp {
op: String::from("-"),
lhs: Box::new(Expression::Reference(String::from("n"))),
rhs: Box::new(Expression::Literal(Literal::Integer(1))),
})),
rhs: Box::new(Expression::Term(Term {
body: Body {
exprs: vec![Expression::Message(Message {
receiver: Box::new(Expression::Reference(String::from("self"))),
signature: String::from("fib:"),
values: vec![Expression::Term(Term {
body: Body {
exprs: vec![Expression::BinaryOp(BinaryOp {
op: String::from("-"),
lhs: Box::new(Expression::Reference(String::from(
"n"
))),
rhs: Box::new(Expression::Literal(
Literal::Integer(2)
)),
})],
full_stopped: false,
}
})],
})],
full_stopped: false,
}
rhs: Box::new(Expression::Message(Message {
receiver: Box::new(Expression::Reference(String::from("self"))),
signature: String::from("fib:"),
values: vec![Expression::BinaryOp(BinaryOp {
op: String::from("-"),
lhs: Box::new(Expression::Reference(String::from("n"))),
rhs: Box::new(Expression::Literal(Literal::Integer(2))),
})],
}))
})],
})],
Expand Down

0 comments on commit eef4502

Please sign in to comment.