Exemple #1
0
        public override object VisitAssignment([NotNull] CalculatorParser.AssignmentContext context)
        {
            object oldValue = context.expression()[0].Accept(this);
            var    varName  = m_iterName;
            var    stack    = m_iterStack;
            object newValue = context.expression()[1].Accept(this);
            var    op       = context.children[1] as ITerminalNode;

            switch (op.Symbol.Type)
            {
            case CalculatorParser.AddAss:
                newValue = BinaryOperation(oldValue, newValue, CalculatorParser.Plus);
                break;

            case CalculatorParser.MinusAss:
                newValue = BinaryOperation(oldValue, newValue, CalculatorParser.Minus);
                break;

            case CalculatorParser.MultiplyAss:
                newValue = BinaryOperation(oldValue, newValue, CalculatorParser.Multiply);
                break;

            case CalculatorParser.DivideAss:
                newValue = BinaryOperation(oldValue, newValue, CalculatorParser.Divide);
                break;
            }
            if (stack is Array array)
            {
                array[Convert.ToInt32(varName)] = newValue;
            }
            else
            {
                stack.Set(varName, newValue);
            }
            m_iterStack = m_stack;
            m_iterName  = "";
            return(newValue);
        }
Exemple #2
0
 /// <summary>
 /// Visit a parse tree produced by <see cref="CalculatorParser.assignment"/>.
 /// <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 VisitAssignment([NotNull] CalculatorParser.AssignmentContext context)
 {
     return(VisitChildren(context));
 }
 public override void ExitAssignment([NotNull] CalculatorParser.AssignmentContext context)
 {
     sym.Add(context.ID().GetText(), stack.Pop());
     //base.ExitAssignment(context);
 }