Ejemplo n.º 1
0
        internal override List Drop(int n)
        {
            if (Elements == null || Elements.Count <= n)
            {
                return(new List());
            }

            if (n < 0)
            {
                n = 0;
            }

            List.Node first = new List.Node()
            {
                car = Elements[n], cdr = null
            };
            List.Node last = first;

            int cnt = Elements.Count;

            for (int i = n + 1; i < cnt; i++)
            {
                last.cdr = new List.Node()
                {
                    car = Elements[i], cdr = null
                };
                last = last.cdr;
            }

            return(new List(first));
        }
Ejemplo n.º 2
0
        internal override List Take(int n)
        {
            if (n <= 0 || Elements == null || Elements.Count == 0)
            {
                return(new List());
            }

            List.Node first = new List.Node()
            {
                car = Elements[0], cdr = null
            };
            List.Node last = first;

            int cnt = Math.Min(n, Elements.Count);

            for (int i = 1; i < cnt; i++)
            {
                last.cdr = new List.Node()
                {
                    car = Elements[i], cdr = null
                };
                last = last.cdr;
            }

            return(new List(first));
        }
Ejemplo n.º 3
0
        internal override List Rest()
        {
            if (Elements == null || Elements.Count < 2)
            {
                return(new List(null as List.Node));
            }

            List.Node first = new List.Node()
            {
                car = Elements[1],
                cdr = null
            };

            List.Node last = first;

            for (int i = 2; i < Elements.Count; i++)
            {
                last.cdr = new List.Node()
                {
                    car = Elements[i],
                    cdr = null
                };

                last = last.cdr;
            }

            return(new List(first));
        }
Ejemplo n.º 4
0
        internal Vector(List elms)
        {
            if (elms == null || elms.IsEmpty())
            {
                Elements = new List <Value>();
            }
            else
            {
                int cnt = elms.Count();
                Elements = new List <Value>(cnt + 1);

                for (List.Node elm = elms.Elements; elm != null; elm = elm.cdr)
                {
                    Elements.Add(elm.car);
                }
            }
        }
Ejemplo n.º 5
0
        static Value Eval_ast(Value arg, Env env)
        {
            if (env == null)
            {
                return(arg);
            }

            if (arg == null)
            {
                return(arg);
            }

            if (arg is Symbol sym)
            {
                Value val = env.Get(sym);

                if (val == null)
                {
                    throw new MalException(string.Format("'{0}' not found", sym.Name));
                }

                return(val);
            }
            else if (arg is List lst)
            {
                if (lst.Elements == null)
                {
                    return(arg);
                }

                List <Value> elms = new List <Value>();
                for (List.Node elm = lst.Elements; elm != null; elm = elm.cdr)
                {
                    elms.Add(EVAL(elm.car, env));
                }

                return(new List(elms));
            }
            else if (arg is Vector vec)
            {
                List <Value> elms = new List <Value>();

                foreach (Value v in vec.Elements)
                {
                    elms.Add(EVAL(v, env));
                }

                return(new Vector(elms));
            }
            else if (arg is HashMap map)
            {
                HashMap hm = new HashMap();
                Dictionary <Value, Value> elms = hm.Elements;

                foreach (var kv in map.Elements)
                {
                    elms[kv.Key] = EVAL(kv.Value, env);
                }

                return(hm);
            }

            return(arg);
        }