use of suite.node.Reference in project suite by stupidsing.
the class Assembler method assemble.
private Bytes assemble(Generalizer generalizer, List<Pair<Reference, Node>> lnis) {
int org = ((Int) generalizer.getVariable(Atom.of(".org")).finalNode()).number;
BytesBuilder out = new BytesBuilder();
for (boolean isPass2 : new boolean[] { false, true }) {
AssemblePredicates.isPass2 = isPass2;
out.clear();
for (Pair<Reference, Node> lni : lnis) {
int address = org + out.size();
if (lni.t0 != null)
if (!isPass2)
lni.t0.bound(Int.of(address));
else if (((Int) lni.t0.finalNode()).number != address)
Fail.t("address varied between passes at " + Integer.toHexString(address));
out.append(assemble(isPass2, address, lni.t1));
}
for (Pair<Reference, Node> lni : lnis) if (lni.t0 != null && isPass2)
lni.t0.unbound();
}
return out.toBytes();
}
use of suite.node.Reference in project suite by stupidsing.
the class Assembler method assemble.
public Bytes assemble(String in0) {
Set<Character> whitespaces = Collections.singleton('\n');
Fun<String, List<Run>> gct = CommentPreprocessor.groupCommentPreprocessor(whitespaces);
Fun<String, List<Run>> lct = CommentPreprocessor.lineCommentPreprocessor(whitespaces);
String in1 = Preprocess.transform(List.of(gct, lct), in0).t0;
Generalizer generalizer = new Generalizer();
List<String> lines = List.of(in1.split("\n"));
Pair<String, String> pe;
int start = 0;
while (!(pe = String_.split2(lines.get(start), "=")).t1.isEmpty()) {
generalizer.getVariable(Atom.of(pe.t0)).bound(Suite.parse(pe.t1));
start++;
}
List<Pair<Reference, Node>> lnis = //
Read.from(//
List_.right(lines, start)).map(line -> {
Pair<String, String> pt = String_.split2(line, "\t");
String label = pt.t0;
String command = pt.t1;
Reference reference = String_.isNotBlank(label) ? generalizer.getVariable(Atom.of(label)) : null;
Node instruction = generalizer.generalize(Suite.parse(command));
return Pair.of(reference, instruction);
}).toList();
return assemble(generalizer, lnis);
}
use of suite.node.Reference in project suite by stupidsing.
the class TypeChecker method getType.
private Node getType(Node data) {
Node type;
Tree tree;
if (data instanceof Reference)
type = variableTypes.computeIfAbsent(IdentityKey.of(data), k -> new Reference()).finalNode();
else if ((tree = Tree.decompose(data)) != null)
if (tree.getOperator() == TermOp.AND___) {
type = Suite.substitute(".0;", getType(tree.getLeft()));
bind(type, getType(tree.getRight()));
} else if (tree.getOperator() == TermOp.TUPLE_) {
Node name = tree.getLeft();
if (name instanceof Atom) {
Node node = tree.getRight();
Node[] ps = TreeUtil.elements(node, TreeUtil.nElements(node));
type = getEnumType(name, Tree.of(TermOp.TUPLE_, Read.from(ps).map(this::getType).toList()));
} else
// free type
return new Reference();
} else {
Atom name = Atom.of(tree.getOperator().getName());
Node lt = getType(tree.getLeft());
Node rt = getType(tree.getRight());
type = getEnumType(name, Tree.of(TermOp.TUPLE_, lt, rt));
}
else if (data == Atom.NIL)
type = Suite.substitute("_;");
else if (data instanceof Atom)
type = getEnumType(data, Atom.NIL);
else
type = Atom.of(data.getClass().getSimpleName());
return type;
}
use of suite.node.Reference in project suite by stupidsing.
the class CompileGeneralizerImpl method generalizer.
@Override
public Generalize_ generalizer(Node node) {
VariableMapper<Reference> mapper = cc.mapper();
Generalizer generalizer = new Generalizer();
Generalize_ generalize = cc.cloner(generalizer.generalize(node))::apply;
Map<Reference, Atom> indices = new IdentityHashMap<>();
for (Atom variableName : generalizer.getVariableNames()) indices.put(generalizer.getVariable(variableName), variableName);
vm = mapper.mapKeys(indices::get);
return generalize;
}
use of suite.node.Reference in project suite by stupidsing.
the class Specializer method specialize.
public Node specialize(Node node) {
if (node instanceof Reference) {
Reference ref = (Reference) node;
node = Atom.of(ref.name());
} else
node = Rewrite_.map(node, this::specialize);
return node;
}
Aggregations