Search in sources :

Example 1 with Fun

use of suite.util.FunUtil.Fun 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 2 with Fun

use of suite.util.FunUtil.Fun in project suite by stupidsing.

the class BuildLr method build.

private Blr build(IList<Pair<String, Set<String>>> ps, Grammar eg, Transition nextx) {
    Fun<Streamlet2<String, Transition>, Blr> mergeAll = st2 -> {
        Transition next = newTransition(readLookahead.readLookahead(eg, nextx.keySet()));
        State state1 = newState(nextx);
        st2.sink((egn, next1) -> {
            next.put_(egn, Pair.of(state1, null));
            merges.add(Pair.of(next, next1));
        });
        return new Blr(1, next);
    };
    Pair<String, Set<String>> k;
    Blr blr;
    switch(eg.type) {
        case AND___:
            if (!eg.children.isEmpty()) {
                Grammar tail = new Grammar(GrammarType.AND___, List_.right(eg.children, 1));
                Blr blr1 = build(ps, tail, nextx);
                Blr blr0 = build(ps, eg.children.get(0), blr1.next);
                blr = new Blr(blr0.nTokens + blr1.nTokens, blr0.next);
            } else
                blr = new Blr(0, nextx);
            break;
        case ENTITY:
            k = Pair.of(eg.content, nextx.keySet());
            Transition next1 = transitions.computeIfAbsent(k, k_ -> new Transition());
            blr = mergeAll.apply(Read.each2(eg.content, next1));
            break;
        case NAMED_:
            Reduce reduce = new Reduce();
            Transition next = newTransition(nextx.keySet(), Pair.of(null, reduce));
            Blr blr1 = build(ps, eg.children.get(0), next);
            reduce.n = blr1.nTokens;
            reduce.name = eg.content;
            blr = new Blr(1, blr1.next);
            break;
        case OR____:
            List<Pair<String, Transition>> pairs = new ArrayList<>();
            for (Grammar eg1 : Read.from(eg.children)) {
                String egn = "OR." + System.identityHashCode(eg1);
                pairs.add(Pair.of(egn, build(ps, new Grammar(GrammarType.NAMED_, egn, eg1), nextx).next));
            }
            blr = mergeAll.apply(Read.from2(pairs));
            break;
        case STRING:
            State state1 = newState(nextx);
            blr = new Blr(1, kv(eg.content, state1));
            break;
        default:
            blr = Fail.t("LR parser cannot recognize " + eg.type);
    }
    return blr;
}
Also used : Read(suite.streamlet.Read) Streamlet2(suite.streamlet.Streamlet2) Set(java.util.Set) List_(suite.util.List_) HashMap(java.util.HashMap) IList(suite.immutable.IList) Grammar(suite.ebnf.Grammar) Fun(suite.util.FunUtil.Fun) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Objects(java.util.Objects) Pair(suite.adt.pair.Pair) List(java.util.List) GrammarType(suite.ebnf.Grammar.GrammarType) Map(java.util.Map) Fail(suite.util.Fail) Set(java.util.Set) HashSet(java.util.HashSet) ArrayList(java.util.ArrayList) Grammar(suite.ebnf.Grammar) Streamlet2(suite.streamlet.Streamlet2) Pair(suite.adt.pair.Pair)

Example 3 with Fun

use of suite.util.FunUtil.Fun in project suite by stupidsing.

the class InterpretFunLazy method lazy.

public Thunk_ lazy(Node node) {
    Node parsed = parse(node);
    Map<String, Thunk_> df = new HashMap<>();
    df.put(TermOp.AND___.name, binary((a, b) -> new Pair_(a, b)));
    df.put("fst", () -> new Fun_(in -> ((Pair_) in.get()).first_));
    df.put("if", () -> new Fun_(a -> () -> new Fun_(b -> () -> new Fun_(c -> a.get() == Atom.TRUE ? b : c))));
    df.put("snd", () -> new Fun_(in -> ((Pair_) in.get()).second));
    for (Entry<Operator, IntInt_Bool> e : TreeUtil.boolOperations.entrySet()) {
        IntInt_Bool fun = e.getValue();
        df.put(e.getKey().getName(), binary((a, b) -> b(fun.apply(compare(a.get(), b.get()), 0))));
    }
    for (Entry<Operator, IntInt_Int> e : TreeUtil.intOperations.entrySet()) {
        IntInt_Int fun = e.getValue();
        df.put(e.getKey().getName(), binary((a, b) -> Int.of(fun.apply(i(a), i(b)))));
    }
    List<String> keys = df.keySet().stream().sorted().collect(Collectors.toList());
    Lazy_ lazy0 = new Lazy_(0, IMap.empty());
    Frame frame = new Frame(null);
    for (String key : keys) {
        lazy0 = lazy0.put(Atom.of(key));
        frame.add(df.get(key));
    }
    return lazy0.lazy_(parsed).apply(frame);
}
Also used : IntInt_Bool(suite.node.util.TreeUtil.IntInt_Bool) APPLY(suite.fp.match.Matchers.APPLY) Prover(suite.lp.doer.Prover) DECONS(suite.fp.match.Matchers.DECONS) Mutable(suite.adt.Mutable) Fun(suite.util.FunUtil.Fun) Node(suite.node.Node) BOOLEAN(suite.fp.match.Matchers.BOOLEAN) VAR(suite.fp.match.Matchers.VAR) Map(java.util.Map) NUMBER(suite.fp.match.Matchers.NUMBER) ERROR(suite.fp.match.Matchers.ERROR) TREE(suite.fp.match.Matchers.TREE) IMap(suite.immutable.IMap) FUN(suite.fp.match.Matchers.FUN) To(suite.util.To) Collectors(java.util.stream.Collectors) Matcher(suite.fp.match.Matcher) PRAGMA(suite.fp.match.Matchers.PRAGMA) Tree(suite.node.Tree) UNWRAP(suite.fp.match.Matchers.UNWRAP) IntInt_Int(suite.primitive.IntInt_Int) Iterate(suite.util.FunUtil.Iterate) Pair(suite.adt.pair.Pair) List(java.util.List) Entry(java.util.Map.Entry) Int(suite.node.Int) TreeUtil(suite.node.util.TreeUtil) IF(suite.fp.match.Matchers.IF) Comparer(suite.node.util.Comparer) HashMap(java.util.HashMap) TCO(suite.fp.match.Matchers.TCO) ArrayList(java.util.ArrayList) CONS(suite.fp.match.Matchers.CONS) Data(suite.node.Data) Formatter(suite.node.io.Formatter) DEFVARS(suite.fp.match.Matchers.DEFVARS) Reference(suite.node.Reference) Suite(suite.Suite) Pattern(suite.BindArrayUtil.Pattern) TermOp(suite.node.io.TermOp) ATOM(suite.fp.match.Matchers.ATOM) WRAP(suite.fp.match.Matchers.WRAP) Streamlet(suite.streamlet.Streamlet) CHARS(suite.fp.match.Matchers.CHARS) Atom(suite.node.Atom) Operator(suite.node.io.Operator) Fail(suite.util.Fail) Str(suite.node.Str) BiFun(suite.util.FunUtil2.BiFun) Operator(suite.node.io.Operator) HashMap(java.util.HashMap) Node(suite.node.Node) IntInt_Int(suite.primitive.IntInt_Int) IntInt_Bool(suite.node.util.TreeUtil.IntInt_Bool)

Example 4 with Fun

use of suite.util.FunUtil.Fun in project suite by stupidsing.

the class InterpretFunLazy0 method lazy.

public Thunk_ lazy(Node node) {
    Thunk_ error = () -> Fail.t("error termination");
    IMap<String, Thunk_> env = IMap.empty();
    env = env.put(Atom.TRUE.name, () -> Atom.TRUE);
    env = env.put(Atom.FALSE.name, () -> Atom.FALSE);
    env = env.put(TermOp.AND___.name, () -> new Fun_(a -> () -> new Fun_(b -> () -> new Pair_(a, b))));
    env = env.put(ERROR.name, error);
    env = env.put(FST__.name, () -> new Fun_(in -> ((Pair_) in.get()).first));
    env = env.put(SND__.name, () -> new Fun_(in -> ((Pair_) in.get()).second));
    for (Entry<Operator, IntInt_Bool> e : TreeUtil.boolOperations.entrySet()) {
        IntInt_Bool fun = e.getValue();
        env = env.put(e.getKey().getName(), () -> new Fun_(a -> () -> new Fun_(b -> () -> b(fun.apply(i(a), i(b))))));
    }
    for (Entry<Operator, IntInt_Int> e : TreeUtil.intOperations.entrySet()) {
        IntInt_Int fun = e.getValue();
        env = env.put(e.getKey().getName(), () -> new Fun_(a -> () -> new Fun_(b -> () -> Int.of(fun.apply(i(a), i(b))))));
    }
    return lazy_(node).apply(env);
}
Also used : Suite(suite.Suite) IntInt_Bool(suite.node.util.TreeUtil.IntInt_Bool) IMap(suite.immutable.IMap) Mutable(suite.adt.Mutable) TermOp(suite.node.io.TermOp) Fun(suite.util.FunUtil.Fun) Tree(suite.node.Tree) Node(suite.node.Node) IntInt_Int(suite.primitive.IntInt_Int) Iterate(suite.util.FunUtil.Iterate) Atom(suite.node.Atom) Entry(java.util.Map.Entry) Int(suite.node.Int) Operator(suite.node.io.Operator) TreeUtil(suite.node.util.TreeUtil) Fail(suite.util.Fail) Operator(suite.node.io.Operator) IntInt_Int(suite.primitive.IntInt_Int) IntInt_Bool(suite.node.util.TreeUtil.IntInt_Bool)

Example 5 with Fun

use of suite.util.FunUtil.Fun in project suite by stupidsing.

the class P2InferType method captureLambdas.

private Funp captureLambdas(Funp node0) {
    class Capture {

        private Fun<String, Funp> accesses;

        private ISet<String> locals;

        private ISet<String> globals;

        private Capture(Fun<String, Funp> accesses, ISet<String> locals, ISet<String> globals) {
            this.accesses = accesses;
            this.locals = locals;
            this.globals = globals;
        }

        private Funp capture(Funp n) {
            return inspect.rewrite(Funp.class, this::capture_, n);
        }

        private Funp capture_(Funp n) {
            return // 
            n.<// 
            Funp>switch_().applyIf(FunpDefine.class, f -> f.apply((isPolyType, var, value, expr) -> {
                Capture c1 = new Capture(accesses, locals.add(var), globals);
                return FunpDefine.of(isPolyType, var, capture(value), c1.capture(expr));
            })).applyIf(FunpDefineRec.class, f -> f.apply((vars, expr) -> {
                List<Pair<String, Funp>> vars1 = new ArrayList<>();
                ISet<String> locals1 = locals;
                for (Pair<String, Funp> pair : vars) {
                    locals1 = locals1.add(pair.t0);
                    vars1.add(Pair.of(pair.t0, capture(pair.t1)));
                }
                Capture c1 = new Capture(accesses, locals1, globals);
                return FunpDefineRec.of(vars1, c1.capture(expr));
            })).applyIf(FunpGlobal.class, f -> f.apply((var, value, expr) -> {
                Capture c1 = new Capture(accesses, locals, globals.add(var));
                return FunpGlobal.of(var, capture(value), c1.capture(expr));
            })).applyIf(FunpIterate.class, f -> f.apply((var, init, cond, iterate) -> {
                Capture c1 = new Capture(accesses, locals.add(var), globals);
                return FunpIterate.of(var, capture(init), c1.capture(cond), c1.capture(iterate));
            })).applyIf(FunpLambda.class, f -> f.apply((var, expr) -> {
                ISet<String> locals1 = ISet.empty();
                String capn = "cap" + Util.temp();
                FunpVariable cap = FunpVariable.of(capn);
                FunpReference ref = FunpReference.of(cap);
                Set<String> set = new HashSet<>();
                List<Pair<String, Funp>> list = new ArrayList<>();
                FunpStruct struct = FunpStruct.of(list);
                Capture c1 = new Capture(v -> {
                    if (set.add(v))
                        list.add(Pair.of(v, FunpVariable.of(v)));
                    return FunpField.of(ref, v);
                }, locals1.add(capn).add(var), globals);
                return FunpGlobal.of(capn, struct, FunpLambdaCapture.of(var, capn, cap, c1.capture(expr)));
            // TODO allocate cap on heap
            // TODO free cap after use
            })).applyIf(FunpVariable.class, f -> f.apply(var -> {
                return locals.contains(var) || globals.contains(var) ? f : accesses.apply(var);
            })).result();
        }
    }
    return new Capture(v -> Fail.t(), ISet.empty(), ISet.empty()).capture(node0);
}
Also used : FunpAllocGlobal(suite.funp.P2.FunpAllocGlobal) FunpInvokeIo(suite.funp.P2.FunpInvokeIo) Mutable(suite.adt.Mutable) FunpOperand(suite.funp.P2.FunpOperand) FunpDefine(suite.funp.P0.FunpDefine) Fun(suite.util.FunUtil.Fun) FunpLambda(suite.funp.P0.FunpLambda) FunpFramePointer(suite.funp.P2.FunpFramePointer) FunpPredefine(suite.funp.P0.FunpPredefine) Operand(suite.assembler.Amd64.Operand) Map(java.util.Map) FunpWhile(suite.funp.P2.FunpWhile) FunpData(suite.funp.P2.FunpData) FunpInvoke(suite.funp.P2.FunpInvoke) UnNode(suite.fp.Unify.UnNode) FunpCoerce(suite.funp.P0.FunpCoerce) IntIntPair(suite.primitive.adt.pair.IntIntPair) FunpDefineRec(suite.funp.P0.FunpDefineRec) IMap(suite.immutable.IMap) Set(java.util.Set) ISet(suite.immutable.ISet) Unify(suite.fp.Unify) FunpRoutineIo(suite.funp.P2.FunpRoutineIo) Pair(suite.adt.pair.Pair) List(java.util.List) FunpInvoke2(suite.funp.P2.FunpInvoke2) FunpRepeat(suite.funp.P0.FunpRepeat) FunpDeref(suite.funp.P0.FunpDeref) FunpMemory(suite.funp.P2.FunpMemory) FunpSaveRegisters(suite.funp.P2.FunpSaveRegisters) FunpVariable(suite.funp.P0.FunpVariable) FunpIterate(suite.funp.P0.FunpIterate) Read(suite.streamlet.Read) Singleton(suite.node.util.Singleton) AutoObject(suite.util.AutoObject) FunpBoolean(suite.funp.P0.FunpBoolean) Util(suite.util.Util) FunpArray(suite.funp.P0.FunpArray) HashMap(java.util.HashMap) OpReg(suite.assembler.Amd64.OpReg) FunpIndex(suite.funp.P0.FunpIndex) FunpIoCat(suite.funp.P0.FunpIoCat) FunpReference(suite.funp.P0.FunpReference) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) FunpApply(suite.funp.P0.FunpApply) FunpLambdaCapture(suite.funp.P0.FunpLambdaCapture) String_(suite.util.String_) FunpIf(suite.funp.P0.FunpIf) Rethrow(suite.util.Rethrow) FunpGlobal(suite.funp.P0.FunpGlobal) FunpIo(suite.funp.P0.FunpIo) FunpAsm(suite.funp.P0.FunpAsm) Inspect(suite.inspect.Inspect) FunpTree(suite.funp.P0.FunpTree) FunpField(suite.funp.P0.FunpField) TermOp(suite.node.io.TermOp) FunpAssign(suite.funp.P2.FunpAssign) FunpAssignReference(suite.funp.P0.FunpAssignReference) FunpCheckType(suite.funp.P0.FunpCheckType) FunpTree2(suite.funp.P0.FunpTree2) Funp(suite.funp.Funp_.Funp) FunpError(suite.funp.P0.FunpError) FunpRoutine2(suite.funp.P2.FunpRoutine2) Obj_Int(suite.primitive.IntPrimitives.Obj_Int) FunpNumber(suite.funp.P0.FunpNumber) FunpRoutine(suite.funp.P2.FunpRoutine) FixieFun1(suite.adt.pair.Fixie_.FixieFun1) FixieFun2(suite.adt.pair.Fixie_.FixieFun2) Switch(suite.util.Switch) FunpStruct(suite.funp.P0.FunpStruct) FixieFun0(suite.adt.pair.Fixie_.FixieFun0) FunpAllocStack(suite.funp.P2.FunpAllocStack) Fail(suite.util.Fail) FunpDontCare(suite.funp.P0.FunpDontCare) FunpReference(suite.funp.P0.FunpReference) Set(java.util.Set) ISet(suite.immutable.ISet) HashSet(java.util.HashSet) ArrayList(java.util.ArrayList) FunpIterate(suite.funp.P0.FunpIterate) FunpLambdaCapture(suite.funp.P0.FunpLambdaCapture) Funp(suite.funp.Funp_.Funp) FunpDefineRec(suite.funp.P0.FunpDefineRec) FunpStruct(suite.funp.P0.FunpStruct) FunpVariable(suite.funp.P0.FunpVariable) List(java.util.List) ArrayList(java.util.ArrayList) ISet(suite.immutable.ISet) Fun(suite.util.FunUtil.Fun) IntIntPair(suite.primitive.adt.pair.IntIntPair) Pair(suite.adt.pair.Pair)

Aggregations

Fun (suite.util.FunUtil.Fun)31 Node (suite.node.Node)18 Fail (suite.util.Fail)13 List (java.util.List)12 TermOp (suite.node.io.TermOp)12 ArrayList (java.util.ArrayList)11 Map (java.util.Map)11 Suite (suite.Suite)11 Tree (suite.node.Tree)11 Source (suite.util.FunUtil.Source)11 Atom (suite.node.Atom)10 Reference (suite.node.Reference)10 Pair (suite.adt.pair.Pair)9 Int (suite.node.Int)9 Read (suite.streamlet.Read)9 Iterate (suite.util.FunUtil.Iterate)8 To (suite.util.To)7 Collections (java.util.Collections)6 Entry (java.util.Map.Entry)6 LogUtil (suite.os.LogUtil)6