Example #1
0
def compile_assign(self):
    code = []
    local_syms = []
    with ExprContext(code, local_syms):
        if isinstance(self.args[0], ivy_ast.Tuple):
            args = [sortify_with_inference(a) for a in self.args]
            if not isinstance(args[1], ivy_ast.Tuple) or len(
                    args[0].args) != len(args[1].args):
                raise IvyError(self, "wrong number of values in assignment")
            for lhs, rhs in zip(args[0].args, args[1].args):
                code.append(AssignAction(lhs, rhs))
        else:
            with top_sort_as_default():
                args = [a.compile() for a in self.args]
            if isinstance(args[1], ivy_ast.Tuple):
                raise IvyError(self, "wrong number of values in assignment")
            with ASTContext(self):
                teq = sort_infer(Equals(*args))
            args = list(teq.args)
            code.append(AssignAction(*args))
        for c in code:
            c.lineno = self.lineno
        if len(code) == 1:
            return code[0]
        res = LocalAction(*(local_syms + [Sequence(*code)]))
        res.lineno = self.lineno
        return res
Example #2
0
def compile_assign(self):
    code = []
    local_syms = []
    with ExprContext(code,local_syms):
        if isinstance(self.args[0],ivy_ast.Tuple):
            args = [sortify_with_inference(a) for a in self.args]
            if not isinstance(args[1],ivy_ast.Tuple) or len(args[0].args) != len(args[1].args):
                raise IvyError(self,"wrong number of values in assignment");
            for lhs,rhs in zip(args[0].args,args[1].args):
                code.append(AssignAction(lhs,rhs))
        else:
            with top_sort_as_default():
                args = [a.compile() for a in self.args]
            if isinstance(args[1],ivy_ast.Tuple):
                raise IvyError(self,"wrong number of values in assignment");
            with ASTContext(self):
                teq = sort_infer(Equals(*args))
            args = list(teq.args)
            code.append(AssignAction(*args))
        for c in code:
            c.lineno = self.lineno
        if len(code) == 1:
            return code[0]
        res = LocalAction(*(local_syms + [Sequence(*code)]))
        res.lineno = self.lineno
        return res
Example #3
0
def p_action_local_params_lcb_action_rcb(p):
    "action : LOCAL params LCB action RCB"
    # we rename the locals to avoid name capture
    lsyms = [s.prefix("loc:") for s in p[2]]
    subst = dict((x.rep, y.rep) for x, y in zip(p[2], lsyms))
    action = subst_prefix_atoms_ast(p[4], subst, None, None)
    p[0] = LocalAction(*(lsyms + [action]))
    p[0].lineno = get_lineno(p, 1)
Example #4
0
def compile_local(self):
    sig = ivy_logic.sig.copy()
    with sig:
        ls = self.args[0:-1]
        cls = [compile_const(v, sig) for v in ls]
        res = LocalAction(*(cls + [sortify(self.args[-1])]))
        res.lineno = self.lineno
        return res
Example #5
0
 def extract(self):
     for c in self.code:
         c.lineno = self.lineno
     if len(self.code) == 1:
         return self.code[0]
     res = LocalAction(*(self.local_syms + [Sequence(*self.code)]))
     res.lineno = self.lineno
     return res
Example #6
0
 def extract(self):
     for c in self.code:
         c.lineno = self.lineno
     if len(self.code) == 1:
         return self.code[0]
     res = LocalAction(*(self.local_syms + [Sequence(*self.code)]))
     res.lineno = self.lineno
     return res
Example #7
0
def compile_local(self):
    sig = ivy_logic.sig.copy()
    with sig:
        ls = self.args[0:-1]
        cls = [compile_const(v,sig) for v in ls]
        res = LocalAction(*(cls+[sortify(self.args[-1])]))
        res.lineno = self.lineno
        return res
Example #8
0
def p_action_local_params_lcb_action_rcb(p):
    'action : LOCAL lparams LCB action RCB'
    # we rename the locals to avoid name capture
    lsyms = [s.prefix('loc:') for s in p[2]]
    subst = dict((x.rep, y.rep) for x, y in zip(p[2], lsyms))
    action = subst_prefix_atoms_ast(p[4], subst, None, None)
    p[0] = LocalAction(*(lsyms + [action]))
    p[0].lineno = get_lineno(p, 1)
Example #9
0
def lower_var_stmts(stmts):
    for idx, stmt in enumerate(stmts):
        if isinstance(stmt, VarAction):
            lhs = stmt.args[0]
            rhs = stmt.args[1] if len(stmt.args) > 1 else None
            lsym = lhs.prefix('loc:')
            subst = {lhs.rep: lsym.rep}
            lines = lower_var_stmts(stmts[idx + 1:])
            lines = [
                subst_prefix_atoms_ast(s, subst, None, None) for s in lines
            ]
            if rhs is not None:
                asgn = AssignAction(lsym, rhs)
                asgn.lineno = stmt.lineno
            else:
                asgn = lsym
            body = Sequence(*lines)
            body.lineno = stmt.lineno
            res = LocalAction(*[asgn, body])
            res.lineno = body.lineno
            return stmts[:idx] + [res]
    return stmts
Example #10
0
def compile_assign(self):
    code = []
    local_syms = []
    with ExprContext(code,local_syms):
        args = [sortify_with_inference(a) for a in self.args]
        if isinstance(args[0],ivy_ast.Tuple):
            if not isinstance(args[1],ivy_ast.Tuple) or len(args[0].args) != len(args[1].args):
                raise IvyError(self,"wrong number of values in assignment");
            for lhs,rhs in zip(args[0].args,args[1].args):
                code.append(AssignAction(lhs,rhs))
        elif isinstance(args[1],ivy_ast.Tuple):
            raise IvyError(self,"wrong number of values in assignment");
        else:
            code.append(AssignAction(*args))
        if len(code) == 1:
            return code[0]
        return LocalAction(*(local_syms + [Sequence(*code)]))
Example #11
0
def compile_local(self):
    sig = ivy_logic.sig.copy()
    with sig:
        ls = self.args[0:-1]
        if len(ls) == 1 and isinstance(ls[0], AssignAction):
            v = ls[0]
            lhs = v.args[0]
            rhs = v.args[1]
            # temporarily rename lhs symbol in case it clashes with an existing symbol
            #            tmp_lhs = lhs.prefix('__var_tmp:')
            tmp_lhs = lhs
            code = []
            local_syms = []
            with ExprContext(code, local_syms):
                with alpha_sort_as_default():
                    sym = compile_const(tmp_lhs, sig)
                    ctmp_lhs = tmp_lhs.compile()
                    crhs = rhs.compile()
            with ASTContext(self):
                teq = sort_infer(Equals(ctmp_lhs, crhs))
            clhs, crhs = list(teq.args)
            #            clhs = clhs.drop_prefix('__var_tmp:')
            asgn = v.clone([clhs, crhs])
            ivy_logic.remove_symbol(sym)
            if clhs.rep.name in sig.symbols:
                del sig.symbols[clhs.rep.name]  # shadow the existing symbol
            ivy_logic.add_symbol(clhs.rep.name, clhs.rep.sort)
            body = sortify(self.args[-1])
            lines = body.args if isinstance(body, Sequence) else [body]
            body = Sequence(*([asgn] + lines))
            code.append(LocalAction(clhs.rep, body))
            for c in code:
                c.lineno = self.lineno
            if len(code) == 1:
                return code[0]
            res = LocalAction(*(local_syms + [Sequence(*code)]))
            res.lineno = self.lineno
            return res
        cls = [compile_const(v, sig) for v in ls]
        body = sortify(self.args[-1])
        res = LocalAction(*(cls + [body]))
        res.lineno = self.lineno
        return res