Esempio n. 1
0
        private object UnaryOperation(CalculatorParser.ExpressionContext e, ITerminalNode op)
        {
            var n = e.Accept(this);

            //var varName = "";
            //object varValue = null;
            switch (op.Symbol.Type)
            {
            case CalculatorParser.Not:
                return(!(bool)n);

            case CalculatorParser.Minus:
                if (n is int)
                {
                    return(-(int)n);
                }
                else
                {
                    return(-(float)n);
                }
            }
            return(n);
        }
Esempio n. 2
0
        public override object VisitExpression([NotNull] CalculatorParser.ExpressionContext context)
        {
            object result = null;

            if (context.children.Count == 1)
            {
                // If it is an Id, then return the value
                if (context.Id() != null)
                {
                    try {
                        result = GetValue(context.Id());
                    }
                    catch (KeyNotFoundException e) {
                        throw new Exception($"{context.Id().Accept(this) as string} is undeclared");
                    }
                    return(result);
                }
                // Else let VisitTerminal handle it
                result = context.children[0].Accept(this);
                return(result);
            }
            var exprs = context.expression();

            if (exprs.Length == 1)
            {
                //if (context.Id() != null) {
                //    var d = exprs[0].Accept(this);
                //    var s = context.Id().Accept(this);
                //    result = ObjectOperation(d, s, CalculatorParser.Dot);
                //    return result;
                //}
                // ++x, --x
                if (context.children[1] is ITerminalNode node && node.Symbol.Type == CalculatorParser.LeftParenthesis)
                {
                    var func      = context.expression()[0].Accept(this);
                    var paramList = context.paramList() is null ? null : context.paramList().Accept(this) as List <object>;
                    result      = FuncOperation(func as IFunction, paramList as List <object>, m_iterStack);
                    m_iterStack = null;
                    m_iterName  = "";
                    return(result);
                }
                var op = context.children[0] as ITerminalNode;
                result = UnaryOperation(exprs[0], op);
                return(result);
            }
            if (exprs.Length == 2)
            {
                object l  = null;
                object r  = null;
                var    op = context.children[1] as ITerminalNode;
                if (op.Symbol.Type == CalculatorParser.Dot)
                {
                    l      = exprs[0].Accept(this);
                    r      = exprs[1].Id().Accept(this);
                    result = ObjectOperation(l as Stack, r, op.Symbol.Type);
                    return(result);
                }
                if (op.Symbol.Type == CalculatorParser.LeftBracket)
                {
                    l      = exprs[0].Accept(this);
                    r      = exprs[1].Accept(this);
                    result = ObjectOperation(l as Stack, r, op.Symbol.Type);
                    return(result);
                }
                l = exprs[0].Accept(this);
                r = exprs[1].Accept(this);
                return(BinaryOperation(l, r, op.Symbol.Type));
            }
            throw new InvalidOperationException();
        }
Esempio n. 3
0
 public override int VisitExpression([NotNull] ExpressionContext context)
 {
     return(HandleGroup(context.operand(), context.OPERATOR()));
 }
Esempio n. 4
0
 private double WalkRight(CalculatorParser.ExpressionContext context)
 {
     return(Visit(context.GetRuleContext <CalculatorParser.ExpressionContext>(1)));
 }
 /// <summary>
 /// Visit a parse tree produced by <see cref="CalculatorParser.expression"/>.
 /// <para>
 /// The default implementation returns the result of calling <see cref="AbstractParseTreeVisitor{Result}.VisitChildren(IRuleNode)"/>
 /// on <paramref name="context"/>.
 /// </para>
 /// </summary>
 /// <param name="context">The parse tree.</param>
 /// <return>The visitor result.</return>
 public virtual Result VisitExpression([NotNull] CalculatorParser.ExpressionContext context)
 {
     return(VisitChildren(context));
 }