private LispList subst(LispDataType newData, LispDataType oldData, LispList list) { Dictionary <string, LispDataType> subs = new Dictionary <string, LispDataType>(); subs[oldData.Evaluate(this).ToString()] = newData.Evaluate(this); return(list.Replace(subs)); }
private LispDataType dotimes(LispList args, LispDataType body) { if (args.Count != 2 && args.Count != 3) { throw new LispException(string.Format(ERR_INVALID_NUMBER_OF_ARGUMENTS, "DOTIMES")); } if (!(args[0] is LispSymbolicAtom)) { throw new LispException(string.Format(ERR_NOT_A_SYMBOL, args[0])); } LispSymbolicAtom counterVar = (LispSymbolicAtom)args[0]; LispSymbolicAtom[] symbolArray = new LispSymbolicAtom[] { counterVar }; setq(symbolArray, new LispNumericAtom[] { new LispNumericAtom((LispNumericAtom)args[1].Evaluate(this)) - 1 }); while ((LispNumericAtom)counterVar.Evaluate(this) >= 0) { body.Evaluate(this); setq(symbolArray, new LispNumericAtom[] { new LispNumericAtom((LispNumericAtom)counterVar.Evaluate(this)) - 1 }); } if (args.Count == 3) { return(args[2].Evaluate(this)); } return(new LispList()); }
private bool eqlBoolean(LispDataType d1, LispDataType d2) { d1 = d1.Evaluate(this); d2 = d2.Evaluate(this); if (d1 is LispList && d2 is LispList) { if (((LispList)d1).Count == 0 && ((LispList)d2).Count == 0) { return(true); } else { return(false); } } else if (d1.ToString() == d2.ToString()) { return(true); } else { return(false); } }
private LispDataType eql(LispDataType d1, LispDataType d2) { LispSymbolicAtom t = new LispSymbolicAtom("T"); LispList nil = new LispList();; d1 = d1.Evaluate(this); d2 = d2.Evaluate(this); if (d1 is LispList && d2 is LispList) { if (((LispList)d1).Count == 0 && ((LispList)d2).Count == 0) { return(t); } else { return(nil); } } else if (d1.ToString() == d2.ToString()) { return(t); } else { return(nil); } }
public LispDataType Evaluate(List <LispDataType> arguments, LispInterpreter context) { // replace function arguments if (arguments.Count != args.Count) { throw new LispException(String.Format(ERR_INVALID_NUMBER_OF_ARGUMENTS, name)); } if (func is LispNumericAtom) { return(func.Evaluate(context)); } else if (func is LispList) { // Create the argument replacement map. Dictionary <string, LispDataType> argReplacements = new Dictionary <string, LispDataType>(); for (int i = 0; i < args.Count; ++i) { LispDataType d = arguments[i].Evaluate(context); if (d is LispList) { argReplacements[args[i].Value] = d.Copy(); } else { argReplacements[args[i].Value] = d; } } // Replace the arguments and evaluate. return(((LispList)func).Replace(argReplacements, false).Evaluate(context)); } else { if (args.Count >= 1 && args[0].Value == ((LispSymbolicAtom)func).Value) { return(arguments[0].Evaluate(context)); } else { return(func.Evaluate(context)); } } }
private LispDataType equal(LispDataType d1, LispDataType d2) { if (d1.Evaluate(this).ToString() == d2.Evaluate(this).ToString()) { return(new LispSymbolicAtom("T")); } else { return(new LispList()); } }
private LispDataType not(LispDataType d) { if (!d.Evaluate(this)) { return(new LispSymbolicAtom("T")); } else { return(new LispList()); } }
private LispDataType isAtom(LispDataType data) { if (data.Evaluate(this).IsAtom) { return(new LispSymbolicAtom("T")); } else { return(new LispList()); } }
private LispDataType isNull(LispDataType data) { if (data.Evaluate(this).ToString() == "NIL") { return(new LispSymbolicAtom("T")); } else { return(new LispList()); } }
private LispList member(LispDataType target, LispList list) { target = target.Evaluate(this); for (int i = 0; i < list.Count; ++i) { if (eql(list[i], target)) { return(list.GetRange(i, list.Count - i)); } } return(new LispList()); }
private LispDataType lispIf(LispDataType pred1, LispDataType result, LispDataType elseResult = null) { if (pred1.Evaluate(this)) { return(result.Evaluate(this)); } else if (elseResult != null) { return(elseResult.Evaluate(this)); } else { return(new LispList()); } }
// Creates Lisp datatypes out of the command buffer and executes them. // Incomplete lists will be preserved in the buffer. public string Eval() { string outputString = string.Empty; try { // Parse the list. foreach (LispDataType data in ProcessInputBuffer(ref buffer)) { CommandQueue.Enqueue(data); } } catch (LispException e) { outputString += e.Message; buffer = string.Empty; } try { while (CommandQueue.Count > 0) { LispDataType command = CommandQueue.Dequeue(); // Exit command. if (command is LispList) { LispList cmd = (LispList)command; if (cmd.Count >= 1 && cmd.First().ToString() == "EXIT") { exit = true; return(outputString); } } // Evaluate the command. outputString += command.Evaluate(this); if (CommandQueue.Count > 0) { outputString += '\n'; } } } catch (LispException e) { outputString += e.Message; CommandQueue.Clear(); } return(outputString); }
private LispDataType dolist(LispList args, LispDataType body) { if (args.Count != 2 && args.Count != 3) { throw new LispException(string.Format(ERR_INVALID_NUMBER_OF_ARGUMENTS, "DOLIST")); } if (!(args.First() is LispSymbolicAtom)) { throw new LispException(string.Format(ERR_NOT_A_SYMBOL, args.First())); } LispDataType list = args[1].Evaluate(this); if (!(list is LispList)) { throw new LispException(string.Format(ERR_NOT_A_LIST, list)); } List <LispSymbolicAtom> varList = new List <LispSymbolicAtom> { (LispSymbolicAtom)args.First() }; foreach (LispDataType data in (LispList)list) { setq(varList, new List <LispDataType> { data }); body.Evaluate(this); } if (args.Count == 3) { return(args[2].Evaluate(this)); } else { return(new LispList()); } }
private LispDataType set(LispSymbolicAtom symbol, LispDataType value) { LispGlobals[symbol.Value] = value.Evaluate(this); return(value); }