Ejemplo n.º 1
0
        /// <summary>
        /// Executed upon exiting expr rule
        /// </summary>
        /// <param name="context">the expr context</param>
        public override void ExitExpr([NotNull] ParamExprGrammarParser.ExprContext context)
        {
            base.ExitExpr(context);
            NodeProperty retExpr = new NodeProperty();

            // value
            if (context.GetChild(0) is ParamExprGrammarParser.ValueContext && context.children.Count == 1)
            {
                retExpr = GetNodePropertyValue(context.GetChild(0));
            }
            // | atomic_param
            else if (context.GetChild(0) is ParamExprGrammarParser.Atomic_paramContext && context.children.Count == 1)
            {
                retExpr = GetNodePropertyValue(context.GetChild(0));
            }
            // | unary_operator expr
            else if (context.GetChild(0) is ParamExprGrammarParser.Unary_operatorContext && context.ChildCount == 2)
            {
                retExpr = ParamExprResolver.ResolveExprUnaryOperator(context.GetChild(0).GetText(), GetNodePropertyValue(context.GetChild(1)));
            }
            // | expr ops expr
            else if (context.GetChild(1) is ParamExprGrammarParser.OpsContext && context.ChildCount == 3)
            {
                retExpr = ParamExprResolver.ResolveExprOpsExpr(context.GetChild(0) as ParamExprGrammarParser.ExprContext, GetNodePropertyValue(context.GetChild(0)),
                                                               context.GetChild(1) as ParamExprGrammarParser.OpsContext, GetNodePropertyValue(context.GetChild(2)));
            }
            // | '(' expr ')' (power_op)?
            else if (context.GetChild(0) is ITerminalNode && context.GetChild(2) is ITerminalNode)
            {
                int powerOp = +1;
                if (context.ChildCount == 4)
                {
                    powerOp = ParamExprResolver.GetPowerOp(context.GetChild(3) as ParamExprGrammarParser.Power_opContext);
                }
                retExpr = ParamExprResolver.ResolveExprPowerOp(GetNodePropertyValue(context.GetChild(1) as ParamExprGrammarParser.ExprContext), powerOp);
            }
            else
            {
                // Not valid rule, return nothing? or the original text?
            }

            SetNodePropertyValue(context, retExpr);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Resolve "expr operator expr"
        /// </summary>
        /// <param name="expr1Ctx">context of expr 1</param>
        /// <param name="expr1">NodeProperty of expr 1</param>
        /// <param name="ops">operator</param>
        /// <param name="expr2">NodeProperty of expr 2</param>
        /// <returns>NodeProperty</returns>
        public static NodeProperty ResolveExprOpsExpr(ParamExprGrammarParser.ExprContext expr1Ctx, NodeProperty expr1,
                                                      ParamExprGrammarParser.OpsContext ops, NodeProperty expr2)
        {
            NodeProperty ret = new NodeProperty();

            ret.originalNodePropertyValue = expr1.originalNodePropertyValue + " " + ops.GetText() + " " + expr2.originalNodePropertyValue;

            if (expr1.nodePropertyValue == null && expr2.nodePropertyValue == null)
            {
                return(ret);
            }

            if (expr1.nodePropertyValue == null)
            {
                // expr1 is null, the ops is undefined, returns only expr2 as it is
                ret.nodePropertyValue = expr2.nodePropertyValue;
                return(ret);
            }

            if (expr2.nodePropertyValue == null)
            {
                // expr2 is null, ops is undefined, returns only expr1 as it is
                ret.nodePropertyValue = expr1.nodePropertyValue;
                return(ret);
            }

            if (expr1.nodePropertyValue is ElementId || expr2.nodePropertyValue is ElementId)
            {
                // For ElementId to be in this oper, the Name of the Element will be used and the rest will be converted to strings
                string expr1Str = null;
                if (expr1.nodePropertyValue is ElementId)
                {
                    expr1Str = _element.Document.GetElement((ElementId)expr1.nodePropertyValue).Name;
                }
                else
                {
                    expr1Str = expr1.nodePropertyValue.ToString();
                }

                string expr2Str = null;
                if (expr2.nodePropertyValue is ElementId)
                {
                    expr2Str = _element.Document.GetElement((ElementId)expr2.nodePropertyValue).Name;
                }
                else
                {
                    expr2Str = expr2.nodePropertyValue.ToString();
                }

                //ret.nodePropertyValue = expr1Str + " " + ops.GetText() + " " + expr2Str;
                ret.nodePropertyValue = expr1Str + expr2Str;
            }
            else if (expr1.nodePropertyValue is string || expr2.nodePropertyValue is string)
            {
                // one of the expr is a string, and therefore the entire expr will returns string
                //ret.nodePropertyValue = expr1.nodePropertyValue.ToString() + " " + ops.GetText() + " " + expr2.nodePropertyValue.ToString();
                ret.nodePropertyValue = expr1.nodePropertyValue.ToString() + expr2.nodePropertyValue.ToString();
            }
            else if (expr1.nodePropertyValue is double || expr2.nodePropertyValue is double)
            {
                if (ops.MULTIPLY() != null)
                {
                    ret.nodePropertyValue = (double)expr1.nodePropertyValue * (double)expr2.nodePropertyValue;
                }
                if (ops.DIVIDE() != null)
                {
                    ret.nodePropertyValue = (double)expr1.nodePropertyValue / (double)expr2.nodePropertyValue;
                }
                if (ops.ADDITION() != null)
                {
                    ret.nodePropertyValue = (double)expr1.nodePropertyValue + (double)expr2.nodePropertyValue;
                }
                if (ops.SUBTRACT() != null)
                {
                    ret.nodePropertyValue = (double)expr1.nodePropertyValue - (double)expr2.nodePropertyValue;
                }
            }
            else
            {
                if (ops.MULTIPLY() != null)
                {
                    ret.nodePropertyValue = (int)expr1.nodePropertyValue * (int)expr2.nodePropertyValue;
                }
                if (ops.DIVIDE() != null)
                {
                    ret.nodePropertyValue = (double)((int)expr1.nodePropertyValue / (int)expr2.nodePropertyValue);
                }
                if (ops.ADDITION() != null)
                {
                    ret.nodePropertyValue = (int)expr1.nodePropertyValue + (int)expr2.nodePropertyValue;
                }
                if (ops.SUBTRACT() != null)
                {
                    ret.nodePropertyValue = (int)expr1.nodePropertyValue - (int)expr2.nodePropertyValue;
                }
            }

            return(ret);
        }