-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
token.go
140 lines (117 loc) · 2 KB
/
token.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Package token contains the list of token-types we accept/recognize.
package token
// Type is a string
type Type string
// Token struct represent the lexer token
type Token struct {
Type Type
Literal string
}
// pre-defined Type
const (
ILLEGAL = "ILLEGAL"
EOF = "EOF"
IDENT = "IDENT"
LABEL = "LABEL"
INT = "INT"
STRING = "STRING"
COMMA = "COMMA"
// math
ADD = "ADD"
AND = "AND"
DEC = "DEC"
DIV = "DIV"
INC = "INC"
MUL = "MUL"
OR = "OR"
SUB = "SUB"
XOR = "XOR"
// control-flow
CALL = "CALL"
JMP = "JMP"
JMPNZ = "JMPNZ"
JMPZ = "JMPZ"
RET = "RET"
// stack
PUSH = "PUSH"
POP = "POP"
// types
IS_STRING = "IS_STRING"
IS_INTEGER = "IS_INTEGER"
STRING2INT = "STRING2INT"
INT2STRING = "INT2STRING"
// compare
CMP = "CMP"
// store
STORE = "STORE"
// print
PRINT_INT = "PRINT_INT"
PRINT_STR = "PRINT_STR"
// memory
PEEK = "PEEK"
POKE = "POKE"
// Misc
CONCAT = "CONCAT"
DATA = "DATA"
DB = "DB"
EXIT = "EXIT"
MEMCPY = "MEMCPY"
NOP = "NOP"
RANDOM = "RANDOM"
SYSTEM = "SYSTEM"
TRAP = "TRAP"
)
// reversed keywords
var keywords = map[string]Type{
// compare
"cmp": CMP,
// types
"is_integer": IS_INTEGER,
"is_string": IS_STRING,
"int2string": INT2STRING,
"string2int": STRING2INT,
// store
"store": STORE,
// print
"print_int": PRINT_INT,
"print_str": PRINT_STR,
// math
"add": ADD,
"and": AND,
"dec": DEC,
"div": DIV,
"inc": INC,
"mul": MUL,
"or": OR,
"sub": SUB,
"xor": XOR,
// control-flow
"call": CALL,
"jmp": JMP,
"jmpnz": JMPNZ,
"jmpz": JMPZ,
"ret": RET,
// stack
"push": PUSH,
"pop": POP,
// memory
"peek": PEEK,
"poke": POKE,
// misc
"exit": EXIT,
"concat": CONCAT,
"DATA": DATA,
"DB": DB,
"int": TRAP,
"memcpy": MEMCPY,
"nop": NOP,
"random": RANDOM,
"system": SYSTEM,
}
// LookupIdentifier used to determinate whether identifier is keyword nor not
func LookupIdentifier(identifier string) Type {
if tok, ok := keywords[identifier]; ok {
return tok
}
return IDENT
}