Example #1
0
        public object Interpret(object exp)
        {
            IASTNode node = ASTCompiler.Compile(null, ListProcess.TransformLibraryForms(exp));

            Env.ReserveGlobalVariables(SymbolTable.GetGlobalSymbolCount());

            return(new ASTNodeVisitor_Interpreter(null, node).Value);
        }
Example #2
0
        public object Interpret(object exp)
        {
            IASTNode node = ASTCompiler.Compile(null, ListProcess.TransformLibraryForms(exp));

            GlobalEnv.Instance().ReserveVariables(SymbolTable.GetGlobalSymbolCount());

            var lambda = new ASTNode_Lambda {
                bodyNode = node, localVarCount = 0, freeAddresses = new List <FreeAddress>(),
            };

            return(mStack.CallScriptProcedure(new ScriptProcedure(null, CompileToByteCode(lambda)), new List <object> {
            }));
        }
Example #3
0
        static void Main(string[] args)
        {
            // ByteCodeInterpreter.Instance().SetMaxStackDepth(1000, 300);

            var tokens = Parser.Tokenize(new System.IO.StreamReader("../../test.rkt").ReadToEnd());

            while (true)
            {
                object s = Parser.Parse(tokens);
                if (s == null)
                {
                    break;
                }
                object v = ByteCodeInterpreter.Instance().Interpret(s);
                // object v = ASTInterpreter.Instance().Interpret(s);
                if (v != null)
                {
                    ListProcess.PrintPairExp(v);
                }
            }
        }
Example #4
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;
            }
        }
Example #5
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);
            }
        }
        static public IASTNode Compile(SymbolTable symTable, object exp)
        {
            if (exp is string)
            {
                return(new ASTNode_GetVar {
                    address = SymbolTable.Lookup(symTable, (string)exp)
                });
            }
            else if (exp is INumber)
            {
                return(new ASTNode_Literal {
                    value = exp
                });
            }

            List <object> form = exp as List <object>;

            switch (form[0] as string)
            {
            case "quote":
                return(new ASTNode_Literal {
                    value = ListProcess.ListExpToPairExp(form[1])
                });

            case "if":
                return(new ASTNode_If {
                    predNode = Compile(symTable, form[1]), thenNode = Compile(symTable, form[2]), elseNode = Compile(symTable, form[3])
                });

            case "begin":
                return(new ASTNode_Begin {
                    nodes = form.Skip(1).Select(e => Compile(symTable, e)).ToList()
                });

            case "lambda": {
                SymbolTable newSymTable = new SymbolTable(symTable);

                foreach (var name in ((List <object>)form[1]).Cast <string>())
                {
                    newSymTable.DefineLocalSymbol(name);
                }

                List <string> defines = new List <string>();
                ListProcess.FindDefinition(defines, (List <object>)form[2], 1);
                foreach (var name in defines)
                {
                    newSymTable.DefineLocalSymbol(name);
                }

                IASTNode body = Compile(newSymTable, form[2]);

                return(new ASTNode_Lambda {
                        localVarCount = newSymTable.GetLocalSymbolCount(),
                        bodyNode = body,
                        freeAddresses = new ASTNodeVisitor_FindFreeAddresses(body).FreeAddresses,
                    });
            }

            case "define":
            case "set!":
                return(new ASTNode_SetVar {
                    address = SymbolTable.Lookup(symTable, (string)form[1]), rightNode = Compile(symTable, form[2])
                });

            default:
                return(new ASTNode_Application {
                    procedureNode = Compile(symTable, form[0]),
                    actualNodes = form.Skip(1).Select(e => Compile(symTable, e)).ToList(),
                });
            }
        }