package org.lvcp.eepperly.main;

import org.lvcp.eepperly.exception.ExprTypeException;
import org.lvcp.eepperly.expr.Expr;
import org.lvcp.eepperly.interpreter.LispParser;
import org.lvcp.eepperly.interpreter.LispTokenizer;
import org.lvcp.eepperly.interpreter.ExprCompiler;
import org.lvcp.eepperly.simplify.AbstractSimplifier;
import org.lvcp.eepperly.simplify.DefaultSimplifier;

import java.util.Random;
import java.util.Scanner;

/**
 * Created by eepperly16 on 12/14/15.
 */
public class Main {
	public static void main(String[] args) {
		Expr expr = Expr.ZERO;
		Random rand = new Random();
		AbstractSimplifier simp = new DefaultSimplifier();
		Scanner scan = new Scanner(System.in);
		while (true) {
			String line = scan.nextLine();
			String strippedLine = line.replaceAll("\\s+","").toLowerCase();
			switch (strippedLine){
				case "simplify":
					try {
						expr = expr.simplify(simp);
						System.out.println(expr);
					} catch (ExprTypeException ex){
						ex.printStackTrace();
					}
					break;
				case "zero":
					try {
						System.out.println(expr.findZero(3 * rand.nextDouble() + 0.05));
					} catch (Exception ex){
						ex.printStackTrace();
					}
					break;
				case "differentiate":
					try{
						expr = expr.differentiate(Expr.getOneElementInSet(expr.getVariables())).simplify(simp);
						System.out.println(expr);
					} catch (Exception ex){
						ex.printStackTrace();
					}
					break;
				default:
					try {
						expr = ExprCompiler.INSTANCE.compile(LispParser.INSTANCE.parse(LispTokenizer.INSTANCE.tokenize(line)));
						System.out.println(expr);
					} catch (Exception ex){
						ex.printStackTrace();
					}
					break;
			}

		}
	}
}

@@ -749,17 +749,16 @@
 ring
- stripped
+%5B%5D split
 Line
@@ -769,42 +769,34 @@
 ine.
-replaceAll(%22%5C%5Cs+%22,%22%22).toLowerCase(
+toLowerCase().split(%22%5C%5Cs+%22
 );%0A%09
@@ -810,19 +810,19 @@
 h (s
-tripped
+plit
 Line
+%5B0%5D
 )%7B%0A%09
@@ -1026,24 +1026,56 @@
 %0A%09%09%09%09%09try %7B%0A
+%09%09%09%09%09%09if(splitLine.length==1)%7B%0A%09
 %09%09%09%09%09%09System
@@ -1129,24 +1129,143 @@
 ) + 0.05));%0A
+%09%09%09%09%09%09%7Delse%7B%0A%09%09%09%09%09%09%09double n = Double.parseDouble(splitLine%5B1%5D);%0A%09%09%09%09%09%09%09System.out.println(expr.findZero(n));%0A%09%09%09%09%09%09%7D%0A%0A
 %09%09%09%09%09%7D catch
@@ -1364,16 +1364,48 @@
 %09%09%09try%7B%0A
+%09%09%09%09%09%09if(splitLine.length==1)%7B%0A%09
 %09%09%09%09%09%09ex
@@ -1482,32 +1482,241 @@
 simplify(simp);%0A
+%09%09%09%09%09%09%7Delse%7B%0A%09%09%09%09%09%09%09int n = Integer.parseInt(splitLine%5B1%5D, 10);%0A%09%09%09%09%09%09%09while(n%3E0)%7B%0A%09%09%09%09%09%09%09%09expr = expr.differentiate(Expr.getOneElementInSet(expr.getVariables())).simplify(simp);%0A%09%09%09%09%09%09%09%09n--;%0A%09%09%09%09%09%09%09%7D%0A%09%09%09%09%09%09%7D%0A
 %09%09%09%09%09%09System.out

Create simple user interface