コード例 #1
0
        public ForAction(ParseInfo parseInfo, Scope scope, DeltinScriptParser.ForContext forContext)
        {
            Scope varScope = scope.Child();

            if (forContext.define() != null)
            {
                DefinedVariable = new ScopedVariable(varScope, new DefineContextHandler(parseInfo, forContext.define()));
            }
            else if (forContext.initialVarset != null)
            {
                InitialVarSet = new SetVariableAction(parseInfo, varScope, forContext.initialVarset);
            }

            if (forContext.expr() != null)
            {
                Condition = DeltinScript.GetExpression(parseInfo, varScope, forContext.expr());
            }

            if (forContext.endingVarset != null)
            {
                SetVariableAction = new SetVariableAction(parseInfo, varScope, forContext.endingVarset);
            }

            // Get the block.
            if (forContext.block() != null)
            {
                Block = new BlockAction(parseInfo.SetLoop(this), varScope, forContext.block());
                // Get the path info.
                Path = new PathInfo(Block, DocRange.GetRange(forContext.FOR()), false);
            }
            else
            {
                parseInfo.Script.Diagnostics.Error("Expected a block.", DocRange.GetRange(forContext.RIGHT_PAREN()));
            }
        }
コード例 #2
0
        private IStatement StatementFromContext(Scope scope, IParseStatement statementContext)
        {
            switch (statementContext)
            {
            case VariableDeclaration declare: {
                var newVar = new ScopedVariable(true, scope, new DefineContextHandler(this, declare)).GetVar();
                return(new DefineAction(newVar));
            }

            case Assignment assignment: return(new SetVariableAction(this, scope, assignment));

            case Increment increment: return(new IncrementAction(this, scope, increment));

            case If @if: return(new IfAction(this, scope, @if));

            case While @while: return(new WhileAction(this, scope, @while));

            case For @for: return(new ForAction(this, scope, @for));

            case Foreach @foreach: return(new ForeachAction(this, scope, @foreach));

            case Return @return: return(new ReturnAction(this, scope, @return));

            case Delete delete: return(new DeleteAction(this, scope, delete));

            case Continue @continue: return(new ContinueAction(this, @continue.Range));

            case Break @break: return(new BreakAction(this, @break.Range));

            case Switch @switch: return(new SwitchAction(this, scope, @switch));

            case Block @block: return(new BlockAction(this, scope, @block));

            case FunctionExpression func: return(new CallMethodAction(this, scope, func, false, scope));

            // Expression statements (functions)
            case ExpressionStatement exprStatement:

                // Parse the expression
                var expr = GetExpression(scope, exprStatement.Expression, false);

                if (!expr.IsStatement())
                {
                    Script.Diagnostics.Error("Expressions can't be used as statements.", statementContext.Range);
                    return(new MissingElementAction(TranslateInfo));
                }
                if (expr is IStatement == false)
                {
                    return(new MissingElementAction(TranslateInfo));
                }
                return((IStatement)expr);

            // New
            case NewExpression newExpression: return(new CreateObjectAction(this, scope, newExpression));

            default: return(new MissingElementAction(TranslateInfo));
            }
        }
コード例 #3
0
        public static IStatement GetStatement(ParseInfo parseInfo, Scope scope, DeltinScriptParser.StatementContext statementContext)
        {
            switch (statementContext)
            {
            case DeltinScriptParser.S_defineContext define: {
                var newVar = new ScopedVariable(scope, new DefineContextHandler(parseInfo, define.define()));
                return(new DefineAction(newVar));
            }

            case DeltinScriptParser.S_methodContext method: return(new CallMethodAction(parseInfo, scope, method.method(), false, scope));

            case DeltinScriptParser.S_varsetContext varset: return(new SetVariableAction(parseInfo, scope, varset.varset()));

            case DeltinScriptParser.S_exprContext s_expr: {
                var expr = GetExpression(parseInfo, scope, s_expr.expr(), true, false);
                if (expr is ExpressionTree == false || ((ExpressionTree)expr)?.Result is IStatement == false)
                {
                    if (expr != null)
                    {
                        parseInfo.Script.Diagnostics.Error("Expressions can't be used as statements.", DocRange.GetRange(statementContext));
                    }
                    return(null);
                }
                else
                {
                    return((ExpressionTree)expr);
                }
            }

            case DeltinScriptParser.S_ifContext s_if: return(new IfAction(parseInfo, scope, s_if.@if()));

            case DeltinScriptParser.S_whileContext s_while: return(new WhileAction(parseInfo, scope, s_while.@while()));

            case DeltinScriptParser.S_forContext s_for: return(new ForAction(parseInfo, scope, s_for.@for()));

            case DeltinScriptParser.S_for_autoContext s_forAuto: return(new AutoForAction(parseInfo, scope, s_forAuto.for_auto()));

            case DeltinScriptParser.S_foreachContext s_foreach: return(new ForeachAction(parseInfo, scope, s_foreach.@foreach()));

            case DeltinScriptParser.S_returnContext s_return: return(new ReturnAction(parseInfo, scope, s_return.@return()));

            case DeltinScriptParser.S_deleteContext s_delete: return(new DeleteAction(parseInfo, scope, s_delete.delete()));

            case DeltinScriptParser.S_continueContext s_continue: return(new ContinueAction(parseInfo, DocRange.GetRange(s_continue)));

            case DeltinScriptParser.S_breakContext s_break: return(new BreakAction(parseInfo, DocRange.GetRange(s_break)));

            case DeltinScriptParser.S_switchContext s_switch: return(new SwitchAction(parseInfo, scope, s_switch.@switch()));

            case DeltinScriptParser.S_blockContext s_block: return(new BlockAction(parseInfo, scope, s_block));

            default: return(null);
            }
        }