use of suite.immutable.IMap 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);
}
use of suite.immutable.IMap in project suite by stupidsing.
the class InterpretFunLazy0 method lazy_.
private Fun<IMap<String, Thunk_>, Thunk_> lazy_(Node node) {
Fun<IMap<String, Thunk_>, Thunk_> result;
Tree tree;
Node[] m;
if ((m = Suite.pattern("define .0 := .1 >> .2").match(node)) != null) {
String vk = v(m[0]);
Fun<IMap<String, Thunk_>, Thunk_> value = lazy_(m[1]);
Fun<IMap<String, Thunk_>, Thunk_> expr = lazy_(m[2]);
result = env -> {
Mutable<Thunk_> val = Mutable.nil();
IMap<String, Thunk_> env1 = env.put(vk, () -> val.get().get());
val.set(value.apply(env1)::get);
return expr.apply(env1);
};
} else if ((m = Suite.pattern("if .0 then .1 else .2").match(node)) != null) {
Fun<IMap<String, Thunk_>, Thunk_> if_ = lazy_(m[0]);
Fun<IMap<String, Thunk_>, Thunk_> then_ = lazy_(m[1]);
Fun<IMap<String, Thunk_>, Thunk_> else_ = lazy_(m[2]);
result = env -> (if_.apply(env).get() == Atom.TRUE ? then_ : else_).apply(env);
} else if ((m = Suite.pattern(".0 => .1").match(node)) != null) {
String vk = v(m[0]);
Fun<IMap<String, Thunk_>, Thunk_> value = lazy_(m[1]);
result = env -> () -> new Fun_(in -> value.apply(env.put(vk, in)));
} else if ((m = Suite.pattern(".0 {.1}").match(node)) != null) {
Fun<IMap<String, Thunk_>, Thunk_> fun = lazy_(m[0]);
Fun<IMap<String, Thunk_>, Thunk_> param = lazy_(m[1]);
result = env -> fun(fun.apply(env).get()).apply(param.apply(env));
} else if ((tree = Tree.decompose(node)) != null) {
Operator operator = tree.getOperator();
Fun<IMap<String, Thunk_>, Thunk_> p0 = lazy_(tree.getLeft());
Fun<IMap<String, Thunk_>, Thunk_> p1 = lazy_(tree.getRight());
result = env -> {
Thunk_ r0 = env.get(operator.getName());
Thunk_ r1 = fun(r0.get()).apply(p0.apply(env));
Thunk_ r2 = fun(r1.get()).apply(p1.apply(env));
return r2;
};
} else if (node instanceof Atom) {
String vk = v(node);
result = env -> env.get(vk);
} else
result = env -> () -> node;
return result;
}
use of suite.immutable.IMap in project suite by stupidsing.
the class P0Parse method expandMacros.
private Node expandMacros(Node node0) {
class Expand {
private IMap<Prototype, Node[]> macros;
private Expand(IMap<Prototype, Node[]> macros) {
this.macros = macros;
}
private Node expand(Node node) {
Tree tree;
Node[] m;
Node[] ht;
if ((m = Suite.pattern("expand .0 := .1 >> .2").match(node)) != null) {
Node head = m[0];
return new Expand(macros.put(Prototype.of(head), new Node[] { head, m[1] })).expand(m[2]);
} else if ((ht = macros.get(Prototype.of(node))) != null) {
Generalizer g = new Generalizer();
Node t0_ = g.generalize(ht[0]);
Node t1_ = g.generalize(ht[1]);
if (Binder.bind(node, t0_, new Trail()))
return expand(t1_);
}
if ((tree = Tree.decompose(node)) != null)
return Tree.of(tree.getOperator(), expand(tree.getLeft()), expand(tree.getRight()));
else
return node;
}
}
return new Expand(IMap.empty()).expand(node0);
}
use of suite.immutable.IMap in project suite by stupidsing.
the class ServerMain method runHttpServer.
private void runHttpServer() {
Authenticator authenticator = (username, password) -> Constants.secrets().prove(Suite.substitute("auth .0 .1", new Str(username), new Str(password)));
HttpHandler handler0 = request -> HttpResponse.of(To.outlet(//
"" + //
"<html>" + "<br/>method = " + //
request.method + "<br/>server = " + //
request.server + "<br/>path = " + //
request.path + "<br/>attrs = " + //
HttpHeaderUtil.getAttrs(request.query) + "<br/>headers = " + //
request.headers + "</html>"));
HttpHandler handler1 = HttpHandler.ofDispatch(//
IMap.<String, HttpHandler>empty().put("path", //
HttpHandler.ofPath(Constants.tmp)).put("site", HttpHandler.ofSession(authenticator, handler0)));
new HttpServer().run(handler1);
}
Aggregations