@@ -19,7 +19,7 @@ def ss_top():
1919
2020
2121def code_gen (token , action ):
22- print (action )
22+ # print(action)
2323 return {
2424 "numeric_label" : numeric_label ,
2525 "break_start" : start_break ,
@@ -54,7 +54,7 @@ def code_gen(token, action):
5454 "mult" : mult ,
5555 "output" : output ,
5656 "call_function" : call_function ,
57- "access_array_index" : access_array_index ,
57+ "access_array_index" : assign_array_index ,
5858 "assign_array_index" : assign_array_index ,
5959 }.get (action )(token )
6060
@@ -142,7 +142,10 @@ def end_break(*args):
142142
143143
144144def get_temp (* args ):
145- SS .append (memory .get_temp_address ())
145+ t = memory .get_temp_address ()
146+ SS .append (t )
147+ program_block .add_instruction (program_block .i , 'ASSIGN' , '#0' , str (t ))
148+ program_block .forward ()
146149 # todo: ask for zero in PB
147150
148151
@@ -189,7 +192,9 @@ def func_backpatching(*args):
189192 if func_name .lower () != 'main' :
190193 program_block .add_instruction (end_of_func , 'JP' , str (program_block .i ))
191194 else :
192- program_block .add_instruction (end_of_func , 'JP' , str (int (end_of_func ) + 1 ))
195+ t = memory .get_temp_address ()
196+ program_block .add_instruction (end_of_func , 'ADD' , '#0' , '#0' , t )
197+ # program_block.add_instruction(end_of_func, 'JP', str(int(end_of_func) + 1))
193198 program_block .forward ()
194199
195200
@@ -290,7 +295,7 @@ def call_function(*args):
290295 program_block .add_instruction (program_block .i , 'ASSIGN' , f'#{ program_block .i + 2 } ' , atts [args_length + 1 ])
291296 program_block .forward ()
292297 # jp to func
293- program_block .add_instruction (program_block .i , 'JP' , atts [0 ])
298+ program_block .add_instruction (program_block .i , 'JP' , atts [0 ] + 1 )
294299 program_block .forward ()
295300 # pop all from SS
296301 for _ in range (args_length + 1 ):
@@ -301,10 +306,45 @@ def call_function(*args):
301306 SS .append (t )
302307 program_block .forward ()
303308
304-
305- def access_array_index (* args ):
306- pass
307-
309+ # def call_function(*args):
310+ # if SS[len(SS) - 1] == 'output':
311+ # return
312+ # function_attributes = []
313+ # for j in range(len(SS) - 1, -1, -1):
314+ # if isinstance(SS[j], list):
315+ # function_attributes = SS[j]
316+ # input_size = len(function_attributes) - 3
317+ # # assign function inputs
318+ # for j in range(input_size):
319+ # program_block.add_instruction(program_block.i, 'ASSIGN', SS[len(SS) - input_size + j], function_attributes[j + 1])
320+ # program_block.forward()
321+ # # assign return address
322+ # program_block.add_instruction(program_block.i, 'ASSIGN', f'#{program_block.i + 2}', function_attributes[input_size + 1])
323+ # program_block.forward()
324+ # # go to function
325+ # program_block.add_instruction(program_block.i, 'JP', function_attributes[0] + 1)
326+ # program_block.forward()
327+ # for j in range(input_size + 1):
328+ # SS.pop()
329+ # # create a new variable and assign function output to it
330+ # address = memory.get_temp_address()
331+ # SS.append(address)
332+ # program_block.add_instruction(program_block.i, 'ASSIGN', function_attributes[input_size + 2], address)
333+ # program_block.forward()
334+
335+ # def access_array_index(*args):
336+ # pass
308337
309338def assign_array_index (* args ):
310- pass
339+ index = SS .pop ()
340+ address = SS .pop ()
341+
342+ t1 = str (memory .get_temp_address ())
343+ program_block .add_instruction (program_block .i , 'MULT' , '#4' , index , t1 )
344+ program_block .forward ()
345+ t2 = str (memory .get_temp_address ())
346+ program_block .add_instruction (program_block .i , 'ASSIGN' , address , t2 )
347+ program_block .forward ()
348+ program_block .add_instruction (program_block .i , 'ADD' , t2 , t1 , t1 )
349+ program_block .forward ()
350+ SS .append ('@' + t1 )
0 commit comments