public override LispNode Operate(LispEnvironment env, LispNode node) { if (node.Nested.Count != 1) { return(new LispTooManyArgsException(Contents, 1, node.Nested.Count)); } var dnode = node.Nested[0] as LispDataList; if (dnode == null) { return(new LispIncorrectArgTypesException(Contents, 0, LispNodeType.DATALIST.ToString(), node.NodeType.ToString())); } if (dnode.Nested.Count == 0) { return(new LispEmptyDataException(Contents)); } var result = dnode; result.Pop(); return(result); }
public static LispEnvironment GetEnvironment() { var env = new LispEnvironment(null); env.Env = GetBuiltInFunctions(); return(env); }
public override LispNode Operate(LispEnvironment env, LispNode node) { if (node.Nested.Count != 2) { return(new LispTooManyArgsException(Contents, 2, node.Nested.Count)); } var f = node.Pop() as LispDataList; if (f == null) { return(new LispIncorrectArgTypesException(Contents, 0, LispNodeType.DATALIST.ToString(), node.Nested[0].NodeType.ToString())); } var b = node.Pop() as LispDataList; if (b == null) { return(new LispIncorrectArgTypesException(Contents, 0, LispNodeType.DATALIST.ToString(), node.Nested[1].NodeType.ToString())); } node.Nested.Clear(); if (!f.Nested.All(n => n is InputSymbol)) { return(new LispNonSymbolException(Contents)); } return(new UserDefinedFunction(f, b)); }
public override LispNode Operate(LispEnvironment env, LispNode node) { LispNode err = null; if (LispNumberUtils.CheckIfAllNumbers(node, out err)) { return(err); } var a = node.Pop() as LispNumber; if (node.Nested.Count == 0) { a.Number = -a.Number; } else { while (node.Nested.Count > 0) { var b = node.Pop() as LispNumber; a.Number -= b.Number; } } return(a); }
public virtual LispNode CallFunction(LispEnvironment env, LispNode node) { if (Apply != null) { return(Apply(env, node)); } else { return(new LispNotSymbolException()); } }
public LispEnvironment GetCopy() { var en = new LispEnvironment(parentEnv); foreach (KeyValuePair <string, LispNode> entry in Env) { en.Env.Add(entry.Key, entry.Value.GetNodeCopy()); } return(en); }
public override LispNode Operate(LispEnvironment env, LispNode node) { var dnode = node as LispList; if (dnode == null) { return(new LispIncorrectArgTypesException(Contents, 0, LispNodeType.LIST.ToString(), node.NodeType.ToString())); } return(new LispDataList { Nested = dnode.Nested }); }
public static LispNode Evaluate(LispEnvironment env, LispNode node) { if (node is InputSymbol) { return(env.Get(((InputSymbol)node).Symbol)); } if (node is LispList) { return(EvaluateExpr(env, node)); } return(node); }
public override LispNode Operate(LispEnvironment env, LispNode node) { if (!node.Nested.All(item => item is LispDataList)) { node.Nested.Clear(); return(new LispNotDataListException(Contents)); } var x = node.Pop(); while (node.Nested.Count > 0) { x = LispNodeUtils.Join(x, node.Pop()); } return(x); }
public override LispNode CallFunction(LispEnvironment env, LispNode node) { int given = node.Nested.Count; int total = formals.Nested.Count; while (node.Nested.Count > 0) { if (formals.Nested.Count == 0) { return(new LispTooManyArgsException(Contents, total, given)); } var sym = formals.Pop() as InputSymbol; var val = node.Pop(); FuncEnv.Add(sym.Symbol, val); } node.Nested.Clear(); if (formals.Nested.Count == 0) { FuncEnv.parentEnv = env; return(Interpreter.Evaluate(FuncEnv, new LispList { Nested = body.GetNodeCopy().Nested })); } else { return(GetNodeCopy()); } // int index = 0; // node.Nested.ForEach(n => { // var sym = formals.Nested[index++] as InputSymbol; // FuncEnv.Add(sym.Symbol,n); // }); // node.Nested.Clear(); // FuncEnv.parentEnv = env; }
public override LispNode Operate(LispEnvironment env, LispNode node) { if (node.Nested.Count != 1) { return(new LispTooManyArgsException(Contents, 1, node.Nested.Count)); } var dnode = node.Take(0) as LispDataList; if (dnode == null) { return(new LispIncorrectArgTypesException(Contents, 0, LispNodeType.DATALIST.ToString(), node.NodeType.ToString())); } var x = new LispList { Nested = dnode.GetNodeCopy().Nested }; return(Interpreter.Evaluate(env, x)); }
public override LispNode Operate(LispEnvironment env, LispNode node) { LispNode err = null; if (LispNumberUtils.CheckIfAllNumbers(node, out err)) { return(err); } var a = node.Pop() as LispNumber; while (node.Nested.Count > 0) { var b = node.Pop() as LispNumber; if (b.Number == 0) { return(new LispDivideByZeroException()); } a.Number /= b.Number; } return(a); }
public override LispNode Operate(LispEnvironment env, LispNode node) { var dnode = node.Nested[0] as LispDataList; var defs = node.Nested.Skip(0).ToList(); if (dnode == null) { return(new LispIncorrectArgTypesException(Contents, 0, LispNodeType.DATALIST.ToString(), node.NodeType.ToString())); } if (!dnode.Nested.All(n => n is InputSymbol)) { return(new LispNonSymbolException(Contents)); } if (dnode.Nested.Count != node.Nested.Count - 1) { return(new LispIncorrectNumberToDefineException(Contents)); } int index = 1; dnode.Nested.ForEach((n) => { var sym = n as InputSymbol; if (GlobalAssign) { env.AddGlobal(sym.Symbol, node.Nested[index++]); } else { env.Add(sym.Symbol, node.Nested[index++]); } }); return(new LispList()); }
private static LispNode EvaluateExpr(LispEnvironment env, LispNode node) { node.Nested = node .Nested .ConvertAll(item => Evaluate(env, item)); if (node.Nested.Any(item => item is LispError)) { var item = node.Nested.First(err => err is LispError); node.Nested.Clear(); return(item); } // Empty Expression if (node.Nested.Count == 0) { return(node); } // Single value expression if (node.Nested.Count == 1) { return(node.Take(0)); } var op = node.Pop() as LispFunction; if (op == null) { node.Nested.Clear(); return(new LispNotSymbolException()); } return(op.CallFunction(env, node)); }
public UserDefinedFunction(UserDefinedFunction fn) { FuncEnv = fn.FuncEnv; formals = fn.formals; body = fn.body; }
public UserDefinedFunction(LispNode _formals, LispNode _body) { FuncEnv = new LispEnvironment(null); formals = _formals; body = _body; }
public override LispNode Operate(LispEnvironment env, LispNode a) { Console.WriteLine(a); return(a); }
public LispEnvironment(LispEnvironment parent) { parentEnv = parent; }
public abstract LispNode Operate(LispEnvironment env, LispNode a);