public class App {
public static void main (String[] args) throws IOException {
BufferedReader we = new BufferedReader(new InputStreamReader(System.in));
PrintWriter wy = new PrintWriter(new OutputStreamWriter(System.out),true);
PrintWriter bl = new PrintWriter(new OutputStreamWriter(System.err),true);
Collection varCollection = new Collection();
bl.println("hello");
bl.print("> ");
bl.flush();
for (String instr=we.readLine(); instr!=null; instr=we.readLine())
try {
instr = instr.trim();
if (instr.matches("^\\s*calc\\s+[\\x00-\\xff]*$") || instr.equals("calc")) {
Pattern pattern = Pattern.compile("^\\s*calc\\s+([\\x00-\\xff]*)$");
Matcher matcher = pattern.matcher(instr);
if (matcher.find()) {
Expression e = new Expression(varCollection,matcher.group(1));
wy.format("%s\n",e.calculate());
varCollection=e.getVarCollection();
}
else
throw new ExceptionRPN("calc needs expression");
}
else if (instr.matches("^\\s*clear\\s+[\\x00-\\xff]*$") || instr.equals("clear")) {
String[] token = instr.split(" ");
if (token.length==1)
for (String s : varCollection.clear())
wy.format("remove %s\n",s);
else
for (int i=1; i<token.length; ++i)
try {
varCollection.delete(token[i]);
wy.format("remove %s\n",token[i]);
}
catch (Exception ex) {
throw new RPN_VarNotFound(token[i]);
}