Skip to content

Commit

Permalink
Use parser example for integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PaddiM8 committed Oct 10, 2024
1 parent 5f1ce86 commit a824ff2
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 2 deletions.
4 changes: 3 additions & 1 deletion examples/parser/ast.elk
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ pub struct Token(kind, value)

pub struct BinaryExpr(left, op, right)

pub struct LiteralExpr(value)
pub struct LiteralExpr(value)

pub struct GroupExpr(value)
5 changes: 5 additions & 0 deletions examples/parser/evaluator.elk
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub fn evaluate(ast) {
fn next(expr) {
return nextBinary(expr) if expr | isType(BinaryExpr)
return nextLiteral(expr) if expr | isType(LiteralExpr)
return nextGroup(expr) if expr | isType(GroupExpr)
}

fn nextBinary(expr) {
Expand All @@ -21,4 +22,8 @@ fn nextBinary(expr) {

fn nextLiteral(expr) {
expr->value->value | into::float
}

fn nextGroup(expr) {
expr->value | next
}
2 changes: 1 addition & 1 deletion examples/parser/lexer.elk
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn next() {

return nil if c == nil

if c in ["+", "-", "*", "/"] {
if c in ["+", "-", "*", "/", "(", ")"] {
advance
return new Token(c, c)
}
Expand Down
15 changes: 15 additions & 0 deletions examples/parser/parser.elk
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ let index = 0
pub fn parse(input) {
tokens = lexer::lex(input)
index = 0
return nil if len(tokens) == 0

expr()
}
Expand All @@ -28,6 +29,13 @@ fn match(kinds...) {
kinds | iter::anyOf => kind { kind == current()->kind }
}

fn expect(kind) {
return advance() if match(kind)

throw new ParseError("Expected '${kind}'") if current() == nil
throw new ParseError("Expected '${kind}' but got '${current()->kind}'")
}

fn expr() {
additive()
}
Expand Down Expand Up @@ -61,6 +69,13 @@ fn primary() {
return new LiteralExpr(advance())
}

if match("(") {
advance()
let group = new GroupExpr(expr())
expect(")")
return group
}

throw new ParseError("Unexpected end of expression") if current() != nil
throw new ParseError("Unexpected token: '${current()->kind}'")
}
7 changes: 7 additions & 0 deletions examples/parser/tests.elk
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
with parse from ./parser
with evaluate from ./evaluator

parse("2 + 3 * 4") | evaluate | assertEqual(14)
parse("(3 + 3 * 4) / 3") | evaluate | assertEqual(5)
parse("2") | evaluate | assertEqual(2)
parse("") | evaluate | assertEqual(nil)
1 change: 1 addition & 0 deletions tests/IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public static IEnumerable<string> ElkFiles()
Directory.GetFiles(dataPath, "*.elk", SearchOption.AllDirectories),
Directory.GetFiles(Path.Combine(examplesPath, "advent-of-code-2022"), "*.elk", SearchOption.AllDirectories),
Directory.GetFiles(Path.Combine(examplesPath, "advent-of-code-2023"), "*.elk", SearchOption.AllDirectories),
Directory.GetFiles(Path.Combine(examplesPath, "parser"), "*.elk", SearchOption.AllDirectories),
}.SelectMany(x => x);
}
}

0 comments on commit a824ff2

Please sign in to comment.