示例#1
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);
        }
示例#2
0
        public override LispNode Operate(LispEnvironment env, LispNode node)
        {
            if (node.Nested.Count != 1)
            {
                return(new LispTooManyArgsException(Contents, 1, node.Nested.Count));
            }

            var dnode = node.Nested[0] as LispDataList;

            if (dnode == null)
            {
                return(new LispIncorrectArgTypesException(Contents, 0, LispNodeType.DATALIST.ToString(), node.NodeType.ToString()));
            }

            if (dnode.Nested.Count == 0)
            {
                return(new LispEmptyDataException(Contents));
            }

            var result = dnode;

            result.Pop();

            return(result);
        }
示例#3
0
        public static LispNode Take(this LispNode node, int index)
        {
            var p = node.Nested[index];

            node.Nested.Clear();
            return(p);
        }
示例#4
0
        public static LispNode Pop(this LispNode node)
        {
            var p = node.Nested.First();

            node.Nested.RemoveAt(0);
            return(p);
        }
示例#5
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));
        }
示例#6
0
 public void Add(string sym, LispNode node)
 {
     if (Env.ContainsKey(sym))
     {
         Env.Remove(sym);
     }
     Env[sym] = node;
 }
示例#7
0
        public static LispNode Join(LispNode x, LispNode y)
        {
            while (y.Nested.Count > 0)
            {
                x.Nested.Add(y.Pop());
            }

            y.Nested.Clear();

            return(x);
        }
示例#8
0
        public void AddGlobal(string sym, LispNode node)
        {
            var global = this;

            while (global.parentEnv != null)
            {
                global = global.parentEnv;
            }

            global.Add(sym, node);
        }
示例#9
0
 public static bool CheckIfAllNumbers(LispNode node, out LispNode err)
 {
     if (!node.Nested.All(item => item is LispNumber))
     {
         node.Nested.Clear();
         err = new LispNotNumberException();
         return(true);
     }
     err = null;
     return(false);
 }
示例#10
0
 public virtual LispNode CallFunction(LispEnvironment env, LispNode node)
 {
     if (Apply != null)
     {
         return(Apply(env, node));
     }
     else
     {
         return(new LispNotSymbolException());
     }
 }
示例#11
0
        public override LispNode Operate(LispEnvironment env, LispNode node)
        {
            var dnode = node as LispList;

            if (dnode == null)
            {
                return(new LispIncorrectArgTypesException(Contents, 0, LispNodeType.LIST.ToString(), node.NodeType.ToString()));
            }

            return(new LispDataList {
                Nested = dnode.Nested
            });
        }
示例#12
0
        public static LispNode Evaluate(LispEnvironment env, LispNode node)
        {
            if (node is InputSymbol)
            {
                return(env.Get(((InputSymbol)node).Symbol));
            }

            if (node is LispList)
            {
                return(EvaluateExpr(env, node));
            }

            return(node);
        }
示例#13
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);
        }
示例#14
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;
        }
示例#15
0
        public static long GetTotalNodes(LispNode node)
        {
            if (node.Nested == null || node.Nested.Count == 0)
            {
                return(1);
            }

            if (node.Nested?.Count >= 1)
            {
                long total = 1;

                node.Nested.ToList().ForEach(childNode => {
                    total += GetTotalNodes(childNode);
                });

                return(total);
            }

            return(0);
        }
示例#16
0
        public override LispNode Operate(LispEnvironment env, LispNode node)
        {
            if (node.Nested.Count != 1)
            {
                return(new LispTooManyArgsException(Contents, 1, node.Nested.Count));
            }

            var dnode = node.Take(0) as LispDataList;

            if (dnode == null)
            {
                return(new LispIncorrectArgTypesException(Contents, 0, LispNodeType.DATALIST.ToString(), node.NodeType.ToString()));
            }


            var x = new LispList {
                Nested = dnode.GetNodeCopy().Nested
            };

            return(Interpreter.Evaluate(env, x));
        }
示例#17
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);
        }
示例#18
0
        public override LispNode Operate(LispEnvironment env, LispNode node)
        {
            var dnode = node.Nested[0] as LispDataList;

            var defs = node.Nested.Skip(0).ToList();

            if (dnode == null)
            {
                return(new LispIncorrectArgTypesException(Contents, 0, LispNodeType.DATALIST.ToString(), node.NodeType.ToString()));
            }

            if (!dnode.Nested.All(n => n is InputSymbol))
            {
                return(new LispNonSymbolException(Contents));
            }

            if (dnode.Nested.Count != node.Nested.Count - 1)
            {
                return(new LispIncorrectNumberToDefineException(Contents));
            }

            int index = 1;

            dnode.Nested.ForEach((n) => {
                var sym = n as InputSymbol;
                if (GlobalAssign)
                {
                    env.AddGlobal(sym.Symbol, node.Nested[index++]);
                }
                else
                {
                    env.Add(sym.Symbol, node.Nested[index++]);
                }
            });

            return(new LispList());
        }
示例#19
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));
        }
示例#20
0
 public UserDefinedFunction(UserDefinedFunction fn)
 {
     FuncEnv = fn.FuncEnv;
     formals = fn.formals;
     body    = fn.body;
 }
示例#21
0
 public UserDefinedFunction(LispNode _formals, LispNode _body)
 {
     FuncEnv = new LispEnvironment(null);
     formals = _formals;
     body    = _body;
 }
示例#22
0
 public override LispNode Operate(LispEnvironment env, LispNode a)
 {
     Console.WriteLine(a);
     return(a);
 }
示例#23
0
 public abstract LispNode Operate(LispEnvironment env, LispNode a);