Beispiel #1
0
        private IExpression[] MakeStatements(string line)
        {
            // 関数ステートメント
            var statement  = Lexer.SplitStatement(line);
            var statements = new IExpression[statement.Length];
            var length     = statements.Length;

            for (var i = 0; i < length; ++i)
            {
                if (Lexer.IsEvaluate(statement[i]))
                {
                    // 括弧内を評価する
                    statements[i] = MakeExpression(statement[i]);
                }
                else if (Lexer.IsString(statement[i]))
                {
                    // クオート済みテキスト
                    statements[i] = new TextExpression(RemoveDoubleQuote(statement[i]));
                }
                else
                {
                    // プレーンテキスト
                    statements[i] = new TextExpression(statement[i]);
                }
            }
            return(statements);
        }
Beispiel #2
0
        public IExpression MakeExpression(string line)
        {
            IExpression statement = null;
            var         token     = RemoveScope(line);
            // トークンに分解する
            var tokens = Lexer.SplitToken(token);

            // トークンの種類を判別する
            if (tokens[0] == "$if")
            {
                // if文
                var value         = Lexer.MakeIfAndWhileStatement(tokens);
                var ifexpressions = new List <IfSubExpression>();
                var length        = value.Length;
                for (var i = 0; i < length; i += 3)
                {
                    IfSubExpression ifexpression;
                    if (value[i] == "$if" || value[i] == "elseif")
                    {
                        var           cells = Lexer.SplitToken(value[i + 1]);
                        IExpression[] statements;
                        var           expression = MakeExpression(cells);
                        if (i + 2 < length)
                        {
                            statements = MakeStatements(RemoveScope(value[i + 2]));
                        }
                        else
                        {
                            statements = new IExpression[0];
                        }
                        ifexpression = new IfSubExpression(expression, statements);
                    }
                    else if (value[i] == "else")
                    {
                        // 常に成り立つ条件式をつける
                        IExpression[] statements;
                        IExpression   expression = new ValueExpression(new Value(true));
                        if (i + 1 < length)
                        {
                            statements = MakeStatements(RemoveScope(value[i + 1]));
                        }
                        else
                        {
                            statements = new IExpression[0];
                        }
                        ifexpression = new IfSubExpression(expression, statements);
                    }
                    else
                    {
                        // パースエラー : if ~ elseif ~ elseの対応がおかしい
                        continue;
                    }
                    // コレクションに追加
                    ifexpressions.Add(ifexpression);
                }
                statement = new IfExpression(ifexpressions.ToArray());
            }
            else if (tokens[0] == "$for")
            {
                // for文
                var expressions = new IExpression[3];
                var value       = Lexer.MakeForAndForeachStatement(tokens);
                for (var i = 0; i < 3; i++)
                {
                    var cells = Lexer.SplitToken(value[i + 1]);
                    expressions[i] = MakeExpression(cells);
                }
                var statements = MakeStatements(RemoveScope(value[4]));
                statement = new ForExpression(expressions, statements);
            }
            else if (tokens[0] == "$foreach")
            {
                // foreach文
                var expressions = new IExpression[3];
                var value       = Lexer.MakeForAndForeachStatement(tokens);
                for (var i = 0; i < 2; i++)
                {
                    var cells = Lexer.SplitToken(value[i + 1]);
                    expressions[i] = MakeExpression(cells);
                }
                var statements = MakeStatements(RemoveScope(value[3]));
                statement = new ForeachExpression(expressions, statements);
            }
            else if (tokens[0] == "$while")
            {
                // while文
                var value      = Lexer.MakeIfAndWhileStatement(tokens);
                var cells      = Lexer.SplitToken(RemoveScope(value[1]));
                var expression = MakeExpression(cells);
                // ステートメントを作成する
                var statements = MakeStatements(RemoveScope(value[2]));
                statement = new WhileExpression(expression, statements);
            }
            else if (tokens[0] == "$switch")
            {
                // switch文
            }
            else
            {
                // 式文
                var name = tokens[0];
                // 引数を作成する
                var arguments = Lexer.MakeFunctionArguments(tokens);
                if (arguments != null)
                {
                    var length      = arguments.Length;
                    var expressions = new IExpression[length];
                    for (var i = 0; i < length; i++)
                    {
                        if (arguments[i].Length != 0)
                        {
                            var cells = Lexer.SplitToken(arguments[i]);
                            expressions[i] = MakeExpression(cells);
                        }
                        else
                        {
                            expressions[i] = new ValueExpression(new Value());
                        }
                    }
                    statement = new FunctionExpression(name, expressions);
                }
                else
                {
                    if (Lexer.IsExpression(tokens))
                    {
                        statement = MakeExpression(tokens);
                    }
                    else if (Lexer.IsVariable(token))
                    {
                        statement = new EvaluateExpression(token);
                    }
                    else
                    {
                        statement = new TextExpression("{" + token + "}");
                    }
                }
            }
            return(statement);
        }