Exemple #1
0
        DefineUnit GetDefineUnit(string identName, string blockName)
        {
            DefineUnit unit = null;

            for (int i = defineList.Count - 1; i >= 0; i--)
            {
                if (defineList[i].name == identName)
                {
                    if (defineList[i].funcName == blockName)
                    {
                        unit = defineList[i];
                        break;
                    }

                    if (defineList[i].funcName == "Global")
                    {
                        unit = defineList[i];
                        break;
                    }
                }
            }

            // = defineList.Find(x => x.name == identName);
            if (unit == null)
            {
                PrintError(ErrorCase.Undefine);
                isError = true;
            }
            return(unit);
        }
Exemple #2
0
        void CallState(int level, string blockName)
        {
            GetSymbol();

            if (currentSymbol == Symbol.Ident)
            {
                DefineUnit unit = GetDefineUnit(currentIdent, blockName);

                if (isError)
                {
                    return;
                }

                GenerateCode(InstructionType.cal, level - unit.level, unit.address);
                GetSymbol();
                GetSymbol();
                GetSymbol();
            }
            else
            {
                PrintError(ErrorCase.NeedIdent);
                isError = true;
                return;
            }
        }
Exemple #3
0
        void Factor(int level, string blockName)
        {
            switch (currentSymbol)
            {
            case Symbol.Ident:
                DefineUnit unit = GetDefineUnit(currentIdent, blockName);

                if (isError)
                {
                    return;
                }

                switch (unit.type)
                {
                case DefineType.Constant:
                    GenerateCode(InstructionType.lit, 0, unit.value);
                    break;

                case DefineType.Variable:
                    GenerateCode(InstructionType.lod, level - unit.level, unit.address);
                    break;

                case DefineType.Procedure:
                    //It's Wrong
                    break;
                }
                GetSymbol();
                break;

            case Symbol.Number:

                GenerateCode(InstructionType.lit, 0, currentNumber);
                GetSymbol();
                break;

            case Symbol.Lparen:
                GetSymbol();
                Exprission(level, blockName);

                if (currentSymbol == Symbol.Rparen)
                {
                    GetSymbol();
                }

                break;

            default:
                PrintError(ErrorCase.NeedFactor);
                isError = true;

                break;
            }
        }
Exemple #4
0
        void DifineListAdd(DefineType type, string name, int value, int level, int address, int size, string parent)
        {
            DefineUnit unit = new DefineUnit();

            unit.type     = type;
            unit.name     = name;
            unit.level    = level;
            unit.value    = currentNumber;
            unit.address  = address;
            unit.size     = size;
            unit.funcName = parent;
            defineList.Add(unit);
        }
Exemple #5
0
        void ReadState(int level, string blockName)
        {
            GetSymbol();
            if (currentSymbol == Symbol.Lparen)
            {
                GetSymbol();
                if (currentSymbol == Symbol.Ident)
                {
                    DefineUnit unit = GetDefineUnit(currentIdent, blockName);

                    if (isError)
                    {
                        return;
                    }
                    GenerateCode(InstructionType.opr, 0, 16);
                    GenerateCode(InstructionType.sto, level - unit.level, unit.address);
                }
                else
                {
                    PrintError(ErrorCase.NeedIdent);
                    isError = true;
                    return;
                }
                GetSymbol();

                if (currentSymbol == Symbol.Rparen)
                {
                    GetSymbol();
                }
                else
                {
                    PrintError(ErrorCase.NeedRparen);
                    isError = true;
                    return;
                }
            }
            else
            {
                PrintError(ErrorCase.NeedLparen);
                isError = true;
                return;
            }
        }
Exemple #6
0
        void AssignState(int level, string blockName)
        {
            DefineUnit unit = GetDefineUnit(currentIdent, blockName);

            if (isError)
            {
                return;
            }

            if (unit.type == DefineType.Variable)
            {
                GetSymbol();
                if (currentSymbol == Symbol.Becomes)
                {
                    GetSymbol();

                    if (currentSymbol == Symbol.CallSym)
                    {
                        CallState(level, blockName);
                    }
                    else
                    {
                        Exprission(level, blockName);
                    }
                    GenerateCode(InstructionType.sto, level - unit.level, unit.address);
                }
                else
                {
                    PrintError(ErrorCase.NeedBecomes);
                    isError = true;
                    return;
                }
            }
            else
            {
                PrintError(ErrorCase.NeedVariable);
                isError = true;
                return;
            }
        }
Exemple #7
0
        void ForState(int level, string blockName)
        {
            GetSymbol();
            if (currentSymbol == Symbol.Ident)
            {
                DefineUnit unit = GetDefineUnit(currentIdent, blockName);

                if (isError)
                {
                    return;
                }

                //GenerateCode(InstructionType.cal, level - unit.level, unit.address);
                GetSymbol();

                if (currentSymbol == Symbol.InSym)
                {
                    GetSymbol();
                }
                else
                {
                    PrintError(ErrorCase.NeedInSym);
                    isError = true;
                    return;
                }


                if (currentSymbol == Symbol.Ident)
                {
                    DefineUnit startCircleUnit = GetDefineUnit(currentIdent, blockName);

                    if (isError)
                    {
                        return;
                    }

                    GenerateCode(InstructionType.lod, level - startCircleUnit.level, startCircleUnit.address);
                    GetSymbol();
                }
                else
                {
                    GenerateCode(InstructionType.lit, 0, currentNumber);
                    GetSymbol();
                }

                GenerateCode(InstructionType.sto, level - unit.level, unit.address);

                int jmpCodeAddress = curCodeIndex;

                if (currentSymbol == Symbol.Range)
                {
                    GenerateCode(InstructionType.lod, level - unit.level, unit.address);
                    GetSymbol();
                }
                else
                {
                    PrintError(ErrorCase.NeedRangeSym);
                    isError = true;
                    return;
                }

                if (currentSymbol == Symbol.Ident)
                {
                    DefineUnit endCircleUnit = GetDefineUnit(currentIdent, blockName);

                    if (isError)
                    {
                        return;
                    }

                    GenerateCode(InstructionType.lod, level - endCircleUnit.level, endCircleUnit.address);
                    GetSymbol();
                }
                else
                {
                    GenerateCode(InstructionType.lit, 0, currentNumber);
                    GetSymbol();
                }
                GenerateCode(InstructionType.opr, 0, 13);
                int jpcCodeIndex = curCodeIndex;
                GenerateCode(InstructionType.jpc, 0, 0);

                if (currentSymbol == Symbol.BeginSym)
                {
                    GetSymbol();
                    Statement(true, level, blockName);
                    GenerateCode(InstructionType.lod, level - unit.level, unit.address);
                    GenerateCode(InstructionType.lit, 0, 1);
                    GenerateCode(InstructionType.opr, 0, 2);
                    GenerateCode(InstructionType.sto, level - unit.level, unit.address);
                    GenerateCode(InstructionType.jmp, 0, jmpCodeAddress);
                    codeList[jpcCodeIndex].a = curCodeIndex;
                }
                else
                {
                    PrintError(ErrorCase.NeedBeginSym);
                    isError = true;
                    return;
                }
            }
            else
            {
                PrintError(ErrorCase.NeedIdent);
                isError = true;
                return;
            }
        }