Skip to content

Commit

Permalink
finish lexer
Browse files Browse the repository at this point in the history
  • Loading branch information
glyh committed Sep 12, 2024
1 parent aa880cf commit 3701394
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
24 changes: 21 additions & 3 deletions src/lex/lex.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn advance(self : Loc, c : Char) -> Loc {
if c == '\n' {
self.advance_line()
} else {
{ ..self, col: self.col + 1 }
{ ..self, col: self.col + 1, index: self.index + 1 }
}
}

Expand Down Expand Up @@ -223,11 +223,29 @@ pub fn lex_string!(input : String) -> Array[(Token, Loc)] {
result
}

fn fst[U, V](pair : (U, V)) -> U {
match pair {
(a, _) => a
}
}

test "simple lexing" {
inspect!(
lex_string!("1, 2, 3"),
lex_string!(
#|fn make_adder(x: Int) -> (Int) -> Int {
#| fn adder(y: Int) -> Int {
#| x + y
#| };
#| adder
#|};
#|
#|fn main {
#| print_int((make_adder(3))(7))
#|};
,
).map(fst),
content=
#|[INT(1), COMMA, INT(2), COMMA, INT(3)]
#|[FN, ID("make_adder"), LPAREN, ID("x"), COLON, INT_T, RPAREN, ARROW, LPAREN, INT_T, RPAREN, ARROW, INT_T, LBRACE, FN, ID("adder"), LPAREN, ID("y"), COLON, INT_T, RPAREN, ARROW, INT_T, LBRACE, ID("x"), ADD, ID("y"), RBRACE, SEMICOL, ID("adder"), RBRACE, SEMICOL, FN, ID("main"), LBRACE, ID("print_int"), LPAREN, LPAREN, ID("make_adder"), LPAREN, INT(3), RPAREN, RPAREN, LPAREN, INT(7), RPAREN, RPAREN, RBRACE, SEMICOL]
,
)
}
2 changes: 1 addition & 1 deletion src/lex/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{}
{}

0 comments on commit 3701394

Please sign in to comment.