def gen_main_check_function(checker_env, id_prop_map): function_name = "neco_check" builder = cyast.Builder() variable_provider = VariableProvider() checker_env.push_cvar_env() checker_env.push_variable_provider(variable_provider) marking_var = variable_provider.new_variable( variable_type=checker_env.marking_type.type) atom_var = variable_provider.new_variable( variable_type=TypeInfo.get('Int')) builder.begin_FunctionCDef( name=function_name, args=(cyast.A(marking_var.name, type=checker_env.type2str(marking_var.type)).param( atom_var.name, type=checker_env.type2str(TypeInfo.get('Int')))), returns=cyast.Name("int"), decl=[], public=True, api=True) for (i, (ident, _)) in enumerate(id_prop_map.iteritems()): if i == 0: builder.begin_If( test=cyast.Compare(left=cyast.Name(atom_var.name), ops=[cyast.Eq()], comparators=[cyast.Num(ident)])) else: builder.begin_Elif( test=cyast.Compare(left=cyast.Name(atom_var.name), ops=[cyast.Eq()], comparators=[cyast.Num(ident)])) builder.emit_Return( checker_env.get_check_function("check_{}".format(ident)).call( [cyast.Name(marking_var.name)])) for _ in id_prop_map: builder.end_If() builder.emit( cyast.Print(dest=cyast.E('sys.stderr'), values=[ cyast.Str(s='!W! invalid proposition identifier'), cyast.Name(atom_var.name) ], nl=True)) builder.emit_Return(cyast.Num(n=0)) builder.end_FunctionDef() tree = cyast.to_ast(builder) checker_env.register_check_function(function_name, FunctionWrapper(function_name, tree)) return tree
def operator_to_cyast(operator): if operator.isLT(): return cyast.Lt() elif operator.isLE(): return cyast.LtE() elif operator.isEQ(): return cyast.Eq() elif operator.isNE(): return cyast.NotEq() elif operator.isGT(): return cyast.Gt() elif operator.isGE(): return cyast.GtE() else: raise NotImplementedError(operator.__class__)
def gen_InPlace_function(checker_env, function_name, place_name): marking_type = checker_env.marking_type variable_provider = VariableProvider() checker_env.push_cvar_env() checker_env.push_variable_provider(variable_provider) builder = cyast.Builder() marking_var = variable_provider.new_variable( variable_type=marking_type.type) place_type = marking_type.get_place_type_by_name(place_name) token_var = variable_provider.new_variable( variable_type=place_type.token_type) # check_var = variable_provider.new_variable(variable_type=TypeInfo.Int) builder.begin_FunctionCDef( name=function_name, args=(cyast.A(marking_var.name, type=checker_env.type2str(marking_var.type)).param( name=token_var.name, type=checker_env.type2str(token_var.type))), returns=cyast.Name("int"), decl=[], public=False, api=False) main_body = [] loop_var = variable_provider.new_variable( variable_type=place_type.token_type) inner_body = cyast.If( cyast.Compare(cyast.Name(token_var.name), [cyast.Eq()], [cyast.Name(loop_var.name)]), [cyast.Return(cyast.Num(1))]) node = place_type.enumerate_tokens(checker_env, loop_var, marking_var, body=inner_body) main_body.append(node) main_body.append(cyast.Return(value=cyast.Num(0))) for stmt in main_body: builder.emit(stmt) builder.end_FunctionDef() return FunctionWrapper(function_name, cyast.to_ast(builder))
def compile_Match(self, node): tuple_info = node.tuple_info seq = [] component_names = [ token_info.data['local_variable'].name for token_info in tuple_info ] seq.append( cyast.Assign(targets=[ cyast.Tuple([cyast.E(name) for name in component_names]) ], value=cyast.Name( tuple_info.data['local_variable'].name))) cur = None for component in tuple_info.components: if component.is_Value: # self.try_declare_cvar(component.data['local_variable'].name, component.type) n = cyast.Builder.If( test=cyast.Builder.Compare( left=cyast.E(component.data['local_variable'].name), ops=[cyast.Eq()], comparators=[cyast.E(repr(component.raw)) ]), # TO DO unify value & pickle orelse=[]) if cur == None: cur = n seq.append(n) else: cur.body = [n] cur = n elif component.is_Variable: self.env.try_declare_cvar( component.data['local_variable'].name, component.type) if cur != None: cur.body = [self.compile(node.body)] else: seq.append(self.compile(node.body)) return seq
def compile_EQ(self, node): return cyast.Eq()