示例#1
0
文件: Program.cs 项目: hww/lbvm
        public object Run(params object[] parameters)
        {
            var envStack = new EnvironmentStack();
            envStack.PushNew(); // Global env
            envStack.Set(Symbol.fromString("sys:args"), new Variable(CliToVm(parameters)));

            int ip = 0;
            var valueStack = new ValueStack();
            var callStack = new CallStack();

            for (var current = Statements[ip]; !(current is EndStatement); current = Statements[ip])
            {
                //System.Diagnostics.Debug.Print("0x" + ip.ToString("x4") + ": " + current);
                current.Execute(ref ip, valueStack, envStack, callStack);
            }

            if (envStack.Count() == 0) throw new exceptions.RuntimeException("Bad program: Global environment deleted!");
            if (envStack.Count() > 1) throw new exceptions.RuntimeException("Bad program: Environment stack not cleaned up");
            if (callStack.Count() > 1) throw new exceptions.RuntimeException("Bad program: Call stack not cleaned up");
            if (valueStack.Count() == 0) throw new exceptions.RuntimeException("Bad program: Value stack empty after running");
            if (valueStack.Count() > 1) throw new exceptions.RuntimeException("Bad program: Value stack not cleaned up");
            return valueStack.Pop();
        }