def do(self, parser): # # FunctionSave activity_record_address = parser.scanner.malloc(2) start_pc_address = activity_record_address return_address_address = activity_record_address + 1 parser.semantic_stack.append(return_address_address) # write the record address to the function symbol memory parser.program.add_inst(Mnemonic.ASSIGN, immval(activity_record_address), 0) # write the start address to the first word of activity record parser.program.add_inst(Mnemonic.ASSIGN, immval(parser.program.pc + 2), start_pc_address) parser.semantic_stack.append(parser.program.pc) parser.program.add_fake_inst( ) # skip running the function on the first pass # # PullID # # Assembly temporary = parser.get_temp() parser.program.add_pop(temporary) parser.program.add_inst(Mnemonic.PRINT, temporary) # # Function parser.program.edit_inst(parser.semantic_stack.pop(), Mnemonic.JUMP, parser.program.pc + 1) parser.program.add_inst(Mnemonic.JUMP, indval(parser.semantic_stack.pop()))
def do(self, parser): function_symbol_address = parser.semantic_stack[-1] function_data_type = parser.scanner.get_token_by_address( function_symbol_address).data_type activity_record_address = parser.get_temp() return_address_address = parser.get_temp() parser.program.add_inst(Mnemonic.ASSIGN, function_symbol_address, activity_record_address) parser.program.add_inst(Mnemonic.ADD, activity_record_address, immval(1), return_address_address) parser.program.add_inst(Mnemonic.ASSIGN, immval(parser.program.pc + 3), indval(return_address_address)) start_pc = parser.get_temp() parser.program.add_inst(Mnemonic.ASSIGN, indval(activity_record_address), start_pc) parser.program.add_inst(Mnemonic.JUMP, indval(start_pc)) if function_data_type == DataType.INTEGER: return_value = parser.get_temp() parser.program.add_pop(return_value) parser.semantic_stack[-1] = return_value else: parser.semantic_stack[-1] = 'None' if parser.argument_stack[-1]: raise SemanticError('Too few arguments passed', parser.scanner) parser.argument_stack.pop()
def do(self, parser): test = parser.semantic_stack[-2] default = parser.semantic_stack[-3] is_equal = parser.get_temp() parser.program.add_inst(Mnemonic.EQUALS, parser.semantic_stack[-1], immval(parser.lookahead_token.attribute), is_equal) parser.program.add_inst(Mnemonic.JUMP_FALSE, is_equal, parser.program.pc + 3) parser.program.add_inst(Mnemonic.ASSIGN, immval(1), test) parser.program.add_inst(Mnemonic.ASSIGN, immval(0), default)
def do(self, parser): test = parser.get_temp() default = parser.get_temp() parser.program.add_inst(Mnemonic.ASSIGN, immval(1), default) parser.program.add_inst(Mnemonic.ASSIGN, immval(0), test) parser.program.add_inst(Mnemonic.JUMP, parser.program.pc + 2) parser.semantic_stack.append(parser.program.pc) parser.break_stack.append(parser.program.pc) parser.program.add_fake_inst() parser.semantic_stack.append(default) parser.semantic_stack.append(test)
def do(self, parser): main_symbol_address = parser.scanner.get_symbol_address('main') activity_record_address = parser.get_temp() return_address_address = parser.get_temp() parser.program.add_inst(Mnemonic.ASSIGN, main_symbol_address, activity_record_address) parser.program.add_inst(Mnemonic.ADD, activity_record_address, immval(1), return_address_address) parser.program.add_inst(Mnemonic.ASSIGN, immval(parser.program.pc + 3), indval(return_address_address)) start_pc = parser.get_temp() parser.program.add_inst(Mnemonic.ASSIGN, indval(activity_record_address), start_pc) parser.program.add_inst(Mnemonic.JUMP, indval(start_pc)) parser.program.add_nop()
def do(self, parser): assert parser.lookahead_token.text == 'NUM' parser.scanner.get_token_by_address( parser.semantic_stack[-1]).declaration_type = DeclarationType.ARRAY parser.scanner.analyze_semantics() addr = parser.scanner.malloc(parser.lookahead_token.attribute) parser.program.add_inst(Mnemonic.ASSIGN, immval(addr), parser.semantic_stack[-1])
def do(self, parser): parser.scanner.get_token_by_address( parser.semantic_stack[-1] ).declaration_type = DeclarationType.FUNCTION parser.function_stack += [ parser.scanner.get_token_by_address(parser.semantic_stack[-1]) ] parser.scanner.analyze_semantics() activity_record_address = parser.scanner.malloc(2) start_pc_address = activity_record_address return_address_address = activity_record_address + 1 parser.return_stack.append(return_address_address) # write the record address to the function symbol memory parser.program.add_inst(Mnemonic.ASSIGN, immval(activity_record_address), parser.semantic_stack[-1]) # write the start address to the first word of activity record parser.program.add_inst(Mnemonic.ASSIGN, immval(parser.program.pc + 2), start_pc_address) parser.semantic_stack.append(parser.program.pc) parser.program.add_fake_inst( ) # skip running the function on the first pass
def add_pop(self, arg): self.add_inst(Mnemonic.SUBTRACT, self.sp, immval(1), self.sp) self.add_inst(Mnemonic.ASSIGN, indval(self.sp), arg)
def add_push(self, arg): self.add_inst(Mnemonic.ASSIGN, arg, indval(self.sp)) self.add_inst(Mnemonic.ADD, self.sp, immval(1), self.sp)
def __init__(self, stack_address): self.insts = [] self.pc = 0 self.sp = stack_address self.add_inst(Mnemonic.ASSIGN, immval(stack_address + 1), self.sp)
def do(self, parser): assert parser.lookahead_token.text == 'NUM' parser.semantic_stack += [immval(parser.lookahead_token.attribute)]