예제 #1
0
        public static IfStatementNode Parse(AstTreeNode lastNode, ScriptToken scriptToken, List <ScriptToken> tokens)
        {
            if (tokens[1].Type != EScriptTokenType.L_PAREN)
            {
                Console.WriteLine("If statement needs to be followed by a condition encased in parenthesis.");
                return(null);
            }
            List <ConditionalNode> nodes = new List <ConditionalNode>();

            nodes.Add(ParseConditional(tokens));

            while (tokens.Count > 0 && EScriptTokenType.ELSE_IF == tokens[0].Type)
            {
                nodes.Add(ParseConditional(tokens));
            }

            if (tokens.Count > 0 && EScriptTokenType.ELSE == tokens[0].Type)
            {
                tokens.RemoveAt(0); // Consume else
                nodes.Add(new ConditionalNode(
                              new LiteralNode <bool>(true),
                              ScriptTree.ProcessTokens(ScriptTree.GetBlockTokens(tokens, EBlockType.BRACE, false)[0])
                              ));
            }

            return(new IfStatementNode(nodes));
        }
예제 #2
0
        public static ForStatementNode Parse(AstTreeNode lastNode, ScriptToken scriptToken, List <ScriptToken> tokens)
        {
            if (tokens[1].Type != EScriptTokenType.L_PAREN)
            {
                Console.WriteLine("Syntax error: Missing ('");
                return(null);
            }

            if (tokens[3].Type != EScriptTokenType.OF)
            {
                Console.WriteLine("Syntax error: Missing of");
                return(null);
            }

            tokens.RemoveAt(0); // consume for
            List <ScriptToken> loopDef      = ScriptTree.GetEnclosedTokens(tokens);
            ScriptToken        variableName = loopDef[0];

            loopDef.RemoveAt(0); // consume variable name
            loopDef.RemoveAt(0); // consume of
            AstTreeNode list  = ScriptTree.ProcessTokens(loopDef);
            AstTreeNode block = ScriptTree.ProcessTokens(ScriptTree.GetBlockTokens(tokens, EBlockType.BRACE, false)[0]);

            return(new ForStatementNode(
                       new LiteralNode <string>(variableName.Value),
                       list,
                       block
                       ));
        }
예제 #3
0
        public static ConditionalNode ParseConditional(List <ScriptToken> tokens)
        {
            List <EScriptTokenType> ifTokens = new List <EScriptTokenType>();

            ifTokens.Add(EScriptTokenType.IF);
            ifTokens.Add(EScriptTokenType.ELSE_IF);

            if (!ifTokens.Contains(tokens[0].Type))
            {
                Console.WriteLine("Invalid Syntax");
                return(null);
            }

            tokens.RemoveAt(0); // consume if and else if
            return(new ConditionalNode(
                       ScriptTree.ProcessTokens(ScriptTree.GetBlockTokens(tokens, EBlockType.PAREN, false)[0]),
                       ScriptTree.ProcessTokens(ScriptTree.GetBlockTokens(tokens, EBlockType.BRACE, false)[0])
                       ));
        }