示例#1
0
        public Cons(LispObject car, LispObject cdr)
        {
            // todo1[ak] check args

            this.Car = new Place(car);
            this.Cdr = new Place(cdr);
        }
示例#2
0
        public Variable(string name, LispObject initialValue)
        {
            // todo1[ak] check args
            // todo1[ak] check upper-case
            // todo1[ak] internal ctor?

            this.Name = name;
            this.Value = initialValue;
        }
示例#3
0
        public override void AddObject(LispObject obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }

            if (this.IsClosed)
            {
                throw new ApplicationException(); // todo2[ak]
            }

            this.Close();

            _innerCons.Car.Value = obj;
        }
示例#4
0
        public static bool Eq(LispObject el1, LispObject el2)
        {
            bool eq;

            if (el1 is Number)
            {
                eq =
                    el2 is Number &&
                    el1.ToString() == el2.ToString();
            }
            else
            {
                throw new NotImplementedException();
            }

            return eq;
        }
示例#5
0
        public LispObject EvaluateSingle(LispObject obj, Context context)
        {
            // todo1[ak] check args

            Machine machine = this.Env.Machine;

            Routine main = new Routine("main", new Instruction[] { new EvalInstruction(), });

            machine.Load(main);
            machine.SetRegisterValue(RegisterName.EvForm, obj);

            machine.Exec();

            LispObject res = machine.GetRegisterValue(RegisterName.EvRes);

            return res;
        }
示例#6
0
        public string Write(LispObject obj)
        {
            // todo1[ak] args

            string res;
            if (obj is Atom)
            {
                res = obj.ToString();
            }
            else if (obj is Cons)
            {
                res = this.WriteCons((Cons)obj);
            }
            else
            {
                throw new ApplicationException(); // todo2[ak]
            }

            return res;
        }
示例#7
0
        public static int GetLengthOfNormalList(LispObject obj)
        {
            int len = 0;

            if (obj == Symbol.Nil)
            {
                // ok
            }
            else if (obj is Cons)
            {
                Cons cons = (Cons)obj;

                while (true)
                {
                    len++;

                    LispObject cdr = cons.Cdr.Value;

                    if (cdr == Symbol.Nil)
                    {
                        break;
                    }
                    else if (cdr is Cons)
                    {
                        cons = (Cons)cdr;
                    }
                    else
                    {
                        throw new ApplicationException(); // todo2[ak]
                    }
                }
            }
            else
            {
                throw new ApplicationException(); // todo2[ak]

            }

            return len;
        }
示例#8
0
        public override void AddObject(LispObject obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }

            if (this.IsClosed)
            {
                throw new ApplicationException(); // todo2[ak]
            }

            if (this.ClosedDot)
            {
                throw new ApplicationException(); // todo2[ak] (a b c . d e) <-- e is wrong!
            }

            if (this.OpenedDot)
            {
                _curr.Cdr.Value = obj;
                this.ClosedDot = true;
            }
            else
            {
                Cons newCons = new Cons(obj, Symbol.Nil);

                if (_head == null)
                {
                    _head = newCons;
                    _curr = _head;
                }
                else
                {
                    _curr.Cdr.Value = newCons;
                    _curr = newCons;
                }
            }
        }
示例#9
0
 public Place(LispObject initialValue)
 {
     // todo1[ak] check args
     this.Value = initialValue;
 }
示例#10
0
 public static Cons MakeQuote(LispObject quoted)
 {
     Cons cons = new Cons(Symbol.Quote, new Cons(quoted, Symbol.Nil));
     return cons;
 }
示例#11
0
 public static Cons MakeFunction(LispObject lambda)
 {
     Cons cons = new Cons(Symbol.Function, new Cons(lambda, Symbol.Nil));
     return cons;
 }
示例#12
0
        public static List<LispObject> LispObjectToArguments(LispObject args)
        {
            // todo1[ak] check not null

            List<LispObject> list = new List<LispObject>();
            LispObject curr = args;

            while (curr != Symbol.Nil)
            {
                if (curr is Cons)
                {
                    Cons cons = (Cons)curr;
                    list.Add(cons.Car.Value);

                    curr = cons.Cdr.Value;
                }
                else
                {
                    throw new ApplicationException("cannot make args"); // todo1[ak]
                }
            }

            return list;
        }
示例#13
0
        public void SetRegisterValue(RegisterName registerName, LispObject value)
        {
            // todo1[ak] check args

            _registers[registerName] = value;
        }
示例#14
0
        public void SetVariable(string name, LispObject value)
        {
            Variable v = new Variable(name, value);

            _variables[v.Name] = v;
        }
示例#15
0
文件: Env.cs 项目: taucode/copperlisp
 public LispObject EvaluateObject(LispObject obj, Context context)
 {
     LispObject res = _evaluator.EvaluateSingle(obj, context);
     return res;
 }
示例#16
0
 private void Push(LispObject obj)
 {
     _stack.Push(obj);
 }
示例#17
0
 public abstract void AddObject(LispObject obj);
示例#18
0
        public static LispObject GetNthElementOfNormalList(int index, LispObject obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }

            if (index < 0)
            {
                throw new ArgumentOutOfRangeException("index");
            }

            LispObject res;

            if (obj == Symbol.Nil)
            {
                res = Symbol.Nil;
            }
            else
            {
                if (obj is Cons)
                {
                    Cons cons = (Cons)obj;

                    if (index == 0)
                    {
                        res = cons.Car.Value;
                    }
                    else
                    {
                        res = GetNthElementOfNormalList(index - 1, cons.Cdr.Value);
                    }
                }
                else
                {
                    throw new ApplicationException(); // todo2[ak]
                }
            }

            return res;
        }
示例#19
0
文件: Env.cs 项目: taucode/copperlisp
 //public LispObject EvaluateFunction(Function f, List<LispObject> args, Context context)
 //{
 //    LispObject res = _evaluator.EvaluateFunction(f, args, context);
 //    return res;
 //}
 //public void AddSpecialForm(SpecialForm form)
 //{
 //    // todo1[ak] check that Env is mine
 //    _specialForms[form.Name] = form;
 //}
 //public void AddFunction(Function f)
 //{
 //    // todo1[ak] check that Env is mine
 //    _functions[f.Name] = f;
 //}
 public void AddVariable(string varName, LispObject value)
 {
     // todo1[ak] check args
     varName = varName.ToUpper(); // todo1[ak] deal with constants, system variables etc
     _rootContext.SetVariable(varName, value);
 }