Esempio n. 1
0
File: Call.cs Progetto: bubbafat/HVM
        public override void Execute(ExecutionEnvironment environment)
        {
            DemandArgs(2);

            int offSet = environment.OpCodes.JumpTable.GetIndex(Arguments[0].Value.StringValue);
            int argCount = Arguments[1].Value.IntegerValue;

            LexicalScope ls = new LexicalScope(environment.GlobalScope);
            ExecutionStack s = new ExecutionStack(ls);

            if(argCount > 0)
            {
                ExecutionStackItem [] tmpItems = new ExecutionStackItem[argCount];
                for(int i = 0; i < argCount; i++)
                {
                    tmpItems[i] = environment.LocalStack.PopItem();
                }

                for(int i = argCount - 1; i >= 0; i--)
                {
                    s.PushItem(tmpItems[i]);
                }
            }

            environment.PushStack(s);
            environment.OpCodes.PushCurrentIPAndSet(offSet);
        }
Esempio n. 2
0
        public override void Execute(ExecutionEnvironment environment)
        {
            DemandArgs(2);

            string name = Arguments[0].Value.StringValue;

            int length = -1;
            if(Arguments[1].Value.Type == HVMType.Variable)
            {
                Variable v = environment.LocalScope.ResolveAny(Arguments[1].Value.StringValue);
                if(v == null)
                {
                    throw new OpCodeArgumentException(1, HVMType.Variable, this);
                }

                VariableItem vi = v as VariableItem;
                if(vi == null)
                {
                    throw new OpCodeArgumentException(1, HVMType.Variable, this);
                }

                length = vi.Value.IntegerValue;
            }
            else
            {
                length = Arguments[1].Value.IntegerValue;
            }

            VariableArray va = new VariableArray(name, length);
            environment.LocalScope.Add(va);
        }
Esempio n. 3
0
        public override void Execute(ExecutionEnvironment environment)
        {
            DemandArgs(1);

            int size = Arguments[0].Value.IntegerValue;
            environment.LocalStack.MaxSize = size;
        }
Esempio n. 4
0
        public override void Execute(ExecutionEnvironment environment)
        {
            environment.LocalStack.DemandSize(1, this);

            ExecutionStackItem item = environment.LocalStack.PopItem();
            VariableItem var = environment.LocalScope.ResolveStackItem(item);
            Console.Write("{0}", var.Value.StringValue);
        }
Esempio n. 5
0
        public override void Execute(ExecutionEnvironment environment)
        {
            DemandArgs(2);
            environment.LocalStack.DemandSize(1, this);

            string name = Arguments[0].Value.StringValue;

            int index = -1;
            if(Arguments[1].Value.Type == HVMType.Variable)
            {
                Variable v = environment.LocalScope.ResolveAny(Arguments[1].Value.StringValue);
                if(v == null)
                {
                    throw new OpCodeArgumentException(1, HVMType.Variable, this);
                }

                VariableItem vi = v as VariableItem;
                if(vi == null)
                {
                    throw new OpCodeArgumentException(1, HVMType.Variable, this);
                }

                index = vi.Value.IntegerValue;
            }
            else
            {
                index = Arguments[1].Value.IntegerValue;
            }

            Variable vName = environment.LocalScope.ResolveAny(name);

            if(vName == null)
            {
                throw new OpCodeException(string.Format("Variable not found: {0}", name), this);
            }

            if(vName.Type != HVMType.Array)
            {
                throw new OpCodeException( string.Format("Attempt to index into non-array element: {0}", name), this);
            }

            VariableArray va = vName as VariableArray;

            if(va == null)
            {
                throw new OpCodeException( string.Format("Variable was not convertable to VariableArray: {0}", name), this);
            }

            ExecutionStackItem esi = environment.LocalStack.PopItem();
            VariableItem vTemp = environment.LocalScope.ResolveStackItem(esi);

            va.SetAtIndex(index, vTemp);

            environment.LocalScope.Update(va);
        }
Esempio n. 6
0
File: Dbg.cs Progetto: bubbafat/HVM
 public override void Execute(ExecutionEnvironment environment)
 {
     if(System.Diagnostics.Debugger.IsAttached)
     {
         System.Diagnostics.Debugger.Break();
     }
     else
     {
         System.Diagnostics.Debug.Assert(false, "dbg opcode hit.");
     }
 }
Esempio n. 7
0
File: Not.cs Progetto: bubbafat/HVM
        public override void Execute(ExecutionEnvironment environment)
        {
            environment.LocalStack.DemandSize(1, this);

            ExecutionStackItem item_rhs = environment.LocalStack.PopItem();
            VariableItem var_rhs = environment.LocalStack.Scope.ResolveStackItem(item_rhs);

            int result = ~var_rhs.Value.IntegerValue;

            Variant vResult = new Variant(result);

            environment.LocalStack.PushItem(new ExecutionStackItem(new Argument(vResult)));
        }
Esempio n. 8
0
        public override void Execute(ExecutionEnvironment environment)
        {
            ExecutionStackItem esi = environment.LocalStack.PopItem();
            if(esi.Value.Value.Type != HVMType.Boolean)
            {
                throw new RuntimeException("Conditional Branch brtrue requires a boolean type on the stack");
            }

            if(esi.Value.Value.BooleanValue == false)
            {
                int offSet = environment.OpCodes.JumpTable.GetIndex(Arguments[0].Value.StringValue);
                environment.OpCodes.BranchTo(offSet);
            }
        }
Esempio n. 9
0
File: Cgt.cs Progetto: bubbafat/HVM
        public override void Execute(ExecutionEnvironment environment)
        {
            environment.LocalStack.DemandSize(2, this);

            ExecutionStackItem item_rhs = environment.LocalStack.PopItem();
            VariableItem var_rhs = environment.LocalStack.Scope.ResolveStackItem(item_rhs);

            ExecutionStackItem item_lhs = environment.LocalStack.PopItem();
            VariableItem var_lhs = environment.LocalStack.Scope.ResolveStackItem(item_lhs);

            bool result = (var_lhs.Value.CompareTo(var_rhs.Value) > 0);

            VariableItem v = new VariableItem(null, result);

            environment.LocalStack.PushItem(new ExecutionStackItem(v));
        }
Esempio n. 10
0
File: PopN.cs Progetto: bubbafat/HVM
        public override void Execute(ExecutionEnvironment environment)
        {
            DemandArgs(1);

            int arg = Arguments[0].Value.IntegerValue;

            if(arg <= 0)
            {
                return;
            }

            environment.LocalStack.DemandSize(arg, this);

            for(int i = 0; i < arg; i++)
            {
                environment.LocalStack.PopItem();
            }
        }
Esempio n. 11
0
File: Main.cs Progetto: bubbafat/HVM
        public static void Main(string [] args)
        {
            DateTime start = DateTime.Now;

            if(args.Length == 0)
            {
                Console.WriteLine("missing argument: file name");
                return;
            }

            if(!File.Exists(args[0]))
            {
                Console.WriteLine("File not found: {0}", args[0]);
                return;
            }

            try
            {
                ExecutionEnvironment ee;
                using(StreamReader sr = new StreamReader(args[0]))
                {
                    ParseStream ps = new ParseStream(sr.BaseStream);
                    ee = new ExecutionEnvironment();
                    ee.Initialize(ps);
                    sr.Close();
                }

                while(!ee.OpCodes.EndOfStream)
                {
                    OpCode oc = ee.OpCodes.GetNext();
                    oc.Execute(ee);
                }
            }
            catch(HVMException he)
            {
                Console.WriteLine(he.Message);
            }

            TimeSpan ts = DateTime.Now - start;

            Console.WriteLine("Runtime: {0}ms", ts.TotalMilliseconds);

            return;
        }
Esempio n. 12
0
        public override void Execute(ExecutionEnvironment environment)
        {
            DemandArgs(1);

            Variable v = environment.LocalScope.ResolveAny(Arguments[0].Value.StringValue);

            if(v == null || v.Type != HVMType.Array)
            {
                throw new OpCodeArgumentException(0, HVMType.Variable, this);
            }

            VariableArray va = v as VariableArray;

            if(va == null)
            {
                throw new OpCodeArgumentException(0, HVMType.Variable, this);
            }

            VariableItem result = new VariableItem(null, va.Length);
            environment.LocalStack.PushItem(new ExecutionStackItem(result));
        }
Esempio n. 13
0
File: Inc.cs Progetto: bubbafat/HVM
        public override void Execute(ExecutionEnvironment environment)
        {
            environment.LocalStack.DemandSize(1, this);

            ExecutionStackItem esi = environment.LocalStack.PopItem();
            if(esi.Value.Value.Type == HVMType.Variable)
            {
                VariableItem var = environment.LocalScope.ResolveStackItem(esi);
                if(var != null)
                {
                    try
                    {
                        var.Value.IntegerValue++;
                    }
                    catch(Exception)
                    {
                        throw new RuntimeException("Opcode can only be used on integer types (or types convertable to int): inc");
                    }
                    environment.LocalScope.Update(var);
                    environment.LocalStack.PushItem(esi);
                }
                else
                {
                    throw new RuntimeException( string.Format("Unable to locate variable named: {0}", esi.Value.Name) );
                }
            }
            else
            {
                try
                {
                    esi.Value.Value.IntegerValue++;
                }
                catch(Exception)
                {
                    throw new RuntimeException("Opcode can only be used on integer types (or types convertable to int): inc");
                }

                environment.LocalStack.PushItem(esi);
            }
        }
Esempio n. 14
0
File: Pop.cs Progetto: bubbafat/HVM
        public override void Execute(ExecutionEnvironment environment)
        {
            DemandArgs(1);
            environment.LocalStack.DemandSize(1, this);

            ExecutionStackItem item = environment.LocalStack.PopItem();

            Variable v = environment.LocalScope.ResolveAny(Arguments[0].Value.StringValue);
            VariableItem vi= null;
            if(v.Type != HVMType.Array)
            {
                vi = v as VariableItem;
            }

            if(v == null)
            {
                throw new OpCodeArgumentException(0, HVMType.String, this);
            }

            vi.Value = item.Value.Value.Clone() as Variant;
            environment.LocalScope.Update(vi);
        }
Esempio n. 15
0
File: Dup.cs Progetto: bubbafat/HVM
        public override void Execute(ExecutionEnvironment environment)
        {
            environment.LocalStack.DemandSize(1, this);

            ExecutionStackItem esi = environment.LocalStack.Peek() as ExecutionStackItem;
            if(esi.Value.Value.Type == HVMType.Variable)
            {
                VariableItem v = environment.LocalScope.ResolveStackItem(esi);
                if(v == null)
                {
                    throw new RuntimeException("Unable to resolve stack item as variable");
                }

                VariableItem v2 = v.Clone() as VariableItem;

                environment.LocalStack.PushItem(new ExecutionStackItem(v2));
            }
            else
            {
                ExecutionStackItem esi2 = new ExecutionStackItem(esi.Value.Clone() as VariableItem);
                environment.LocalStack.PushItem(esi2);
            }
        }
Esempio n. 16
0
File: Lbl.cs Progetto: bubbafat/HVM
 public override void Execute(ExecutionEnvironment environment)
 {
 }
Esempio n. 17
0
File: Push.cs Progetto: bubbafat/HVM
 public override void Execute(ExecutionEnvironment environment)
 {
     DemandArgs(1);
     environment.LocalStack.PushItem(new ExecutionStackItem(Arguments[0]));
 }
Esempio n. 18
0
 public override void Execute(ExecutionEnvironment environment)
 {
     DemandArgs(1);
     VariableItem v = new VariableItem(null, Arguments[0].Value.StringValue);
     environment.LocalStack.PushItem(new ExecutionStackItem(v));
 }
Esempio n. 19
0
File: Exit.cs Progetto: bubbafat/HVM
 public override void Execute(ExecutionEnvironment environment)
 {
     environment.OpCodes.BranchTo(environment.OpCodes.Length);
 }
Esempio n. 20
0
 public override void Execute(ExecutionEnvironment environment)
 {
     environment.LocalScope.Add(GetVariable());
 }
Esempio n. 21
0
 public override void Execute(ExecutionEnvironment environment)
 {
     string val = Console.ReadLine();
     VariableItem v = new VariableItem(null, val);
     environment.LocalStack.PushItem(new ExecutionStackItem(v));
 }
Esempio n. 22
0
 public abstract void Execute(ExecutionEnvironment environment);