Browse Source

§1.4 more single-character tokens.

Frederic G. MARAND 5 years ago
parent
commit
19c76ffe57
3 changed files with 41 additions and 6 deletions
  1. 17 5
      lexer/lexer.go
  2. 17 1
      lexer/lexer_test.go
  3. 7 0
      token/token.go

+ 17 - 5
lexer/lexer.go

@@ -7,7 +7,7 @@ package lexer
 
 import (
 	"fgm/waiig15/token"
-)
+	)
 
 type Lexer struct {
 	input        string
@@ -41,16 +41,28 @@ func (l *Lexer) NextToken() token.Token {
 	switch l.ch {
 	case '=':
 		tok = newToken(token.ASSIGN, l.ch)
-	case ';':
-		tok = newToken(token.SEMICOLON, l.ch)
 	case '(':
 		tok = newToken(token.LPAREN, l.ch)
 	case ')':
 		tok = newToken(token.RPAREN, l.ch)
-	case ',':
-		tok = newToken(token.COMMA, l.ch)
 	case '+':
 		tok = newToken(token.PLUS, l.ch)
+	case '-':
+		tok = newToken(token.MINUS, l.ch)
+	case '!':
+		tok = newToken(token.BANG, l.ch)
+	case '/':
+		tok = newToken(token.SLASH, l.ch)
+	case '*':
+		tok = newToken(token.ASTERISK, l.ch)
+	case '<':
+		tok = newToken(token.LT, l.ch)
+	case '>':
+		tok = newToken(token.GT, l.ch)
+	case ';':
+		tok = newToken(token.SEMICOLON, l.ch)
+	case ',':
+		tok = newToken(token.COMMA, l.ch)
 	case '{':
 		tok = newToken(token.LBRACE, l.ch)
 	case '}':

+ 17 - 1
lexer/lexer_test.go

@@ -15,6 +15,8 @@ let add = fn(x, y) {
 }
 
 let result = add(five, ten);
+!-/*5;
+5 < 10 > 5;
 `
 
 	tests := []struct {
@@ -59,7 +61,21 @@ let result = add(five, ten);
 		{ token.IDENT, "ten" },
 		{ token.RPAREN, ")" },
 		{token.SEMICOLON, ";"},
-		{token.EOF, ""},
+
+		{ token.BANG, "!" },
+		{ token.MINUS, "-" },
+		{ token.SLASH, "/" },
+		{ token.ASTERISK, "*" },
+		{ token.INT, "5" },
+		{ token.SEMICOLON, ";" },
+
+		{ token.INT, "5" },
+		{ token.LT, "<" },
+		{ token.INT, "10" },
+		{ token.GT, ">" },
+		{ token.INT, "5" },
+		{ token.SEMICOLON, ";" },
+		{ token.EOF, ""},
 	}
 
 	l := New(input)

+ 7 - 0
token/token.go

@@ -18,6 +18,13 @@ const (
 	// Operators.
 	ASSIGN = "="
 	PLUS   = "+"
+	MINUS = "-"
+	BANG = "!"
+	ASTERISK = "*"
+	SLASH = "/"
+
+	LT = "<"
+	GT = ">"
 
 	// Delimiters.
 	COMMA     = ","