changeset 3:6bd28d00a4f4 default tip lexer

...
author anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
date Tue, 19 May 2020 18:23:57 +0900
parents 595b3e2dc94e
children
files lexer/lexer_test.go token/token.go
diffstat 2 files changed, 39 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/lexer/lexer_test.go	Fri May 15 10:26:01 2020 +0900
+++ b/lexer/lexer_test.go	Tue May 19 18:23:57 2020 +0900
@@ -16,6 +16,12 @@
 	let result = add(five, ten);
 	!-/*5;
 	5 < 10 > 5;
+
+	if (5 < 10) {
+		return true;
+	} else {
+		return false;
+	}
 	`
 
 	tests := []struct {
@@ -70,6 +76,23 @@
 		{token.GT, ">"},
 		{token.INT, "5"},
 		{token.SEMICOLON, ";"},
+		{token.IF, "if"},
+		{token.LPAREN, "("},
+		{token.INT, "5"},
+		{token.LT, "<"},
+		{token.INT, "10"},
+		{token.RPAREN, ")"},
+		{token.LBRACE, "{"},
+		{token.RETURN, "return"},
+		{token.TRUE, "true"},
+		{token.SEMICOLON, ";"},
+		{token.RBRACE, "}"},
+		{token.ELSE, "else"},
+		{token.LBRACE, "{"},
+		{token.RETURN, "return"},
+		{token.FALSE, "false"},
+		{token.SEMICOLON, ";"},
+		{token.RBRACE, "}"},
 		{token.EOF, ""},
 	}
 
--- a/token/token.go	Fri May 15 10:26:01 2020 +0900
+++ b/token/token.go	Tue May 19 18:23:57 2020 +0900
@@ -7,9 +7,9 @@
 	IDENT = "IDENT"
 	INT   = "INT"
 
-	ASSIGN = "="
-	PLUS   = "+"
-
+	// Operators
+	ASSIGN   = "="
+	PLUS     = "+"
 	MINUS    = "-"
 	BANG     = "!"
 	ASTERISK = "*"
@@ -26,8 +26,14 @@
 	LBRACE = "{"
 	RBRACE = "}"
 
+	// Keywords
 	FUNCTION = "FUNCTION"
 	LET      = "LET"
+	TRUE     = "TRUE"
+	FALSE    = "FALSE"
+	IF       = "IF"
+	ELSE     = "ELSE"
+	RETURN   = "RETURN"
 )
 
 type TokenType string
@@ -38,8 +44,13 @@
 }
 
 var keywords = map[string]TokenType{
-	"fn":  FUNCTION,
-	"let": LET,
+	"fn":     FUNCTION,
+	"let":    LET,
+	"true":   TRUE,
+	"false":  FALSE,
+	"if":     IF,
+	"else":   ELSE,
+	"return": RETURN,
 }
 
 //LookupIdent is jugde ident or defined keywords