/* Copyright 2011 Toby D. Rule
This file is part of CompPad, an OpenOffice extension to provide live
mathematical and engineering calculations within a Writer document.
CompPad is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
CompPad is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with CompPad. If not, see <http://www.gnu.org/licenses/>.
*/
package com.CompPad.shell;
//import com.CompPad.model.Expression;
import com.CompPad.model.Expression2;
import com.CompPad.model.Listener;
import com.CompPad.model.Utility;
import com.CompPad.model.Evaluator;
/**
*
* @author trule
*/
public class Shell {
private Listener outputListener = new OutputListener();
private ErrorListener errorListener = new ErrorListener();
private Expression2 shellExpression;
private Object shellOutputObj;
private Evaluator evaluator;
public static void main(String args[]) throws Exception{
Shell shell = new Shell();
String fs[] = {"x := 1","x ="};
shell.evaluate(fs);
}
public Shell() throws Exception {
evaluator = new Evaluator();
/* Create one expression which will be used to evaluate formulas */
shellExpression = new Expression2("", outputListener);
shellExpression.addListener(errorListener);
}
public Object evaluate(String argFormulaStr) throws Exception{
/* No output unless it's an output expression */
shellOutputObj = null;
shellExpression=new Expression2(argFormulaStr,outputListener,null);
shellExpression.addListener(errorListener);
System.out.println(argFormulaStr);
shellExpression.evaluate(evaluator);
return shellOutputObj;
}
public Object evaluate(String expressions[]) throws Exception{
Object o;
for (String e:expressions){
o = this.evaluate(e);
if (Exception.class.isInstance(o)){
return o;
}
}
return null;
}
private class OutputListener implements Listener{
public void setValue(Object arg) throws Exception {
if (arg!=null){
shellOutputObj = arg;
System.out.println(" RESULT: "+evaluator.outputString(shellOutputObj));
}
}
public Object getValue() {
return shellOutputObj;
}
public void refresh() {
throw new UnsupportedOperationException("Not supported yet.");
}
}
private class ErrorListener implements Listener{
public void setValue(Object arg) throws Exception {
if (arg!=null){
if (Exception.class.isInstance(arg)){
System.out.println(" ERROR: "+((Exception)arg).getMessage());
shellOutputObj = arg;
}
else{
System.out.println(" ERROR "+arg.getClass().getName() +": "+arg.toString());
shellOutputObj = arg;
}
}
}
public Object getValue() {
throw new UnsupportedOperationException("Not supported yet.");
}
public void refresh() {
throw new UnsupportedOperationException("Not supported yet.");
}
}
}