forked from countingpine/tinybasic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlex.bas
More file actions
296 lines (241 loc) · 6.79 KB
/
lex.bas
File metadata and controls
296 lines (241 loc) · 6.79 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include once "compiler/src/inc/lex.bi"
#include once "compiler/src/inc/consts.bi"
#include once "compiler/src/inc/keywords.bi"
'::::::::
sub read_char _
( _
)
lexer_stack.curr_lex->look = fgetc( lexer_stack.curr_lex->hfile )
end sub
'::::::::
sub skip_white _
( _
)
while (lexer_stack.curr_lex->look = asc( " " )) or (lexer_stack.curr_lex->look = CHAR_TAB)
read_char( )
wend
end sub
'::::::::
function read_ident _
( _
) as string
dim as string result
while isalnum( lexer_stack.curr_lex->look ) or (lexer_stack.curr_lex->look = asc( "_" ))
result += chr( lexer_stack.curr_lex->look )
read_char( )
wend
function = result
end function
'::::::::
function read_litint _
( _
) as string
dim as string result
while isdigit( lexer_stack.curr_lex->look )
result += chr( lexer_stack.curr_lex->look )
read_char( )
wend
function = result
end function
'::::::::
function read_litstr _
( _
) as string
dim as string result
if lexer_stack.curr_lex->look <> CHAR_DBLQUOTE then
print !"Expected '" & chr( CHAR_DBLQUOTE ) & "'"
exit_( 1 )
end if
read_char( )
while (lexer_stack.curr_lex->look <> EOF_) and (lexer_stack.curr_lex->look <> CHAR_DBLQUOTE)
result += chr( lexer_stack.curr_lex->look )
read_char( )
wend
if lexer_stack.curr_lex->look <> CHAR_DBLQUOTE then
print !"Expected '" & chr( CHAR_DBLQUOTE ) & "'"
exit_( 1 )
end if
read_char( )
function = result
end function
sub skip_newlines _
( _
)
while (lexer_stack.curr_lex->look = 13) or (lexer_stack.curr_lex->look = 10)
if lexer_stack.curr_lex->look = 13 then
read_char( )
if lexer_stack.curr_lex->look = 10 then
read_char( )
end if
elseif lexer_stack.curr_lex->look = 10 then
read_char( )
end if
lexer_stack.curr_lex->curr_line += 1
skip_white( )
wend
end sub
'::::::::
sub read_token _
( _
)
read_token_start:
skip_white( )
lexer_stack.curr_lex->old_tk_str = lexer_stack.curr_lex->tk_str
lexer_stack.curr_lex->old_tk_typ = lexer_stack.curr_lex->tk_typ
lexer_stack.curr_lex->tk_str = "<<BAD>>"
lexer_stack.curr_lex->tk_typ = TK_BAD
dim as integer first_char = lexer_stack.curr_lex->look
if first_char = EOF_ then
lexer_stack.curr_lex->tk_str = "<<EOF>>"
lexer_stack.curr_lex->tk_typ = TK_EOF
elseif isalpha( first_char ) or (first_char = asc( "_" )) then
lexer_stack.curr_lex->tk_str = ucase( read_ident( ) )
lexer_stack.curr_lex->tk_typ = TK_IDENT
if lexer_stack.curr_lex->tk_str = "_" then
skip_newlines( )
goto read_token_start
end if
dim as integer keyword_tk
keyword_tk = keyword_find( lexer_stack.curr_lex->tk_str )
if keyword_tk <> - 1 then
lexer_stack.curr_lex->tk_typ = keyword_tk
end if
elseif isdigit( first_char ) then
lexer_stack.curr_lex->tk_str = read_litint( )
lexer_stack.curr_lex->tk_typ = TK_LITINT
elseif first_char = CHAR_DBLQUOTE then
lexer_stack.curr_lex->tk_str = read_litstr( )
lexer_stack.curr_lex->tk_typ = TK_LITSTR
elseif first_char = asc( "!" ) then
read_char( )
lexer_stack.curr_lex->tk_str = read_litstr( )
lexer_stack.curr_lex->tk_typ = TK_LITSTR
elseif (first_char = 13) or (first_char = 10) then
skip_newlines( )
lexer_stack.curr_lex->tk_str = "<<EOL>>"
lexer_stack.curr_lex->tk_typ = TK_EOL
elseif (first_char = asc( "'" )) then
while (lexer_stack.curr_lex->look <> 10) and (lexer_stack.curr_lex->look <> 13) and (lexer_stack.curr_lex->look <> EOF_)
read_char( )
wend
goto read_token_start
elseif first_char = asc( "+" ) then
lexer_stack.curr_lex->tk_str = chr( lexer_stack.curr_lex->look )
lexer_stack.curr_lex->tk_typ = lexer_stack.curr_lex->look
read_char( )
if lexer_stack.curr_lex->look = asc( "=" ) then
lexer_stack.curr_lex->tk_str = "+="
lexer_stack.curr_lex->tk_typ = TK_SELFADD
read_char( )
end if
elseif first_char = asc( "-" ) then
lexer_stack.curr_lex->tk_str = chr( lexer_stack.curr_lex->look )
lexer_stack.curr_lex->tk_typ = lexer_stack.curr_lex->look
read_char( )
if lexer_stack.curr_lex->look = asc( "=" ) then
lexer_stack.curr_lex->tk_str = "-="
lexer_stack.curr_lex->tk_typ = TK_SELFSUB
read_char( )
elseif lexer_stack.curr_lex->look = asc( ">" ) then
lexer_stack.curr_lex->tk_str = "->"
lexer_stack.curr_lex->tk_typ = TK_ARROW
read_char( )
end if
elseif first_char = asc( "<" ) then
lexer_stack.curr_lex->tk_str = chr( lexer_stack.curr_lex->look )
lexer_stack.curr_lex->tk_typ = lexer_stack.curr_lex->look
read_char( )
if lexer_stack.curr_lex->look = asc( ">" ) then
lexer_stack.curr_lex->tk_str = "<>"
lexer_stack.curr_lex->tk_typ = TK_NE
read_char( )
elseif lexer_stack.curr_lex->look = asc( "=" ) then
lexer_stack.curr_lex->tk_str = "<="
lexer_stack.curr_lex->tk_typ = TK_LE
read_char( )
end if
elseif first_char = asc( ">" ) then
lexer_stack.curr_lex->tk_str = chr( lexer_stack.curr_lex->look )
lexer_stack.curr_lex->tk_typ = lexer_stack.curr_lex->look
read_char( )
if lexer_stack.curr_lex->look = asc( "=" ) then
lexer_stack.curr_lex->tk_str = ">="
lexer_stack.curr_lex->tk_typ = TK_GE
read_char( )
end if
else
lexer_stack.curr_lex->tk_str = chr( lexer_stack.curr_lex->look )
lexer_stack.curr_lex->tk_typ = lexer_stack.curr_lex->look
read_char( )
end if
'print " // " & lexer_stack.curr_lex->tk_str
''''print "<<" & lexer_stack.curr_lex->tk_str & ">>"
end sub
'::::::::
function match _
( _
byval t as integer _
) as integer
if lexer_stack.curr_lex->tk_typ = t then
function = -1
read_token( )
else
function = 0
end if
end function
'::::::::
function match_str _
( _
byref s as string _
) as integer
if ucase( lexer_stack.curr_lex->tk_str ) = ucase( s ) then
function = -1
read_token( )
else
function = 0
end if
end function
sub lexer_push _
( _
byref file_name as string, _
byval hfile as FILE ptr _
)
if file_name = "stdin" then
lexer_stack.curr_lex = 0
lexer_stack.sp = 0
end if
if lexer_stack.sp = 16 then
print "#include depth too deep (= 16)"
exit_( 1 )
else
dim as lexer_t ptr lexer = @lexer_stack.lex(lexer_stack.sp)
lexer->file_name = file_name
lexer->hfile = hfile
lexer->curr_line = 1
lexer_stack.curr_lex = lexer
lexer_stack.sp += 1
read_char( )
read_token( )
end if
'print "/* push: now in " & lexer_stack.curr_lex->file_name & " */"
end sub
function lexer_pop _
( _
) as FILE ptr
if lexer_stack.sp = 0 then
print "#include stack fault (= 0)"
exit_( 1 )
else
lexer_stack.sp -= 1
if lexer_stack.sp = 0 then
lexer_stack.curr_lex = @lexer_stack.lex(0)
'print "/* closed all files */"
else
'print "/* about to fclose " & lexer_stack.curr_lex->hfile & "*/"
function = lexer_stack.curr_lex->hfile
'print "/* switching to " & lexer_stack.sp - 1 & " */"
lexer_stack.curr_lex = @lexer_stack.lex(lexer_stack.sp - 1)
end if
end if
'print "/* pop: now in " & lexer_stack.curr_lex->file_name & " */"
end function