use of suite.node.Reference in project suite by stupidsing.
the class Grapher method load.
public void load(DataInputStream dis) throws IOException {
int size = dis.readInt();
id = dis.readInt();
for (int index = 0; index < size; index++) {
ReadType type = ReadType.of(dis.readByte());
Node terminal;
Operator op;
List<IntIntPair> children = new ArrayList<>();
if (type == ReadType.TERM) {
char ch = (char) dis.readByte();
switch(ch) {
case 'a':
terminal = Atom.of(dis.readUTF());
break;
case 'i':
terminal = Int.of(dis.readInt());
break;
case 'r':
terminal = new Reference();
break;
case 's':
terminal = new Str(dis.readUTF());
break;
default:
terminal = Fail.t("unknown type " + ch);
}
} else
terminal = null;
if (type == ReadType.TREE) {
op = TermOp.find(dis.readUTF());
children.add(IntIntPair.of(0, dis.readInt() + index));
children.add(IntIntPair.of(0, dis.readInt() + index));
} else
op = null;
if (type == ReadType.DICT || type == ReadType.TUPLE) {
int size1 = dis.readInt();
for (int i = 0; i < size1; i++) {
int i0 = type != ReadType.DICT ? 0 : dis.readInt() + index;
int i1 = dis.readInt() + index;
children.add(IntIntPair.of(i0, i1));
}
}
gns.add(new GN(type, terminal, op, children));
}
}
use of suite.node.Reference in project suite by stupidsing.
the class Grapher method ungraph_.
private Node ungraph_(int id) {
int size = gns.size();
List<Node> nodes = //
Read.from(//
gns).map(gn -> {
switch(gn.type) {
case DICT:
return new Dict();
case TERM:
return gn.terminal;
case TREE:
return Tree.of(gn.op, null, null);
case TUPLE:
return Tuple.of(new Node[gn.children.size()]);
default:
return Fail.t();
}
}).toList();
for (int i = 0; i < size; i++) {
GN gn = gns.get(i);
Node node = nodes.get(i);
List<Pair<Node, Node>> children = Read.from(gn.children).map(p -> Pair.of(nodes.get(p.t0), nodes.get(p.t1))).toList();
switch(gn.type) {
case DICT:
((Dict) node).map.putAll(Read.from2(children).mapValue(Reference::of).collect(As::map));
break;
case TERM:
break;
case TREE:
Tree tree = (Tree) node;
Tree.forceSetLeft(tree, children.get(0).t1);
Tree.forceSetRight(tree, children.get(1).t1);
break;
case TUPLE:
Node[] list = ((Tuple) node).nodes;
for (int j = 0; j < children.size(); j++) list[j] = children.get(j).t1;
}
}
return nodes.get(id);
}
use of suite.node.Reference in project suite by stupidsing.
the class ReversePolish method fromRpn.
public Node fromRpn(Reader reader) throws IOException {
BufferedReader br = new BufferedReader(reader);
Map<String, Reference> references = new HashMap<>();
Deque<Node> deque = new ArrayDeque<>();
br.lines().filter(elem -> !elem.isEmpty()).forEach(elem -> {
char type = elem.charAt(0);
String s = elem.substring(1);
Node n;
if (type == '\\')
n = Atom.of(s);
else if (type == '^') {
String[] a = s.split(":");
int size = Integer.valueOf(a[3]);
List<Pair<Node, Node>> children = new ArrayList<>();
for (int i = 0; i < size; i++) {
Node key = deque.pop();
Node value = deque.pop();
children.add(Pair.of(key, value));
}
n = new //
NodeWrite(//
ReadType.valueOf(a[0]), //
!String_.equals(a[1], "null") ? Suite.parse(a[1]) : null, //
TermOp.valueOf(a[2]), children).node;
// n = Suite.parse(s);
} else if (type == 'i')
n = Int.of(Integer.parseInt(s));
else if (type == 'r')
n = references.computeIfAbsent(s, key -> new Reference());
else if (type == 't') {
TermOp op = TermOp.valueOf(s);
Node left = deque.pop();
Node right = deque.pop();
n = Tree.of(op, left, right);
} else
n = Fail.t("RPN conversion error: " + elem);
deque.push(n);
});
return deque.pop();
}
use of suite.node.Reference in project suite by stupidsing.
the class InstructionExecutor method yawnThunk_.
private Node yawnThunk_(Thunk thunk0) {
Frame f0 = new Frame(null, 2);
f0.registers[0] = thunk0;
Activation current = new Activation(f0, yawnEntryPoint, null);
Node[] stack = new Node[Suite.stackSize];
int ip = 0, sp = 0;
Node returnValue = null;
Exec exec = new Exec();
exec.stack = stack;
Comparer comparer = comparer();
Tree tree;
while (true) try {
Frame frame = current.frame;
Node[] regs = frame != null ? frame.registers : null;
Instruction insn = instructions[ip = current.ip++];
Thunk thunk;
TermOp op;
int i;
switch(insn.insn) {
case ASSIGNCONST___:
regs[insn.op0] = constantPool.get(insn.op1);
break;
case ASSIGNFRAMEREG:
i = insn.op1;
while (i++ < 0) frame = frame.previous;
regs[insn.op0] = frame.registers[insn.op2];
break;
case ASSIGNINT_____:
regs[insn.op0] = number(insn.op1);
break;
case ASSIGNRESULT__:
regs[insn.op0] = returnValue;
break;
case ASSIGNTHUNK___:
regs[insn.op0] = new Thunk(frame, insn.op1);
break;
case ASSIGNTHUNKRES:
regs[insn.op0] = returnValue;
thunk = (Thunk) regs[insn.op1];
// facilitates garbage collection
thunk.frame = null;
thunk.result = returnValue;
break;
case CALL__________:
current = new Activation(frame, insn.op0, current);
break;
case CALLTHUNK_____:
thunk = (Thunk) regs[insn.op0];
if (thunk.result == null)
current = new Activation(thunk, current);
else
returnValue = thunk.result;
break;
case ENTER_________:
AnalyzedFrame af = analyzer.getFrame(ip);
Frame parent = af.isRequireParent() ? frame : null;
Instruction frameBegin = instructions[af.getFrameBeginIp()];
current.frame = new Frame(parent, frameBegin.op0);
break;
case ERROR_________:
Fail.t("error termination");
case EVALADD_______:
regs[insn.op0] = number(i(regs[insn.op1]) + i(regs[insn.op2]));
break;
case EVALDIV_______:
regs[insn.op0] = number(i(regs[insn.op1]) / i(regs[insn.op2]));
break;
case EVALEQ________:
i = comparer.compare(regs[insn.op1], regs[insn.op2]);
regs[insn.op0] = atom(i == 0);
break;
case EVALLE________:
i = comparer.compare(regs[insn.op1], regs[insn.op2]);
regs[insn.op0] = atom(i <= 0);
break;
case EVALLT________:
i = comparer.compare(regs[insn.op1], regs[insn.op2]);
regs[insn.op0] = atom(i < 0);
break;
case EVALNE________:
i = comparer.compare(regs[insn.op1], regs[insn.op2]);
regs[insn.op0] = atom(i != 0);
break;
case EVALMOD_______:
regs[insn.op0] = number(i(regs[insn.op1]) % i(regs[insn.op2]));
break;
case EVALMUL_______:
regs[insn.op0] = number(i(regs[insn.op1]) * i(regs[insn.op2]));
break;
case EVALSUB_______:
regs[insn.op0] = number(i(regs[insn.op1]) - i(regs[insn.op2]));
break;
case EXIT__________:
return returnValue;
case FORMTREE0_____:
Node left = regs[insn.op0];
Node right = regs[insn.op1];
insn = instructions[current.ip++];
op = TermOp.find(((Atom) constantPool.get(insn.op0)).name);
regs[insn.op1] = Tree.of(op, left, right);
break;
case FRAMEBEGIN____:
case FRAMEEND______:
break;
case IFFALSE_______:
if (regs[insn.op0] != Atom.TRUE)
current.ip = insn.op1;
break;
case IFNOTCONS_____:
if ((tree = Tree.decompose(regs[insn.op0], TermOp.OR____)) != null) {
stack[sp++] = tree.getLeft();
stack[sp++] = tree.getRight();
} else
current.ip = insn.op1;
break;
case IFNOTPAIR_____:
if ((tree = Tree.decompose(regs[insn.op0], TermOp.AND___)) != null) {
stack[sp++] = tree.getLeft();
stack[sp++] = tree.getRight();
} else
current.ip = insn.op1;
break;
case IFNOTEQUALS___:
if (regs[insn.op1] != regs[insn.op2])
current.ip = insn.op0;
break;
case JUMP__________:
current.ip = insn.op0;
break;
case JUMPCLOSURE___:
thunk = (Thunk) regs[insn.op0];
current = current.previous;
if (thunk.result == null)
current = new Activation(thunk, current);
else
returnValue = thunk.result;
break;
case LEAVE_________:
current.frame = current.frame.previous;
break;
case LOGREG________:
LogUtil.info(regs[insn.op0].toString());
break;
case NEWNODE_______:
regs[insn.op0] = new Reference();
break;
case PUSH__________:
stack[sp++] = regs[insn.op0];
break;
case POP___________:
regs[insn.op0] = stack[--sp];
break;
case POPANY________:
--sp;
break;
case REMARK________:
break;
case RETURN________:
current = current.previous;
break;
case SETRESULT_____:
returnValue = regs[insn.op0];
break;
case TOP___________:
regs[insn.op0] = stack[sp + insn.op1];
break;
default:
exec.current = current;
exec.sp = sp;
handle(exec, insn);
current = exec.current;
sp = exec.sp;
}
} catch (Exception ex) {
Fail.t("at IP = " + ip, ex);
}
}
use of suite.node.Reference in project suite by stupidsing.
the class InstructionExtractor method getRegisterNumber.
private int getRegisterNumber(List<Node> rs, int index) {
if (index < rs.size()) {
Node node = rs.get(index).finalNode();
Tree tree;
if (node instanceof Int)
return ((Int) node).number;
else if (node instanceof Reference) {
// transient register
// allocates new register in current local frame
Instruction frameBegin = frameBegins.getFirst();
int registerNumber = frameBegin.op0++;
Binder.bind(node, Int.of(registerNumber), trail);
return registerNumber;
} else if ((tree = Tree.decompose(node, TermOp.COLON_)) != null) {
Node key = tree.getLeft(), value = tree.getRight();
if (key == KEYC)
return allocateInPool(value);
else if (key == KEYL)
return ipByLabelId.get(IdentityKey.of(value));
else if (key == KEYR)
return 0;
}
return Fail.t("cannot parse instruction " + rs.get(0) + " operand " + node);
} else
return 0;
}
Aggregations