Пример #1
0
        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));
        }
Пример #2
0
        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);
        }
Пример #3
0
        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);
        }
Пример #4
0
        public static LispNode Join(LispNode x, LispNode y)
        {
            while (y.Nested.Count > 0)
            {
                x.Nested.Add(y.Pop());
            }

            y.Nested.Clear();

            return(x);
        }
Пример #5
0
        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);
        }
Пример #6
0
        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;
        }
Пример #7
0
        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));
        }