-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokens.py
221 lines (191 loc) · 5.53 KB
/
tokens.py
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import sys
PROMPT = ">> "
class TokenType:
ILLEGAL = "ILLEGAL"
EOF = "EOF"
# Identifiers + literals
IDENT = "IDENT" # add, foobar, x, y, ...
INT = "INT" # 1343456
STRING = "STRING"
# Operators
ASSIGN = "="
PLUS = "+"
MINUS = "-"
BANG = "!"
ASTERISK = "*"
SLASH = "/"
LT = "<"
GT = ">"
EQ = "=="
NOT_EQ = "!="
# Delimiters
COMMA = ","
SEMICOLON = ";"
COLON = ":"
LPAREN = "("
RPAREN = ")"
LBRACE = "{"
RBRACE = "}"
LBRACKET = "["
RBRACKET = "]"
# Keywords
FUNCTION = "FUNCTION"
LET = "LET"
TRUE = "TRUE"
FALSE = "FALSE"
IF = "IF"
ELSE = "ELSE"
RETURN = "RETURN"
KEYWORDS = {
"fn": TokenType.FUNCTION,
"let": TokenType.LET,
"true": TokenType.TRUE,
"false": TokenType.FALSE,
"if": TokenType.IF,
"else": TokenType.ELSE,
"return": TokenType.RETURN,
}
class Token:
def __init__(self, type_, literal):
self.type = type_
self.literal = literal
def __repr__(self):
return f"Token ( type = [ {self.type} ] , literal = '{ self.literal }' ) "
def lookup_ident(ident):
return KEYWORDS.get(ident, TokenType.IDENT)
class Lexer:
def __init__(self, input_):
self.input = input_
self.position = 0
self.read_position = 0
self.ch = None
self.read_char()
def read_char(self):
if self.read_position >= len(self.input):
self.ch = None
else:
self.ch = self.input[self.read_position]
self.position = self.read_position
self.read_position += 1
def peek_char(self):
if self.read_position >= len(self.input):
return None
return self.input[self.read_position]
def next_token(self):
self.skip_whitespace()
if self.ch is None:
return Token(TokenType.EOF, "")
tok = None
if self.ch == '=':
if self.peek_char() == '=':
ch = self.ch
self.read_char()
tok = Token(TokenType.EQ, ch + self.ch)
else:
tok = Token(TokenType.ASSIGN, self.ch)
elif self.ch == '+':
tok = Token(TokenType.PLUS, self.ch)
elif self.ch == '-':
tok = Token(TokenType.MINUS, self.ch)
elif self.ch == '!':
if self.peek_char() == '=':
ch = self.ch
self.read_char()
tok = Token(TokenType.NOT_EQ, ch + self.ch)
else:
tok = Token(TokenType.BANG, self.ch)
elif self.ch == '/':
tok = Token(TokenType.SLASH, self.ch)
elif self.ch == '*':
tok = Token(TokenType.ASTERISK, self.ch)
elif self.ch == '<':
tok = Token(TokenType.LT, self.ch)
elif self.ch == '>':
tok = Token(TokenType.GT, self.ch)
elif self.ch == ';':
tok = Token(TokenType.SEMICOLON, self.ch)
elif self.ch == ':':
tok = Token(TokenType.COLON, self.ch)
elif self.ch == ',':
tok = Token(TokenType.COMMA, self.ch)
elif self.ch == '{':
tok = Token(TokenType.LBRACE, self.ch)
elif self.ch == '}':
tok = Token(TokenType.RBRACE, self.ch)
elif self.ch == '(':
tok = Token(TokenType.LPAREN, self.ch)
elif self.ch == ')':
tok = Token(TokenType.RPAREN, self.ch)
elif self.ch == '"':
tok = Token(TokenType.STRING, self.read_string())
elif self.ch == '[':
tok = Token(TokenType.LBRACKET, self.ch)
elif self.ch == ']':
tok = Token(TokenType.RBRACKET, self.ch)
elif self.ch.isalpha():
literal = self.read_identifier()
return Token(lookup_ident(literal), literal)
elif self.ch.isdigit():
return Token(TokenType.INT, self.read_number())
else:
tok = Token(TokenType.ILLEGAL, self.ch)
self.read_char()
return tok
def skip_whitespace(self):
x = self.ch
while x == ' ' or x == '\t' or x == '\n' or x == '\r':
self.read_char()
x = self.ch
def read_identifier(self):
position = self.position
while self.ch and self.ch.isalpha():
self.read_char()
return self.input[position:self.position]
def read_number(self):
position = self.position
while self.ch and self.ch.isdigit():
self.read_char()
return self.input[position:self.position]
def read_string(self):
position = self.position + 1
while True:
self.read_char()
if self.ch == '"' or self.ch == None:
break
return self.input[position:self.position]
def start_repl():
print("Welcome to the Monkey programming language!")
print("Feel free to type in commands.")
while True:
sys.stdout.write(PROMPT)
sys.stdout.flush()
#line = sys.stdin.readline()
line = "x"
if not line:
break
line = line.strip(" \n")
line = """let five = 5;
let ten = 10;
let add = fn(x, y) {
x + y;
};
let result = add(five, ten);
!-/*5;
5 < 10 > 5;
if (5 < 10) {
return true;
} else {
return false;
}
10 == 10;
10 != 9;"""
lexer = Lexer(line)
tok = lexer.next_token()
while True:
print(tok)
if (tok.type == TokenType.EOF):
break
tok = lexer.next_token()
break
if __name__ == "__main__":
start_repl()