コード例 #1
0
ファイル: Utils.cs プロジェクト: jmptrader/alsing-1
        public static ConsNode NewCons(RuntimeStack stack)
        {
            var resNode = new ConsNode();

            resNode.HideFromCallstack = true;
            resNode.Stack             = stack;
            return(resNode);
        }
コード例 #2
0
        public Engine()
        {
            Stack        = new RuntimeStack();
            Stack.Engine = this;
            Parser       = new LispParser();

            CoreConfigurator.Configure(Stack);

            ParseInfo info = SetupParseInfo(Resources.CoreLib.Replace("\r\n", "\n"), "core", true);

            ConsNode root = Parser.Parse(info);

            root.Eval(Stack.StackFrame);
        }
コード例 #3
0
 private void Reset()
 {
     EIP = 0;
     TempPool.Clear();
     RuntimeStack.Clear();
     VarSeg      = Generator.VarSeg;
     DataSegment = new int[Generator.VarSeg.Count];
     Initialized = new bool[Generator.VarSeg.Count];
     for (int i = 0; i < DataSegment.Length; ++i)
     {
         DataSegment[i] = 0;
         Initialized[i] = false;
     }
 }
コード例 #4
0
        public override bool Equals(object obj)
        {
            if (!(obj is RuntimeStack))
            {
                return(false);
            }

            if (obj == this)
            {
                return(true);
            }
            RuntimeStack rhs = (RuntimeStack)obj;

            return(ToString().Equals(rhs.ToString()));
        }
コード例 #5
0
    public Script()
    {
        // 寄存器初始化
        _ret_val = new Op {
            type = OpType.Null
        };

        funcTable   = new FuncTable();
        symbolTable = new SymbolTable();
        labelTable  = new LabelTable();
        stringTable = new StringTable();
        instrStream = new InstrStream();
        stack       = new RuntimeStack();

        // 初始化运行时堆栈大小
        stack.SetStackSize(1024);
    }
コード例 #6
0
        private static RuntimeStack LexParseEmitAndInterpret(string source)
        {
            var reader = source.CharwiseWithTrimmedLines();

            using var lexer = new Lexer(reader);
            var tokens    = lexer.Lex().ToList();
            var parser    = new Parser(tokens);
            var ast       = (ProgramSyntax)parser.Parse();
            var functions = new SymbolWaddler().WaddleProgram(ast);
            var emitter   = new EmittingVisitor(functions[Naming.EntryPointFunctionName].Variables
                                                .ToDictionary(kvp => kvp.Key, kvp => kvp.Value as Symbol));

            ast.Accept(emitter);
            var program     = emitter.BuildProgram();
            var interpreter = new Interpreter();
            var stack       = new RuntimeStack();

            interpreter.Interpret(program, stack);
            return(stack);
        }
コード例 #7
0
ファイル: Functions.cs プロジェクト: jmptrader/alsing-1
        private static ConsNode WrapInProgn(List <object> args, int startIndex, RuntimeStack stack)
        {
            var definedBody = new ConsNode();

            definedBody.Stack             = stack;
            definedBody.HideFromCallstack = true;
            definedBody.Code     = "";
            definedBody.CodeName = "runtime";
            definedBody.Args     = new List <object>();

            var progn = new SymbolNode();

            progn.Name              = "progn";
            progn.Stack             = stack;
            progn.HideFromCallstack = true;
            progn.Code              = "";
            progn.CodeName          = "runtime";

            definedBody.Args.Add(progn);
            definedBody.Args.AddRange(args.GetRange(startIndex, args.Count - startIndex));
            return(definedBody);
        }
コード例 #8
0
        internal unsafe void DumpStack(StackObject *esp, RuntimeStack stack)
        {
            var            start           = stack.StackBase;
            var            end             = esp + 10;
            var            frames          = stack.Frames;
            var            mStack          = stack.ManagedStack;
            var            valuePointerEnd = stack.ValueTypeStackPointer;
            StringBuilder  final           = new StringBuilder();
            HashSet <long> leakVObj        = new HashSet <long>();

            for (var i = stack.ValueTypeStackBase; i > stack.ValueTypeStackPointer;)
            {
                leakVObj.Add((long)i);
                i = Minus(i, i->ValueLow + 1);
            }
            for (var i = start; i <= end; i++)
            {
                StringBuilder sb = new StringBuilder();
                ILMethod      localMethod = null, baseMethod = null;
                bool          isLocal  = false;
                bool          isBase   = false;
                int           localIdx = 0;
                if (i == esp)
                {
                    sb.Append("->");
                }
                foreach (var j in frames)
                {
                    if (i >= j.LocalVarPointer && i < j.BasePointer)
                    {
                        isLocal     = true;
                        localIdx    = (int)(i - j.LocalVarPointer);
                        localMethod = j.Method;
                    }
                    else if (i == j.BasePointer)
                    {
                        isBase     = true;
                        baseMethod = j.Method;
                    }
                }
                sb.Append(string.Format("(0x{0:X8}) Type:{1} ", (long)i, i->ObjectType));
                GetStackObjectText(sb, i, mStack, valuePointerEnd);
                if (i < esp)
                {
                    if (i->ObjectType == ObjectTypes.ValueTypeObjectReference)
                    {
                        VisitValueTypeReference(*(StackObject **)&i->Value, leakVObj);
                    }
                }
                if (isLocal)
                {
                    sb.Append(string.Format("|Loc:{0}", localIdx));
                    if (localIdx == 0)
                    {
                        sb.Append(" Method:");
                        sb.Append(localMethod.ToString());
                    }
                }
                if (isBase)
                {
                    sb.Append("|Base");
                    sb.Append(" Method:");
                    sb.Append(baseMethod.ToString());
                }

                final.AppendLine(sb.ToString());
            }

            for (var i = stack.ValueTypeStackBase; i > stack.ValueTypeStackPointer;)
            {
                var  vt   = domain.GetType(i->Value);
                var  cnt  = i->ValueLow;
                bool leak = leakVObj.Contains((long)i);
                final.AppendLine("----------------------------------------------");
                final.AppendLine(string.Format("{2}(0x{0:X8}){1}", (long)i, vt, leak ? "*" : ""));
                for (int j = 0; j < cnt; j++)
                {
                    StringBuilder sb  = new StringBuilder();
                    var           ptr = Minus(i, j + 1);
                    sb.Append(string.Format("(0x{0:X8}) Type:{1} ", (long)ptr, ptr->ObjectType));
                    GetStackObjectText(sb, ptr, mStack, valuePointerEnd);
                    final.AppendLine(sb.ToString());
                }
                i = Minus(i, i->ValueLow + 1);
            }
            final.AppendLine("Managed Objects:");
            for (int i = 0; i < mStack.Count; i++)
            {
                final.AppendLine(string.Format("({0}){1}", i, mStack[i]));
            }
#if !UNITY_5 && !UNITY_2017 && !UNITY_4
            System.Diagnostics.Debug.Print(final.ToString());
#else
            UnityEngine.Debug.LogWarning(final.ToString());
#endif
        }
コード例 #9
0
        public static void Configure(RuntimeStack stack)
        {
            //known types
            stack.StackFrame.Scope.SetSymbolValue("Form", typeof(Form));
            stack.StackFrame.Scope.SetSymbolValue("Button", typeof(Button));
            stack.StackFrame.Scope.SetSymbolValue("int", typeof(int));
            stack.StackFrame.Scope.SetSymbolValue("big", typeof(BigInteger));
            stack.StackFrame.Scope.SetSymbolValue("long", typeof(long));
            stack.StackFrame.Scope.SetSymbolValue("bool", typeof(bool));
            stack.StackFrame.Scope.SetSymbolValue("string", typeof(string));
            stack.StackFrame.Scope.SetSymbolValue("char", typeof(char));
            stack.StackFrame.Scope.SetSymbolValue("double", typeof(double));
            stack.StackFrame.Scope.SetSymbolValue("float", typeof(float));
            stack.StackFrame.Scope.SetSymbolValue("date-time", typeof(DateTime));
            stack.StackFrame.Scope.SetSymbolValue("Console", typeof(Console));

            //atoms
            stack.StackFrame.Scope.SetSymbolValue("null", null);
            stack.StackFrame.Scope.SetSymbolValue("true", true);
            stack.StackFrame.Scope.SetSymbolValue("false", false);

            //functions
            stack.StackFrame.Scope.SetSymbolValue("progn", Functions.Progn);
            stack.StackFrame.Scope.SetSymbolValue("concat", Functions.Concat);
            stack.StackFrame.Scope.SetSymbolValue("format", Functions.Format);

            stack.StackFrame.Scope.SetSymbolValue("+", Functions.Add);
            stack.StackFrame.Scope.SetSymbolValue("-", Functions.Sub);
            stack.StackFrame.Scope.SetSymbolValue("*", Functions.Mul);
            stack.StackFrame.Scope.SetSymbolValue("/", Functions.Div);
            stack.StackFrame.Scope.SetSymbolValue("||", Functions.LogicalOr);
            stack.StackFrame.Scope.SetSymbolValue("==", Functions.Equal);
            stack.StackFrame.Scope.SetSymbolValue("!=", Functions.NotEqual);
            stack.StackFrame.Scope.SetSymbolValue("is", Functions.Is);
            stack.StackFrame.Scope.SetSymbolValue(">", Functions.GreaterThan);
            stack.StackFrame.Scope.SetSymbolValue("<", Functions.LessThan);
            stack.StackFrame.Scope.SetSymbolValue(">=", Functions.GreaterOrEqualTo);
            stack.StackFrame.Scope.SetSymbolValue("<=", Functions.LessOrEqualTo);

            stack.StackFrame.Scope.SetSymbolValue("break-debugger", Functions.BreakDebugger);
            stack.StackFrame.Scope.SetSymbolValue("if", Functions.If);
            stack.StackFrame.Scope.SetSymbolValue("#", Functions.EvalFormula);
            stack.StackFrame.Scope.SetSymbolValue("arr", Functions.Arr);
            stack.StackFrame.Scope.SetSymbolValue("list", Functions.List);
            stack.StackFrame.Scope.SetSymbolValue("hash", Functions.Hash);
            stack.StackFrame.Scope.SetSymbolValue("while", Functions.While);
            stack.StackFrame.Scope.SetSymbolValue("foreach", Functions.ForEach);
            stack.StackFrame.Scope.SetSymbolValue("defun", Functions.Defun);
            stack.StackFrame.Scope.SetSymbolValue("defmacro", Functions.Defmacro);
            stack.StackFrame.Scope.SetSymbolValue("lambda", Functions.Lambda);
            stack.StackFrame.Scope.SetSymbolValue("force", Functions.EnsureNotLazy);
            stack.StackFrame.Scope.SetSymbolValue("delay", Functions.Delay);
            stack.StackFrame.Scope.SetSymbolValue("tail", Functions.Tail);
            stack.StackFrame.Scope.SetSymbolValue("setf", Functions.Setf);
            stack.StackFrame.Scope.SetSymbolValue("let", Functions.Let);
            stack.StackFrame.Scope.SetSymbolValue("new-thread", Functions.NewThread);
            stack.StackFrame.Scope.SetSymbolValue("eval-string", Functions.EvalString);
            stack.StackFrame.Scope.SetSymbolValue("eval", Functions.EvalList);
            stack.StackFrame.Scope.SetSymbolValue("include", Functions.Include);
            stack.StackFrame.Scope.SetSymbolValue("reverse", Functions.Reverse);
            stack.StackFrame.Scope.SetSymbolValue("car", Functions.Car);
            stack.StackFrame.Scope.SetSymbolValue("cdr", Functions.Cdr);
            stack.StackFrame.Scope.SetSymbolValue("empty?", Functions.IsEmpty);
            stack.StackFrame.Scope.SetSymbolValue("cast", Functions.Cast);


            stack.StackFrame.Scope.SetSymbolValue("print", call =>
            {
                call.EvalArgs().ToList().ForEach(
                    arg =>
                    stack.Engine.OnPrint(arg == null
                                                                                            ? ""
                                                                                            : arg.ToString()));
                return(null);
            });

            stack.StackFrame.Scope.SetSymbolValue("quote", call =>
            {
                var info              = new CloneInfo();
                info.StackFrame       = call.StackFrame;
                info.LocalIdentifiers = new List <string>();
                info.BackQuote        = true;

                object expression = call.Args[1];
                object clone      = Utils.Clone(info, expression);
                return(clone);
            });

            stack.StackFrame.Scope.SetSymbolValue("operator-prio", call =>
            {
                var func =
                    Utils.Eval(call.StackFrame, call.Args[1]) as
                    LispFunc;
                var prio =
                    (int)
                    Utils.Eval(call.StackFrame, call.Args[2]);

                if (prio == 0)
                {
                    //remove
                    stack.OperatorPriority.Remove(func);
                }
                else
                {
                    stack.OperatorPriority[func] = prio;
                }

                return(null);
            });


            stack.StackFrame.Scope.SetSymbolValue("input", call =>
            {
                var var    = call.Args[1] as SymbolNode;
                string res = Console.ReadLine();
                var.GetSymbol(call.StackFrame).Value = res;
                return(null);
            });

            stack.StackFrame.Scope.SetSymbolValue("new", call =>
            {
                var typeId = call.Args[1] as SymbolNode;
                var type   = typeId.GetSymbol(call.StackFrame).Value as Type;
                object res = Activator.CreateInstance(type);

                return(res);
            });

            stack.StackFrame.Scope.SetSymbolValue("call-by-name", call =>
            {
                string methodName =
                    Utils.Eval(call.StackFrame, call.Args[2]).
                    ToString();
                return(stack.HandleCall(call, methodName));
            });

            stack.StackFrame.Scope.SetSymbolValue("application-run", call =>
            {
                Application.Run();
                return(null);
            });
        }
コード例 #10
0
 private void Push(int r)
 {
     RuntimeStack.Push(r);
 }
コード例 #11
0
 private int Pop()
 {
     return(RuntimeStack.Pop());
 }
コード例 #12
0
ファイル: Runnable.cs プロジェクト: ricardoborges/NPortugol
        private void Setup(InstrucStream instStream, FunctionTable fTable, SymbolTable symbolTable)
        {
            stream = instStream;
            functionTable = fTable;

            if (functionTable.ContainsKey(Function.MainName))
            {
                mainFunction = functionTable[Function.MainName];
            }

            ScriptSymbolTable = SymbolTable.CreateFor(symbolTable);

            RuntimeStack = new RuntimeStack();

            ParamStack = new ParamStack();
        }
コード例 #13
0
 public RuntimeInterpreter(ILEnvironment environment)
 {
     this.environment = environment;
     stack            = new RuntimeStack();
     clrArguments.Add(0, null);
 }