package ee.ioc.phon.android.arvutaja.command;

import java.util.regex.Pattern;

import ee.ioc.phon.android.arvutaja.R;

import android.content.Intent;

/**
 * <p>Examples of supported expressions:</p>
 * <ul>
 * <li>( 12 + - 34 ) * 123</li>
 * </ul>
 */
public class Expr extends DefaultCommand {

	public static final Pattern p1 = Pattern.compile("^[0-9.]+$");
	public static final Pattern p2 = Pattern.compile("^\\(.*\\)$");
	// Tolerate anything that is composed of digits, operator symbols and brackets
	public static final Pattern p3 = Pattern.compile("^[0-9().+*/ -]+$");

	public Expr(String command) {
		super(command);
	}

	public int getMessage() {
		return R.string.msgActionViewWolframAlpha;
	}

	@Override
	public Intent getIntent() throws CommandParseException {
		return getActionView("http://www.wolframalpha.com/input/?i=", getCommand());
	}

	public String getOut() {
		MathEval math = new MathEval();
		// We remove all the space characters otherwise MathEval's tokenizer can fail
		return Double.toString(math.evaluate(getCommand().replaceAll("\\s+", "")));
	}

	public static boolean isCommand(String command) {
		return p1.matcher(command).matches() || p2.matcher(command).matches() || p3.matcher(command).matches();
	}

}
@@ -234,16 +234,37 @@
 23%3C/li%3E%0A
+ * %3Cli%3EPI %5E 0.1%3C/li%3E%0A
  * %3C/ul%3E
@@ -264,16 +264,16 @@
 * %3C/ul%3E%0A
-
  */%0Apubl
@@ -316,137 +316,8 @@
  %7B%0A%0A
-%09public static final Pattern p1 = Pattern.compile(%22%5E%5B0-9.%5D+$%22);%0A%09public static final Pattern p2 = Pattern.compile(%22%5E%5C%5C(.*%5C%5C)$%22);%0A
 %09// 
@@ -421,18 +421,28 @@
 Pattern 
-p3
+PATTERN_EXPR
  = Patte
@@ -461,16 +461,19 @@
 %5E%5B0-9().
+%5C%5C%5E
 +*/ -%5D+$
@@ -1044,10 +1044,20 @@
 urn 
-p1
+PATTERN_EXPR
 .mat
@@ -1072,74 +1072,27 @@
 mand
-).matches() %7C%7C p2.matcher(command).matches() %7C%7C p3.matcher(command
+.replace(%22PI%22, %220%22)
 ).ma

More straightforward arith. expression detection