Search in sources :

Example 1 with Node

use of suite.node.Node 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 Node

use of suite.node.Node 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 Node

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

the class CommandDispatcher method dispatchFilter.

public boolean dispatchFilter(List<String> inputs, Reader reader, Writer writer) throws IOException {
    boolean isChars = opt.isChars();
    Node node = parseNode(inputs);
    node = isChars ? Suite.applyCharsReader(node, reader) : Suite.applyStringReader(node, reader);
    if (opt.isDo())
        node = Suite.applyPerform(node, isChars ? Suite.parse("[n^Chars]") : Atom.of("string"));
    printEvaluated(writer, isChars ? node : Suite.applyWriter(node));
    return true;
}
Also used : Node(suite.node.Node)

Example 4 with Node

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

the class CommandDispatcher method dispatchCommand_.

private boolean dispatchCommand_(String input, Writer writer) throws IOException {
    PrintWriter pw = new PrintWriter(writer);
    boolean code = true;
    Pair<InputType, String> pair = new CommandUtil<>(InputType.values()).recognize(input);
    InputType type = pair.t0;
    input = pair.t1.trim();
    if (input.endsWith("#"))
        input = String_.range(input, 0, -1);
    Node node = Suite.parse(input.trim());
    switch(type) {
        case EVALUATE:
            pw.println(Formatter.dump(evaluateFunctional(node)));
            break;
        case EVALUATEDO:
            node = Suite.applyPerform(node, Atom.of("any"));
            pw.println(Formatter.dump(evaluateFunctional(node)));
            break;
        case EVALUATEDOCHARS:
            node = Suite.applyPerform(node, Suite.parse("[n^Chars]"));
            printEvaluated(writer, node);
            break;
        case EVALUATEDOSTR:
            node = Suite.applyPerform(node, Atom.of("string"));
            printEvaluated(writer, Suite.applyWriter(node));
            break;
        case EVALUATEEFI:
            InterpretFunEager efi = new InterpretFunEager();
            efi.setLazyify(opt.isLazy());
            pw.println(efi.eager(node));
            break;
        case EVALUATELFI0:
            pw.println(new InterpretFunLazy0().lazy(node).get());
            break;
        case EVALUATELFI:
            pw.println(new InterpretFunLazy().lazy(node).get());
            break;
        case EVALUATESTR:
            node = Suite.substitute("string of .0", node);
            printEvaluated(writer, Suite.applyWriter(node));
            break;
        case EVALUATETYPE:
            pw.println(Formatter.dump(Suite.evaluateFunType(opt.fcc(node))));
            break;
        case FACT:
            ruleSet.addRule(Rule.of(node));
            break;
        case OPTION:
            Source<String> source = To.source(("-" + input).split(" "));
            String option;
            while ((option = source.source()) != null) opt.processOption(option, source);
            break;
        case PRETTYPRINT:
            pw.println(new PrettyPrinter().prettyPrint(node));
            break;
        case QUERY:
            code = query(new InterpretedProverBuilder(opt.pc(ruleSet)), ruleSet, node);
            break;
        case QUERYCOMPILED:
            code = query(CompiledProverBuilder.level1(opt.pc(ruleSet)), ruleSet, node);
            break;
        case QUERYCOMPILED2:
            if (builderLevel2 == null)
                builderLevel2 = CompiledProverBuilder.level2(opt.pc(ruleSet));
            code = query(builderLevel2, ruleSet, node);
            break;
        case QUERYELABORATE:
            elaborate(node, new Prover(opt.pc(ruleSet))::prove);
            break;
        case QUERYSEWING:
            code = query(new SewingProverBuilder(opt.pc(ruleSet)), ruleSet, node);
            break;
        case QUERYSEWINGELAB:
            elaborate(node, n -> new SewingProverImpl(ruleSet).prover(n).test(new ProverConfig(ruleSet)));
            break;
        case RESET:
            ruleSet = Suite.newRuleSet();
            importFiles(List.of());
    }
    pw.flush();
    return code;
}
Also used : InterpretFunLazy(suite.fp.InterpretFunLazy) SewingProverBuilder(suite.lp.search.SewingProverBuilder) InterpretFunLazy0(suite.fp.InterpretFunLazy0) Node(suite.node.Node) Prover(suite.lp.doer.Prover) InterpretFunEager(suite.fp.InterpretFunEager) SewingProverImpl(suite.lp.sewing.impl.SewingProverImpl) PrettyPrinter(suite.node.pp.PrettyPrinter) InterpretedProverBuilder(suite.lp.search.InterpretedProverBuilder) ProverConfig(suite.lp.Configuration.ProverConfig) PrintWriter(java.io.PrintWriter)

Example 5 with Node

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

the class EditorController method evaluate.

public void evaluate() {
    run(text -> {
        String result;
        try {
            Node node = Suite.evaluateFun(text, true);
            result = Formatter.dump(node);
        } catch (Exception ex) {
            result = To.string(ex);
        }
        return result;
    });
}
Also used : Node(suite.node.Node) IOException(java.io.IOException)

Aggregations

Node (suite.node.Node)139 Tree (suite.node.Tree)50 Reference (suite.node.Reference)41 Atom (suite.node.Atom)37 Int (suite.node.Int)33 ArrayList (java.util.ArrayList)32 Pair (suite.adt.pair.Pair)25 List (java.util.List)24 TermOp (suite.node.io.TermOp)21 Read (suite.streamlet.Read)21 Test (org.junit.Test)20 Map (java.util.Map)19 Suite (suite.Suite)18 Generalizer (suite.lp.doer.Generalizer)18 Fail (suite.util.Fail)18 Fun (suite.util.FunUtil.Fun)18 Str (suite.node.Str)17 Trail (suite.lp.Trail)16 RuleSet (suite.lp.kb.RuleSet)16 Tuple (suite.node.Tuple)16