Ejemplo n.º 1
0
 public OpcodeStream(ParseStream strm, ExecutionStack stack)
 {
     _jumpTable = new JumpTable();
     opCodes = new ArrayList();
     currentIP = new Stack();
     Init(strm, stack);
 }
Ejemplo n.º 2
0
        public void Initialize(ParseStream strm)
        {
            if(strm == null)
            {
                throw new ArgumentNullException("strm", "Can not initialize from a null stream");
            }

            globalLexicalScope = new LexicalScope(null);
            globalLexicalScope.Add(new VariableItem("true", true));
            globalLexicalScope.Add(new VariableItem("false", false));

            executionStacks.Push(new ExecutionStack(globalLexicalScope));

            opcodeStream = new OpcodeStream(strm, LocalStack);
        }
Ejemplo n.º 3
0
Archivo: Push.cs Proyecto: bubbafat/HVM
        public override int ReadArguments(ParseStream strm, LexicalScope scope)
        {
            int read = 0;

            Argument arg = new Argument();

            if(strm.NextTokenCouldBeVariable())
            {
                string vresult = null;
                if(strm.ReadVariableName(ref vresult))
                {
                    arg.Value.StringValue = vresult;
                    arg.Value.Type = HVMType.Variable;
                    read++;
                }
            }
            else
            {
                if(strm.Eof)
                {
                    throw new ParseException("Argument to opcode: push", "Unexpected end of file");
                }

                int val = int.MinValue;
                if(strm.ReadNumeric(ref val))
                {
                    arg.Value.Type = HVMType.Integer;
                    arg.Value.IntegerValue = val;
                    read++;
                }
            }

            if(read == 0)
            {
                if(strm.Eof)
                {
                    throw new ParseException("Argument to opcode: push", "Unexpected end of file");
                }

                throw new ParseException("Argument to opcode: push", "Unable to read valid data from stream");
            }

            Arguments[0] = arg;

            return read;
        }
Ejemplo n.º 4
0
Archivo: Main.cs Proyecto: 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;
        }
Ejemplo n.º 5
0
 public override int ReadArguments(ParseStream strm, LexicalScope scope)
 {
     return this.ReadVariableNameAndIntegerOrVariableInt(strm, scope);
 }
Ejemplo n.º 6
0
        private void Init(ParseStream strm, ExecutionStack stack)
        {
            InstructionPointer = 0;
            Hashtable ocTypes = InitOpCodes();

            while(true)
            {
                string word = null;
                if(strm.ReadWord(ref word))
                {
                    if(ocTypes.ContainsKey(word))
                    {
                        OpCode oc = OpCode.Create(ocTypes[word] as Type);
                        oc.Line = strm.CurrentLine;
                        if(oc != null)
                        {
                            bool addToOpcodes = true;

                            oc.ReadArguments(strm, stack.Scope);

                            if(oc.GetType().IsSubclassOf(typeof(LabelOpCode)))
                            {
                                string name = (oc as LabelOpCode).GetName();
                                JumpTable.AddIndex(name, opCodes.Count);
                                addToOpcodes = false;
                            }

                            if(addToOpcodes)
                            {
                                opCodes.Add(oc);
                            }
                        }
                        else
                        {
                            throw new ParseException( string.Format("Activate Opcode: {0}", word),
                                string.Format("Unable to activate Opcode: {0}", word));
                        }
                    }
                    else
                    {
                        throw new ParseException("Valid token", string.Format("Unexpected token found: {0}", word) );
                    }
                }
                else
                {
                    break;
                }
            }
        }
Ejemplo n.º 7
0
        public virtual int ReadArguments(ParseStream strm, LexicalScope scope)
        {
            int read = 0;

            for(int i = 0; i < ArgumentCount; i++)
            {
                HVMType type = ArgumentTypes[i];
                Argument arg = new Argument();
                arg.Value.Type = type;
                bool valParse = false;

                switch(type)
                {
                    case HVMType.Boolean:
                        bool bresult = false;
                        if(strm.ReadBoolean(ref bresult))
                        {
                            arg.Value.BooleanValue = bresult;
                            valParse = true;
                        }
                        break;
                    case HVMType.Integer:
                        int iresult = int.MinValue;
                        if(strm.ReadNumeric(ref iresult))
                        {
                            arg.Value.IntegerValue = iresult;
                            valParse = true;
                        }
                        break;
                    case HVMType.String:
                        string sresult = null;
                        if(strm.ReadWord(ref sresult))
                        {
                            arg.Value.StringValue = sresult;
                            valParse = true;
                        }
                        break;
                    case HVMType.VariableName:
                        string vnresult = null;
                        valParse = strm.ReadVariableName(ref vnresult);
                        arg.Value.StringValue = vnresult;
                        if(!valParse)
                        {
                            throw new ParseException("Variable name", vnresult);
                        }
                        break;
                    case HVMType.QuotedString:
                        string qsresult = null;
                        if(strm.ReadQuotedString(ref qsresult))
                        {
                            arg.Value.StringValue = qsresult;
                            valParse = true;
                        }
                        break;
                    case HVMType.Variable:
                        string vresult = null;
                        valParse = strm.ReadVariableName(ref vresult);
                        arg.Value.StringValue = vresult;
                        if(!valParse)
                        {
                            throw new ParseException("word begining with @ (variable name)", vresult);
                        }
                        break;
                }

                if(!valParse)
                {
                    if(strm.Eof)
                    {
                        throw new ParseException( string.Format("{0} data", type.ToString()),
                            "unexpected end of file");
                    }

                    throw new OpCodeArgumentException(i, type, this);
                }

                Arguments[i] = arg;
                read++;
            }

            return read;
        }
Ejemplo n.º 8
0
        protected int ReadVariableNameAndIntegerOrVariableInt(ParseStream strm, LexicalScope scope)
        {
            int read = 0;

            Argument argName = new Argument();
            Argument argIndex = new Argument();

            if(strm.NextTokenCouldBeVariable())
            {
                string vresult = null;
                if(strm.ReadVariableName(ref vresult))
                {
                    argName.Value.StringValue = vresult;
                    argName.Value.Type = HVMType.Variable;
                    read++;
                }
            }
            else
            {
                throw new OpCodeArgumentException(0, HVMType.Variable, this);
            }

            if(strm.NextTokenCouldBeVariable())
            {
                string vresult = null;
                if(strm.ReadVariableName(ref vresult))
                {
                    argIndex.Value.StringValue = vresult;
                    argIndex.Value.Type = HVMType.Variable;
                    read++;
                }
            }
            else
            {
                if(strm.Eof)
                {
                    throw new ParseException("Argument to opcode: stidx", "Unexpected end of file");
                }
                int val = int.MinValue;
                if(strm.ReadNumeric(ref val))
                {
                    argIndex.Value.Type = HVMType.Integer;
                    argIndex.Value.IntegerValue = val;
                    read++;
                }
            }

            if(read != 2)
            {
                if(strm.Eof)
                {
                    throw new ParseException("Argument to opcode: stidx", "Unexpected end of file");
                }

                throw new ParseException("Argument to opcode: stidx", "Unable to read valid data from stream");
            }

            Arguments[0] = argName;
            Arguments[1] = argIndex;

            return read;
        }