-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.asm
More file actions
414 lines (329 loc) · 8.63 KB
/
Copy pathparser.asm
File metadata and controls
414 lines (329 loc) · 8.63 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
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
.DATA
EXTERN print_error:PROC
EXTERN print_string:PROC
EXTERN print_number:PROC
; Debug messages
debug_parsing_comment db "DEBUG: Parsing comment", 13, 10, 0
debug_parsing_identifier db "DEBUG: Parsing identifier: ", 0
debug_parsing_print db "DEBUG: Parsing print statement", 13, 10, 0
debug_var_in_print db "DEBUG: Found variable in print: ", 0
debug_fstring_start db "DEBUG: Found f-string start", 13, 10, 0
debug_rparen db "DEBUG: Found right parenthesis", 13, 10, 0
debug_newline db 13, 10, 0
EXTERN token_buffer:BYTE
EXTERN current_pos:QWORD
EXTERN get_next_token:PROC
; Import token types from lexer
EXTERN TOKEN_EOF:ABS
EXTERN TOKEN_IDENTIFIER:ABS
EXTERN TOKEN_STRING:ABS
EXTERN TOKEN_EQUALS:ABS
EXTERN TOKEN_LPAREN:ABS
EXTERN TOKEN_RPAREN:ABS
EXTERN TOKEN_COMMENT:ABS
EXTERN TOKEN_NEWLINE:ABS
EXTERN TOKEN_FUNCTION:ABS
EXTERN TOKEN_FSTRING:ABS
EXTERN TOKEN_LCURLY_BRACE:ABS
EXTERN TOKEN_RCURLY_BRACE:ABS
; Operation types
OP_INPUT equ 1
OP_PRINT equ 2
; Data structures (simplified)
operations db 32 * 72 dup(0) ; Each operation: type(8) + var_count(8) + var_indices(8*8)
op_count dq 0
string_table db 32 * 256 dup(0)
string_count dq 0
symbol_table db 32 * 256 dup(0)
symbol_count dq 0
current_token dq 0
; Make these public for the code generator
PUBLIC operations
PUBLIC op_count
PUBLIC string_table
PUBLIC string_count
PUBLIC symbol_table
PUBLIC symbol_count
.CODE
; Simplified add_symbol - assumes valid input
add_symbol PROC
push rbp
mov rbp, rsp ; Create stack frame
sub rsp, 20h ; Allocate shadow space + alignment
push rbx
push rsi
push rdi
; Save pointer parameter to local variable
mov [rbp-8], rdx ; Store original rdx value safely on stack
; Get destination in symbol table
mov rax, [symbol_count]
mov rbx, 256
mul rbx ; RAX = offset into symbol table, RDX modified here
lea rdi, symbol_table
add rdi, rax
mov rdx, [rbp-8] ; Restore original pointer from stack
mov rsi, rdx ; source string in rdx/rsi
; First verify rdx is not null
test rdx, rdx
jz add_symbol_error
; Add safety bounds check
cmp QWORD PTR [symbol_count], 32
jae add_symbol_error
copy_sym:
; Bounds check for source string
mov al, [rsi]
mov [rdi], al
test al, al
jz copy_done
inc rsi
inc rdi
; Bounds check - ensure we don't exceed 256 bytes
mov rcx, rdi
sub rcx, rax ; Calculate how many bytes we've copied
cmp rcx, 256
jae copy_done ; If we've copied 256 bytes, force termination
jmp copy_sym
copy_done:
mov rax, [symbol_count] ; Return current index
inc QWORD PTR [symbol_count]
pop rdi
pop rsi
pop rbx
mov rsp, rbp
pop rbp
ret
add_symbol_error:
mov rax, -1 ; Return -1 to indicate error
pop rdi
pop rsi
pop rbx
mov rsp, rbp
pop rbp
ret
add_symbol ENDP
; Find symbol - returns index in RAX, -1 if not found
find_symbol PROC
push rbp
mov rbp, rsp
sub rsp, 20h ; Shadow space + alignment
push rbx
push rsi
push rdi
; Save input pointer
mov [rbp-8], rdx ; Store original pointer safely
xor rbx, rbx ; Initialize counter
find_loop:
cmp rbx, [symbol_count]
jae not_found
mov rax, rbx
mov rcx, 256
mul rcx ; This modifies RDX
lea rdi, symbol_table
add rdi, rax
mov rdx, [rbp-8] ; Restore original pointer
mov rsi, rdx ; Set up source pointer
compare:
mov al, [rsi]
mov ah, [rdi]
test al, al
jz check_end
cmp al, ah
jne next_sym
inc rsi
inc rdi
jmp compare
check_end:
test ah, ah
jnz next_sym
mov rax, rbx
jmp find_done
next_sym:
inc rbx
jmp find_loop
not_found:
mov rax, -1
find_done:
pop rdi
pop rsi
pop rbx
mov rsp, rbp
pop rbp
ret
find_symbol ENDP
; Handle input statement
parse_input PROC
push rbx
push rsi
push rdi
; Save variable index
mov rbx, rax
; Skip '('
call get_next_token
; Get prompt string
call get_next_token
; Store string
mov rax, [string_count]
mov rcx, 256
mul rcx
lea rdi, string_table
add rdi, rax
lea rsi, token_buffer
copy_str:
mov al, [rsi]
mov [rdi], al
test al, al
jz str_done
inc rsi
inc rdi
jmp copy_str
str_done:
; Create operation record
mov rax, [op_count]
mov rcx, 16
mul rcx
lea rdi, operations
add rdi, rax
mov QWORD PTR [rdi], OP_INPUT
mov QWORD PTR [rdi+8], rbx
inc QWORD PTR [string_count]
inc QWORD PTR [op_count]
; Skip ')'
call get_next_token
pop rdi
pop rsi
pop rbx
ret
parse_input ENDP
; Handle print statement with f-string
parse_print PROC
push rbp
mov rbp, rsp
sub rsp, 40h ; Allocate shadow space + local variables
push rbx
push rsi
push rdi
; Debug print for print statement start (only once)
lea rcx, debug_parsing_print
call print_string
; Skip '('
call get_next_token
; Create operation record
mov rax, [op_count]
mov rcx, 72 ; Size: 8 (type) + 8 (var_count) + 8*8 (var_indices)
mul rcx
lea rdi, operations
add rdi, rax
mov QWORD PTR [rdi], OP_PRINT ; Set operation type
mov QWORD PTR [rdi+8], 0 ; Initialize var_count to 0
; Look for f-string
call get_next_token
cmp rax, TOKEN_FSTRING
jne not_fstring
; Debug print for f-string
lea rcx, debug_fstring_start
call print_string
; Process until ')'
parse_print_loop:
call get_next_token
cmp rax, TOKEN_RPAREN
je print_done
cmp rax, TOKEN_LCURLY_BRACE
jne parse_print_loop
; Handle variable interpolation
mov rbx, [rdi+8] ; Get current var_count
cmp rbx, 8 ; Check if we have room for another variable
jae skip_var ; Skip if too many variables
call get_next_token
cmp rax, TOKEN_IDENTIFIER
jne skip_var
; Debug print for found variable
push rax
lea rcx, debug_var_in_print
call print_string
lea rcx, token_buffer
call print_string
lea rcx, debug_newline
call print_string
pop rax
; Look up variable in symbol table
lea rdx, token_buffer
call find_symbol
; Calculate offset for this variable index
mov rcx, rbx ; Current var_count
shl rcx, 3 ; Multiply by 8
add rcx, 16 ; Skip past type and var_count
; Store variable index
mov [rdi + rcx], rax ; Store symbol index
inc QWORD PTR [rdi+8] ; Increment var_count
skip_var:
; Skip '}'
call get_next_token
jmp parse_print_loop
print_done:
; Debug print for right parenthesis
lea rcx, debug_rparen
call print_string
inc QWORD PTR [op_count]
mov rsp, rbp
pop rbp
ret
not_fstring:
mov rsp, rbp
pop rbp
ret
parse_print ENDP
; Main parsing procedure
parse_program PROC
push rbp
mov rbp, rsp
mov QWORD PTR [current_pos], 0
parse_loop:
call get_next_token
mov [current_token], rax
; Check only tokens that can start statements
cmp rax, TOKEN_EOF
je parse_done
cmp rax, TOKEN_COMMENT ; Skip comments
jne not_comment
lea rcx, debug_parsing_comment
call print_string
jmp parse_loop
not_comment:
cmp rax, TOKEN_NEWLINE ; Skip newlines
je parse_loop
cmp rax, TOKEN_FUNCTION ; print statement
je handle_print
cmp rax, TOKEN_IDENTIFIER ; variable assignment
je handle_assignment
jmp parse_loop ; Skip any other tokens
handle_print:
lea rcx, debug_parsing_print
call print_string
call parse_print
jmp parse_loop
handle_assignment:
lea rdx, token_buffer ; Get variable name
push rdx
lea rcx, debug_parsing_identifier
call print_string
mov rcx, rdx
call print_string
lea rcx, debug_newline
call print_string
call get_next_token ; Should be '='
pop rdx
call find_symbol ; Find or add symbol
cmp rax, -1
jne assignment_existing
call add_symbol
dec rax
assignment_existing:
call get_next_token ; Should be 'input'
call parse_input
jmp parse_loop
parse_done:
mov rsp, rbp
pop rbp
ret
parse_program ENDP
END