Esempio n. 1
0
 internal override void Execute(ref int ip, ValueStack valueStack, EnvironmentStack envStack, CallStack callStack)
 {
     object tos = valueStack.Pop();
     object under_tos = valueStack.Pop();
     valueStack.Push(operation(tos, under_tos));
     ip += Length;
 }
Esempio n. 2
0
File: Program.cs Progetto: 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();
        }
Esempio n. 3
0
 internal override void Execute(ref int ip, ValueStack valueStack, EnvironmentStack envStack, CallStack callStack)
 {
     if (callStack.GetLastNumberOfParameters() != NumberOfParameters)
         throw new exceptions.RuntimeException(Symbol + ": Invalid parameter count");
     envStack.PushNew();
     ip += Length;
 }
Esempio n. 4
0
 internal override void Execute(ref int ip, ValueStack valueStack, EnvironmentStack envStack, CallStack callStack)
 {
     object o = valueStack.Pop();
     if (o is Variable) envStack.Set(Symbol, (Variable)o); // Link to variable, e.g. in Closure
     else
     {
         if (envStack.HasVariable(Symbol))
             envStack.Get(Symbol).SetValue(o);
         else
             envStack.Set(Symbol, new Variable(o));
     }
     ip += Length;
 }
Esempio n. 5
0
        internal override void Execute(ref int ip, ValueStack valueStack, EnvironmentStack envStack, CallStack callStack)
        {
            int provided = callStack.GetLastNumberOfParameters();
            if (provided < NumberOfParameters - 1) // -1 as there may be an empty list as rest parameter
                throw new exceptions.RuntimeException(Symbol + ": Invalid parameter count");

            object restParameter = Nil.GetInstance();
            for (int i = 0; i < NumberOfParametersToSkip; ++i) skipStack.Push(valueStack.Pop());
            int numberOfValuesForRestParameter = 1 + provided - NumberOfParameters;
            for (int i = 0; i < numberOfValuesForRestParameter; ++i) restParameter = new Pair(valueStack.Pop(), restParameter);
            valueStack.Push(restParameter);
            for (int i = 0; i < NumberOfParametersToSkip; ++i) valueStack.Push(skipStack.Pop());

            envStack.PushNew();
            ip += Length;
        }
Esempio n. 6
0
 internal override void Execute(ref int ip, ValueStack valueStack, EnvironmentStack envStack, CallStack callStack)
 {
     valueStack.Push(new IP(Number));
     ip += Length;
 }
Esempio n. 7
0
 internal override void Execute(ref int ip, ValueStack valueStack, EnvironmentStack envStack, CallStack callStack)
 {
     valueStack.Push(((Pair)valueStack.Pop()).Second);
     ip += Length;
 }
Esempio n. 8
0
 internal override void Execute(ref int ip, ValueStack valueStack, EnvironmentStack envStack, CallStack callStack)
 {
     valueStack.Push((char)(int)valueStack.Pop());
     ip += Length;
 }
Esempio n. 9
0
 internal override void Execute(ref int ip, ValueStack valueStack, EnvironmentStack envStack, CallStack callStack)
 {
     valueStack.Push(valueStack.Pop() is Symbol);
     ip += Length;
 }
Esempio n. 10
0
 internal override void Execute(ref int ip, ValueStack valueStack, EnvironmentStack envStack, CallStack callStack)
 {
     char c = (char)valueStack.Pop();
     int index = (int)valueStack.Pop();
     StringObject str = (StringObject)valueStack.Pop();
     str.SetCharAt(index, c);
     valueStack.Push(c);
     ip += Length;
 }
Esempio n. 11
0
 internal abstract void Execute(ref int ip, ValueStack valueStack, EnvironmentStack envStack, CallStack callStack);
Esempio n. 12
0
 internal override void Execute(ref int ip, ValueStack valueStack, EnvironmentStack envStack, CallStack callStack)
 {
     envStack.Pop();
     ip = callStack.GetLastIp();
     callStack.Pop();
 }
Esempio n. 13
0
 internal override void Execute(ref int ip, ValueStack valueStack, EnvironmentStack envStack, CallStack callStack)
 {
     object o = valueStack.Pop();
     envStack.Get(Symbol).SetValue(o);
     ip += Length;
 }
Esempio n. 14
0
 internal override void Execute(ref int ip, ValueStack valueStack, EnvironmentStack envStack, CallStack callStack)
 {
     object o = valueStack.Pop();
     valueStack.Push(random.Next(Convert.ToInt32(o)));
     ip += Length;
 }
Esempio n. 15
0
 internal override void Execute(ref int ip, ValueStack valueStack, EnvironmentStack envStack, CallStack callStack)
 {
     valueStack.Push(envStack.Get(Symbol).GetValue());
     ip += Length;
 }
Esempio n. 16
0
 internal override void Execute(ref int ip, ValueStack valueStack, EnvironmentStack envStack, CallStack callStack)
 {
     valueStack.Push(Nil.GetInstance());
     ip += Length;
 }
Esempio n. 17
0
 internal override void Execute(ref int ip, ValueStack valueStack, EnvironmentStack envStack, CallStack callStack)
 {
     throw new exceptions.RuntimeException("Error in program: Jump into the middle of a statement");
 }
Esempio n. 18
0
 internal override void Execute(ref int ip, ValueStack valueStack, EnvironmentStack envStack, CallStack callStack)
 {
     valueStack.Push(Symbol.fromString(((StringObject)valueStack.Pop()).Value));
     ip += Length;
 }
Esempio n. 19
0
 internal override void Execute(ref int ip, ValueStack valueStack, EnvironmentStack envStack, CallStack callStack)
 {
     object o = valueStack.Pop();
     if (o is bool && (bool)o == false) ip = Target;
     else ip += Length;
 }
Esempio n. 20
0
 internal override void Execute(ref int ip, ValueStack valueStack, EnvironmentStack envStack, CallStack callStack)
 {
     ip = Target;
 }
Esempio n. 21
0
 internal override void Execute(ref int ip, ValueStack valueStack, EnvironmentStack envStack, CallStack callStack)
 {
     object o = valueStack.Pop();
     valueStack.Push(o is int || o is double);
     ip += Length;
 }
Esempio n. 22
0
 internal override void Execute(ref int ip, ValueStack valueStack, EnvironmentStack envStack, CallStack callStack)
 {
     valueStack.Push(new StringObject((int)valueStack.Pop()));
     ip += Length;
 }
Esempio n. 23
0
 internal override void Execute(ref int ip, ValueStack valueStack, EnvironmentStack envStack, CallStack callStack)
 {
     int end = (int)valueStack.Pop();
     int start = (int)valueStack.Pop();
     StringObject str = (StringObject)valueStack.Pop();
     valueStack.Push(new StringObject(str.Value.Substring(start, end - start)));
     ip += Length;
 }
Esempio n. 24
0
 internal override void Execute(ref int ip, ValueStack valueStack, EnvironmentStack envStack, CallStack callStack)
 {
     object o = valueStack.TopOfStack();
     if (o is bool) printer.Print(((bool)o) ? "#t" : "#f");
     else printer.Print(string.Format(CultureInfo.InvariantCulture, "{0}", o));
     ip += Length;
 }
Esempio n. 25
0
        internal override void Execute(ref int ip, ValueStack valueStack, EnvironmentStack envStack, CallStack callStack)
        {
            List<Variable> values = new List<Variable>();

            for (int i = 0; i < NumberOfPushedArguments; ++i)
            {
                Symbol symbol = (Symbol)valueStack.Pop();
                values.Add(envStack.Get(symbol));
            }
            values.Reverse();
            valueStack.Push(new Closure((IP)valueStack.Pop(), values));
            ip += Length;
        }
Esempio n. 26
0
        internal override void Execute(ref int ip, ValueStack valueStack, EnvironmentStack envStack, CallStack callStack)
        {
            envStack.Pop();
            int oldIp = callStack.GetLastIp();
            callStack.Pop();

            object target = valueStack.GetFromTop(NumberOfPushedArguments);
            if (target is IP)
            {
                callStack.Push(oldIp, NumberOfPushedArguments);
                ip = ((IP)target).Value;
                return;
            }
            if (target is Closure)
            {
                Closure c = (Closure)target;
                foreach (var value in c.ClosedOverValues) valueStack.Push(value);
                callStack.Push(oldIp, NumberOfPushedArguments + c.ClosedOverValues.Count);
                ip = c.Target;
                return;
            }
            throw new exceptions.RuntimeException("Invalid CALL target");
        }
Esempio n. 27
0
 internal override void Execute(ref int ip, ValueStack valueStack, EnvironmentStack envStack, CallStack callStack)
 {
     envStack.Set(Symbol, new Variable());
     ip += Length;
 }
Esempio n. 28
0
 internal override void Execute(ref int ip, ValueStack valueStack, EnvironmentStack envStack, CallStack callStack)
 {
     throw new exceptions.Error(valueStack.Pop());
 }