public Cons(LispObject car, LispObject cdr) { // todo1[ak] check args this.Car = new Place(car); this.Cdr = new Place(cdr); }
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; }
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; }
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; }
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; }
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; } } }
public Place(LispObject initialValue) { // todo1[ak] check args this.Value = initialValue; }
public static Cons MakeQuote(LispObject quoted) { Cons cons = new Cons(Symbol.Quote, new Cons(quoted, Symbol.Nil)); return cons; }
public static Cons MakeFunction(LispObject lambda) { Cons cons = new Cons(Symbol.Function, new Cons(lambda, Symbol.Nil)); return cons; }
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; }
public void SetRegisterValue(RegisterName registerName, LispObject value) { // todo1[ak] check args _registers[registerName] = value; }
public void SetVariable(string name, LispObject value) { Variable v = new Variable(name, value); _variables[v.Name] = v; }
public LispObject EvaluateObject(LispObject obj, Context context) { LispObject res = _evaluator.EvaluateSingle(obj, context); return res; }
private void Push(LispObject obj) { _stack.Push(obj); }
public abstract void AddObject(LispObject obj);
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; }
//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); }