Пример #1
0
        //---------------------
        // Парсер бинарных выражений
        //---------------------

        private static ASTNode MemberBinOperation()
        {
            ASTNode node = null;

            switch (curTok.token)
            {
            case Tokens.Token.ID:
                string nameID = curTok.subString;
                Point  point  = new Point(curTok.y, curTok.x);
                node = new IdentificatorAST(nameID, point);
                GetNextToken();
                if (curTok.token == Tokens.Token.BRACKET_L)
                {
                    return(new IdentificatorAST(nameID, point, new BracketsAST(ParseBrackets(true)), true));
                }
                //return new BracketsAST(node, ParseBrackets(true));
                return(node);

            case Tokens.Token.STRING:
                node = new StringAST(curTok.subString);
                break;

            case Tokens.Token.CHAR:
                node = new CharAST(curTok.subString, new Point(curTok.y, curTok.x));
                break;

            case Tokens.Token.PARENTHESIS_L:
                node = ParseParenthesis();
                break;

            case Tokens.Token.BOOL:
                node = new BoolAST(curTok.subString, null, new Point(curTok.y, curTok.x));
                break;

            case Tokens.Token.INT_VALUE:
            case Tokens.Token.DOUBLE_VALUE:
            case Tokens.Token.X16_VALUE:
            case Tokens.Token.X8_VALUE:
            case Tokens.Token.X2_VALUE:
                node = new NumAST(curTok.token, curTok.subString);
                break;

            default:
                ConsoleHelper.WriteErrorAST("Impossible token in this area", curTok.y, curTok.x);
                break;
            }
            GetNextToken();
            return(node);
        }
Пример #2
0
 //---------------------
 // Парсер метода
 //---------------------
 private static ASTNode ParseArgInMethod(bool isCall)
 {
     if (curTok.token == Tokens.Token.ID && isCall)
     {
         IdentificatorAST identificatorAST = new IdentificatorAST(curTok.subString, new Point(curTok.y, curTok.x));
         return(identificatorAST);
     }
     else if (curTok.token == Tokens.Token.TYPE && !isCall)
     {
         return(ParseType(false, false));
     }
     else if (curTok.token == Tokens.Token.PARENTHESIS_R)
     {
         return(null);
     }
     else
     {
         ConsoleHelper.WriteErrorAST("Expected 'type' for identificator in method declaration", curTok.y, curTok.x);
     }
     return(null);
 }
Пример #3
0
        //---------------------
        // Парсер id
        //---------------------

        private static ASTNode ParseId(string type, bool isArray)
        {
            bool   isCall = type == "";
            string idName = curTok.subString;
            string typeId = type == "" ? GetTypeID(idName) : type;

            ASTNode id = new IdentificatorAST(typeId, idName, new Point(curTok.y, curTok.x), isArray);

            GetNextToken();
            Point point = new Point(curTok.y, curTok.x);

            // массив
            if (curTok.token == Tokens.Token.BRACKET_L)
            {
                ASTNode memberBrackets = ParseBrackets(true);
                isArray = true;
                BracketsAST brackets = new BracketsAST(memberBrackets);
                if (curTok.token == Tokens.Token.ASSIGNMENT)
                {
                    return(new IdentificatorAST(typeId, idName, ParseInitID(), brackets, point, true));
                }
            }

            if (curTok.token == Tokens.Token.CREMENT)
            {
                return(ParseCrement(id));
            }
            if (curTok.token == Tokens.Token.PARENTHESIS_L)
            {
                return(ParseMethod("", idName, isCall));
            }
            else if (curTok.token == Tokens.Token.ASSIGNMENT)
            {
                return(new IdentificatorAST(typeId, idName, ParseInitID(), point, isArray));
            }
            return(id);
        }
Пример #4
0
        private static List <ASTNode> ParseDeclaredVarInFor()
        {
            List <ASTNode> declaredVar = new List <ASTNode>();
            string         generalType = null;
            bool           isComm      = true;
            bool           isFirst     = true;

            while (true)
            {
                GetNextToken();
                if (curTok.token == Tokens.Token.TYPE && isComm && isFirst)
                {
                    generalType = curTok.subString;
                    ASTNode id = ParseType(false, false);
                    if (id != null)
                    {
                        declaredVar.Add(id);
                    }
                }
                else if (curTok.token == Tokens.Token.ID && isComm)
                {
                    isFirst = false;
                    ASTNode id     = null;
                    string  idName = curTok.subString;
                    GetNextToken();
                    if (curTok.token == Tokens.Token.ASSIGNMENT)
                    {
                        string typeId = GetTypeID(idName);

                        if (typeId == null && generalType == null)
                        {
                            ConsoleHelper.WriteErrorAST("ID not init", curTok.y, curTok.x);
                            return(null);
                        }
                        else if (typeId == null && generalType != null && !isFirst)
                        {
                            Point   point = new Point(curTok.y, curTok.x);
                            ASTNode expr  = ParseInitID();
                            id = new IdentificatorAST(typeId, idName, expr, point, false);
                            declaredVar.Add(id);
                        }
                        else
                        {
                            ConsoleHelper.WriteErrorAST("ID not init", curTok.y, curTok.x);
                            return(null);
                        }
                    }
                    else
                    {
                        ConsoleHelper.WriteErrorAST("Expected '=", curTok.y, curTok.x);
                    }
                    isComm = curTok.token == Tokens.Token.COMM;
                }
                else if (curTok.token == Tokens.Token.SEMILICON)
                {
                    break;
                }
                else
                {
                    ConsoleHelper.WriteErrorAST("Ecpected 'type' or 'id'", curTok.y, curTok.x);
                    return(null);
                }
                if (curTok.token == Tokens.Token.SEMILICON)
                {
                    break;
                }
            }
            return(declaredVar);
        }