PairToList() 공개 정적인 메소드

public static PairToList ( Pair l ) : List
l Pair
리턴 List
예제 #1
0
        private ByteCodeInterpreter()
        {
            Utils.QueryPerformanceCounter(out mTimerStart);
            Utils.QueryPerformanceFrequency(out mTimerFreq);

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

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

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

                { "pretty-print", (stack, ret) => {
                      ListProcess.PrintPairExp(stack[ret + 1]);
                      stack[ret] = null;
                  } },
                { "display", (stack, ret) => {
                      ListProcess.PrintListExp(ListProcess.PairExpToListExp(stack[ret + 1]));
                      stack[ret] = null;
                  } },
                { "current-inexact-milliseconds", (stack, ret) => {
                      long now;
                      Utils.QueryPerformanceCounter(out now);
                      stack[ret] = Number.Create((decimal)(now - mTimerStart) * 1000 / mTimerFreq);
                  } },
                { "exit", (stack, ret) => {
                      Environment.Exit(0);
                      stack[ret] = null;
                  } },
                { "random", (stack, ret) => stack[ret] = Number.Create(mRandom.Next(((NumberInteger)stack[ret + 1]).value)) },
                { "eval", (stack, ret) => stack[ret] = Interpret(ListProcess.PairExpToListExp(stack[ret + 1])) },
            };

            GlobalEnv.Instance().ReserveVariables(builtinVars.Count + builtinProcedures.Count);
            foreach (KeyValuePair <string, object> kv in builtinVars)
            {
                GlobalEnv.Instance().variables[SymbolTable.DefineOrGetGlobalSymbol(kv.Key).index] = kv.Value;
            }
            foreach (KeyValuePair <string, HostProcedure> kv in builtinProcedures)
            {
                GlobalEnv.Instance().variables[SymbolTable.DefineOrGetGlobalSymbol(kv.Key).index] = kv.Value;
            }
        }
예제 #2
0
        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);
            }
        }