Skip to content

Commit

Permalink
allow := (hi walrus) as alias for = / assign (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldemailly authored Jul 20, 2024
1 parent 63ea372 commit b15808e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func NewLineMode(input string) *Lexer {
return &Lexer{input: input, lineMode: true}
}

func (l *Lexer) NextToken() token.Token { //nolint:funlen // many cases to lex.
func (l *Lexer) NextToken() token.Token { //nolint:funlen,gocyclo // many cases to lex.
l.skipWhitespace()

ch := l.readChar()
Expand Down Expand Up @@ -87,6 +87,10 @@ func (l *Lexer) NextToken() token.Token { //nolint:funlen // many cases to lex.
case ']':
return newToken(token.RBRACKET, ch)
case ':':
if l.peekChar() == '=' { // semi hacky treat := as = (without changing literal etc so tests work with either)
nextChar := l.readChar()
return newToken(token.ASSIGN, nextChar)
}
return newToken(token.COLON, ch)
case '"':
return token.Token{Type: token.STRING, Literal: l.readString()}
Expand Down
4 changes: 4 additions & 0 deletions lexer/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ a2=3
{"foo": "bar"}
return // nil return
macro(x, y) { x + y }
a3:=5
`

tests := []struct {
Expand Down Expand Up @@ -142,6 +143,9 @@ macro(x, y) { x + y }
{token.PLUS, "+"},
{token.IDENT, "y"},
{token.RBRACE, "}"},
{token.IDENT, "a3"},
{token.ASSIGN, "="}, // `:=` changed to `=`.
{token.INT, "5"},
{token.EOF, ""},
}

Expand Down
2 changes: 1 addition & 1 deletion repl/repl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Factorial of 5 is 120` + " \n120\n" // there is an extra space before \n that vs
}

func TestEvalStringParsingError(t *testing.T) {
s := `x:=3`
s := `.`
expected := ""
res, errs := repl.EvalString(s)
if len(errs) == 0 {
Expand Down

0 comments on commit b15808e

Please sign in to comment.