bind expression (:=) instead of let
Some checks failed
Build / build (push) Successful in 10m26s
Test / build (push) Failing after 16m44s

This commit is contained in:
Chuck Smith
2024-03-21 17:43:03 -04:00
parent 66d5453ecc
commit 6282075e66
36 changed files with 425 additions and 583 deletions

View File

@@ -134,7 +134,14 @@ func (l *Lexer) NextToken() token.Token {
case ']':
tok = newToken(token.RBRACKET, l.ch)
case ':':
tok = newToken(token.COLON, l.ch)
if l.peekChar() == '=' {
ch := l.ch
l.readChar()
literal := string(ch) + string(l.ch)
tok = token.Token{Type: token.BIND, Literal: literal}
} else {
tok = newToken(token.COLON, l.ch)
}
default:
if isLetter(l.ch) {
tok.Literal = l.readIdentifier()

View File

@@ -7,15 +7,15 @@ import (
func TestNextToken(t *testing.T) {
input := `#!./monkey-lang
let five = 5;
let ten = 10;
five := 5;
ten := 10;
let add = fn(x, y) {
add := fn(x, y) {
x + y;
};
# this is a comment
let result = add(five, ten);
result := add(five, ten);
!-/*5;
5 < 10 > 5;
@@ -40,19 +40,16 @@ func TestNextToken(t *testing.T) {
expectedLiteral string
}{
{token.COMMENT, "!./monkey-lang"},
{token.LET, "let"},
{token.IDENT, "five"},
{token.ASSIGN, "="},
{token.BIND, ":="},
{token.INT, "5"},
{token.SEMICOLON, ";"},
{token.LET, "let"},
{token.IDENT, "ten"},
{token.ASSIGN, "="},
{token.BIND, ":="},
{token.INT, "10"},
{token.SEMICOLON, ";"},
{token.LET, "let"},
{token.IDENT, "add"},
{token.ASSIGN, "="},
{token.BIND, ":="},
{token.FUNCTION, "fn"},
{token.LPAREN, "("},
{token.IDENT, "x"},
@@ -67,9 +64,8 @@ func TestNextToken(t *testing.T) {
{token.RBRACE, "}"},
{token.SEMICOLON, ";"},
{token.COMMENT, " this is a comment"},
{token.LET, "let"},
{token.IDENT, "result"},
{token.ASSIGN, "="},
{token.BIND, ":="},
{token.IDENT, "add"},
{token.LPAREN, "("},
{token.IDENT, "five"},
@@ -163,9 +159,9 @@ func TestNextToken(t *testing.T) {
func TestStringEscapes(t *testing.T) {
input := `#!./monkey-lang
let a = "\"foo\""
let b = "\x00\x0a\x7f"
let c = "\r\n\t"
a := "\"foo\""
b := "\x00\x0a\x7f"
c := "\r\n\t"
`
tests := []struct {
@@ -173,17 +169,14 @@ let c = "\r\n\t"
expectedLiteral string
}{
{token.COMMENT, "!./monkey-lang"},
{token.LET, "let"},
{token.IDENT, "a"},
{token.ASSIGN, "="},
{token.BIND, ":="},
{token.STRING, "\"foo\""},
{token.LET, "let"},
{token.IDENT, "b"},
{token.ASSIGN, "="},
{token.BIND, ":="},
{token.STRING, "\x00\n\u007f"},
{token.LET, "let"},
{token.IDENT, "c"},
{token.ASSIGN, "="},
{token.BIND, ":="},
{token.STRING, "\r\n\t"},
{token.EOF, ""},
}