Exemplo n.º 1
0
        public DeclarationsNode Declarations()
        {
            var declarations = new DeclarationsNode();

            while (token.value == "var")
            {
                Match(TokenTypes.ID, "var");
                VarTypes type     = VarTypes.NONE;
                var      tempList = new List <string>();//holds the names of declared variables
                Match(TokenTypes.ID);
                tempList.Add(returnToken.value);
                while (token.value == ",")
                {
                    Match(TokenTypes.COMMA);
                    Match(TokenTypes.ID);
                    tempList.Add(returnToken.value);
                }
                Match(TokenTypes.COLON);

                if (token.value == "array")//if its an array
                {
                    Match(TokenTypes.ID, "array");
                    Match(TokenTypes.LBRACKET);
                    Match(TokenTypes.NUM);
                    int startIndex = int.Parse(returnToken.value);
                    Match(TokenTypes.COLON);
                    Match(TokenTypes.NUM);
                    int endIndex = int.Parse(returnToken.value);
                    Match(TokenTypes.RBRACKET);
                    Match(TokenTypes.ID, "of");

                    if (token.value == "integer")
                    {
                        type = VarTypes.INT;
                    }
                    else if (token.value == "real")
                    {
                        type = VarTypes.REAL;
                    }
                    Match(TokenTypes.ID);
                    foreach (var name in tempList)
                    {
                        var var = new ArrayVariableNode(name, type, startIndex, endIndex);
                        declarations.variableList.Add(var);
                    }
                }
                else//if its not an array
                {
                    if (token.value == "integer")
                    {
                        type = VarTypes.INT;
                    }
                    else if (token.value == "real")
                    {
                        type = VarTypes.REAL;
                    }
                    Match(TokenTypes.ID);
                    foreach (var name in tempList)
                    {
                        var vari = new VariableNode(name, type);
                        declarations.variableList.Add(vari);
                    }
                }
                Match(TokenTypes.SEMICOLON);
            }
            return(declarations);
        }
Exemplo n.º 2
0
        public SubProgramNode SubProgram()
        {
            SubProgramNode subProgramNode = new SubProgramNode();

            Match(TokenTypes.ID);
            subProgramNode.id = returnToken.value;
            if (token.value == "(")
            {
                bool end;
                Match(TokenTypes.LPAR);
                do
                {
                    VarTypes type     = VarTypes.NONE;
                    var      tempList = new List <string>();//holds the names of declared variables
                    Match(TokenTypes.ID);
                    tempList.Add(returnToken.value);
                    while (token.value == ",")
                    {
                        Match(TokenTypes.COMMA);
                        Match(TokenTypes.ID);
                        tempList.Add(returnToken.value);
                    }
                    Match(TokenTypes.COLON);
                    if (token.value == "array")//if its an array
                    {
                        Match(TokenTypes.ID, "array");
                        Match(TokenTypes.LBRACKET);
                        Match(TokenTypes.NUM);
                        int startIndex = int.Parse(returnToken.value);
                        Match(TokenTypes.COLON);
                        Match(TokenTypes.NUM);
                        int endIndex = int.Parse(returnToken.value);
                        Match(TokenTypes.RBRACKET);
                        Match(TokenTypes.ID, "of");

                        if (token.value == "integer")
                        {
                            type = VarTypes.INT;
                        }
                        else if (token.value == "real")
                        {
                            type = VarTypes.REAL;
                        }
                        Match(TokenTypes.ID);
                        foreach (var name in tempList)
                        {
                            var var = new ArrayVariableNode(name, type, startIndex, endIndex);
                            subProgramNode.argumentList.Add(var);
                        }
                    }
                    else//if its not an array
                    {
                        if (token.value == "integer")
                        {
                            type = VarTypes.INT;
                        }
                        else if (token.value == "real")
                        {
                            type = VarTypes.REAL;
                        }
                        Match(TokenTypes.ID);
                        foreach (var name in tempList)
                        {
                            var vari = new VariableNode(name, type);
                            subProgramNode.argumentList.Add(vari);
                        }
                    }
                    end = token.value == ")";
                    if (!end)
                    {
                        Match(TokenTypes.SEMICOLON);
                    }
                } while (!end);
                Match(TokenTypes.RPAR);
                if (token.value == ":")
                {
                    Match(TokenTypes.COLON);
                    Match(TokenTypes.ID);
                    subProgramNode.type = returnToken.value;
                }
                Match(TokenTypes.SEMICOLON);
            }
            subProgramNode.declarations = Declarations();
            SubProgramDeclarations(subProgramNode.methodList);
            CompoundStatement(subProgramNode.statementList);
            return(subProgramNode);
        }