Exemple #1
0
 public static Pair ConcatListAndPairToNewPair(List<object> l, Pair p)
 {
     for (int i = l.Count - 1; i >= 0; --i) {
         p = new Pair { Car = l[i], Cdr = p };
     }
     return p;
 }
Exemple #2
0
 public static List<object> PairToList(Pair l)
 {
     List<object> r = new List<object>();
     while (l != null) {
         r.Add(l.Car);
         l = (Pair)l.Cdr;
     }
     return r;
 }
Exemple #3
0
 public static Pair ListToPair(List<object> l)
 {
     Pair r = null;
     for (int i = l.Count - 1; i >= 0; --i) {
         r = new Pair() {
             Car = l[i], Cdr = r
         };
     }
     return r;
 }
        private ASTInterpreter()
        {
            Utils.QueryPerformanceCounter(out sTimerStart);
            Utils.QueryPerformanceFrequency(out sTimerFreq);

            Dictionary <string, object> builtinVars = new Dictionary <string, object>()
            {
                { "true", true },
                { "false", false },
                { "else", true },
                { "null", null },
            };

            Dictionary <string, Procedure> builtinProcedures = new Dictionary <string, Procedure>()
            {
                { "not", (args) => !(bool)args[0] },
                { "identity", (args) => args[0] },
                { "sqr", (args) => { var a = args[0] as INumber; return(a.Mul(a)); } },
                { "+", (args) => ((INumber)args[0]).Add((INumber)args[1]) },
                { "-", (args) => ((INumber)args[0]).Sub((INumber)args[1]) },
                { "*", (args) => ((INumber)args[0]).Mul((INumber)args[1]) },
                { "/", (args) => ((INumber)args[0]).Div((INumber)args[1]) },
                { "quotient", (args) => ((INumber)args[0]).Div((INumber)args[1]).CastToInteger() },
                { "remainder", (args) => ((INumber)args[0]).Mod((INumber)args[1]) },
                { "=", (args) => (args[0].Equals(args[1])) },
                { "<", (args) => (args[0] as IComparable).CompareTo(args[1]) < 0 },
                { "<=", (args) => (args[0] as IComparable).CompareTo(args[1]) <= 0 },
                { ">", (args) => (args[0] as IComparable).CompareTo(args[1]) > 0 },
                { ">=", (args) => (args[0] as IComparable).CompareTo(args[1]) >= 0 },
                { "eq?", (args) => object.ReferenceEquals(args[0], args[1]) },

                { "cons", (args) => new Pair()
                  {
                      Car = args[0], Cdr = args[1]
                  } },
                { "car", (args) => ((Pair)args[0]).Car },
                { "cdr", (args) => ((Pair)args[0]).Cdr },
                { "drop", (args) => {
                      Pair l = (Pair)args[0]; int n = ((NumberInteger)args[1]).value;
                      for (; n > 0; --n)
                      {
                          l = (Pair)l.Cdr;
                      }
                      return(l);
                  } },
                { "length", (args) => {
                      int n = 0;
                      for (Pair l = (Pair)args[0]; l != null; ++n, l = (Pair)l.Cdr)
                      {
                          ;
                      }
                      return(Number.Create(n));
                  } },
                { "append", (args) => {
                      var l = ListProcess.PairToList((Pair)args[0]);
                      l.InsertRange(l.Count, ListProcess.PairToList((Pair)args[1]));
                      return(ListProcess.ListToPair(l));
                  } },
                { "empty?", (args) => args[0] == null },

                { "pretty-print", (args) => {
                      ListProcess.PrintPairExp(args[0]);
                      return(null);
                  } },
                { "display", (args) => {
                      ListProcess.PrintListExp(ListProcess.PairExpToListExp(args[0]));
                      return(null);
                  } },
                { "current-inexact-milliseconds", (args) => {
                      long now;
                      Utils.QueryPerformanceCounter(out now);
                      return(Number.Create((decimal)(now - sTimerStart) * 1000 / sTimerFreq));
                  } },
                { "exit", (args) => {
                      Environment.Exit(0);
                      return(null);
                  } },
                { "random", (args) => Number.Create(sRandom.Next(((NumberInteger)args[0]).value)) },
                { "eval", (args) => Instance().Interpret(ListProcess.PairExpToListExp(args[0])) },
            };

            Env.ReserveGlobalVariables(builtinVars.Count + builtinProcedures.Count);
            foreach (KeyValuePair <string, object> kv in builtinVars)
            {
                Env.DefineBuiltin(SymbolTable.DefineOrGetGlobalSymbol(kv.Key), kv.Value);
            }
            foreach (KeyValuePair <string, Procedure> kv in builtinProcedures)
            {
                Env.DefineBuiltin(SymbolTable.DefineOrGetGlobalSymbol(kv.Key), kv.Value);
            }
        }