/************************************** * Folder: expr Document: Expr.java **************************************/ // Mathematical expressions. // Copyright 1996 by Darius Bacon; see the file COPYING. // 14May96: added constant folding // 6June02: changes made by David Protas indicated by /*DP*/ package expr; /** * A mathematical expression, built out of literal numbers, variables, * arithmetic operators, and elementary functions. The operator names * are from java.lang.Math. */ public abstract class Expr { /** @return the value given the current variable values */ public abstract double value (); /** Binary operator. */ public static final int ADD = 0; /** Binary operator. */ public static final int SUB = 1; /** Binary operator. */ public static final int MUL = 2; /** Binary operator. */ public static final int DIV = 3; /** Binary operator. */ public static final int POW = 4; /** Unary operator. */ public static final int ABS = 100; /** Unary operator. */ public static final int ACOS = 101; /** Unary operator. */ public static final int ASIN = 102; /** Unary operator. */ public static final int ATAN = 103; /** Unary operator. */ public static final int CEIL = 104; /** Unary operator. */ public static final int COS = 105; /** Unary operator. */ public static final int EXP = 106; /** Unary operator. */ public static final int FLOOR = 107; /** Unary operator. */ public static final int LN = 114; /*DP*/ /** Unary operator. */ public static final int LOG = 108; /** Unary minus operator. */ public static final int NEG = 109; /** Unary operator. */ public static final int ROUND = 110; /** Unary operator. */ public static final int SIN = 111; /** Unary operator. */ public static final int SQRT = 112; /** Unary operator. */ public static final int TAN = 113; public static Expr make_literal (double v) { return new Literal (v); } public static Expr make_var_ref (Variable var) { return new Var_ref (var); } /** * @param rator unary operator * @param rand operand */ public static Expr make_app1 (int rator, Expr rand) { Expr app = new App1 (rator, rand); return rand instanceof Literal ? new Literal (app.value ()) : app; } /** * @param rator binary operator * @param rand0 left operand * @param rand1 right operand */ public static Expr make_app2 (int rator, Expr rand0, Expr rand1) { Expr app = new App2 (rator, rand0, rand1); return rand0 instanceof Literal && rand1 instanceof Literal ? new Literal (app.value ()) : app; } }