Example #1
0
        public override ICodeContext CreateContext(CodeContextTypes contextType, string name = null, Bag <string> attributes = null)
        {
            switch (contextType)
            {
            case CodeContextTypes.MethodCall:
                return(new MethodCallContext(name, (methodCall) =>
                {
                    Add(OperatorTypes.Noop, methodCall);
                }, attributes?.Get <string>("hook")));

            case CodeContextTypes.Expression:
                return(new ExpressionContext((expression) =>
                {
                    Add(_defaultOperator, expression);
                }));

            case CodeContextTypes.SignedExpression:
                return(new ExpressionContext((expression) =>
                {
                    Add(OperatorTypes.UnaryMinus, expression);
                }));

            case CodeContextTypes.ParenthesizedExpression:
                return(new ExpressionContext((expression) =>
                {
                    Add(_defaultOperator, new ExpressionNode(GetOperatorMap(OperatorTypes.Parenthesis), expression));
                }));

            case CodeContextTypes.BinaryExpression:
                if (attributes?.Contains("operator") ?? false)
                {
                    var operatorType = attributes.Get <OperatorTypes>("operator");
                    return(new ExpressionContext((expression) =>
                    {
                        Add(operatorType, expression);
                    }));
                }
                break;

            case CodeContextTypes.IndexExpression:
                bool last = attributes?.Get <bool>("last", false) ?? false;
                return(new ExpressionContext((expression) =>
                {
                    Add(last?OperatorTypes.LastIndex:OperatorTypes.Index, expression);
                }));

            case CodeContextTypes.OfExpression:
                bool lastOf = attributes?.Get <bool>("last", false) ?? false;
                return(new ExpressionContext((expression) =>
                {
                    Add(_defaultOperator, expression);
                }, lastOf? OperatorTypes.LastIndexOf:OperatorTypes.IndexOf));
            }
            return(base.CreateContext(contextType, name, attributes));
        }
Example #2
0
        public override ICodeContext CreateContext(CodeContextTypes contextType, string name, Bag <string> attributes = null)
        {
            MacroDefinition macroDefinition;

            switch (contextType)
            {
            case CodeContextTypes.MethodCall:
                macroDefinition = attributes?.Get <MacroDefinition>("definition");
                return(new MethodCallContext(name, (callExpression) =>
                {
                    WriteStatement(callExpression);
                }, attributes?.Get <string>("hook"), macroDefinition.RequiresState));

            case CodeContextTypes.If:
                return(new IfContext(name, (ifStatement) =>
                {
                    Add(ifStatement);
                }));

            case CodeContextTypes.Choose:
                return(new ChooseContext((chooseStatement) =>
                {
                    Add(chooseStatement);
                }));

            case CodeContextTypes.Each:
                return(new EachContext(name, (eachStatement) =>
                {
                    Add(eachStatement);
                }));

            case CodeContextTypes.Set:
                return(new SetContext(name, (setStatement) =>
                {
                    Add(setStatement);
                }));

            case CodeContextTypes.HookDefinition:
                return(new HookDefinitionContext(name, (setStatement) =>
                {
                    Add(setStatement);
                }));

            case CodeContextTypes.TagOpen:
                return(new TagOpenContext(name, null, (expression) =>
                {
                    Add(expression);
                }, attributes?.Get <bool>("closing") ?? false));

            case CodeContextTypes.TagClose:
                return(new TagCloseContext(name, null, (expression) =>
                {
                    Add(expression);
                }));

            case CodeContextTypes.Block:
                return(new BlockContext((block) =>
                {
                    Add(block);
                }));
            }
            return(base.CreateContext(contextType, name, attributes));
        }