Skip to content

Commit

Permalink
feat: add continue and break tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
silverhairs committed Aug 9, 2023
1 parent 688fec8 commit 89ebf43
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
5 changes: 4 additions & 1 deletion glox/lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ func (s *Lexer) lex() error {
}
}


return err
}

Expand Down Expand Up @@ -253,7 +254,7 @@ func (s *Lexer) slash() error {
} else if s.match('*') {
for s.peek() != '*' && !s.isAtEnd() {
s.advance()
}
}

if s.match('/') {
literal := s.Source[s.start+2 : s.current-2]
Expand All @@ -267,3 +268,5 @@ func (s *Lexer) slash() error {
}
return nil
}


16 changes: 10 additions & 6 deletions glox/lexer/lexer_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package lexer

import (
"glox/token"
"strings"
"testing"
"glox/token"
"strings"
"testing"
)

func TestTokenize(t *testing.T) {
input := `
let age = 12;
5 + 10
input := `
let age = 12;
5 + 10
1 - 2
5 > 0
1 < 12
Expand All @@ -32,6 +32,8 @@ func TestTokenize(t *testing.T) {
many lines"
and
or
break
continue
`

tests := []struct {
Expand Down Expand Up @@ -96,6 +98,8 @@ func TestTokenize(t *testing.T) {
many lines"`},
{token.AND, "and"},
{token.OR, "or"},
{token.BREAK, "break"},
{token.CONTINUE, "continue"},
{token.EOF, ""},
}

Expand Down
4 changes: 4 additions & 0 deletions glox/token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ const (
FUNCTION = "FUNCTION"
FOR = "FOR"
OR = "OR"
BREAK = "BREAK"
CONTINUE = "CONTINUE"
NIL = "NIL"
PRINT = "PRINT"
RETURN = "RETURN"
Expand Down Expand Up @@ -82,6 +84,8 @@ var keywords = map[string]TokenType{
"let": LET,
"while": WHILE,
"var": LET,
"break": BREAK,
"continue": CONTINUE,
}

func LookupIdentifier(keyword string) TokenType {
Expand Down

0 comments on commit 89ebf43

Please sign in to comment.