/* Copyright (C) 2014 A.Vontobel
*
* This program 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 2
* of the License, or (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The license is in LICENCE-GPL.txt.
* If not, see http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
*/
package net.sf.drawj2d.cmd;
import org.hecl.*;
/**
* ExprCmd implements the "expr" command, which evaluates
* a mathematical expression.
*/
public class ExprCmd implements Command {
API api;
boolean debug = false;
public ExprCmd (API api) {
this.api = api;
}
@Override
public Thing cmdCode(Interp interp, Thing[] argv) throws HeclException {
assert argv.length > 0;
if (debug) System.out.println(argv[0].toString());
StringBuilder sb = new StringBuilder();
for (int i = 1; i < argv.length; i++) {
if (i > 1) sb.append(" ");
sb.append(StringThing.get(argv[i]));
}
String expression = sb.toString();
Thing th;
if (argv.length > 1) {
double val = api.expr(expression);
th = DoubleThing.create(val);
} else {
throw new HeclException ("Illegal nb of arguments.");
}
return th;
}
/**
* Test the expr command.
* @param args
*/
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
boolean first = true;
for (String arg : args) {
if (first) first = false;
else sb.append(" ");
sb.append(arg);
}
String expression = sb.toString();
if (expression.equals("")) expression = "25 + 3.0**2 + 50";
System.out.println(expression);
int imgtype = java.awt.image.BufferedImage.TYPE_INT_ARGB;
java.awt.image.BufferedImage bi = new java.awt.image.BufferedImage(100, 100, imgtype);
API a = new API(bi.createGraphics());
double val = a.expr(expression);
System.out.println(val);
}
}
Ads help maintain this website.