Exemple #1
0
        /// <summary>
        /// 解析一条赋值或者调用指令
        /// </summary>
        /// <param name="Lex">词法分析器</param>
        /// <returns>解析结果</returns>
        private static ASTNode_Statement ParseAssignmentOrCall(Lexer Lex)
        {
            string tIdentifier = MatchIdentifier(Lex);

            if (TryMatchToken(Lex, Lexer.Token.LeftBracket))  // call
            {
                ASTNode_Call tCall = new ASTNode_Call(Lex.Line, tIdentifier, ParseArgList(Lex));

                MatchToken(Lex, Lexer.Token.RightBracket); // ')'
                MatchToken(Lex, Lexer.Token.Semico);       // ';'
                return(tCall);
            }
            else if (TryMatchToken(Lex, Lexer.Token.Is))  // assignment
            {
                // 读取表达式
                ASTNode_Assignment tAssign = new ASTNode_Assignment(Lex.Line, tIdentifier, ParseExpression(Lex));

                MatchToken(Lex, Lexer.Token.Semico);  // ';'
                return(tAssign);
            }
            else
            {
                throw new SyntaxException(Lex.Position, Lex.Line, Lex.Row,
                                          String.Format("unexpected token {0}.", Lex.FormatCurrentToken()));
            }
        }
Exemple #2
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.");
                }
            }
        }