def add_constructor(self, type_defaults, context): child_context = context.create_child() child_context.self = child_context.define_local() function_name = type_defaults.class_name + '.ctor' argument_list = [ast_cil.CILArgument('self', child_context.self)] # localvars = [child_context.self] localvars = [] code = [] for feature_attr in type_defaults.defaults: attr_offset = self.cilProgram.dotTYPES.types[ self.current_type.name].attributes.get( feature_attr.name).offset if feature_attr.type_attribute in ['Int', 'Bool']: local_var = child_context.define_local() localvars.append(local_var) local_tag = child_context.define_local( self.types_dict[feature_attr.type_attribute]) localvars.append(local_tag) cil_allocate = ast_cil.CILAllocate(local_tag) cil_assign1 = ast_cil.CILAssignment(local_var, cil_allocate) code.append(cil_assign1) cil_setattr1 = ast_cil.CILSetAttr(child_context.self, attr_offset, local_var) code.append(cil_setattr1) elif feature_attr.type_attribute == 'String': local_var = child_context.define_local() localvars.append(local_var) cil_str = ast_cil.CILString() cil_assign2 = ast_cil.CILAssignment(local_var, cil_str) code.append(cil_assign2) cil_setattr2 = ast_cil.CILSetAttr(child_context.self, attr_offset, local_var) code.append(cil_setattr2) for feature_attr in type_defaults.defaults: if feature_attr.expression is not None: attr_offset = self.cilProgram.dotTYPES.types[ self.current_type.name].attributes.get( feature_attr.name).offset self.visit(feature_attr.expression, child_context) localvars += feature_attr.expression.locals code += feature_attr.expression.code cil_setattr3 = ast_cil.CILSetAttr( child_context.self, attr_offset, feature_attr.expression.value) code.append(cil_setattr3) code.append(ast_cil.CILReturn(child_context.self)) cil_func = ast_cil.CILFunction(function_name, argument_list, localvars, code) self.cilProgram.dotCODE.append(cil_func)
def visit(self, node, context): self.visit(node.expression, context) code = node.expression.code localvars = node.expression.locals local_one = context.define_local(1) localvars.append(local_one) local_bool_content = context.define_local() localvars.append(local_bool_content) cil_getattr = ast_cil.CILGetAttr(node.expression.value, 0) cil_assign1 = ast_cil.CILAssignment(local_bool_content, cil_getattr) code.append(cil_assign1) local_bool_value = context.define_local() localvars.append(local_bool_value) cil_neg = ast_cil.CILMinus(local_one, local_bool_content) cil_assign2 = ast_cil.CILAssignment(local_bool_value, cil_neg) code.append(cil_assign2) local_value = context.define_local() localvars.append(local_value) local_bool_tag = context.define_local(self.types_dict['Bool']) localvars.append(local_bool_tag) cil_allocate = ast_cil.CILAllocate(local_bool_tag) cil_assign3 = ast_cil.CILAssignment(local_value, cil_allocate) cil_setattr = ast_cil.CILSetAttr(local_value, 0, local_bool_value) code += [cil_assign3, cil_setattr] node.value = local_value node.locals = localvars node.code = code
def visit(self, node, context): self.visit(node.expression, context) localvars = node.expression.locals code = node.expression.code vinfo = context.find_variable(node.instance.name) if vinfo is None: # check if this variable is an attr attr = self.cilProgram.dotTYPES.types[ self.current_type.name].attributes.get(node.instance.name) if attr is None: # throw an error because var is not defined(this shouldn't happend) raise Exception('Attr is not defined.') cil_setattr = ast_cil.CILSetAttr(context.self, attr.offset, node.expression.value) code.append(cil_setattr) else: cil_assign = ast_cil.CILAssignment( vinfo.cil_name, ast_cil.CILVar(node.expression.value)) code.append(cil_assign) node.value = node.expression.value node.locals = localvars node.code = code
def visit(self, node, context): self.visit(node.left_expression, context) code = node.left_expression.code localvars = node.left_expression.locals self.visit(node.right_expression, context) code += node.right_expression.code localvars += node.right_expression.locals local_value = context.define_local() localvars.append(local_value) local_bool_tag = context.define_local(self.types_dict['Bool']) localvars.append(local_bool_tag) cil_allocate = ast_cil.CILAllocate(local_bool_tag) cil_assign1 = ast_cil.CILAssignment(local_value, cil_allocate) code.append(cil_assign1) local_bool_content = context.define_local() localvars.append(local_bool_content) cil_eq = ast_cil.CILEqual(node.left_expression.value, node.right_expression.value) cil_assign3 = ast_cil.CILAssignment(local_bool_content, cil_eq) cil_setattr_bool = ast_cil.CILSetAttr(local_value, 0, local_bool_content) code += [cil_assign3, cil_setattr_bool] node.value = local_value node.locals = localvars node.code = code
def visit(self, node, context): self.visit(node.left_expression, context) code = node.left_expression.code localvars = node.left_expression.locals self.visit(node.right_expression, context) code += node.right_expression.code localvars += node.right_expression.locals local_int_left = context.define_local() localvars.append(local_int_left) cil_getattr1 = ast_cil.CILGetAttr(node.left_expression.value, 0) cil_assign1 = ast_cil.CILAssignment(local_int_left, cil_getattr1) code.append(cil_assign1) local_int_right = context.define_local() localvars.append(local_int_right) cil_getattr2 = ast_cil.CILGetAttr(node.right_expression.value, 0) cil_assign2 = ast_cil.CILAssignment(local_int_right, cil_getattr2) code.append(cil_assign2) self.num_labels += 1 cil_notnone_label = ast_cil.CILLabel('NOT_NONE' + str(self.num_labels)) self.num_labels += 1 cil_divisionby0_label = ast_cil.CILLabel('DIVISION_BY_0' + str(self.num_labels)) cil_condition = ast_cil.CILCondition(local_int_right, cil_notnone_label.label) code.append(cil_condition) code.append(cil_divisionby0_label) # DIVISION_BY_0 cil_goto = ast_cil.CILGoTo('_divide_by_0') code.append(cil_goto) code.append(cil_notnone_label) # NOT_NONE local_int_content = context.define_local() localvars.append(local_int_content) cil_div = ast_cil.CILDiv(local_int_left, local_int_right) cil_assign3 = ast_cil.CILAssignment(local_int_content, cil_div) code.append(cil_assign3) local_value = context.define_local() localvars.append(local_value) local_int_tag = context.define_local(self.types_dict['Int']) localvars.append(local_int_tag) cil_allocate = ast_cil.CILAllocate(local_int_tag) cil_assign4 = ast_cil.CILAssignment(local_value, cil_allocate) cil_setattr = ast_cil.CILSetAttr(local_value, 0, local_int_content) code += [cil_assign4, cil_setattr] node.value = local_value node.locals = localvars node.code = code
def visit(self, node, context): self.visit(node.expression, context) code = node.expression.code localvars = node.expression.locals self.num_labels += 1 cil_false_label = ast_cil.CILLabel('FALSE' + str(self.num_labels)) self.num_labels += 1 cil_end_label = ast_cil.CILLabel('END' + str(self.num_labels)) local_value = context.define_local() localvars.append(local_value) local_bool_tag = context.define_local(self.types_dict['Bool']) localvars.append(local_bool_tag) cil_allocate = ast_cil.CILAllocate(local_bool_tag) cil_assign = ast_cil.CILAssignment(local_value, cil_allocate) code.append(cil_assign) cil_condition = ast_cil.CILCondition(node.expression.value, cil_false_label.label) code.append(cil_condition) # TRUE local_true_content = context.define_local(1) localvars.append(local_true_content) cil_setattr_true = ast_cil.CILSetAttr(local_value, 0, local_true_content) code.append(cil_setattr_true) cil_goto = ast_cil.CILGoTo(cil_end_label.label) code.append(cil_goto) code.append(cil_false_label) # FALSE local_false_content = context.define_local(0) localvars.append(local_false_content) cil_setattr_false = ast_cil.CILSetAttr(local_value, 0, local_false_content) code.append(cil_setattr_false) code.append(cil_end_label) #END node.value = local_value node.locals = localvars node.code = code
def visit(self, node, context): local_int_content = context.define_local(int(node.int_token)) local_int_tag = context.define_local(self.types_dict['Int']) local_value = context.define_local() cil_allocate = ast_cil.CILAllocate(local_int_tag) cil_assign = ast_cil.CILAssignment(local_value, cil_allocate) cil_setattr = ast_cil.CILSetAttr(local_value, 0, local_int_content) node.value = local_value node.locals = [local_value, local_int_content, local_int_tag] node.code = [cil_assign, cil_setattr]
def visit(self, node, context): self.visit(node.left_expression, context) code = node.left_expression.code localvars = node.left_expression.locals self.visit(node.right_expression, context) code += node.right_expression.code localvars += node.right_expression.locals local_int_left = context.define_local() localvars.append(local_int_left) cil_getattr1 = ast_cil.CILGetAttr(node.left_expression.value, 0) cil_assign1 = ast_cil.CILAssignment(local_int_left, cil_getattr1) code.append(cil_assign1) local_int_right = context.define_local() localvars.append(local_int_right) cil_getattr2 = ast_cil.CILGetAttr(node.right_expression.value, 0) cil_assign2 = ast_cil.CILAssignment(local_int_right, cil_getattr2) code.append(cil_assign2) local_int_content = context.define_local() localvars.append(local_int_content) cil_mult = ast_cil.CILMult(local_int_left, local_int_right) cil_assign3 = ast_cil.CILAssignment(local_int_content, cil_mult) code.append(cil_assign3) local_value = context.define_local() localvars.append(local_value) local_int_tag = context.define_local(self.types_dict['Int']) localvars.append(local_int_tag) cil_allocate = ast_cil.CILAllocate(local_int_tag) cil_assign4 = ast_cil.CILAssignment(local_value, cil_allocate) cil_setattr = ast_cil.CILSetAttr(local_value, 0, local_int_content) code += [cil_assign4, cil_setattr] node.value = local_value node.locals = localvars node.code = code