Пример #1
0
        private string WriteStatement(WriteStatementNode node)
        {
            string output = "";

            output += Expression(node.expression);
            return(output);
        }
Пример #2
0
        public StatementNode Statement()
        {
            StatementNode statementNode = null;

            if (token.value == "if")
            {
                var ifStatementNode = new IfThenStatementNode();
                Match(TokenTypes.ID, "if");
                ifStatementNode.ifExpression = Expression();
                Match(TokenTypes.ID, "then");
                ifStatementNode.thenStatement = Statement();
                Match(TokenTypes.ID, "else");
                ifStatementNode.elseStatement = Statement();
                statementNode = ifStatementNode;
            }
            else if (token.value == "while")
            {
                var whileStatementNode = new WhileStatementNode();
                Match(TokenTypes.ID, "while");
                whileStatementNode.whileExpression = Expression();
                Match(TokenTypes.ID, "do");
                whileStatementNode.doStatement = Statement();
                statementNode = whileStatementNode;
            }
            else if (token.value == "begin")
            {
                var statementList = new List <StatementNode>();
                CompoundStatement(statementList);
            }
            else if (token.value == "read")
            {
                Match(TokenTypes.ID, "read");
                Match(TokenTypes.LPAR);
                Match(TokenTypes.ID);
                var readStatementNode = new ReadStatementNode(returnToken.value);
                Match(TokenTypes.RPAR);
                statementNode = readStatementNode;
            }
            else if (token.value == "write")
            {
                Match(TokenTypes.ID, "write");
                Match(TokenTypes.LPAR);
                var writeStatementNode = new WriteStatementNode();
                writeStatementNode.expression = Expression();
                Match(TokenTypes.RPAR);
                statementNode = writeStatementNode;
            }
            //else if(nextToken.type==TokenTypes.ASSIGNOP)
            else
            {
                Match(TokenTypes.ID);
                string id             = returnToken.value;
                bool   array          = false;
                var    index          = new ExpressionNode();        //for use in array assignops
                var    expressionList = new List <ExpressionNode>(); //for use in procedure statement
                if (token.value == "[")
                {
                    Match(TokenTypes.LBRACKET);
                    index = Expression();
                    Match(TokenTypes.RBRACKET);
                    array = true;
                }
                else if (token.value == "(")
                {
                    Match(TokenTypes.LPAR);
                    do
                    {
                        expressionList.Add(Expression());
                    } while (token.value == ",");
                    Match(TokenTypes.RPAR);
                }
                if (token.type == TokenTypes.ASSIGNOP)//assignop statement
                {
                    var assignStatementNode = new AssignOpStatementNode();
                    int i = 0;
                    while (program.Declarations.variableList[i].variableName != id)
                    {
                        i++;
                        if (i > program.Declarations.variableList.Count)
                        {
                            throw new Exception("");
                        }
                    }

                    if (array)
                    {
                        ArrayVariableNode tempArrayVar = program.Declarations.variableList[i] as ArrayVariableNode;
                        if (tempArrayVar == null)
                        {
                            throw new Exception("");
                        }
                        assignStatementNode.variable        = new ArrayVariableNode(id, program.Declarations.variableList[i].type, tempArrayVar.startIndex, tempArrayVar.endIndex);
                        assignStatementNode.arrayExpression = index;
                    }
                    else
                    {
                        assignStatementNode.variable = new VariableNode(id, program.Declarations.variableList[i].type);
                    }
                    Match(TokenTypes.ASSIGNOP);
                    assignStatementNode.expression = Expression();
                    statementNode = assignStatementNode;
                }
                else//procedure statement
                {
                    var procedureStatementNode = new ProcedureStatementNode();
                    procedureStatementNode.id             = id;
                    procedureStatementNode.expressionList = expressionList;
                    statementNode = procedureStatementNode;
                }
            }

            return(statementNode);
        }