Example #1
0
        public bool FindInstr(string instrName, out InstrLookUp instr)
        {
            LinkedList <InstrLookUp> .Enumerator enumerator = instrTable.GetEnumerator();

            while (enumerator.MoveNext())
            {
                if (enumerator.Current.InstrName == instrName)
                {
                    instr = enumerator.Current;
                    return(true);
                }
            }
            instr = default(InstrLookUp);
            return(false);
        }
Example #2
0
        public void Parser()
        {
            while (true)
            {
                AttributeType currAttribute = Lexer.Instance.GetNextToken();
                if (currAttribute == AttributeType.TYPE_END_OF_FILE)
                {
                    break;
                }

                switch (currAttribute)
                {
                case AttributeType.TYPE_FUNC:
                    DealFunc();
                    break;

                case AttributeType.TYPE_PARAM:
                    DealParam();
                    break;

                case AttributeType.TYPE_VAR:
                    DealVar();
                    break;

                case AttributeType.TYPE_SETSTACKSIZE:
                    DealStackSize();
                    break;

                case AttributeType.TYPE_IDENTIFER:
                    DealRowLabel();
                    break;

                case AttributeType.TYPE_CLOSE_BRACE:
                    DealCloseBracket();
                    break;

                case AttributeType.TYPE_INSTR:
                    DealInstr();
                    break;

                default:
                    ExDebug.Instance.ExitOnCode("出现异常情况");
                    break;
                }
                //此刻完成了对一条指令的解析 应该还需要检验其后是否有其他杂乱字符串  Mov vxa, 20 dsdsds    这样的情况也应该报错
                //其实对于
                if (Lexer.Instance.GetLookAheadChar() != '')
                {
                    ExDebug.Instance.ExitOnCode("出现杂乱码");
                }

                //这一句代码仅仅是为了让指令只能在一行内
                if (!Lexer.Instance.SkipNextLine())
                {
                    break;
                }
            }

            instrStream = new Instr[SyntaxHelper.InstrStreamSize];
            Lexer.Instance.ResetLexer();
            SyntaxHelper.CurrInstrIndex = 0;

            while (true)
            {
                AttributeType currAttribute = Lexer.Instance.GetNextToken();

                switch (currAttribute)
                {
                case AttributeType.TYPE_FUNC:
                    SyntaxHelper.IsAliveFunc   = true;
                    SyntaxHelper.CurrFuncParam = 0;
                    FuncTable.Instance.FindFunc(Lexer.Instance.GetCurrLexeme(), out SyntaxHelper.CurrFuncIndex);
                    break;

                case AttributeType.TYPE_PARAM:
                    if (Lexer.Instance.GetNextToken() != AttributeType.TYPE_IDENTIFER)
                    {
                        ExDebug.Instance.ExitOnCode("Param 后面不是字符串");
                    }
                    //堆栈索引等于
                    int stackIndex = -(FuncTable.Instance.GetLocalSize(SyntaxHelper.CurrFuncIndex) + 3 + SyntaxHelper.CurrFuncParam);
                    SymbolTable.Instance.AddSymbol(Lexer.Instance.GetCurrLexeme(), SyntaxHelper.CurrFuncIndex, stac);

                    break;

                case AttributeType.TYPE_INSTR:
                    string      instrName   = Lexer.Instance.GetCurrLexeme();
                    InstrLookUp instrLookUp = default(InstrLookUp);
                    if (!InstrTable.Instance.FindInstr(instrName, out instrLookUp))
                    {
                        ExDebug.Instance.ExitOnCode("找不到对应指令");
                    }
                    instrStream[SyntaxHelper.CurrInstrIndex].OpList   = new Op[instrLookUp.OpCount];
                    instrStream[SyntaxHelper.CurrInstrIndex].InstCode = instrLookUp.InstCode;

                    for (int i = 0; i < instrLookUp.OpCount; i++)
                    {
                        AttributeType type      = Lexer.Instance.GetNextToken();
                        int           opFlag    = instrLookUp.OpFlagList[i];
                        string        opContent = Lexer.Instance.GetCurrLexeme();

                        if (i != instrLookUp.OpCount - 1)
                        {
                            if (Lexer.Instance.GetNextToken() != AttributeType.TYPE_COMMA)
                            {
                                ExDebug.Instance.ExitOnCode("操作数之间没有逗号分隔");
                            }
                        }

                        switch (type)
                        {
                        case AttributeType.TYPE_INT:
                            #region 立即数
                            if ((opFlag & (int)Op_Type_Flag.OP_FLAG_INT) == 0)
                            {
                                ExDebug.Instance.ExitOnCode("当前操作数类型不符合");
                            }
                            instrStream[SyntaxHelper.CurrInstrIndex].OpList[i].Type = Op_Type.OP_TYPE_INT;
                            int intValue = 0;
                            if (int.TryParse(opContent, out intValue))
                            {
                                ExDebug.Instance.ExitOnCode("解析数字出现错误");
                            }
                            instrStream[SyntaxHelper.CurrInstrIndex].OpList[i].index = intValue;
                            break;
                            #endregion

                        case AttributeType.TYPE_FLOAT:
                            #region 立即数
                            if ((opFlag & (int)Op_Type_Flag.OP_FLAG_FLOAT) == 0)
                            {
                                ExDebug.Instance.ExitOnCode("当前操作数类型不符合");
                            }
                            instrStream[SyntaxHelper.CurrInstrIndex].OpList[i].Type = Op_Type.OP_TYPE_FLOAT;
                            float floatValue = 0;
                            if (float.TryParse(opContent, out floatValue))
                            {
                                ExDebug.Instance.ExitOnCode("解析数字出现错误");
                            }
                            instrStream[SyntaxHelper.CurrInstrIndex].OpList[i].floatLiteral = floatValue;
                            break;
                            #endregion

                        case AttributeType.TYPE_DOUBLE_QUOTA:
                            #region 字符串类型
                            //这里会正常输出空字符串吗
                            if ((opFlag & (int)Op_Type_Flag.OP_FLAG_STRING) == 0)
                            {
                                ExDebug.Instance.ExitOnCode("当前操作数类型不符合");
                            }

                            AttributeType token = Lexer.Instance.GetNextToken();

                            if (token == AttributeType.TYPE_DOUBLE_QUOTA &&
                                Lexer.Instance.RetLexState() == LexState.EndString)
                            {
                                instrStream[SyntaxHelper.CurrInstrIndex].OpList[i].Type  = Op_Type.OP_TYPE_STRING;
                                instrStream[SyntaxHelper.CurrInstrIndex].OpList[i].index = stringTable.AddString("");
                            }
                            else if (token == AttributeType.TYPE_STRING)
                            {
                                instrStream[SyntaxHelper.CurrInstrIndex].OpList[i].Type  = Op_Type.OP_TYPE_STRING;
                                instrStream[SyntaxHelper.CurrInstrIndex].OpList[i].index =
                                    stringTable.AddString(Lexer.Instance.GetCurrLexeme());
                            }
                            else
                            {
                                ExDebug.Instance.ExitOnCode("双引号后面出现其他字符");
                            }
                            break;
                            #endregion

                        case AttributeType.TYPE_RETVAL:
                            #region 寄存器类型
                            if ((opFlag & (int)Op_Type_Flag.OP_FLAG_RETVAL) == 0)
                            {
                                ExDebug.Instance.ExitOnCode("当前操作数类型不符合");
                            }
                            instrStream[SyntaxHelper.CurrInstrIndex].OpList[i].Type  = Op_Type.OP_TYPE_RETVAL;
                            instrStream[SyntaxHelper.CurrInstrIndex].OpList[i].index = 0;
                            break;
                            #endregion

                        case AttributeType.TYPE_IDENTIFER:
                            //对于数组和变量是不能重名的
                            //保持简单 所以函数不支持复杂的寻址能力
                            #region 标识符
                            if ((opFlag & (int)Op_Type_Flag.OP_FLAG_MEM) == 0)
                            {
                                int baseIndex = 0;
                                if (!SymbolTable.Instance.FindSymbol(opContent, SyntaxHelper.CurrFuncIndex, out baseIndex))
                                {
                                    ExDebug.Instance.ExitOnCode("没有对应变量 ");
                                }

                                int size = SymbolTable.Instance.FindSymbolSize(opContent, SyntaxHelper.CurrFuncIndex);
                                //如果是数组,那么会将其覆盖
                                if (Lexer.Instance.GetLookAheadChar() == '[')
                                {
                                    if (Lexer.Instance.GetNextToken() != AttributeType.TYPE_OPEN_BRACKET)
                                    {
                                        ExDebug.Instance.ExitOnCode("数组没有中括号");
                                    }
                                    if (size < 1)
                                    {
                                        ExDebug.Instance.ExitOnCode("数组尺寸小于1");
                                    }
                                    AttributeType token = Lexer.Instance.GetNextToken();

                                    if (token == AttributeType.TYPE_INT)
                                    {
                                        int offset = int.Parse(Lexer.Instance.GetCurrLexeme());

                                        //绝对堆栈索引在原程序里面没有判断正负
                                        if (baseIndex > 0)
                                        {
                                            baseIndex += offset;
                                        }
                                        else
                                        {
                                            baseIndex -= offset;
                                        }

                                        instrStream[SyntaxHelper.CurrInstrIndex].OpList[i].Type  = Op_Type.OP_TYPE_ABS_STACKINDEX;
                                        instrStream[SyntaxHelper.CurrInstrIndex].OpList[i].index = baseIndex;
                                    }
                                    else if (token == AttributeType.TYPE_IDENTIFER)
                                    {
                                        string identName   = Lexer.Instance.GetCurrLexeme();
                                        int    offsetStack = 0;
                                        if (!SymbolTable.Instance.FindSymbol(identName, SyntaxHelper.CurrFuncIndex, out offsetStack))
                                        {
                                            ExDebug.Instance.ExitOnCode("没有定义的变量");
                                        }

                                        int identSize = SymbolTable.Instance.FindSymbolSize(identName, SyntaxHelper.CurrFuncIndex);
                                        if (identSize > 1 || identSize < 0)
                                        {
                                            ExDebug.Instance.ExitOnCode("数组的索引不是变量");
                                        }

                                        instrStream[SyntaxHelper.CurrInstrIndex].OpList[i].Type   = Op_Type.OP_TYPE_REL_STACKINDEX;
                                        instrStream[SyntaxHelper.CurrInstrIndex].OpList[i].index  = baseIndex;
                                        instrStream[SyntaxHelper.CurrInstrIndex].OpList[i].offset = offsetStack;
                                    }
                                    else
                                    {
                                        ExDebug.Instance.ExitOnCode("数组出现不合法的序号");
                                    }

                                    if (Lexer.Instance.GetNextToken() != AttributeType.TYPE_CLOSE_BRACKET)
                                    {
                                        ExDebug.Instance.ExitOnCode("数组没有闭合");
                                    }
                                }
                                else
                                {
                                    if (size > 1)
                                    {
                                        ExDebug.Instance.ExitOnCode("变量的size不能大于1");
                                    }
                                    instrStream[SyntaxHelper.CurrInstrIndex].OpList[i].Type  = Op_Type.OP_TYPE_ABS_STACKINDEX;
                                    instrStream[SyntaxHelper.CurrInstrIndex].OpList[i].index = baseIndex;
                                }
                            }

                            if ((opFlag & (int)Op_Type_Flag.OP_FLAG_FUNC) == 0)
                            {
                                int index = 0;
                                if (!FuncTable.Instance.FindFunc(opContent, out index))
                                {
                                    ExDebug.Instance.ExitOnCode("没有定义对应函数");
                                }

                                instrStream[SyntaxHelper.CurrInstrIndex].OpList[i].Type  = Op_Type.OP_TYPE_FUNC_INDEX;
                                instrStream[SyntaxHelper.CurrInstrIndex].OpList[i].index = index;
                            }

                            if ((opFlag & (int)Op_Type_Flag.OP_FLAG_HOSTFUNC) == 0)
                            {
                                //如果这个API不存在呢;
                                //所以还应该有一个主程序API的表
                                int index = hostApiTable.AddString(opContent);
                                instrStream[SyntaxHelper.CurrInstrIndex].OpList[i].Type  = Op_Type.OP_TYPE_HOST_FUNC;
                                instrStream[SyntaxHelper.CurrInstrIndex].OpList[i].index = index;
                            }

                            //要是标签
                            if ((opFlag & (int)Op_Type_Flag.OP_FLAG_LABEL) == 0)
                            {
                                int index = 0;
                                if (!LabelTable.Instance.FindLabel(opContent, SyntaxHelper.CurrFuncIndex, out index))
                                {
                                    ExDebug.Instance.ExitOnCode("没有定义该标签");
                                }
                                instrStream[SyntaxHelper.CurrInstrIndex].OpList[i].Type  = Op_Type.OP_TYPE_LABEL_INDEX;
                                instrStream[SyntaxHelper.CurrInstrIndex].OpList[i].index = index;
                            }

                            break;
                            #endregion

                        case AttributeType.TYPE_END_OF_FILE:
                            break;

                        default:
                            ExDebug.Instance.ExitOnCode("指令错误");
                            break;
                        }
                    }
                    break;

                default:
                    ExDebug.Instance.ExitOnCode("发现以其他字符进行开头");
                    break;
                }

                if (!Lexer.Instance.SkipNextLine())
                {
                    break;
                }
            }
        }
Example #3
0
 public void AddInstr(InstrLookUp instr)
 {
     instrTable.AddLast(instr);
 }
Example #4
0
        public AttributeType GetNextToken()
        {
            lexerHelper.index0 = lexerHelper.index1;

            //该行已经读取完毕
            //if (lexerHelper.index1 >= sourceFile[lexerHelper.currLine].Length)
            //{
            //    if (!SkipNextLine())
            //    {
            //        return AttributeType.TYPE_END_OF_FILE;
            //    }
            //}

            if (lexerHelper.lexState == LexState.EndString)
            {
                lexerHelper.lexState = LexState.NoString;
            }

            if (lexerHelper.lexState == LexState.NoString)
            {
                while (true)
                {
                    if (lexerHelper.index0 >= sourceFile[lexerHelper.currLine].Length)
                    {
                        if (!SkipNextLine())
                        {
                            return(AttributeType.TYPE_END_OF_FILE);
                        }
                    }

                    if (!IsWhiteSpace(sourceFile[lexerHelper.currLine][lexerHelper.index0]))
                    {
                        break;
                    }
                    lexerHelper.index0++;
                }
                lexerHelper.index1 = lexerHelper.index0;

                while (!IsSeparationChar(sourceFile[lexerHelper.currLine][lexerHelper.index1]))
                {
                    //
                    if (lexerHelper.index1 >= sourceFile[lexerHelper.currLine].Length)
                    {
                        break;
                    }
                    //如果index1也超过了呢
                    lexerHelper.index1++;
                }
            }

            if (lexerHelper.lexState == LexState.InString)
            {
                while (true)
                {
                    if (lexerHelper.index0 >= sourceFile[lexerHelper.currLine].Length)
                    {
                        //该字符串没有结尾符
                        return(AttributeType.TYPE_INVALID);
                    }

                    if (sourceFile[lexerHelper.currLine][lexerHelper.index0] == '\\')
                    {
                        lexerHelper.index0 += 2;
                        continue;
                    }

                    if (sourceFile[lexerHelper.currLine][lexerHelper.index0] == '\"')
                    {
                        break;
                    }
                    lexerHelper.index1++;
                }
            }

            if (lexerHelper.index1 - lexerHelper.index0 == 0)
            {
                lexerHelper.index1++;
            }

            int iDestIndex = 0;

            for (int iSourceIndex = lexerHelper.index0; iSourceIndex < lexerHelper.index1; iSourceIndex++)
            {
                if (sourceFile[lexerHelper.currLine][iSourceIndex] == '\\')
                {
                    iSourceIndex++;
                }
                lexerHelper.destStr[iDestIndex] = sourceFile[lexerHelper.currLine][iSourceIndex];
                iDestIndex++;
            }

            lexerHelper.destStr[iDestIndex] = '\0';
            //已经获得目标字符串, 开始进行判断

            if (lexerHelper.destStr.Length < 1 || lexerHelper.destStr[0] == '"')
            {
                if (lexerHelper.lexState == LexState.InString)
                {
                    return(AttributeType.TYPE_STRING);
                }
            }

            //单字符长度
            if (lexerHelper.destStr.Length == 1)
            {
                switch (lexerHelper.destStr[0])
                {
                case '"':
                    if (lexerHelper.lexState == LexState.NoString)
                    {
                        lexerHelper.lexState = LexState.InString;
                    }
                    else
                    {
                        lexerHelper.lexState = LexState.EndString;
                    }
                    return(AttributeType.TYPE_DOUBLE_QUOTA);

                case ':': return(AttributeType.TYPE_COLON);

                case ',':   return(AttributeType.TYPE_COMMA);

                case '{': return(AttributeType.TYPE_OPEN_BRACE);

                case '}': return(AttributeType.TYPE_CLOSE_BRACE);

                case '[':  return(AttributeType.TYPE_OPEN_BRACKET);

                case ']': return(AttributeType.TYPE_CLOSE_BRACKET);
                }
            }


            //长字符长度
            if (IsIdent(lexerHelper.destStr.ToString()))
            {
                return(AttributeType.TYPE_IDENTIFER);
            }
            if (IsInt(lexerHelper.ToString()))
            {
                return(AttributeType.TYPE_INT);
            }
            if (IsFloat(lexerHelper.destStr.ToString()))
            {
                return(AttributeType.TYPE_FLOAT);
            }
            if (IsIdent(lexerHelper.destStr.ToString()))
            {
                return(AttributeType.TYPE_IDENTIFER);
            }

            //指示符
            if (lexerHelper.destStr.ToString() == "Func")
            {
                return(AttributeType.TYPE_FUNC);
            }
            if (lexerHelper.destStr.ToString() == "Var")
            {
                return(AttributeType.TYPE_VAR);
            }
            if (lexerHelper.destStr.ToString() == "Param")
            {
                return(AttributeType.TYPE_PARAM);
            }
            if (lexerHelper.destStr.ToString() == "SetStackSize")
            {
                return(AttributeType.TYPE_SETSTACKSIZE);
            }
            if (lexerHelper.destStr.ToString() == "RetVal")
            {
                return(AttributeType.TYPE_RETVAL);
            }

            //指令
            InstrLookUp instr = default(InstrLookUp);

            if (InstrTable.Instance.FindInstr(lexerHelper.destStr.ToString(), out instr))
            {
                return(AttributeType.TYPE_INSTR);
            }

            return(AttributeType.TYPE_INVALID);
        }