Added lexing of LAMBDA to monkey1 version

main
oabrivard 2 years ago
parent 60ee2d266f
commit 8a5d7ab5d3

@ -33,7 +33,14 @@ func (l *Lexer) NextToken() token.Token {
case '+': case '+':
tok = newToken(token.PLUS, l.ch) tok = newToken(token.PLUS, l.ch)
case '-': case '-':
tok = newToken(token.MINUS, l.ch) if l.peekChar() == '>' {
ch := l.ch
l.readChar()
literal := string(ch) + string(l.ch)
tok = token.Token{Type: token.LAMBDA, Literal: literal}
} else {
tok = newToken(token.MINUS, l.ch)
}
case '!': case '!':
if l.peekChar() == '=' { if l.peekChar() == '=' {
ch := l.ch ch := l.ch

@ -14,6 +14,8 @@ let add = fn(x, y) {
x + y; x + y;
}; };
let addlambda = fn(x,y) -> x + y;
let result = add(five, ten); let result = add(five, ten);
!-/*5; !-/*5;
5 < 10 > 5; 5 < 10 > 5;
@ -59,6 +61,20 @@ if (5 < 10) {
{token.RBRACE, "}"}, {token.RBRACE, "}"},
{token.SEMICOLON, ";"}, {token.SEMICOLON, ";"},
{token.LET, "let"}, {token.LET, "let"},
{token.IDENT, "addlambda"},
{token.ASSIGN, "="},
{token.FUNCTION, "fn"},
{token.LPAREN, "("},
{token.IDENT, "x"},
{token.COMMA, ","},
{token.IDENT, "y"},
{token.RPAREN, ")"},
{token.LAMBDA, "->"},
{token.IDENT, "x"},
{token.PLUS, "+"},
{token.IDENT, "y"},
{token.SEMICOLON, ";"},
{token.LET, "let"},
{token.IDENT, "result"}, {token.IDENT, "result"},
{token.ASSIGN, "="}, {token.ASSIGN, "="},
{token.IDENT, "add"}, {token.IDENT, "add"},

@ -33,6 +33,8 @@ const (
LBRACE = "{" LBRACE = "{"
RBRACE = "}" RBRACE = "}"
LAMBDA = "->"
// Keywords // Keywords
FUNCTION = "FUNCTION" FUNCTION = "FUNCTION"
LET = "LET" LET = "LET"

Loading…
Cancel
Save