Search in sources :

Example 1 with Reference

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();
}
Also used : Reference(suite.node.Reference) Node(suite.node.Node) Int(suite.node.Int) BytesBuilder(suite.primitive.Bytes.BytesBuilder)

Example 2 with Reference

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);
}
Also used : Read(suite.streamlet.Read) SewingProverBuilder2(suite.lp.search.SewingProverBuilder2) Fun(suite.util.FunUtil.Fun) ArrayList(java.util.ArrayList) Node(suite.node.Node) CommentPreprocessor(suite.parser.CommentPreprocessor) String_(suite.util.String_) Preprocess(suite.text.Preprocess) Run(suite.text.Preprocess.Run) Binder(suite.lp.doer.Binder) RuleSet(suite.lp.kb.RuleSet) Generalizer(suite.lp.doer.Generalizer) Reference(suite.node.Reference) Suite(suite.Suite) Trail(suite.lp.Trail) Finder(suite.lp.search.ProverBuilder.Finder) Set(java.util.Set) TermOp(suite.node.io.TermOp) Bytes(suite.primitive.Bytes) BytesBuilder(suite.primitive.Bytes.BytesBuilder) List_(suite.util.List_) To(suite.util.To) Tree(suite.node.Tree) Pair(suite.adt.pair.Pair) List(java.util.List) Atom(suite.node.Atom) Int(suite.node.Int) Collections(java.util.Collections) Fail(suite.util.Fail) Reference(suite.node.Reference) Node(suite.node.Node) Generalizer(suite.lp.doer.Generalizer) ArrayList(java.util.ArrayList) List(java.util.List) Pair(suite.adt.pair.Pair)

Example 3 with Reference

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;
}
Also used : Reference(suite.node.Reference) Suite(suite.Suite) Trail(suite.lp.Trail) Read(suite.streamlet.Read) Rule(suite.lp.kb.Rule) TermOp(suite.node.io.TermOp) HashMap(java.util.HashMap) Tree(suite.node.Tree) Node(suite.node.Node) Pair(suite.adt.pair.Pair) List(java.util.List) Atom(suite.node.Atom) Map(java.util.Map) Prototype(suite.lp.kb.Prototype) IdentityKey(suite.adt.IdentityKey) Binder(suite.lp.doer.Binder) TreeUtil(suite.node.util.TreeUtil) Generalizer(suite.lp.doer.Generalizer) Fail(suite.util.Fail) Dict(suite.node.Dict) Reference(suite.node.Reference) Node(suite.node.Node) Tree(suite.node.Tree) Atom(suite.node.Atom)

Example 4 with Reference

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;
}
Also used : Generalizer(suite.lp.doer.Generalizer) Reference(suite.node.Reference) IdentityHashMap(java.util.IdentityHashMap) Atom(suite.node.Atom)

Example 5 with Reference

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;
}
Also used : Reference(suite.node.Reference)

Aggregations

Reference (suite.node.Reference)38 Node (suite.node.Node)31 Tree (suite.node.Tree)21 Atom (suite.node.Atom)17 ArrayList (java.util.ArrayList)15 Pair (suite.adt.pair.Pair)13 Int (suite.node.Int)13 List (java.util.List)11 Generalizer (suite.lp.doer.Generalizer)10 TermOp (suite.node.io.TermOp)10 Read (suite.streamlet.Read)10 Trail (suite.lp.Trail)9 Dict (suite.node.Dict)9 Tuple (suite.node.Tuple)9 HashMap (java.util.HashMap)8 Map (java.util.Map)7 Binder (suite.lp.doer.Binder)7 Str (suite.node.Str)7 Fail (suite.util.Fail)7 Suite (suite.Suite)6