From 8a5d7ab5d3c83b846cb1ea7150daf202cb5d0373 Mon Sep 17 00:00:00 2001 From: oabrivard Date: Wed, 27 Sep 2023 09:55:41 +0200 Subject: [PATCH] Added lexing of LAMBDA to monkey1 version --- monkey1/lexer/lexer.go | 9 ++++++++- monkey1/lexer/lexer_test.go | 16 ++++++++++++++++ monkey1/token/token.go | 2 ++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/monkey1/lexer/lexer.go b/monkey1/lexer/lexer.go index 3613013..45e72e2 100644 --- a/monkey1/lexer/lexer.go +++ b/monkey1/lexer/lexer.go @@ -33,7 +33,14 @@ func (l *Lexer) NextToken() token.Token { case '+': tok = newToken(token.PLUS, l.ch) 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 '!': if l.peekChar() == '=' { ch := l.ch diff --git a/monkey1/lexer/lexer_test.go b/monkey1/lexer/lexer_test.go index 17d1aca..7d4fd99 100644 --- a/monkey1/lexer/lexer_test.go +++ b/monkey1/lexer/lexer_test.go @@ -14,6 +14,8 @@ let add = fn(x, y) { x + y; }; +let addlambda = fn(x,y) -> x + y; + let result = add(five, ten); !-/*5; 5 < 10 > 5; @@ -59,6 +61,20 @@ if (5 < 10) { {token.RBRACE, "}"}, {token.SEMICOLON, ";"}, {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.ASSIGN, "="}, {token.IDENT, "add"}, diff --git a/monkey1/token/token.go b/monkey1/token/token.go index 12158fa..029a392 100644 --- a/monkey1/token/token.go +++ b/monkey1/token/token.go @@ -33,6 +33,8 @@ const ( LBRACE = "{" RBRACE = "}" + LAMBDA = "->" + // Keywords FUNCTION = "FUNCTION" LET = "LET"