Exemplo n.º 1
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;
                }
            }
        }
Exemplo n.º 2
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;
        }