src/main/java/com/acme/calculator/main/Calculator.java
/**
     * Visits the branches in the expression tree recursively until
     * reaching a leaf, performing relevant calculations as needed at each
     * node to calculate the final result.
     *
     * @param context AST of the expression to be calculated
     * @return BigDecimal with total of the calculations in the expression
     */
Visits the branches in the expression tree recursively until
reaching a leaf, performing relevant calculations as needed at each
node to calculate the final result.

@param context AST of the expression to be calculated
@return BigDecimal with total of the calculations in the expression
Visits the branches in the expression tree recursively until reaching a leaf performing relevant calculations as needed at each node to calculate the final result . @param context AST of the expression to be calculated @return BigDecimal with total of the calculations in the expression
private BigDecimal visit(final BasicCalculatorParser.ExprContext context) {
        if (context.INTEGER() != null) {
            String text = context.INTEGER().getText();
            logger.trace("visited INTEGER with text: '{}'", text);
            return new BigDecimal(text, mathContext);
        } else if (context.assignment() != null) {
            final String variableName = context.VARIABLE().getText();
            final BigDecimal value = visit(context.expr(0));
            variablesMap.put(variableName, value);
            logger.trace("Stored var value {} -> {}", variableName, value);
            return visit(context.expr(1));
        } else if (context.VARIABLE() != null) {
            final String variableName = context.VARIABLE().getText();
            final BigDecimal value = variablesMap.get(variableName);
            if (value == null) {
                throw new IllegalArgumentException(
                        String.format("Syntax error: invalid use of function or variable: '%s'", variableName));
            }
            logger.trace("Retrieved var value {} -> {}", variableName, value);
            return value;
        } else if (context.function().ADD() != null) {
            logger.trace("ADD");
            return visit(context.expr(0)).add(visit(context.expr(1)), mathContext);
        } else if (context.function().SUBTRACT() != null) {
            logger.trace("SUBTRACT");
            return visit(context.expr(0)).subtract(visit(context.expr(1)), mathContext);
        } else if (context.function().MULTIPLY() != null) {
            logger.trace("MULTIPLY");
            return visit(context.expr(0)).multiply(visit(context.expr(1)), mathContext);
        } else if (context.function().DIVIDE() != null) {
            logger.trace("DIVIDE");
            return visit(context.expr(0)).divide(visit(context.expr(1)), mathContext);
        } else {
            throw new IllegalStateException();
        }
    }
private BigDecimal visit ( final BasicCalculatorParser . ExprContext context ) { if ( context . INTEGER ( ) != null ) { String text = context . INTEGER ( ) . getText ( ) ; logger . trace ( "visited INTEGER with text: '{}'" , text ) ; return new BigDecimal ( text , mathContext ) ; } else if ( context . assignment ( ) != null ) { final String variableName = context . VARIABLE ( ) . getText ( ) ; final BigDecimal value = visit ( context . expr ( 0 ) ) ; variablesMap . put ( variableName , value ) ; logger . trace ( "Stored var value {} -> {}" , variableName , value ) ; return visit ( context . expr ( 1 ) ) ; } else if ( context . VARIABLE ( ) != null ) { final String variableName = context . VARIABLE ( ) . getText ( ) ; final BigDecimal value = variablesMap . get ( variableName ) ; if ( value == null ) { throw new IllegalArgumentException ( String . format ( "Syntax error: invalid use of function or variable: '%s'" , variableName ) ) ; } logger . trace ( "Retrieved var value {} -> {}" , variableName , value ) ; return value ; } else if ( context . function ( ) . ADD ( ) != null ) { logger . trace ( "ADD" ) ; return visit ( context . expr ( 0 ) ) . add ( visit ( context . expr ( 1 ) ) , mathContext ) ; } else if ( context . function ( ) . SUBTRACT ( ) != null ) { logger . trace ( "SUBTRACT" ) ; return visit ( context . expr ( 0 ) ) . subtract ( visit ( context . expr ( 1 ) ) , mathContext ) ; } else if ( context . function ( ) . MULTIPLY ( ) != null ) { logger . trace ( "MULTIPLY" ) ; return visit ( context . expr ( 0 ) ) . multiply ( visit ( context . expr ( 1 ) ) , mathContext ) ; } else if ( context . function ( ) . DIVIDE ( ) != null ) { logger . trace ( "DIVIDE" ) ; return visit ( context . expr ( 0 ) ) . divide ( visit ( context . expr ( 1 ) ) , mathContext ) ; } else { throw new IllegalStateException ( ) ; } }
Visits the branches in the expression tree recursively until
 reaching a leaf, performing relevant calculations as needed at each
 node to calculate the final result.
Visits the branches in the expression tree recursively until reaching a leaf performing relevant calculations as needed at each node to calculate the final result .