コード例 #1
0
        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());
        }
コード例 #2
0
        public void ShouldApplyFunctionToAtomsOrLists2()
        {
            var context = new LispContext();

            var expr1 = ToyLispParser.ParseExpression("(- (* (/ (+ 1 2) 3) 4) 5)").Reduce(context);

            Assert.AreEqual("-1", expr1.ToString());
        }
コード例 #3
0
        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());
        }
コード例 #4
0
        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());
        }
コード例 #5
0
        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());
            }
        }
コード例 #6
0
ファイル: Program.cs プロジェクト: taukita/My-Toy-Language
        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();
            }
        }
コード例 #7
0
        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);
        }
コード例 #8
0
        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)));
        }
コード例 #9
0
        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));
        }
コード例 #10
0
        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)));
        }
コード例 #11
0
        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]));
        }
コード例 #12
0
        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)));
        }