Search in sources :

Example 21 with Atom

use of suite.node.Atom in project suite by stupidsing.

the class FunInstructionExecutor method handle.

@Override
protected void handle(Exec exec, Instruction insn) {
    Activation current = exec.current;
    Frame frame = current.frame;
    Object[] regs = frame != null ? frame.registers : null;
    Object[] ds = exec.stack;
    int dsp = exec.sp;
    Node n0, n1, result;
    Data<?> data;
    switch(insn.insn) {
        case CALLINTRINSIC_:
            data = (Data<?>) ds[--dsp];
            List<Node> ps = new ArrayList<>(3);
            for (int i = 1; i < insn.op1; i++) ps.add((Node) ds[--dsp]);
            result = Data.<Intrinsic>get(data).invoke(intrinsicCallback, ps);
            break;
        case COMPARE_______:
            n0 = (Node) ds[--dsp];
            n1 = (Node) ds[--dsp];
            result = Int.of(comparer.compare(n0, n1));
            break;
        case CONSLIST______:
            n0 = (Node) ds[--dsp];
            n1 = (Node) ds[--dsp];
            result = Tree.of(TermOp.OR____, n0, n1);
            break;
        case CONSPAIR______:
            n0 = (Node) ds[--dsp];
            n1 = (Node) ds[--dsp];
            result = Tree.of(TermOp.AND___, n0, n1);
            break;
        case DATACHARS_____:
            result = new Data<>(To.chars(((Str) regs[insn.op1]).value));
            break;
        case GETINTRINSIC__:
            Atom atom = (Atom) ds[--dsp];
            String intrinsicName = atom.name.split("!")[1];
            result = new Data<>(Intrinsics.intrinsics.get(intrinsicName));
            break;
        case HEAD__________:
            result = Tree.decompose((Node) ds[--dsp]).getLeft();
            break;
        case ISCONS________:
            result = atom(Tree.decompose((Node) ds[--dsp]) != null);
            break;
        case TAIL__________:
            result = Tree.decompose((Node) ds[--dsp]).getRight();
            break;
        default:
            result = Fail.t("unknown instruction " + insn);
    }
    exec.sp = dsp;
    regs[insn.op0] = result;
}
Also used : Frame(suite.instructionexecutor.InstructionUtil.Frame) Node(suite.node.Node) ArrayList(java.util.ArrayList) Activation(suite.instructionexecutor.InstructionUtil.Activation) Atom(suite.node.Atom) Intrinsic(suite.fp.intrinsic.Intrinsics.Intrinsic)

Example 22 with Atom

use of suite.node.Atom in project suite by stupidsing.

the class InstructionExtractor method extract.

private Instruction extract(List<Node> rs) {
    String insnName = ((Atom) rs.get(0)).name;
    Insn insn;
    if (Objects.equals(insnName, "EVALUATE")) {
        Atom atom = (Atom) rs.remove(3);
        TermOp operator = TermOp.find(atom.name);
        insn = InstructionUtil.getEvalInsn(operator);
    } else
        insn = InstructionUtil.getInsn(insnName);
    if (insn != null) {
        Instruction instruction = new // 
        Instruction(// 
        insn, // 
        getRegisterNumber(rs, 1), // 
        getRegisterNumber(rs, 2), getRegisterNumber(rs, 3));
        if (insn == Insn.FRAMEBEGIN____)
            frameBegins.push(instruction);
        else if (insn == Insn.FRAMEEND______)
            frameBegins.pop();
        return instruction;
    } else
        return Fail.t("unknown opcode " + insnName);
}
Also used : Insn(suite.instructionexecutor.InstructionUtil.Insn) TermOp(suite.node.io.TermOp) Instruction(suite.instructionexecutor.InstructionUtil.Instruction) Atom(suite.node.Atom)

Aggregations

Atom (suite.node.Atom)22 Node (suite.node.Node)17 Reference (suite.node.Reference)11 Tree (suite.node.Tree)11 TermOp (suite.node.io.TermOp)9 ArrayList (java.util.ArrayList)7 Pair (suite.adt.pair.Pair)7 Int (suite.node.Int)7 List (java.util.List)6 HashMap (java.util.HashMap)5 Map (java.util.Map)5 Suite (suite.Suite)5 Generalizer (suite.lp.doer.Generalizer)5 Prototype (suite.lp.kb.Prototype)5 RuleSet (suite.lp.kb.RuleSet)5 Data (suite.node.Data)5 TreeUtil (suite.node.util.TreeUtil)5 Read (suite.streamlet.Read)5 Fail (suite.util.Fail)5 Operator (suite.node.io.Operator)4