예제 #1
0
        /// <summary>
        /// 执行一个区块
        /// </summary>
        /// <param name="StatementList">语句列表语法树</param>
        public void ExecBlock(ASTNode_StatementList StatementList)
        {
            foreach (ASTNode_Statement s in StatementList.Statements)
            {
                switch (s.Type)
                {
                case ASTNode.ASTType.Assignment:
                {
                    ASTNode_Assignment tAssignment = (ASTNode_Assignment)s;
                    object             tResult     = ExecExpression(tAssignment.Expression);
                    if (!_Environment.ContainsKey(tAssignment.IdentifierLower))
                    {
                        _Environment.Add(tAssignment.IdentifierLower, tResult);
                    }
                    else
                    {
                        _Environment[tAssignment.IdentifierLower] = tResult;
                    }
                }
                break;

                case ASTNode.ASTType.Call:
                {
                    ASTNode_Call tCall = (ASTNode_Call)s;
                    applyCallOperator(tCall.Identifier, tCall.IdentifierLower, tCall.ArgList, s.LineNumber);
                }
                break;

                case ASTNode.ASTType.ForStatement:
                {
                    ASTNode_ForStatement tForStatement = (ASTNode_ForStatement)s;
                    object tFromResult = ExecExpression(tForStatement.FromExpression);
                    object tToResult   = ExecExpression(tForStatement.ToExpression);
                    object tStepResult = tForStatement.StepExpression == null ? null : ExecExpression(tForStatement.StepExpression);
                    doForLoop(
                        tForStatement.Identifier,
                        tForStatement.IdentifierLower,
                        tFromResult,
                        tToResult,
                        tStepResult,
                        tForStatement.ExecBlock,
                        s.LineNumber
                        );
                }
                break;

                default:
                    throw new RuntimeException(s.LineNumber, "internal error.");
                }
            }
        }
예제 #2
0
        /// <summary>
        /// 解析For语句
        /// </summary>
        /// <param name="Lex">词法分析器</param>
        /// <returns>解析结果</returns>
        private static ASTNode_ForStatement ParseForStatement(Lexer Lex)
        {
            ASTNode_ForStatement tRet = new ASTNode_ForStatement(Lex.Line);

            // for <identifier> from <expression> to <expression> {step <expression>} <block>
            MatchToken(Lex, Lexer.Token.For);
            tRet.Identifier = MatchIdentifier(Lex);
            MatchToken(Lex, Lexer.Token.From);
            tRet.FromExpression = ParseExpression(Lex);
            MatchToken(Lex, Lexer.Token.To);
            tRet.ToExpression = ParseExpression(Lex);
            if (TryMatchToken(Lex, Lexer.Token.Step))
            {
                tRet.StepExpression = ParseExpression(Lex);
            }
            tRet.ExecBlock = ParseBlock(Lex);

            return(tRet);
        }