-
Notifications
You must be signed in to change notification settings - Fork 1
/
modPARSER.bas
343 lines (273 loc) · 11.2 KB
/
modPARSER.bas
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
Attribute VB_Name = "modPARSER"
Option Explicit
Private private_section As Boolean
Sub PARSE(Optional file As String)
Dim tokens() As CML_TOKEN
Dim idx As Long
If file = Empty Then file = main_file
Assert FILE_EXISTS(file), "El archivo '[1]' no pudo ser abierto.", file
private_section = False
tokens = TOKENIZATE(FILE_READ_AS_ARRAY(file))
'ShowTokens tokens
'End
PushS source_list, file
PARSE_TOKENS tokens, idx
PopS source_list
End Sub
Private Sub PARSE_VARIABLE_DECLARATIONS(tokens() As CML_TOKEN, idx As Long, ret() As CML_VARIABLE_DEFINITION, ByVal ptype As CML_VARIABLE_PARSING_TYPE)
Dim initialpos As Long
Dim offset As Long
initialpos = UBound(ret) + 1
'If initialpos = 1 Then
ReDim Preserve ret(initialpos)
'End If
offset = ret(UBound(ret)).offset + SIZE_OF(ret(UBound(ret)))
Do While True
Select Case tokens(idx).t
Case TOKEN_TYPE_EOL, TOKEN_TYPE_EOF
Exit Do
Case TOKEN_TYPE_SEPARATOR
Select Case tokens(idx).s
Case "["
Dim expr() As CML_TOKEN
Assert ptype = variable_parsing_type_parameter, "Imposible establecer una dimensión a un parámetro."
expr = EXTRACT_FROM_SEPARATORS(tokens, idx)
Case ","
ReDim Preserve ret(UBound(ret) + 1)
ret(UBound(ret)).private = private_section
ret(UBound(ret)).file = PeekS(source_list)
Case "@"
Inc ret(UBound(ret)).pointer
Case ":"
Dim t As CML_TYPE_VARIABLE
Dim idxc As Long
Inc idx
Assert tokens(idx).t = TOKEN_TYPE_IDENTIFIER, "Se identificador del tipo de dato."
Assert IS_DATATYPE(tokens(idx).s, t, idxc), "Tipo de dato inválido: '[1]'.", tokens(idx).s
For initialpos = initialpos To UBound(ret)
If ptype = variable_parsing_type_parameter And t = var_type_compose Then
Assert ret(initialpos).pointer, "Un parámetro de tipo compuesto debe ser un puntero."
End If
ret(initialpos).datatype = t
If t = var_type_compose Then
ret(initialpos).extra = idxc
End If
Next
'Inc initialpos
End Select
Case TOKEN_TYPE_INSTRUCTION
Select Case tokens(idx).i
Case i_private
Assert ptype = variable_parsing_type_member, "Solamente se pueden privatizar miembros."
ret(UBound(ret)).private = True
End Select
Case TOKEN_TYPE_IDENTIFIER
Assert ret(UBound(ret)).name = Empty, "La variable, miembro o parámetro ya tiene nombre."
ret(UBound(ret)).uname = UString
ret(UBound(ret)).name = tokens(idx).s
Case Else
Exit Do
End Select
Inc idx
Loop
End Sub
Private Function EXTRACT_TOKENS(tokens() As CML_TOKEN, ByVal min As Long, ByVal max As Long) As CML_TOKEN()
Dim ret() As CML_TOKEN
ReDim ret(0)
While min < max
ReDim Preserve ret(UBound(ret) + 1)
ret(UBound(ret)) = tokens(min)
Inc min
Wend
EXTRACT_TOKENS = ret
End Function
Function EXTRACT_FROM_SEPARATORS(tokens() As CML_TOKEN, idx As Long, Optional min As Long, Optional max As Long) As CML_TOKEN()
Dim par As Long
Dim cor As Long
Dim lla As Long
Dim ret() As CML_TOKEN
Assert tokens(idx).t = TOKEN_TYPE_SEPARATOR, "Se esperaba separador."
If min = 0 Then
min = idx + 1
End If
Do While True
Select Case tokens(idx).t
Case TOKEN_TYPE_SEPARATOR
Select Case tokens(idx).s
Case "("
Inc par
Case ")"
Dec par
Case "["
Inc cor
Case "]"
Dec cor
Case "{"
Inc lla
Case "}"
Dec lla
End Select
End Select
Inc idx
If par = 0 And cor = 0 And lla = 0 Then
Exit Do
End If
Loop
max = idx - 1
ret = EXTRACT_TOKENS(tokens, min, max)
PushEOL ret
EXTRACT_FROM_SEPARATORS = ret
End Function
Private Sub PARSE_TOKENS(tokens() As CML_TOKEN, idx As Long)
Inc idx
Select Case tokens(idx).t
Case TOKEN_TYPE_INSTRUCTION
Select Case tokens(idx).i
Case i_private, i_public
PARSE_SECTION tokens, idx, tokens(idx).i = i_private
Case i_struct, i_union
PARSE_COMPOSE tokens, idx, tokens(idx).i = i_union
Case i_include
PARSE_INCLUDE tokens, idx
Case i_import
Case i_var
PARSE_VAR tokens, idx
Case i_if
Case i_then
Case i_proc
PARSE_PROC tokens, idx
Case i_end
PARSE_END tokens, idx
End Select
Case Else
PARSE_EXPRESSION tokens, idx
End Select
If tokens(idx).t = TOKEN_TYPE_EOF Then
Exit Sub
End If
PARSE_TOKENS tokens, idx
End Sub
Private Sub PARSE_EXPRESSION(tokens() As CML_TOKEN, idx As Long)
Dim n1 As Long
Dim n2 As Long
n1 = UBound(G_EXPRESSIONS)
n2 = UBound(G_EXPRESSIONS(n1).tokens) + 1
ReDim Preserve G_EXPRESSIONS(n1).tokens(n2)
G_EXPRESSIONS(n1).tokens(n2) = tokens(idx)
If tokens(idx).t = TOKEN_TYPE_EOL Or tokens(idx).t = TOKEN_TYPE_EOF Then
n1 = UBound(G_EXPRESSIONS)
AddNewLine i_expression, n1
ReDim Preserve G_EXPRESSIONS(n1 + 1)
ReDim Preserve G_EXPRESSIONS(n1 + 1).tokens(0)
End If
End Sub
Private Sub PARSE_SECTION(tokens() As CML_TOKEN, idx As Long, ByVal isprivate As Boolean)
Inc idx
Assert tokens(idx).t = TOKEN_TYPE_SEPARATOR, "Se esperaba separador."
Assert tokens(idx).s = ":", "Se esperaba ':'."
private_section = isprivate
End Sub
Private Sub PARSE_COMPOSE(tokens() As CML_TOKEN, idx As Long, ByVal isunion As Boolean)
Dim name As String
Dim idxc As Long
Inc idx
Assert tokens(idx).t = TOKEN_TYPE_IDENTIFIER, "Se esperaba identificador."
Assert IS_VALID_IDENTIFIER(tokens(idx).s), "Identificador inválido."
name = tokens(idx).s
idxc = UBound(G_GLOBAL_COMPOSES) + 1
Inc idx
Assert tokens(idx).t = TOKEN_TYPE_SEPARATOR, "Se esperaba separador."
Assert tokens(idx).s = ",", "Se esperaba ','."
Inc idx
ReDim Preserve G_GLOBAL_COMPOSES(idxc)
ReDim G_GLOBAL_COMPOSES(idxc).members(0)
G_GLOBAL_COMPOSES(idxc).file = PeekS(source_list)
G_GLOBAL_COMPOSES(idxc).private = private_section
G_GLOBAL_COMPOSES(idxc).name = name
PARSE_VARIABLE_DECLARATIONS tokens, idx, G_GLOBAL_COMPOSES(idxc).members, variable_parsing_type_member
End Sub
Private Sub PARSE_INCLUDE(tokens() As CML_TOKEN, idx As Long)
Inc idx
Assert tokens(idx).t = TOKEN_TYPE_STRING, "Se esperaba una cadena."
' TODO: COMPLETAR
' Añadir variables
PARSE tokens(idx).s
End Sub
Private Sub PARSE_PROC(tokens() As CML_TOKEN, idx As Long)
Dim name As String
Dim idxp As Long
Inc idx
idxp = UBound(G_GLOBAL_PROCEDURES) + 1
PushL G_PROC_IDX_ARRAY, idxp
If UBound(G_PROC_IDX_ARRAY) > 1 Then
Assert tokens(idx).t = TOKEN_TYPE_SEPARATOR Or tokens(idx).t = TOKEN_TYPE_EOL, "Se esperaba separador o salto de línea."
name = "Procedure_" + CStr(idxp)
Else
Assert tokens(idx).t = TOKEN_TYPE_IDENTIFIER, "Se esperaba identificador."
Assert IS_VALID_IDENTIFIER(tokens(idx).s), "Identificador inválido."
name = tokens(idx).s
Inc idx
End If
ReDim Preserve G_GLOBAL_PROCEDURES(idxp)
ReDim G_GLOBAL_PROCEDURES(idxp).locals(0)
ReDim G_GLOBAL_PROCEDURES(idxp).params(0)
ReDim G_GLOBAL_PROCEDURES(idxp).instructions(0)
G_GLOBAL_PROCEDURES(idxp).file = PeekS(source_list)
G_GLOBAL_PROCEDURES(idxp).name = name
If tokens(idx).t = TOKEN_TYPE_SEPARATOR Then
Select Case tokens(idx).s
Case "("
Dim variables() As CML_TOKEN
variables = EXTRACT_FROM_SEPARATORS(tokens, idx)
If UBound(variables) Then
PARSE_VARIABLE_DECLARATIONS variables, 1, G_GLOBAL_PROCEDURES(idxp).params, variable_parsing_type_parameter
End If
Case ":"
Dim t As CML_TYPE_VARIABLE
Dim idxc As Long
Dim ptr As Long
Inc idx
Assert tokens(idx).t = TOKEN_TYPE_IDENTIFIER, "Se identificador del tipo de dato."
Assert IS_DATATYPE(tokens(idx).s, t, idxc), "Tipo de dato inválido: '[1]'.", tokens(idx).s
If t = var_type_compose Then
Inc idx
Assert tokens(idx).t = TOKEN_TYPE_SEPARATOR, "Se esperaba '@'."
Assert tokens(idx).s = "@", "Se esperaba '@'."
Inc G_GLOBAL_PROCEDURES(idxp).return.pointer
End If
G_GLOBAL_PROCEDURES(idxp).return.datatype = t
Case Else
Assert False, "Se esperaba '(' o ':'."
End Select
Inc idx
End If
Assert tokens(idx).t = TOKEN_TYPE_EOL, "Se esperaba salto de línea."
AddNewLine i_proc, idxp
End Sub
Private Sub PARSE_VAR(tokens() As CML_TOKEN, idx As Long)
Dim idxp As Long
Inc idx
Select Case True
Case UBound(G_PROC_IDX_ARRAY)
idxp = PeekL(G_PROC_IDX_ARRAY)
PARSE_VARIABLE_DECLARATIONS tokens, idx, G_GLOBAL_PROCEDURES(idxp).locals, variable_parsing_type_variable
Case Else
PARSE_VARIABLE_DECLARATIONS tokens, idx, G_GLOBAL_VARIABLES, variable_parsing_type_variable
End Select
AddNewLine i_var
End Sub
Private Sub PARSE_END(tokens() As CML_TOKEN, idx As Long)
Dim idxe As Long
Inc idx
Assert tokens(idx).t = TOKEN_TYPE_INSTRUCTION, "Se esperaba instrucción a terminar."
idxe = UBound(G_ENDED) + 1
ReDim Preserve G_ENDED(idxe)
Select Case tokens(idx).i
Case i_proc
Assert UBound(G_PROC_IDX_ARRAY), "Cierre de procedimiento inválido."
PopL G_PROC_IDX_ARRAY
G_ENDED(idxe).instruction = i_proc
Case i_if
End Select
AddNewLine i_end, idxe
End Sub