def _process_rule(non_terminals, nt_type, syn_ctx, arg_vars, var_map, synth_fun, rule_data): ph_let_bound_vars, let_bound_vars = [], [] if type(rule_data) == tuple: value = sexp_to_value(rule_data) ret = grammars.ExpressionRewrite(exprs.ConstantExpression(value)) elif rule_data[0] == 'Constant': typ = sexp_to_type(rule_data[1]) ret = grammars.NTRewrite('Constant' + str(typ), typ) elif rule_data[0] in [ 'Variable', 'InputVariable', 'LocalVariable' ]: raise NotImplementedError('Variable rules in grammars') elif type(rule_data) == str: if rule_data in [ a.variable_info.variable_name for a in arg_vars ]: (parameter_position, variable) = next((i, x) for (i, x) in enumerate(arg_vars) if x.variable_info.variable_name == rule_data) expr = exprs.FormalParameterExpression(synth_fun, variable.variable_info.variable_type, parameter_position) ret = grammars.ExpressionRewrite(expr) elif rule_data in non_terminals: ret = grammars.NTRewrite(rule_data, nt_type[rule_data]) elif rule_data in var_map: ret = grammars.ExpressionRewrite(var_map[rule_data]) else: # Could be a 0 arity function func = syn_ctx.make_function(rule_data) if func != None: ret = grammars.ExpressionRewrite(syn_ctx.make_function_expr(rule_data)) else: # Could be a let bound variable bound_var_ph = exprs.VariableExpression(exprs.VariableInfo(exprtypes.BoolType(), 'ph_' + rule_data)) ph_let_bound_vars.append(bound_var_ph) ret = grammars.ExpressionRewrite(bound_var_ph) elif type(rule_data) == list: function_name = rule_data[0] if function_name != 'let': function_args = [] for child in rule_data[1:]: ph_lbv, lbv, arg = _process_rule(non_terminals, nt_type, syn_ctx, arg_vars, var_map, synth_fun, child) ph_let_bound_vars.extend(ph_lbv) let_bound_vars.extend(lbv) function_args.append(arg) function_arg_types = tuple([ x.type for x in function_args ]) function = syn_ctx.make_function(function_name, *function_arg_types) else: def child_processing_func(rd, syn_ctx, new_var_map): ph_lbv, lbv, a = _process_rule(non_terminals, nt_type, syn_ctx, arg_vars, new_var_map, synth_fun, rd) ph_let_bound_vars.extend(ph_lbv) let_bound_vars.extend(lbv) return a def get_return_type(r): return r.type function, function_args = sexp_to_let(rule_data, syn_ctx, child_processing_func, get_return_type, var_map) let_bound_vars.extend(function.binding_vars) assert function is not None ret = grammars.FunctionRewrite(function, *function_args) else: raise Exception('Unknown right hand side: %s' % rule_data) return ph_let_bound_vars, let_bound_vars, ret
def _merge_grammars(sf_grammar_list): start = "MergedStart" nts = [start] nt_type = {} rules = {} starts = [] for sf_name, sf_obj, grammar in sf_grammar_list: renamed_grammar = grammar.add_prefix(sf_name) nts.extend(renamed_grammar.non_terminals) nt_type.update(renamed_grammar.nt_type) rules.update(renamed_grammar.rules) starts.append(renamed_grammar.start) comma_function = semantics_core.CommaFunction([ nt_type[s] for s in starts ]) rules[start] = [ grammars.FunctionRewrite(comma_function, *tuple([ grammars.NTRewrite(s, nt_type[s]) for s in starts ])) ] nt_type[start] = None merged_grammar = grammars.Grammar(nts, nt_type, rules, start) return merged_grammar