Package

Source Code of App

import tools.Expression;
import tools.dictionary.Collection;
import tools.exceptions.*;
import java.io.*;
import java.util.regex.*;
import java.util.*;

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]);
              }
        }
        else
          throw new ExceptionRPN("no such method");
      }
      catch (RPN_BadExpression ex) { bl.format("Exception RPN: bad expression\n"); }
      catch (RPN_DivBy0 ex) { bl.format("Exception RPN: div by 0\n"); }
      catch (RPN_EmptyStack ex) { bl.format("Exception RPN: empty stack\n"); }
      catch (RPN_SymbolNotFound ex) { bl.format("Exception RPN: symbol %s not found\n",ex.getMessage()); }
      catch (RPN_VarNotFound ex) { bl.format("Exception RPN: variable %s not found\n",ex.getMessage()); }
      catch (ExceptionRPN ex) { bl.format("Exception RPN: %s\n",ex.getMessage()); }
      finally { bl.print("> "); bl.flush(); }
    bl.println("bye");
  }
}
TOP

Related Classes of App

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.