示例#1
0
文件: Sta.cs 项目: pBactusp/Shpoon
        public static StatementNode Parse(TokenString tStr, ref int index)
        {
            switch (tStr[index].Type)
            {
            case TokenType.cBraceOpen:
                return(CompoundStaNode.Parse(tStr, ref index));

            case TokenType.ifStatement:
                return(ConditionalStaNode.Parse(tStr, ref index));

            case TokenType.doStatement:
                return(DoLoopStaNode.Parse(tStr, ref index));

            case TokenType.whileStatement:
                return(WhileLoopStaNode.Parse(tStr, ref index));

            case TokenType.forStatement:
                return(ForLoopStaNode.Parse(tStr, ref index));

            case TokenType.returnStatement:
                return(ReturnStaNode.Parse(tStr, ref index));

            case TokenType.lineEnd:
                return(NopStaNode.Parse(tStr, ref index));

            default:
                return(ExpressionStaNode.Parse(tStr, ref index));
            }
        }
示例#2
0
        public static ForLoopStaNode Parse(TokenString tStr, ref int index)
        {
            int startIndex = index;

            if (!tStr.Match(index, TokenType.forStatement, TokenType.rBraceOpen))
            {
                return(null);
            }

            index++;

            var forDef = tStr.GetRangeInBrackets(ref index).Split(false, TokenType.lineEnd);

            if (forDef.Count != 3)
            {
                index = startIndex;
                return(null);
            }

            VariableDefExpNode declarations = Exp.Parse(forDef[0]) as VariableDefExpNode;
            ExpressionNode     condition    = Exp.Parse(forDef[1]);

            if (condition == null)
            {
                index = startIndex;
                return(null);
            }

            StatementNode body = Sta.Parse(tStr, ref index);

            ForLoopStaNode node = new ForLoopStaNode(declarations, condition, body);

            var exps = forDef[2].Split(false, TokenType.comma);

            foreach (var exp in exps)
            {
                node.AddExpression(Exp.Parse(exp));
            }

            return(node);
        }