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());
        }
Ejemplo n.º 5
0
        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 void ShouldUnderstandEmptyList()
        {
            var expr = ToyLispParser.ParseExpression("()");

            Assert.AreEqual("()", expr.ToString());
        }