public void LoadVariable(List v) { int level = v.Head; int pos = v.Tail; List frame = Nth(level, Env); dynamic val = Nth(pos, frame); Push(val); }
public void Eval(MethodInfo mi) { var args = new List<dynamic>(); for (int i = 0; i < mi.GetParameters().Length; ++i) args.Add(Pop()); var result = mi.Invoke(this, args.ToArray()); if (mi.ReturnType != typeof(void)) Push(result); }
public static bool Occurs(string s, List e) { if (e.Head is string) if (e.Head == s) return true; if (e.Head is List) return Occurs(s, e.Head); return e.Tail == null ? false : Occurs(s, e.Tail); }
public Expression LetToExpr(Node node) { var exprs = new List<Expression>(); foreach (var binding in node[0].Nodes) { var name = binding[0].Text; var param = AddBinding(Expression.Parameter(typeof(Object), name)); exprs.Add(param); exprs.Add(Expression.Assign(Lookup(name), ToExpr(binding[1]))); } exprs.Add(ToExpr(node["Term"])); return Expression.Block(exprs); }
public void Select(List a, List b) { Push(Code, ref Dump); Code = Pop() == List.Empty ? a : b; }
public void SetVariable(List v, dynamic val) { }
public dynamic Pop(ref List list) { var top = list.Head; list = list.Tail; return top; }
public dynamic Nth(int n, List list) { return n == 0 ? list.Head : Nth(n - 1, list.Tail); }
public void Apply() { Push(Stack, ref Dump); Push(Env, ref Dump); Push(Code, ref Dump); List closure = Pop(); List paramlist = Pop(); Env = closure.Tail; Push(paramlist, ref Env); Code = closure.Head; Stack = List.Empty; }
public void Return() { var r = Pop(); Code = Pop(ref Dump); Env = Pop(ref Dump); Stack = Pop(ref Dump); Push(r); }
public void Eval(List xs) { for (; !xs.IsEmpty; xs = xs.Tail) Eval(xs.Head); }
public static void PrintBindings(List e1, List e2) { var bindings = Unifier.Unify(e1, e2); foreach (var kv in bindings) Console.WriteLine("Variable = {0}, Value = {1}", kv.Key, kv.Value); }
/// <summary> /// The node has to be transformed before being passed /// </summary> /// <param name="n"></param> /// <returns></returns> public Expression ToExpr(Node n) { switch (n.Label) { case "Empty": return Noop; case "Return": return (n.Count > 0) ? Expression.Return(GetReturnTarget(), ToExpr(n[0]), typeof(Object)) : Expression.Return(GetReturnTarget(), Expression.Constant(typeof(Object), null)); case "If": return n.Count > 2 ? Expression.IfThenElse(Expression.Convert(ToExpr(n[0]), typeof(Boolean)), ToExpr(n[1]), ToExpr(n[2])) : Expression.IfThen(Expression.Convert(ToExpr(n[0]), typeof(Boolean)), ToExpr(n[1])); case "Else": return ToExpr(n[0]); case "For": return CompileForLoop(ToExpr(n[0]), ToExpr(n[1]), ToExpr(n[2]), ToExpr(n[3])); case "VarDecl": { var r = AddBinding(Expression.Variable(typeof(object), n[0].Text)); if (n.Count > 1) r = Expression.Assign(r, ToExpr(n[1])); return r; } case "Block": return ScopedExpr(() => Expression.Block(n.Nodes.Select(ToExpr))); case "TertiaryExpr": return Expression.Condition(ToExpr(n[0]), ToExpr(n[1]), ToExpr(n[2])); case "BinaryExpr": return Expression.Call(null, Primitives.GetMethodFromBinaryOperator(n[1].Text), ToExpr(n[0]), ToExpr(n[2])); case "AssignExpr": return Expression.Assign(ToExpr(n[0]), ToExpr(n[2])); case "FieldExpr": return Expression.Field(ToExpr(n[0]), n[1].Text); case "IndexExpr": return CompileIndexExpr(ToExpr(n[0]), ToExpr(n[2])); case "CallExpr": return CompileCallExpr(ToExpr(n[0]), n[1].Nodes.Select(ToExpr)); case "MethodCallExpr": { var self = ToExpr(n[0]); var func = Expression.Field(self, n[1].Text); var args = new List<Expression>() { self }; args.AddRange(n[2].Nodes.Select(ToExpr)); return CompileCallExpr(func, args); } case "PrefixExpr": switch (n[0].Text) { case "!": return Expression.Not(ToExpr(n[1])); case "~": return Expression.OnesComplement(ToExpr(n[1])); case "-": return Expression.Negate(ToExpr(n[1])); default: throw new Exception("Unrecognized prefix operator " + n[0].Text); } case "NewExpr": return ScopedExpr(() => { AddBinding("this", Expression.Constant(new JsonObject())); return ToExpr(n[0]); }); case "ParenExpr": return ToExpr(n[0]); case "AnonFunc": { var ps = n[0].Nodes.Select(x => Expression.Parameter(typeof(Object), x.Text)); return CreateStatementLambda(ps, () => ToExpr(n[1])); }; case "Object": throw new NotImplementedException(); case "Array": return Expression.NewArrayInit(typeof(object), n.Nodes.Select(ToExpr)); case "Identifier": return Lookup(n.Text); case "String": return Expression.Constant(Utilities.Unquote(n.Text)); case "Integer": return Expression.Convert(Expression.Constant(int.Parse(n.Text)), typeof(Object)); case "Float": return Expression.Convert(Expression.Constant(float.Parse(n.Text)), typeof(Object)); case "True": return Expression.Convert(Expression.Constant(true), typeof(Object)); case "False": return Expression.Convert(Expression.Constant(false), typeof(Object)); case "Null": return Expression.Constant(null); default: throw new Exception("Unrecognized node type " + n.Label); } }
public Rule(IEnumerable<Rule> rules) { if (rules.Any(r => r == null)) throw new Exception("No child rule can be null"); Children = new List<Rule>(rules); }
public void Join() { Code = Pop(ref Dump); }
public void LoadFunction(List f) { Push(List.Cons(f, Env)); }
public void TailApply() { List closure = Pop(); List paramlist = Pop(); Env = List.Cons(closure.Tail, Env.Tail); Push(paramlist, ref Env); Code = closure.Head; Stack = List.Empty; }
public void Push(object o, ref List list) { list = List.Cons(o, list); }
public void Assign(ParserState state) { input = state.input; pos = state.pos; nodes = state.nodes; }