public void ShouldUnderstandLambdasAndApplyAnonymousFunctions() { var context = new LispContext(); var expr = ToyLispParser.ParseExpression("((lambda (x y) (+ x y)) 1 2)").Reduce(context); Assert.AreEqual("3", expr.ToString()); }
public void ShouldApplyFunctionToAtomsOrLists2() { var context = new LispContext(); var expr1 = ToyLispParser.ParseExpression("(- (* (/ (+ 1 2) 3) 4) 5)").Reduce(context); Assert.AreEqual("-1", expr1.ToString()); }
public void ShouldReturnRightValueFromIfFunction() { var context = new LispContext(); var expr1 = ToyLispParser.ParseExpression("(if 1 1 2)").Reduce(context); var expr2 = ToyLispParser.ParseExpression("(if '() 1 2)").Reduce(context); Assert.AreEqual("1", expr1.ToString()); Assert.AreEqual("2", expr2.ToString()); }
public void ShouldApplyFunctionToAtomsOrLists3() { var context = new LispContext(); var expr1 = ToyLispParser.ParseExpression("(+ (+ 3))").Reduce(context); var expr2 = ToyLispParser.ParseExpression("(- (- 3))").Reduce(context); Assert.AreEqual("3", expr1.ToString()); Assert.AreEqual("3", expr2.ToString()); }
public static void Main(string[] args) { LispContext ctx = new LispContext(); ctx.register("+", new LispAdd()); ctx.register("get", new LispGet(ref ctx)); ctx.register("set", new LispSet(ref ctx)); ctx.register("range", new LispRange()); ctx.register("store", new LispBoundValue(new LispFloating(0.001))); while (true) { Console.Write("> "); string n = Console.ReadLine(); Console.WriteLine(ctx.evaluate(n).eval().getString()); } }
static void Main(string[] args) { var context = new LispContext(); while (true) { Console.Write("<<< "); var input = Console.ReadLine(); try { var expression = ToyLispParser.ParseExpression(input).Reduce(context); Console.WriteLine(">>> {0}", expression); } catch (CloseReplException) { break; } catch (Exception e) { Console.WriteLine(e.Message); } Console.WriteLine(); } }
public override LispExpression Apply(IEnumerable <LispExpression> expressions, LispContext context) { var e = expressions as LispExpression[] ?? expressions.ToArray(); CanBeCalledWithOnlyNArguments(Id, e, 2); var id = e[0].ToString(); var value = e[1].Reduce(context); context[id] = value; return(value); }
public override LispExpression Apply(IEnumerable <LispExpression> expressions, LispContext context) { var e = expressions as LispExpression[] ?? expressions.ToArray(); CannotBeCalledWithZeroArguments(Id, e); if (e.Length == 1) { var number = ((LispAtom)e.ElementAt(0).Reduce(context)).ToDecimal(); return(new LispAtom(number.ToString(CultureInfo.InvariantCulture))); } return(new LispAtom(e.Select(a => a.Reduce(context)) .Cast <LispAtom>() .Select(a => a.ToDecimal()) .Sum() .ToString(CultureInfo.InvariantCulture))); }
public override LispExpression Apply(IEnumerable <LispExpression> expressions, LispContext context) { var e = expressions as LispExpression[] ?? expressions.ToArray(); CanBeCalledWithOnlyNArguments(Id, e, 3); return(e[0].Reduce(context).ToString() != "()" ? e[1].Reduce(context) : e[2].Reduce(context)); }
public override LispExpression Apply(IEnumerable <LispExpression> expressions, LispContext context) { var e = expressions as LispExpression[] ?? expressions.ToArray(); CannotBeCalledWithFewerThanNArguments(Id, e, 2); return(new LispAtom(e.Select(a => a.Reduce(context)) .Cast <LispAtom>() .Select(a => a.ToDecimal()) .Aggregate(1m, (current, d) => current * d) .ToString(CultureInfo.InvariantCulture))); }
public override LispExpression Apply(IEnumerable <LispExpression> expressions, LispContext context) { var e = expressions as LispExpression[] ?? expressions.ToArray(); CanBeCalledWithOnlyNArguments(Id, e, 2); var arguments = ((LispList)e[0]).Expressions.OfType <LispAtom>().Select(a => a.ToString()).ToArray(); return(new AnonymousFunction(arguments, e[1])); }
public override LispExpression Apply(IEnumerable <LispExpression> expressions, LispContext context) { var e = expressions as LispExpression[] ?? expressions.ToArray(); CannotBeCalledWithZeroArguments(Id, e); if (e.Length == 1) { var number = ((LispAtom)e.ElementAt(0).Reduce(context)).ToDecimal(); return(new LispAtom((-number).ToString(CultureInfo.InvariantCulture))); } var result = ((LispAtom)e.ElementAt(0).Reduce(context)).ToDecimal(); result = e.Skip(1) .Select(a => a.Reduce(context)) .Cast <LispAtom>() .Select(a => a.ToDecimal()) .Aggregate(result, (current, d) => current - d); return(new LispAtom(result.ToString(CultureInfo.InvariantCulture))); }