aboutsummaryrefslogtreecommitdiff
path: root/users/fcuny/exp/monkey/pkg/token/token.go
diff options
context:
space:
mode:
authorFranck Cuny <franck@fcuny.net>2022-05-29 09:57:07 -0700
committerFranck Cuny <franck@fcuny.net>2022-05-29 09:57:07 -0700
commitd284191a8b1ed3aad31583709fddcca1049b30aa (patch)
treee2663f93b1ded9fe949169433ec6d9ea68235665 /users/fcuny/exp/monkey/pkg/token/token.go
parenttools(govanity): add the tool to flake.nix (diff)
parentreadme: convert to org-mode (diff)
downloadinfra-d284191a8b1ed3aad31583709fddcca1049b30aa.tar.gz
Merge remote-tracking branch 'monkey/master'
Change-Id: I790690b0877ae309d1d5feb5150f136085e78206
Diffstat (limited to 'users/fcuny/exp/monkey/pkg/token/token.go')
-rw-r--r--users/fcuny/exp/monkey/pkg/token/token.go71
1 files changed, 71 insertions, 0 deletions
diff --git a/users/fcuny/exp/monkey/pkg/token/token.go b/users/fcuny/exp/monkey/pkg/token/token.go
new file mode 100644
index 0000000..5eadc5e
--- /dev/null
+++ b/users/fcuny/exp/monkey/pkg/token/token.go
@@ -0,0 +1,71 @@
+// Package token provides a tokenizer for the monkey language.
+package token
+
+// TokenType represents the type of the token
+type TokenType string
+
+// Token represents a token, with the type and the literal value of the token
+type Token struct {
+ Type TokenType
+ Literal string
+}
+
+const (
+ ILLEGAL = "ILLEGAL"
+ EOF = "EOF"
+
+ IDENT = "IDENT"
+ INT = "INT"
+
+ COMMA = ","
+ SEMICOLON = ";"
+
+ LPAREN = "("
+ RPAREN = ")"
+ LBRACE = "{"
+ RBRACE = "}"
+
+ // The following tokens are keywords
+ FUNCTION = "FUNCTION"
+ LET = "LET"
+ TRUE = "TRUE"
+ FALSE = "FALSE"
+ IF = "IF"
+ ELSE = "ELSE"
+ RETURN = "RETURN"
+
+ // The following tokens are for operators
+ ASSIGN = "="
+ PLUS = "+"
+ MINUS = "-"
+ BANG = "!"
+ ASTERISK = "*"
+ SLASH = "/"
+ LT = "<"
+ GT = ">"
+
+ EQ = "=="
+ NOT_EQ = "!="
+)
+
+// List of our keywords for the language
+var keywords = map[string]TokenType{
+ "fn": FUNCTION,
+ "let": LET,
+ "true": TRUE,
+ "false": FALSE,
+ "if": IF,
+ "else": ELSE,
+ "return": RETURN,
+}
+
+// LookupIdent returns the token type for a given identifier.
+// First we check if the identifier is a keyword. If it is, we return they
+// keyword TokenType constant. If it isn't, we return the token.IDENT which is
+// the TokenType for all user-defined identifiers.
+func LookupIdent(ident string) TokenType {
+ if tok, ok := keywords[ident]; ok {
+ return tok
+ }
+ return IDENT
+}