-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanner.jl
175 lines (160 loc) · 5.14 KB
/
scanner.jl
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
include("tokens.jl")
function peek_ahead(line, i)
if i == length(line)
return
end
line[i + 1]
end
function error(line, msg)
println("[line $line] Error: $msg")
exit(1)
end
function scan(code)
keywords = Dict(
"and" => AND,
"class" => CLASS,
"else" => ELSE,
"false" => FALSE,
"for" => FOR,
"fun" => FUN,
"if" => IF,
"nil" => NIL,
"or" => OR,
"print" => PRINT,
"return" => RETURN,
"super" => SUPER,
"this" => THIS,
"true" => TRUE,
"var" => VAR,
"while" => WHILE
)
tokens = Token[]
line_nb = 1
i = 1
while i <= length(code)
c = code[i]
if c == '('
push!(tokens, Token(type=LEFT_PAREN, line=line_nb))
elseif c == ')'
push!(tokens, Token(type=RIGHT_PAREN, line=line_nb))
elseif c == '{'
push!(tokens, Token(type=LEFT_BRACE, line=line_nb))
elseif c == '}'
push!(tokens, Token(type=RIGHT_BRACE, line=line_nb))
elseif c == ','
push!(tokens, Token(type=COMMA, line=line_nb))
elseif c == '.'
push!(tokens, Token(type=DOT, line=line_nb))
elseif c == '-'
push!(tokens, Token(type=MINUS, line=line_nb))
elseif c == '+'
push!(tokens, Token(type=PLUS, line=line_nb))
elseif c == ';'
push!(tokens, Token(type=SEMICOLON, line=line_nb))
elseif c == '*'
push!(tokens, Token(type=STAR, line=line_nb))
elseif c == '!'
if peek_ahead(code, i) == '='
push!(tokens, Token(type=BANG_EQUAL, line=line_nb))
i += 1
else
push!(tokens, Token(type=BANG, line=line_nb))
end
elseif c == '='
if peek_ahead(code, i) == '='
push!(tokens, Token(type=EQUAL_EQUAL, line=line_nb))
i += 1
else
push!(tokens, Token(type=EQUAL, line=line_nb))
end
elseif c == '<'
if peek_ahead(code, i) == '='
push!(tokens, Token(type=LESS_EQUAL, line=line_nb))
i += 1
else
push!(tokens, Token(type=LESS, line=line_nb))
end
elseif c == '>'
if peek_ahead(code, i) == '='
push!(tokens, Token(type=GREATER_EQUAL, line=line_nb))
i += 1
else
push!(tokens, Token(type=GREATER, line=line_nb))
end
elseif c == '/'
if peek_ahead(code, i) == '/'
while(i <= length(code) && code[i] != '\n')
i += 1
end
line_nb += 1
elseif peek_ahead(code, i) == '*'
i += 2
while(i + 1 <= length(code) && (code[i] != '*' || code[i + 1] != '/'))
if(code[i] == '\n')
line_nb += 1
end
i += 1
end
else
push!(tokens, Token(type=SLASH, line=line_nb))
end
elseif c == ' ' || c == '\r' || c == '\t' || c == '\n'
i += 1
if c == '\n'
line_nb += 1
end
continue
elseif c == '"'
j = i + 1
while j <= length(code) && code[j] != '"'
if code[j] == '\n'
line_nb += 1
end
j += 1
end
if j > length(code)
error(line_nb, "Unclosed string")
else
push!(tokens, Token(type=STRING, lexeme=code[i + 1:j - 1], literal=code[i + 1:j - 1], line=line_nb))
i = j
end
elseif isnumeric(c)
j = i + 1
while j <= length(code) && isnumeric(code[j])
j += 1
end
if j <= length(code) && code[j] == '.'
j += 1
while j <= length(code) && isnumeric(code[j])
j += 1
end
end
push!(tokens, Token(type=NUMBER, lexeme=code[i:j - 1], literal=parse('.' in code[i:j - 1] ? Float64 : Int64 , code[i:j - 1]), line=line_nb))
i = j - 1
elseif isletter(c)
j = i + 1
while j <= length(code) && (isnumeric(code[j]) || isletter(code[j]))
j += 1
end
lexeme = code[i:j - 1]
if haskey(keywords, lexeme)
if lexeme == "true"
literal = true
elseif lexeme == "false"
literal = false
else
literal = nothing
end
push!(tokens, Token(type=keywords[lexeme], lexeme=lexeme, literal=literal, line=line_nb))
else
push!(tokens, Token(type=IDENTIFIER, lexeme=lexeme, line=line_nb))
end
i = j - 1
else
error(line_nb, "Unexpected character '$c'")
end
i += 1
end
push!(tokens, Token(type=EOF, line=line_nb))
tokens
end