package net.codenest.antlr.calculator;

import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.*;

public class Calculator {
	public static void main(String[] args) throws Exception {
		ANTLRInputStream input = new ANTLRInputStream("1 + 2 - 6 * 3");
		CalculatorLexer lexer = new CalculatorLexer(input);
		CommonTokenStream tokens = new CommonTokenStream(lexer);
		CalculatorParser parser = new CalculatorParser(tokens);
		ParseTree tree = parser.input();

		CalculatorVisitorImpl visitor = new CalculatorVisitorImpl();
		Double result = visitor.visit(tree);
		System.out.println("Result: " + result);
	}
}

@@ -35,16 +35,45 @@
 tor;%0D%0A%0D%0A
+import java.util.Scanner;%0D%0A%0D%0A
 import o
@@ -92,17 +92,32 @@
 runtime.
-*
+ANTLRInputStream
 ;%0D%0Aimpor
@@ -143,14 +143,70 @@
 ime.
-tree.*
+CommonTokenStream;%0D%0Aimport org.antlr.v4.runtime.tree.ParseTree
 ;%0D%0A%0D
@@ -299,73 +299,270 @@
 %0D%0A%09%09
-ANTLRInputStream input = new ANTLRInputStream(%221 + 2 - 6 * 3%22);%0D%0A
+try (Scanner sc = new Scanner(System.in)) %7B%0D%0A%09%09%09while (true) %7B%0D%0A%09%09%09%09String input = sc.nextLine();%0D%0A%09%09%09%09%0D%0A%09%09%09%09if (input.equals(%22q%22)) %7B%0D%0A%09%09%09%09%09System.exit(0);%0D%0A%09%09%09%09%7D else if (input.trim().equals(%22%22) %7C%7C input.trim().equals(%22%5Cn%22)) %7B%0D%0A%09%09%09%09%09continue;%0D%0A%09%09%09%09%7D else %7B%0D%0A%09%09%09
 %09%09Ca
@@ -607,17 +607,42 @@
 xer(
+new ANTLRInputStream(
 input)
+)
 ;%0D%0A
+%09%09%09
 %09%09Co
@@ -697,16 +697,19 @@
 exer);%0D%0A
+%09%09%09
 %09%09Calcul
@@ -759,16 +759,21 @@
 kens);%0D%0A
+%0D%0A%09%09%09
 %09%09ParseT
@@ -800,18 +800,19 @@
 put();%0D%0A
-%0D%0A
+%09%09%09
 %09%09Calcul
@@ -873,88 +873,69 @@
 %0D%0A%09%09
-Double result = visitor.visit(tree);%0D%0A%09%09System.out.println(%22Result: %22 + result);
+%09%09%09System.out.println(visitor.visit(tree));%0D%0A%09%09%09%09%7D%0D%0A%09%09%09%7D%0D%0A%09%09%7D
 %0D%0A%09%7D

update the console input