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

            this.Car = new Place(car);
            this.Cdr = new Place(cdr);
        }
Exemple #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;
        }
        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;
        }
Exemple #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;
        }
Exemple #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;
        }
Exemple #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;
        }
Exemple #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;
        }
        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;
                }
            }
        }
Exemple #9
0
 public Place(LispObject initialValue)
 {
     // todo1[ak] check args
     this.Value = initialValue;
 }
Exemple #10
0
 public static Cons MakeQuote(LispObject quoted)
 {
     Cons cons = new Cons(Symbol.Quote, new Cons(quoted, Symbol.Nil));
     return cons;
 }
Exemple #11
0
 public static Cons MakeFunction(LispObject lambda)
 {
     Cons cons = new Cons(Symbol.Function, new Cons(lambda, Symbol.Nil));
     return cons;
 }
Exemple #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;
        }
Exemple #13
0
        public void SetRegisterValue(RegisterName registerName, LispObject value)
        {
            // todo1[ak] check args

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

            _variables[v.Name] = v;
        }
Exemple #15
0
 public LispObject EvaluateObject(LispObject obj, Context context)
 {
     LispObject res = _evaluator.EvaluateSingle(obj, context);
     return res;
 }
Exemple #16
0
 private void Push(LispObject obj)
 {
     _stack.Push(obj);
 }
Exemple #17
0
 public abstract void AddObject(LispObject obj);
Exemple #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;
        }
Exemple #19
0
 //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);
 }