Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1 | /* |
Hans Boehm | e457912 | 2015-05-26 17:52:32 -0700 | [diff] [blame] | 2 | * Copyright (C) 2015 The Android Open Source Project |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.calculator2; |
| 18 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 19 | |
| 20 | import com.hp.creals.CR; |
| 21 | import com.hp.creals.UnaryCRFunction; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 22 | |
| 23 | import android.content.Context; |
Hans Boehm | 8a4f81c | 2015-07-09 10:41:25 -0700 | [diff] [blame] | 24 | import android.text.SpannableString; |
| 25 | import android.text.SpannableStringBuilder; |
| 26 | import android.text.Spanned; |
| 27 | import android.text.style.TtsSpan; |
| 28 | import android.text.style.TtsSpan.TextBuilder; |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 29 | import android.util.Log; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 30 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 31 | import java.math.BigInteger; |
| 32 | import java.io.DataInput; |
| 33 | import java.io.DataOutput; |
| 34 | import java.io.IOException; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 35 | import java.util.ArrayList; |
| 36 | import java.util.HashMap; |
| 37 | import java.util.IdentityHashMap; |
| 38 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 39 | /** |
| 40 | * A mathematical expression represented as a sequence of "tokens". |
| 41 | * Many tokens are represented by button ids for the corresponding operator. |
| 42 | * A token may also represent the result of a previously evaluated expression. |
| 43 | * The add() method adds a token to the end of the expression. The delete method() removes one. |
| 44 | * Clear() deletes the entire expression contents. Eval() evaluates the expression, |
| 45 | * producing both a constructive real (CR), and possibly a BoundedRational result. |
| 46 | * Expressions are parsed only during evaluation; no explicit parse tree is maintained. |
| 47 | * |
| 48 | * The write() method is used to save the current expression. Note that CR provides no |
| 49 | * serialization facility. Thus we save all previously computed values by writing out the |
| 50 | * expression that was used to compute them, and reevaluate on input. |
| 51 | */ |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 52 | class CalculatorExpr { |
| 53 | private ArrayList<Token> mExpr; // The actual representation |
| 54 | // as a list of tokens. Constant |
| 55 | // tokens are always nonempty. |
| 56 | |
| 57 | private static enum TokenKind { CONSTANT, OPERATOR, PRE_EVAL }; |
| 58 | private static TokenKind[] tokenKindValues = TokenKind.values(); |
| 59 | private final static BigInteger BIG_MILLION = BigInteger.valueOf(1000000); |
| 60 | private final static BigInteger BIG_BILLION = BigInteger.valueOf(1000000000); |
| 61 | |
| 62 | private static abstract class Token { |
| 63 | abstract TokenKind kind(); |
Hans Boehm | 8a4f81c | 2015-07-09 10:41:25 -0700 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * Write kind as Byte followed by data needed by subclass constructor. |
| 67 | */ |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 68 | abstract void write(DataOutput out) throws IOException; |
Hans Boehm | 8a4f81c | 2015-07-09 10:41:25 -0700 | [diff] [blame] | 69 | |
| 70 | /** |
| 71 | * Return a textual representation of the token. |
| 72 | * The result is suitable for either display as part od the formula or TalkBack use. |
| 73 | * It may be a SpannableString that includes added TalkBack information. |
| 74 | * @param context context used for converting button ids to strings |
| 75 | */ |
| 76 | abstract CharSequence toCharSequence(Context context); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 79 | /** |
| 80 | * Representation of an operator token |
| 81 | */ |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 82 | private static class Operator extends Token { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 83 | public final int id; // We use the button resource id |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 84 | Operator(int resId) { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 85 | id = resId; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 86 | } |
| 87 | Operator(DataInput in) throws IOException { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 88 | id = in.readInt(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 89 | } |
| 90 | @Override |
| 91 | void write(DataOutput out) throws IOException { |
| 92 | out.writeByte(TokenKind.OPERATOR.ordinal()); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 93 | out.writeInt(id); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 94 | } |
| 95 | @Override |
Hans Boehm | 8a4f81c | 2015-07-09 10:41:25 -0700 | [diff] [blame] | 96 | public CharSequence toCharSequence(Context context) { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 97 | String desc = KeyMaps.toDescriptiveString(context, id); |
Hans Boehm | 8a4f81c | 2015-07-09 10:41:25 -0700 | [diff] [blame] | 98 | if (desc != null) { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 99 | SpannableString result = new SpannableString(KeyMaps.toString(context, id)); |
Hans Boehm | 8a4f81c | 2015-07-09 10:41:25 -0700 | [diff] [blame] | 100 | Object descSpan = new TtsSpan.TextBuilder(desc).build(); |
| 101 | result.setSpan(descSpan, 0, result.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
| 102 | return result; |
| 103 | } else { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 104 | return KeyMaps.toString(context, id); |
Hans Boehm | 8a4f81c | 2015-07-09 10:41:25 -0700 | [diff] [blame] | 105 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 106 | } |
| 107 | @Override |
| 108 | TokenKind kind() { return TokenKind.OPERATOR; } |
| 109 | } |
| 110 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 111 | /** |
| 112 | * Representation of a (possibly incomplete) numerical constant. |
| 113 | * Supports addition and removal of trailing characters; hence mutable. |
| 114 | */ |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 115 | private static class Constant extends Token implements Cloneable { |
| 116 | private boolean mSawDecimal; |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 117 | private String mWhole; // String preceding decimal point. |
Hans Boehm | 0b9806f | 2015-06-29 16:07:15 -0700 | [diff] [blame] | 118 | private String mFraction; // String after decimal point. |
| 119 | private int mExponent; // Explicit exponent, only generated through addExponent. |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 120 | |
| 121 | Constant() { |
| 122 | mWhole = ""; |
| 123 | mFraction = ""; |
Hans Boehm | 0b9806f | 2015-06-29 16:07:15 -0700 | [diff] [blame] | 124 | mSawDecimal = false; |
| 125 | mExponent = 0; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | Constant(DataInput in) throws IOException { |
| 129 | mWhole = in.readUTF(); |
| 130 | mSawDecimal = in.readBoolean(); |
| 131 | mFraction = in.readUTF(); |
Hans Boehm | 0b9806f | 2015-06-29 16:07:15 -0700 | [diff] [blame] | 132 | mExponent = in.readInt(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | @Override |
| 136 | void write(DataOutput out) throws IOException { |
| 137 | out.writeByte(TokenKind.CONSTANT.ordinal()); |
| 138 | out.writeUTF(mWhole); |
| 139 | out.writeBoolean(mSawDecimal); |
| 140 | out.writeUTF(mFraction); |
Hans Boehm | 0b9806f | 2015-06-29 16:07:15 -0700 | [diff] [blame] | 141 | out.writeInt(mExponent); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | // Given a button press, append corresponding digit. |
| 145 | // We assume id is a digit or decimal point. |
| 146 | // Just return false if this was the second (or later) decimal point |
| 147 | // in this constant. |
Hans Boehm | 0b9806f | 2015-06-29 16:07:15 -0700 | [diff] [blame] | 148 | // Assumes that this constant does not have an exponent. |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 149 | public boolean add(int id) { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 150 | if (id == R.id.dec_point) { |
Hans Boehm | 0b9806f | 2015-06-29 16:07:15 -0700 | [diff] [blame] | 151 | if (mSawDecimal || mExponent != 0) return false; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 152 | mSawDecimal = true; |
| 153 | return true; |
| 154 | } |
| 155 | int val = KeyMaps.digVal(id); |
Hans Boehm | 0b9806f | 2015-06-29 16:07:15 -0700 | [diff] [blame] | 156 | if (mExponent != 0) { |
| 157 | if (Math.abs(mExponent) <= 10000) { |
| 158 | if (mExponent > 0) { |
| 159 | mExponent = 10 * mExponent + val; |
| 160 | } else { |
| 161 | mExponent = 10 * mExponent - val; |
| 162 | } |
| 163 | return true; |
| 164 | } else { // Too large; refuse |
| 165 | return false; |
| 166 | } |
| 167 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 168 | if (mSawDecimal) { |
| 169 | mFraction += val; |
| 170 | } else { |
| 171 | mWhole += val; |
| 172 | } |
| 173 | return true; |
| 174 | } |
| 175 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 176 | public void addExponent(int exp) { |
Hans Boehm | 0b9806f | 2015-06-29 16:07:15 -0700 | [diff] [blame] | 177 | // Note that adding a 0 exponent is a no-op. That's OK. |
| 178 | mExponent = exp; |
| 179 | } |
| 180 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 181 | /** |
| 182 | * Undo the last add or remove last exponent digit. |
| 183 | * Assumes the constant is nonempty. |
| 184 | */ |
| 185 | public void delete() { |
Hans Boehm | 0b9806f | 2015-06-29 16:07:15 -0700 | [diff] [blame] | 186 | if (mExponent != 0) { |
| 187 | mExponent /= 10; |
| 188 | // Once zero, it can only be added back with addExponent. |
| 189 | } else if (!mFraction.isEmpty()) { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 190 | mFraction = mFraction.substring(0, mFraction.length() - 1); |
| 191 | } else if (mSawDecimal) { |
| 192 | mSawDecimal = false; |
| 193 | } else { |
| 194 | mWhole = mWhole.substring(0, mWhole.length() - 1); |
| 195 | } |
| 196 | } |
| 197 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 198 | public boolean isEmpty() { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 199 | return (mSawDecimal == false && mWhole.isEmpty()); |
| 200 | } |
| 201 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 202 | /** |
| 203 | * Produce human-readable string representation of constant, as typed. |
| 204 | * Result is internationalized. |
| 205 | */ |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 206 | @Override |
| 207 | public String toString() { |
| 208 | String result = mWhole; |
| 209 | if (mSawDecimal) { |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 210 | result += '.'; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 211 | result += mFraction; |
| 212 | } |
Hans Boehm | 0b9806f | 2015-06-29 16:07:15 -0700 | [diff] [blame] | 213 | if (mExponent != 0) { |
| 214 | result += "E" + mExponent; |
| 215 | } |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 216 | return KeyMaps.translateResult(result); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 219 | /** |
Hans Boehm | fa5203c | 2015-08-17 16:14:52 -0700 | [diff] [blame] | 220 | * Return BoundedRational representation of constant, if well-formed. |
| 221 | * Result is never null. |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 222 | */ |
Hans Boehm | fa5203c | 2015-08-17 16:14:52 -0700 | [diff] [blame] | 223 | public BoundedRational toRational() throws SyntaxException { |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 224 | String whole = mWhole; |
Hans Boehm | fa5203c | 2015-08-17 16:14:52 -0700 | [diff] [blame] | 225 | if (whole.isEmpty()) { |
| 226 | if (mFraction.isEmpty()) { |
| 227 | // Decimal point without digits. |
| 228 | throw new SyntaxException(); |
| 229 | } else { |
| 230 | whole = "0"; |
| 231 | } |
| 232 | } |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 233 | BigInteger num = new BigInteger(whole + mFraction); |
Hans Boehm | 682ff5e | 2015-03-09 14:40:25 -0700 | [diff] [blame] | 234 | BigInteger den = BigInteger.TEN.pow(mFraction.length()); |
Hans Boehm | 0b9806f | 2015-06-29 16:07:15 -0700 | [diff] [blame] | 235 | if (mExponent > 0) { |
| 236 | num = num.multiply(BigInteger.TEN.pow(mExponent)); |
| 237 | } |
| 238 | if (mExponent < 0) { |
| 239 | den = den.multiply(BigInteger.TEN.pow(-mExponent)); |
| 240 | } |
Hans Boehm | 682ff5e | 2015-03-09 14:40:25 -0700 | [diff] [blame] | 241 | return new BoundedRational(num, den); |
| 242 | } |
| 243 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 244 | @Override |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 245 | public CharSequence toCharSequence(Context context) { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 246 | return toString(); |
| 247 | } |
| 248 | |
| 249 | @Override |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 250 | public TokenKind kind() { |
| 251 | return TokenKind.CONSTANT; |
| 252 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 253 | |
| 254 | // Override clone to make it public |
| 255 | @Override |
| 256 | public Object clone() { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 257 | Constant result = new Constant(); |
| 258 | result.mWhole = mWhole; |
| 259 | result.mFraction = mFraction; |
| 260 | result.mSawDecimal = mSawDecimal; |
| 261 | result.mExponent = mExponent; |
| 262 | return result; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 266 | // Hash maps used to detect duplicate subexpressions when we write out CalculatorExprs and |
| 267 | // read them back in. |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 268 | private static final ThreadLocal<IdentityHashMap<CR,Integer>>outMap = |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 269 | new ThreadLocal<IdentityHashMap<CR,Integer>>(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 270 | // Maps expressions to indices on output |
| 271 | private static final ThreadLocal<HashMap<Integer,PreEval>>inMap = |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 272 | new ThreadLocal<HashMap<Integer,PreEval>>(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 273 | // Maps expressions to indices on output |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 274 | private static final ThreadLocal<Integer> exprIndex = new ThreadLocal<Integer>(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 275 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 276 | /** |
| 277 | * Prepare for expression output. |
| 278 | * Initializes map that will lbe used to avoid duplicating shared subexpressions. |
| 279 | * This avoids a potential exponential blow-up in the expression size. |
| 280 | */ |
| 281 | public static void initExprOutput() { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 282 | outMap.set(new IdentityHashMap<CR,Integer>()); |
| 283 | exprIndex.set(Integer.valueOf(0)); |
| 284 | } |
| 285 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 286 | /** |
| 287 | * Prepare for expression input. |
| 288 | * Initializes map that will be used to reconstruct shared subexpressions. |
| 289 | */ |
| 290 | public static void initExprInput() { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 291 | inMap.set(new HashMap<Integer,PreEval>()); |
| 292 | } |
| 293 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 294 | /** |
| 295 | * The "token" class for previously evaluated subexpressions. |
| 296 | * We treat previously evaluated subexpressions as tokens. These are inserted when we either |
| 297 | * continue an expression after evaluating some of it, or copy an expression and paste it back |
| 298 | * in. |
| 299 | * The representation includes both CR and possibly BoundedRational values. In order to |
| 300 | * support saving and restoring, we also include the underlying expression itself, and the |
| 301 | * context (currently just degree mode) used to evaluate it. The short string representation |
| 302 | * is also stored in order to avoid potentially expensive recomputation in the UI thread. |
| 303 | */ |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 304 | private static class PreEval extends Token { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 305 | public final CR value; |
| 306 | public final BoundedRational ratValue; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 307 | private final CalculatorExpr mExpr; |
| 308 | private final EvalContext mContext; |
Hans Boehm | 50ed320 | 2015-06-09 14:35:49 -0700 | [diff] [blame] | 309 | private final String mShortRep; // Not internationalized. |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 310 | PreEval(CR val, BoundedRational ratVal, CalculatorExpr expr, |
| 311 | EvalContext ec, String shortRep) { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 312 | value = val; |
| 313 | ratValue = ratVal; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 314 | mExpr = expr; |
| 315 | mContext = ec; |
| 316 | mShortRep = shortRep; |
| 317 | } |
| 318 | // In writing out PreEvals, we are careful to avoid writing |
| 319 | // out duplicates. We assume that two expressions are |
Hans Boehm | 8afd0f8 | 2015-07-30 17:00:33 -0700 | [diff] [blame] | 320 | // duplicates if they have the same CR value. This avoids a |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 321 | // potential exponential blow up in certain off cases and |
| 322 | // redundant evaluation after reading them back in. |
| 323 | // The parameter hash map maps expressions we've seen |
| 324 | // before to their index. |
| 325 | @Override |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 326 | public void write(DataOutput out) throws IOException { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 327 | out.writeByte(TokenKind.PRE_EVAL.ordinal()); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 328 | Integer index = outMap.get().get(value); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 329 | if (index == null) { |
| 330 | int nextIndex = exprIndex.get() + 1; |
| 331 | exprIndex.set(nextIndex); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 332 | outMap.get().put(value, nextIndex); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 333 | out.writeInt(nextIndex); |
| 334 | mExpr.write(out); |
| 335 | mContext.write(out); |
| 336 | out.writeUTF(mShortRep); |
| 337 | } else { |
| 338 | // Just write out the index |
| 339 | out.writeInt(index); |
| 340 | } |
| 341 | } |
| 342 | PreEval(DataInput in) throws IOException { |
| 343 | int index = in.readInt(); |
| 344 | PreEval prev = inMap.get().get(index); |
| 345 | if (prev == null) { |
| 346 | mExpr = new CalculatorExpr(in); |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 347 | mContext = new EvalContext(in, mExpr.mExpr.size()); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 348 | // Recompute other fields We currently do this in the UI thread, but we only |
| 349 | // create PreEval expressions that were previously successfully evaluated, and |
| 350 | // thus don't diverge. We also only evaluate to a constructive real, which |
| 351 | // involves substantial work only in fairly contrived circumstances. |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 352 | // TODO: Deal better with slow evaluations. |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 353 | EvalRet res = null; |
| 354 | try { |
| 355 | res = mExpr.evalExpr(0, mContext); |
| 356 | } catch (SyntaxException e) { |
| 357 | // Should be impossible, since we only write out |
| 358 | // expressions that can be evaluated. |
| 359 | Log.e("Calculator", "Unexpected syntax exception" + e); |
| 360 | } |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 361 | value = res.val; |
| 362 | ratValue = res.ratVal; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 363 | mShortRep = in.readUTF(); |
| 364 | inMap.get().put(index, this); |
| 365 | } else { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 366 | value = prev.value; |
| 367 | ratValue = prev.ratValue; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 368 | mExpr = prev.mExpr; |
| 369 | mContext = prev.mContext; |
| 370 | mShortRep = prev.mShortRep; |
| 371 | } |
| 372 | } |
| 373 | @Override |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 374 | public CharSequence toCharSequence(Context context) { |
Hans Boehm | 50ed320 | 2015-06-09 14:35:49 -0700 | [diff] [blame] | 375 | return KeyMaps.translateResult(mShortRep); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 376 | } |
| 377 | @Override |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 378 | public TokenKind kind() { |
Hans Boehm | 187d3e9 | 2015-06-09 18:04:26 -0700 | [diff] [blame] | 379 | return TokenKind.PRE_EVAL; |
| 380 | } |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 381 | public boolean hasEllipsis() { |
Hans Boehm | 187d3e9 | 2015-06-09 18:04:26 -0700 | [diff] [blame] | 382 | return mShortRep.lastIndexOf(KeyMaps.ELLIPSIS) != -1; |
| 383 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 384 | } |
| 385 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 386 | /** |
| 387 | * Read token from in. |
| 388 | */ |
| 389 | public static Token newToken(DataInput in) throws IOException { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 390 | TokenKind kind = tokenKindValues[in.readByte()]; |
| 391 | switch(kind) { |
| 392 | case CONSTANT: |
| 393 | return new Constant(in); |
| 394 | case OPERATOR: |
| 395 | return new Operator(in); |
| 396 | case PRE_EVAL: |
| 397 | return new PreEval(in); |
| 398 | default: throw new IOException("Bad save file format"); |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | CalculatorExpr() { |
| 403 | mExpr = new ArrayList<Token>(); |
| 404 | } |
| 405 | |
| 406 | private CalculatorExpr(ArrayList<Token> expr) { |
| 407 | mExpr = expr; |
| 408 | } |
| 409 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 410 | /** |
| 411 | * Construct CalculatorExpr, by reading it from in. |
| 412 | */ |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 413 | CalculatorExpr(DataInput in) throws IOException { |
| 414 | mExpr = new ArrayList<Token>(); |
| 415 | int size = in.readInt(); |
| 416 | for (int i = 0; i < size; ++i) { |
| 417 | mExpr.add(newToken(in)); |
| 418 | } |
| 419 | } |
| 420 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 421 | /** |
| 422 | * Write this expression to out. |
| 423 | */ |
| 424 | public void write(DataOutput out) throws IOException { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 425 | int size = mExpr.size(); |
| 426 | out.writeInt(size); |
| 427 | for (int i = 0; i < size; ++i) { |
| 428 | mExpr.get(i).write(out); |
| 429 | } |
| 430 | } |
| 431 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 432 | /** |
| 433 | * Does this expression end with a numeric constant? |
| 434 | * As opposed to an operator or preevaluated expression. |
| 435 | */ |
Hans Boehm | 0b9806f | 2015-06-29 16:07:15 -0700 | [diff] [blame] | 436 | boolean hasTrailingConstant() { |
| 437 | int s = mExpr.size(); |
| 438 | if (s == 0) { |
| 439 | return false; |
| 440 | } |
| 441 | Token t = mExpr.get(s-1); |
| 442 | return t instanceof Constant; |
| 443 | } |
| 444 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 445 | /** |
| 446 | * Does this expression end with a binary operator? |
| 447 | */ |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 448 | private boolean hasTrailingBinary() { |
| 449 | int s = mExpr.size(); |
| 450 | if (s == 0) return false; |
| 451 | Token t = mExpr.get(s-1); |
| 452 | if (!(t instanceof Operator)) return false; |
| 453 | Operator o = (Operator)t; |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 454 | return (KeyMaps.isBinary(o.id)); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 455 | } |
| 456 | |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 457 | /** |
| 458 | * Append press of button with given id to expression. |
| 459 | * If the insertion would clearly result in a syntax error, either just return false |
| 460 | * and do nothing, or make an adjustment to avoid the problem. We do the latter only |
| 461 | * for unambiguous consecutive binary operators, in which case we delete the first |
| 462 | * operator. |
| 463 | */ |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 464 | boolean add(int id) { |
| 465 | int s = mExpr.size(); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 466 | final int d = KeyMaps.digVal(id); |
| 467 | final boolean binary = KeyMaps.isBinary(id); |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 468 | Token lastTok = s == 0 ? null : mExpr.get(s-1); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 469 | int lastOp = lastTok instanceof Operator ? ((Operator) lastTok).id : 0; |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 470 | // Quietly replace a trailing binary operator with another one, unless the second |
| 471 | // operator is minus, in which case we just allow it as a unary minus. |
| 472 | if (binary && !KeyMaps.isPrefix(id)) { |
| 473 | if (s == 0 || lastOp == R.id.lparen || KeyMaps.isFunc(lastOp) |
| 474 | || KeyMaps.isPrefix(lastOp) && lastOp != R.id.op_sub) { |
| 475 | return false; |
| 476 | } |
| 477 | while (hasTrailingBinary()) { |
| 478 | delete(); |
| 479 | } |
| 480 | // s invalid and not used below. |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 481 | } |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 482 | final boolean isConstPiece = (d != KeyMaps.NOT_DIGIT || id == R.id.dec_point); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 483 | if (isConstPiece) { |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 484 | // Since we treat juxtaposition as multiplication, a constant can appear anywhere. |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 485 | if (s == 0) { |
| 486 | mExpr.add(new Constant()); |
| 487 | s++; |
| 488 | } else { |
| 489 | Token last = mExpr.get(s-1); |
| 490 | if(!(last instanceof Constant)) { |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 491 | if (last instanceof PreEval) { |
| 492 | // Add explicit multiplication to avoid confusing display. |
| 493 | mExpr.add(new Operator(R.id.op_mul)); |
| 494 | s++; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 495 | } |
| 496 | mExpr.add(new Constant()); |
| 497 | s++; |
| 498 | } |
| 499 | } |
| 500 | return ((Constant)(mExpr.get(s-1))).add(id); |
| 501 | } else { |
| 502 | mExpr.add(new Operator(id)); |
| 503 | return true; |
| 504 | } |
| 505 | } |
| 506 | |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 507 | /** |
Hans Boehm | 0b9806f | 2015-06-29 16:07:15 -0700 | [diff] [blame] | 508 | * Add exponent to the constant at the end of the expression. |
| 509 | * Assumes there is a constant at the end of the expression. |
| 510 | */ |
| 511 | void addExponent(int exp) { |
| 512 | Token lastTok = mExpr.get(mExpr.size() - 1); |
| 513 | ((Constant) lastTok).addExponent(exp); |
| 514 | } |
| 515 | |
| 516 | /** |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 517 | * Remove trailing op_add and op_sub operators. |
| 518 | */ |
| 519 | void removeTrailingAdditiveOperators() { |
| 520 | while (true) { |
| 521 | int s = mExpr.size(); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 522 | if (s == 0) { |
| 523 | break; |
| 524 | } |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 525 | Token lastTok = mExpr.get(s-1); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 526 | if (!(lastTok instanceof Operator)) { |
| 527 | break; |
| 528 | } |
| 529 | int lastOp = ((Operator) lastTok).id; |
| 530 | if (lastOp != R.id.op_add && lastOp != R.id.op_sub) { |
| 531 | break; |
| 532 | } |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 533 | delete(); |
| 534 | } |
| 535 | } |
| 536 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 537 | /** |
| 538 | * Append the contents of the argument expression. |
| 539 | * It is assumed that the argument expression will not change, and thus its pieces can be |
| 540 | * reused directly. |
| 541 | */ |
| 542 | public void append(CalculatorExpr expr2) { |
Hans Boehm | fbcef70 | 2015-04-27 18:07:47 -0700 | [diff] [blame] | 543 | int s = mExpr.size(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 544 | int s2 = expr2.mExpr.size(); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 545 | // Check that we're not concatenating Constant or PreEval tokens, since the result would |
| 546 | // look like a single constant, with very mysterious results for the user. |
Hans Boehm | fbcef70 | 2015-04-27 18:07:47 -0700 | [diff] [blame] | 547 | if (s != 0 && s2 != 0) { |
| 548 | Token last = mExpr.get(s-1); |
| 549 | Token first = expr2.mExpr.get(0); |
| 550 | if (!(first instanceof Operator) && !(last instanceof Operator)) { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 551 | // Fudge it by adding an explicit multiplication. We would have interpreted it as |
| 552 | // such anyway, and this makes it recognizable to the user. |
Hans Boehm | fbcef70 | 2015-04-27 18:07:47 -0700 | [diff] [blame] | 553 | mExpr.add(new Operator(R.id.op_mul)); |
| 554 | } |
| 555 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 556 | for (int i = 0; i < s2; ++i) { |
| 557 | mExpr.add(expr2.mExpr.get(i)); |
| 558 | } |
| 559 | } |
| 560 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 561 | /** |
| 562 | * Undo the last key addition, if any. |
| 563 | * Or possibly remove a trailing exponent digit. |
| 564 | */ |
| 565 | public void delete() { |
| 566 | final int s = mExpr.size(); |
| 567 | if (s == 0) { |
| 568 | return; |
| 569 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 570 | Token last = mExpr.get(s-1); |
| 571 | if (last instanceof Constant) { |
| 572 | Constant c = (Constant)last; |
| 573 | c.delete(); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 574 | if (!c.isEmpty()) { |
| 575 | return; |
| 576 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 577 | } |
| 578 | mExpr.remove(s-1); |
| 579 | } |
| 580 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 581 | /** |
| 582 | * Remove all tokens from the expression. |
| 583 | */ |
| 584 | public void clear() { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 585 | mExpr.clear(); |
| 586 | } |
| 587 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 588 | public boolean isEmpty() { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 589 | return mExpr.isEmpty(); |
| 590 | } |
| 591 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 592 | /** |
| 593 | * Returns a logical deep copy of the CalculatorExpr. |
| 594 | * Operator and PreEval tokens are immutable, and thus aren't really copied. |
| 595 | */ |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 596 | public Object clone() { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 597 | CalculatorExpr result = new CalculatorExpr(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 598 | for (Token t: mExpr) { |
| 599 | if (t instanceof Constant) { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 600 | result.mExpr.add((Token)(((Constant)t).clone())); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 601 | } else { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 602 | result.mExpr.add(t); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 603 | } |
| 604 | } |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 605 | return result; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | // Am I just a constant? |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 609 | public boolean isConstant() { |
| 610 | if (mExpr.size() != 1) { |
| 611 | return false; |
| 612 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 613 | return mExpr.get(0) instanceof Constant; |
| 614 | } |
| 615 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 616 | /** |
| 617 | * Return a new expression consisting of a single token representing the current pre-evaluated |
| 618 | * expression. |
| 619 | * The caller supplies the value, degree mode, and short string representation, which must |
| 620 | * have been previously computed. Thus this is guaranteed to terminate reasonably quickly. |
| 621 | */ |
| 622 | public CalculatorExpr abbreviate(CR val, BoundedRational ratVal, |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 623 | boolean dm, String sr) { |
| 624 | CalculatorExpr result = new CalculatorExpr(); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 625 | Token t = new PreEval(val, ratVal, new CalculatorExpr((ArrayList<Token>) mExpr.clone()), |
| 626 | new EvalContext(dm, mExpr.size()), sr); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 627 | result.mExpr.add(t); |
| 628 | return result; |
| 629 | } |
| 630 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 631 | /** |
| 632 | * Internal evaluation functions return an EvalRet triple. |
| 633 | * We compute rational (BoundedRational) results when possible, both as a performance |
| 634 | * optimization, and to detect errors exactly when we can. |
| 635 | */ |
| 636 | private static class EvalRet { |
| 637 | public int pos; // Next position (expression index) to be parsed. |
| 638 | public final CR val; // Constructive Real result of evaluating subexpression. |
| 639 | public final BoundedRational ratVal; // Exact Rational value or null. |
Hans Boehm | 682ff5e | 2015-03-09 14:40:25 -0700 | [diff] [blame] | 640 | EvalRet(int p, CR v, BoundedRational r) { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 641 | pos = p; |
| 642 | val = v; |
| 643 | ratVal = r; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 644 | } |
| 645 | } |
| 646 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 647 | /** |
| 648 | * Internal evaluation functions take an EvalContext argument. |
| 649 | */ |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 650 | private static class EvalContext { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 651 | public final int mPrefixLength; // Length of prefix to evaluate. Not explicitly saved. |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 652 | public final boolean mDegreeMode; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 653 | // If we add any other kinds of evaluation modes, they go here. |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 654 | EvalContext(boolean degreeMode, int len) { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 655 | mDegreeMode = degreeMode; |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 656 | mPrefixLength = len; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 657 | } |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 658 | EvalContext(DataInput in, int len) throws IOException { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 659 | mDegreeMode = in.readBoolean(); |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 660 | mPrefixLength = len; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 661 | } |
| 662 | void write(DataOutput out) throws IOException { |
| 663 | out.writeBoolean(mDegreeMode); |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | private final CR RADIANS_PER_DEGREE = CR.PI.divide(CR.valueOf(180)); |
| 668 | |
| 669 | private final CR DEGREES_PER_RADIAN = CR.valueOf(180).divide(CR.PI); |
| 670 | |
| 671 | private CR toRadians(CR x, EvalContext ec) { |
| 672 | if (ec.mDegreeMode) { |
| 673 | return x.multiply(RADIANS_PER_DEGREE); |
| 674 | } else { |
| 675 | return x; |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | private CR fromRadians(CR x, EvalContext ec) { |
| 680 | if (ec.mDegreeMode) { |
| 681 | return x.multiply(DEGREES_PER_RADIAN); |
| 682 | } else { |
| 683 | return x; |
| 684 | } |
| 685 | } |
| 686 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 687 | // The following methods can all throw IndexOutOfBoundsException in the event of a syntax |
| 688 | // error. We expect that to be caught in eval below. |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 689 | |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 690 | private boolean isOperatorUnchecked(int i, int op) { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 691 | Token t = mExpr.get(i); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 692 | if (!(t instanceof Operator)) { |
| 693 | return false; |
| 694 | } |
| 695 | return ((Operator)(t)).id == op; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 696 | } |
| 697 | |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 698 | private boolean isOperator(int i, int op, EvalContext ec) { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 699 | if (i >= ec.mPrefixLength) { |
| 700 | return false; |
| 701 | } |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 702 | return isOperatorUnchecked(i, op); |
| 703 | } |
| 704 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 705 | public static class SyntaxException extends Exception { |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 706 | public SyntaxException() { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 707 | super(); |
| 708 | } |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 709 | public SyntaxException(String s) { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 710 | super(s); |
| 711 | } |
| 712 | } |
| 713 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 714 | // The following functions all evaluate some kind of expression starting at position i in |
| 715 | // mExpr in a specified evaluation context. They return both the expression value (as |
| 716 | // constructive real and, if applicable, as BoundedRational) and the position of the next token |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 717 | // that was not used as part of the evaluation. |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 718 | // This is essentially a simple recursive descent parser combined with expression evaluation. |
| 719 | |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 720 | private EvalRet evalUnary(int i, EvalContext ec) throws SyntaxException { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 721 | final Token t = mExpr.get(i); |
Hans Boehm | 0b9806f | 2015-06-29 16:07:15 -0700 | [diff] [blame] | 722 | BoundedRational ratVal; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 723 | if (t instanceof Constant) { |
| 724 | Constant c = (Constant)t; |
Hans Boehm | 0b9806f | 2015-06-29 16:07:15 -0700 | [diff] [blame] | 725 | ratVal = c.toRational(); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 726 | return new EvalRet(i+1, ratVal.CRValue(), ratVal); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 727 | } |
| 728 | if (t instanceof PreEval) { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 729 | final PreEval p = (PreEval)t; |
| 730 | return new EvalRet(i+1, p.value, p.ratValue); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 731 | } |
| 732 | EvalRet argVal; |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 733 | switch(((Operator)(t)).id) { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 734 | case R.id.const_pi: |
| 735 | return new EvalRet(i+1, CR.PI, null); |
| 736 | case R.id.const_e: |
Hans Boehm | 4db31b4 | 2015-05-31 12:19:05 -0700 | [diff] [blame] | 737 | return new EvalRet(i+1, REAL_E, null); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 738 | case R.id.op_sqrt: |
Hans Boehm | fbcef70 | 2015-04-27 18:07:47 -0700 | [diff] [blame] | 739 | // Seems to have highest precedence. |
| 740 | // Does not add implicit paren. |
| 741 | // Does seem to accept a leading minus. |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 742 | if (isOperator(i+1, R.id.op_sub, ec)) { |
Hans Boehm | fbcef70 | 2015-04-27 18:07:47 -0700 | [diff] [blame] | 743 | argVal = evalUnary(i+2, ec); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 744 | ratVal = BoundedRational.sqrt(BoundedRational.negate(argVal.ratVal)); |
| 745 | if (ratVal != null) { |
| 746 | break; |
| 747 | } |
| 748 | return new EvalRet(argVal.pos, |
| 749 | argVal.val.negate().sqrt(), null); |
Hans Boehm | fbcef70 | 2015-04-27 18:07:47 -0700 | [diff] [blame] | 750 | } else { |
| 751 | argVal = evalUnary(i+1, ec); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 752 | ratVal = BoundedRational.sqrt(argVal.ratVal); |
| 753 | if (ratVal != null) { |
| 754 | break; |
| 755 | } |
| 756 | return new EvalRet(argVal.pos, argVal.val.sqrt(), null); |
Hans Boehm | fbcef70 | 2015-04-27 18:07:47 -0700 | [diff] [blame] | 757 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 758 | case R.id.lparen: |
| 759 | argVal = evalExpr(i+1, ec); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 760 | if (isOperator(argVal.pos, R.id.rparen, ec)) { |
| 761 | argVal.pos++; |
| 762 | } |
| 763 | return new EvalRet(argVal.pos, argVal.val, argVal.ratVal); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 764 | case R.id.fun_sin: |
| 765 | argVal = evalExpr(i+1, ec); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 766 | if (isOperator(argVal.pos, R.id.rparen, ec)) { |
| 767 | argVal.pos++; |
| 768 | } |
| 769 | ratVal = ec.mDegreeMode ? BoundedRational.degreeSin(argVal.ratVal) |
| 770 | : BoundedRational.sin(argVal.ratVal); |
| 771 | if (ratVal != null) { |
| 772 | break; |
| 773 | } |
| 774 | return new EvalRet(argVal.pos, toRadians(argVal.val,ec).sin(), null); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 775 | case R.id.fun_cos: |
| 776 | argVal = evalExpr(i+1, ec); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 777 | if (isOperator(argVal.pos, R.id.rparen, ec)) { |
| 778 | argVal.pos++; |
| 779 | } |
| 780 | ratVal = ec.mDegreeMode ? BoundedRational.degreeCos(argVal.ratVal) |
| 781 | : BoundedRational.cos(argVal.ratVal); |
| 782 | if (ratVal != null) { |
| 783 | break; |
| 784 | } |
| 785 | return new EvalRet(argVal.pos, toRadians(argVal.val,ec).cos(), null); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 786 | case R.id.fun_tan: |
| 787 | argVal = evalExpr(i+1, ec); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 788 | if (isOperator(argVal.pos, R.id.rparen, ec)) { |
| 789 | argVal.pos++; |
| 790 | } |
| 791 | ratVal = ec.mDegreeMode ? BoundedRational.degreeTan(argVal.ratVal) |
| 792 | : BoundedRational.tan(argVal.ratVal); |
| 793 | if (ratVal != null) { |
| 794 | break; |
| 795 | } |
| 796 | CR argCR = toRadians(argVal.val, ec); |
| 797 | return new EvalRet(argVal.pos, argCR.sin().divide(argCR.cos()), null); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 798 | case R.id.fun_ln: |
| 799 | argVal = evalExpr(i+1, ec); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 800 | if (isOperator(argVal.pos, R.id.rparen, ec)) { |
| 801 | argVal.pos++; |
| 802 | } |
| 803 | ratVal = BoundedRational.ln(argVal.ratVal); |
| 804 | if (ratVal != null) { |
| 805 | break; |
| 806 | } |
| 807 | return new EvalRet(argVal.pos, argVal.val.ln(), null); |
Hans Boehm | 4db31b4 | 2015-05-31 12:19:05 -0700 | [diff] [blame] | 808 | case R.id.fun_exp: |
| 809 | argVal = evalExpr(i+1, ec); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 810 | if (isOperator(argVal.pos, R.id.rparen, ec)) { |
| 811 | argVal.pos++; |
| 812 | } |
| 813 | ratVal = BoundedRational.exp(argVal.ratVal); |
| 814 | if (ratVal != null) { |
| 815 | break; |
| 816 | } |
| 817 | return new EvalRet(argVal.pos, argVal.val.exp(), null); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 818 | case R.id.fun_log: |
| 819 | argVal = evalExpr(i+1, ec); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 820 | if (isOperator(argVal.pos, R.id.rparen, ec)) { |
| 821 | argVal.pos++; |
| 822 | } |
| 823 | ratVal = BoundedRational.log(argVal.ratVal); |
| 824 | if (ratVal != null) { |
| 825 | break; |
| 826 | } |
| 827 | return new EvalRet(argVal.pos, argVal.val.ln().divide(CR.valueOf(10).ln()), null); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 828 | case R.id.fun_arcsin: |
| 829 | argVal = evalExpr(i+1, ec); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 830 | if (isOperator(argVal.pos, R.id.rparen, ec)) { |
| 831 | argVal.pos++; |
| 832 | } |
| 833 | ratVal = ec.mDegreeMode ? BoundedRational.degreeAsin(argVal.ratVal) |
| 834 | : BoundedRational.asin(argVal.ratVal); |
| 835 | if (ratVal != null) { |
| 836 | break; |
| 837 | } |
| 838 | return new EvalRet(argVal.pos, |
| 839 | fromRadians(UnaryCRFunction.asinFunction.execute(argVal.val),ec), null); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 840 | case R.id.fun_arccos: |
| 841 | argVal = evalExpr(i+1, ec); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 842 | if (isOperator(argVal.pos, R.id.rparen, ec)) { |
| 843 | argVal.pos++; |
| 844 | } |
| 845 | ratVal = ec.mDegreeMode ? BoundedRational.degreeAcos(argVal.ratVal) |
| 846 | : BoundedRational.acos(argVal.ratVal); |
| 847 | if (ratVal != null) { |
| 848 | break; |
| 849 | } |
| 850 | return new EvalRet(argVal.pos, |
| 851 | fromRadians(UnaryCRFunction.acosFunction.execute(argVal.val),ec), null); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 852 | case R.id.fun_arctan: |
| 853 | argVal = evalExpr(i+1, ec); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 854 | if (isOperator(argVal.pos, R.id.rparen, ec)) { |
| 855 | argVal.pos++; |
| 856 | } |
| 857 | ratVal = ec.mDegreeMode ? BoundedRational.degreeAtan(argVal.ratVal) |
| 858 | : BoundedRational.atan(argVal.ratVal); |
| 859 | if (ratVal != null) { |
| 860 | break; |
| 861 | } |
| 862 | return new EvalRet(argVal.pos, |
| 863 | fromRadians(UnaryCRFunction.atanFunction.execute(argVal.val),ec), null); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 864 | default: |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 865 | throw new SyntaxException("Unrecognized token in expression"); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 866 | } |
Hans Boehm | 682ff5e | 2015-03-09 14:40:25 -0700 | [diff] [blame] | 867 | // We have a rational value. |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 868 | return new EvalRet(argVal.pos, ratVal.CRValue(), ratVal); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 869 | } |
| 870 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 871 | /** |
| 872 | * Compute an integral power of a constructive real. |
| 873 | * Unlike the "general" case using logarithms, this handles a negative base. |
| 874 | */ |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 875 | private static CR pow(CR base, BigInteger exp) { |
| 876 | if (exp.compareTo(BigInteger.ZERO) < 0) { |
| 877 | return pow(base, exp.negate()).inverse(); |
| 878 | } |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 879 | if (exp.equals(BigInteger.ONE)) { |
| 880 | return base; |
| 881 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 882 | if (exp.and(BigInteger.ONE).intValue() == 1) { |
| 883 | return pow(base, exp.subtract(BigInteger.ONE)).multiply(base); |
| 884 | } |
| 885 | if (exp.equals(BigInteger.ZERO)) { |
| 886 | return CR.valueOf(1); |
| 887 | } |
| 888 | CR tmp = pow(base, exp.shiftRight(1)); |
| 889 | return tmp.multiply(tmp); |
| 890 | } |
| 891 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 892 | // Number of bits past binary point to test for integer-ness. |
Hans Boehm | 682ff5e | 2015-03-09 14:40:25 -0700 | [diff] [blame] | 893 | private static final int TEST_PREC = -100; |
Hans Boehm | 682ff5e | 2015-03-09 14:40:25 -0700 | [diff] [blame] | 894 | private static final BigInteger MASK = |
| 895 | BigInteger.ONE.shiftLeft(-TEST_PREC).subtract(BigInteger.ONE); |
Hans Boehm | 4db31b4 | 2015-05-31 12:19:05 -0700 | [diff] [blame] | 896 | private static final CR REAL_E = CR.valueOf(1).exp(); |
| 897 | private static final CR REAL_ONE_HUNDREDTH = CR.valueOf(100).inverse(); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 898 | private static final BoundedRational RATIONAL_ONE_HUNDREDTH = new BoundedRational(1,100); |
Hans Boehm | 682ff5e | 2015-03-09 14:40:25 -0700 | [diff] [blame] | 899 | private static boolean isApprInt(CR x) { |
| 900 | BigInteger appr = x.get_appr(TEST_PREC); |
| 901 | return appr.and(MASK).signum() == 0; |
| 902 | } |
| 903 | |
Hans Boehm | 4db31b4 | 2015-05-31 12:19:05 -0700 | [diff] [blame] | 904 | private EvalRet evalSuffix(int i, EvalContext ec) throws SyntaxException { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 905 | final EvalRet tmp = evalUnary(i, ec); |
| 906 | int cpos = tmp.pos; |
| 907 | CR crVal = tmp.val; |
| 908 | BoundedRational ratVal = tmp.ratVal; |
Hans Boehm | 4db31b4 | 2015-05-31 12:19:05 -0700 | [diff] [blame] | 909 | boolean isFact; |
| 910 | boolean isSquared = false; |
| 911 | while ((isFact = isOperator(cpos, R.id.op_fact, ec)) || |
| 912 | (isSquared = isOperator(cpos, R.id.op_sqr, ec)) || |
| 913 | isOperator(cpos, R.id.op_pct, ec)) { |
| 914 | if (isFact) { |
| 915 | if (ratVal == null) { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 916 | // Assume it was an integer, but we didn't figure it out. |
Hans Boehm | 4db31b4 | 2015-05-31 12:19:05 -0700 | [diff] [blame] | 917 | // KitKat may have used the Gamma function. |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 918 | if (!isApprInt(crVal)) { |
Hans Boehm | 4db31b4 | 2015-05-31 12:19:05 -0700 | [diff] [blame] | 919 | throw new ArithmeticException("factorial(non-integer)"); |
| 920 | } |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 921 | ratVal = new BoundedRational(crVal.BigIntegerValue()); |
Hans Boehm | 682ff5e | 2015-03-09 14:40:25 -0700 | [diff] [blame] | 922 | } |
Hans Boehm | 4db31b4 | 2015-05-31 12:19:05 -0700 | [diff] [blame] | 923 | ratVal = BoundedRational.fact(ratVal); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 924 | crVal = ratVal.CRValue(); |
Hans Boehm | 4db31b4 | 2015-05-31 12:19:05 -0700 | [diff] [blame] | 925 | } else if (isSquared) { |
| 926 | ratVal = BoundedRational.multiply(ratVal, ratVal); |
| 927 | if (ratVal == null) { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 928 | crVal = crVal.multiply(crVal); |
Hans Boehm | 4db31b4 | 2015-05-31 12:19:05 -0700 | [diff] [blame] | 929 | } else { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 930 | crVal = ratVal.CRValue(); |
Hans Boehm | 4db31b4 | 2015-05-31 12:19:05 -0700 | [diff] [blame] | 931 | } |
| 932 | } else /* percent */ { |
| 933 | ratVal = BoundedRational.multiply(ratVal, RATIONAL_ONE_HUNDREDTH); |
| 934 | if (ratVal == null) { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 935 | crVal = crVal.multiply(REAL_ONE_HUNDREDTH); |
Hans Boehm | 4db31b4 | 2015-05-31 12:19:05 -0700 | [diff] [blame] | 936 | } else { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 937 | crVal = ratVal.CRValue(); |
Hans Boehm | 4db31b4 | 2015-05-31 12:19:05 -0700 | [diff] [blame] | 938 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 939 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 940 | ++cpos; |
| 941 | } |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 942 | return new EvalRet(cpos, crVal, ratVal); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 943 | } |
| 944 | |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 945 | private EvalRet evalFactor(int i, EvalContext ec) throws SyntaxException { |
Hans Boehm | 4db31b4 | 2015-05-31 12:19:05 -0700 | [diff] [blame] | 946 | final EvalRet result1 = evalSuffix(i, ec); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 947 | int cpos = result1.pos; // current position |
| 948 | CR crVal = result1.val; // value so far |
| 949 | BoundedRational ratVal = result1.ratVal; // int value so far |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 950 | if (isOperator(cpos, R.id.op_pow, ec)) { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 951 | final EvalRet exp = evalSignedFactor(cpos + 1, ec); |
| 952 | cpos = exp.pos; |
Hans Boehm | 682ff5e | 2015-03-09 14:40:25 -0700 | [diff] [blame] | 953 | // Try completely rational evaluation first. |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 954 | ratVal = BoundedRational.pow(ratVal, exp.ratVal); |
Hans Boehm | 682ff5e | 2015-03-09 14:40:25 -0700 | [diff] [blame] | 955 | if (ratVal != null) { |
| 956 | return new EvalRet(cpos, ratVal.CRValue(), ratVal); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 957 | } |
Hans Boehm | 682ff5e | 2015-03-09 14:40:25 -0700 | [diff] [blame] | 958 | // Power with integer exponent is defined for negative base. |
| 959 | // Thus we handle that case separately. |
| 960 | // We punt if the exponent is an integer computed from irrational |
| 961 | // values. That wouldn't work reliably with floating point either. |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 962 | BigInteger int_exp = BoundedRational.asBigInteger(exp.ratVal); |
Hans Boehm | 682ff5e | 2015-03-09 14:40:25 -0700 | [diff] [blame] | 963 | if (int_exp != null) { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 964 | crVal = pow(crVal, int_exp); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 965 | } else { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 966 | crVal = crVal.ln().multiply(exp.val).exp(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 967 | } |
Hans Boehm | 682ff5e | 2015-03-09 14:40:25 -0700 | [diff] [blame] | 968 | ratVal = null; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 969 | } |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 970 | return new EvalRet(cpos, crVal, ratVal); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 971 | } |
| 972 | |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 973 | private EvalRet evalSignedFactor(int i, EvalContext ec) throws SyntaxException { |
| 974 | final boolean negative = isOperator(i, R.id.op_sub, ec); |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 975 | int cpos = negative ? i + 1 : i; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 976 | EvalRet tmp = evalFactor(cpos, ec); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 977 | cpos = tmp.pos; |
| 978 | CR crVal = negative ? tmp.val.negate() : tmp.val; |
| 979 | BoundedRational ratVal = negative ? BoundedRational.negate(tmp.ratVal) |
| 980 | : tmp.ratVal; |
| 981 | return new EvalRet(cpos, crVal, ratVal); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | private boolean canStartFactor(int i) { |
| 985 | if (i >= mExpr.size()) return false; |
| 986 | Token t = mExpr.get(i); |
| 987 | if (!(t instanceof Operator)) return true; |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 988 | int id = ((Operator)(t)).id; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 989 | if (KeyMaps.isBinary(id)) return false; |
| 990 | switch (id) { |
| 991 | case R.id.op_fact: |
| 992 | case R.id.rparen: |
| 993 | return false; |
| 994 | default: |
| 995 | return true; |
| 996 | } |
| 997 | } |
| 998 | |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 999 | private EvalRet evalTerm(int i, EvalContext ec) throws SyntaxException { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1000 | EvalRet tmp = evalSignedFactor(i, ec); |
| 1001 | boolean is_mul = false; |
| 1002 | boolean is_div = false; |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 1003 | int cpos = tmp.pos; // Current position in expression. |
| 1004 | CR crVal = tmp.val; // Current value. |
| 1005 | BoundedRational ratVal = tmp.ratVal; // Current rational value. |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 1006 | while ((is_mul = isOperator(cpos, R.id.op_mul, ec)) |
| 1007 | || (is_div = isOperator(cpos, R.id.op_div, ec)) |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1008 | || canStartFactor(cpos)) { |
| 1009 | if (is_mul || is_div) ++cpos; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 1010 | tmp = evalSignedFactor(cpos, ec); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1011 | if (is_div) { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 1012 | ratVal = BoundedRational.divide(ratVal, tmp.ratVal); |
Hans Boehm | 682ff5e | 2015-03-09 14:40:25 -0700 | [diff] [blame] | 1013 | if (ratVal == null) { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 1014 | crVal = crVal.divide(tmp.val); |
Hans Boehm | 682ff5e | 2015-03-09 14:40:25 -0700 | [diff] [blame] | 1015 | } else { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 1016 | crVal = ratVal.CRValue(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1017 | } |
| 1018 | } else { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 1019 | ratVal = BoundedRational.multiply(ratVal, tmp.ratVal); |
Hans Boehm | 682ff5e | 2015-03-09 14:40:25 -0700 | [diff] [blame] | 1020 | if (ratVal == null) { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 1021 | crVal = crVal.multiply(tmp.val); |
Hans Boehm | 682ff5e | 2015-03-09 14:40:25 -0700 | [diff] [blame] | 1022 | } else { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 1023 | crVal = ratVal.CRValue(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1024 | } |
| 1025 | } |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 1026 | cpos = tmp.pos; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1027 | is_mul = is_div = false; |
| 1028 | } |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 1029 | return new EvalRet(cpos, crVal, ratVal); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1030 | } |
| 1031 | |
Hans Boehm | 8afd0f8 | 2015-07-30 17:00:33 -0700 | [diff] [blame] | 1032 | /** |
| 1033 | * Is the subexpression starting at pos a simple percent constant? |
| 1034 | * This is used to recognize exppressions like 200+10%, which we handle specially. |
| 1035 | * This is defined as a Constant or PreEval token, followed by a percent sign, and followed |
| 1036 | * by either nothing or an additive operator. |
| 1037 | * Note that we are intentionally far more restrictive in recognizing such expressions than |
| 1038 | * e.g. http://blogs.msdn.com/b/oldnewthing/archive/2008/01/10/7047497.aspx . |
| 1039 | * When in doubt, we fall back to the the naive interpretation of % as 1/100. |
| 1040 | * Note that 100+(10)% yields 100.1 while 100+10% yields 110. This may be controversial, |
| 1041 | * but is consistent with Google web search. |
| 1042 | */ |
| 1043 | private boolean isPercent(int pos) { |
| 1044 | if (mExpr.size() < pos + 2 || !isOperatorUnchecked(pos + 1, R.id.op_pct)) { |
| 1045 | return false; |
| 1046 | } |
| 1047 | Token number = mExpr.get(pos); |
| 1048 | if (number instanceof Operator) { |
| 1049 | return false; |
| 1050 | } |
| 1051 | if (mExpr.size() == pos + 2) { |
| 1052 | return true; |
| 1053 | } |
| 1054 | if (!(mExpr.get(pos + 2) instanceof Operator)) { |
| 1055 | return false; |
| 1056 | } |
| 1057 | Operator op = (Operator) mExpr.get(pos + 2); |
| 1058 | return op.id == R.id.op_add || op.id == R.id.op_sub; |
| 1059 | } |
| 1060 | |
| 1061 | /** |
| 1062 | * Compute the multiplicative factor corresponding to an N% addition or subtraction. |
| 1063 | * @param pos position of Constant or PreEval expression token corresponding to N |
| 1064 | * @param isSubtraction this is a subtraction, as opposed to addition |
| 1065 | * @param ec usable evaluation contex; only length matters |
| 1066 | * @return Rational and CR values; position is pos + 2, i.e. after percent sign |
| 1067 | */ |
| 1068 | private EvalRet getPercentFactor(int pos, boolean isSubtraction, EvalContext ec) |
| 1069 | throws SyntaxException { |
| 1070 | EvalRet tmp = evalUnary(pos, ec); |
| 1071 | BoundedRational ratVal = isSubtraction ? BoundedRational.negate(tmp.ratVal) |
| 1072 | : tmp.ratVal; |
| 1073 | CR crVal = isSubtraction ? tmp.val.negate() : tmp.val; |
| 1074 | ratVal = BoundedRational.add(BoundedRational.ONE, |
| 1075 | BoundedRational.multiply(ratVal, RATIONAL_ONE_HUNDREDTH)); |
| 1076 | if (ratVal == null) { |
| 1077 | crVal = CR.ONE.add(crVal.multiply(REAL_ONE_HUNDREDTH)); |
| 1078 | } else { |
| 1079 | crVal = ratVal.CRValue(); |
| 1080 | } |
| 1081 | return new EvalRet(pos + 2 /* after percent sign */, crVal, ratVal); |
| 1082 | } |
| 1083 | |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 1084 | private EvalRet evalExpr(int i, EvalContext ec) throws SyntaxException { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1085 | EvalRet tmp = evalTerm(i, ec); |
| 1086 | boolean is_plus; |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 1087 | int cpos = tmp.pos; |
| 1088 | CR crVal = tmp.val; |
| 1089 | BoundedRational ratVal = tmp.ratVal; |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 1090 | while ((is_plus = isOperator(cpos, R.id.op_add, ec)) |
| 1091 | || isOperator(cpos, R.id.op_sub, ec)) { |
Hans Boehm | 8afd0f8 | 2015-07-30 17:00:33 -0700 | [diff] [blame] | 1092 | if (isPercent(cpos + 1)) { |
| 1093 | tmp = getPercentFactor(cpos + 1, !is_plus, ec); |
| 1094 | ratVal = BoundedRational.multiply(ratVal, tmp.ratVal); |
Hans Boehm | 682ff5e | 2015-03-09 14:40:25 -0700 | [diff] [blame] | 1095 | if (ratVal == null) { |
Hans Boehm | 8afd0f8 | 2015-07-30 17:00:33 -0700 | [diff] [blame] | 1096 | crVal = crVal.multiply(tmp.val); |
Hans Boehm | 682ff5e | 2015-03-09 14:40:25 -0700 | [diff] [blame] | 1097 | } else { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 1098 | crVal = ratVal.CRValue(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1099 | } |
| 1100 | } else { |
Hans Boehm | 8afd0f8 | 2015-07-30 17:00:33 -0700 | [diff] [blame] | 1101 | tmp = evalTerm(cpos + 1, ec); |
| 1102 | if (is_plus) { |
| 1103 | ratVal = BoundedRational.add(ratVal, tmp.ratVal); |
| 1104 | if (ratVal == null) { |
| 1105 | crVal = crVal.add(tmp.val); |
| 1106 | } else { |
| 1107 | crVal = ratVal.CRValue(); |
| 1108 | } |
Hans Boehm | 682ff5e | 2015-03-09 14:40:25 -0700 | [diff] [blame] | 1109 | } else { |
Hans Boehm | 8afd0f8 | 2015-07-30 17:00:33 -0700 | [diff] [blame] | 1110 | ratVal = BoundedRational.subtract(ratVal, tmp.ratVal); |
| 1111 | if (ratVal == null) { |
| 1112 | crVal = crVal.subtract(tmp.val); |
| 1113 | } else { |
| 1114 | crVal = ratVal.CRValue(); |
| 1115 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1116 | } |
| 1117 | } |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 1118 | cpos = tmp.pos; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1119 | } |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 1120 | return new EvalRet(cpos, crVal, ratVal); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1121 | } |
| 1122 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 1123 | /** |
| 1124 | * Externally visible evaluation result. |
| 1125 | */ |
| 1126 | public static class EvalResult { |
| 1127 | public final CR val; |
| 1128 | public final BoundedRational ratVal; |
| 1129 | EvalResult (CR v, BoundedRational rv) { |
| 1130 | val = v; |
| 1131 | ratVal = rv; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1132 | } |
Hans Boehm | 65a99a4 | 2016-02-03 18:16:07 -0800 | [diff] [blame^] | 1133 | /* |
| 1134 | * Return decimal String result to the indicated precision. |
| 1135 | * For rational values this is the exactly truncated result. |
| 1136 | * Otherwise the error is < 1 in the last included digit. |
| 1137 | * @param precOffset Always non-negative. 1 is accurate 1/10, 2 means 1/100, etc. |
| 1138 | */ |
| 1139 | public String toString(int precOffset) { |
| 1140 | return ratVal == null ? val.toString(precOffset) : ratVal.toString(precOffset); |
| 1141 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1142 | } |
| 1143 | |
Hans Boehm | e855376 | 2015-06-26 17:56:52 -0700 | [diff] [blame] | 1144 | /** |
| 1145 | * Return the starting position of the sequence of trailing binary operators. |
| 1146 | */ |
| 1147 | private int trailingBinaryOpsStart() { |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 1148 | int result = mExpr.size(); |
| 1149 | while (result > 0) { |
| 1150 | Token last = mExpr.get(result - 1); |
| 1151 | if (!(last instanceof Operator)) break; |
| 1152 | Operator o = (Operator)last; |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 1153 | if (!KeyMaps.isBinary(o.id)) break; |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 1154 | --result; |
| 1155 | } |
| 1156 | return result; |
| 1157 | } |
| 1158 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 1159 | /** |
| 1160 | * Is the current expression worth evaluating? |
| 1161 | */ |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 1162 | public boolean hasInterestingOps() { |
Hans Boehm | e855376 | 2015-06-26 17:56:52 -0700 | [diff] [blame] | 1163 | int last = trailingBinaryOpsStart(); |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 1164 | int first = 0; |
| 1165 | if (last > first && isOperatorUnchecked(first, R.id.op_sub)) { |
| 1166 | // Leading minus is not by itself interesting. |
| 1167 | first++; |
| 1168 | } |
| 1169 | for (int i = first; i < last; ++i) { |
| 1170 | Token t1 = mExpr.get(i); |
Hans Boehm | 187d3e9 | 2015-06-09 18:04:26 -0700 | [diff] [blame] | 1171 | if (t1 instanceof Operator |
| 1172 | || t1 instanceof PreEval && ((PreEval)t1).hasEllipsis()) { |
| 1173 | return true; |
| 1174 | } |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 1175 | } |
| 1176 | return false; |
| 1177 | } |
| 1178 | |
Hans Boehm | e855376 | 2015-06-26 17:56:52 -0700 | [diff] [blame] | 1179 | /** |
| 1180 | * Evaluate the expression excluding trailing binary operators. |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 1181 | * Errors result in exceptions, most of which are unchecked. Should not be called |
| 1182 | * concurrently with modification of the expression. May take a very long time; avoid calling |
| 1183 | * from UI thread. |
Hans Boehm | e855376 | 2015-06-26 17:56:52 -0700 | [diff] [blame] | 1184 | * |
| 1185 | * @param degreeMode use degrees rather than radians |
| 1186 | */ |
| 1187 | EvalResult eval(boolean degreeMode) throws SyntaxException |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 1188 | // And unchecked exceptions thrown by CR |
| 1189 | // and BoundedRational. |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1190 | { |
| 1191 | try { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 1192 | // We currently never include trailing binary operators, but include other trailing |
| 1193 | // operators. Thus we usually, but not always, display results for prefixes of valid |
| 1194 | // expressions, and don't generate an error where we previously displayed an instant |
| 1195 | // result. This reflects the Android L design. |
Hans Boehm | e855376 | 2015-06-26 17:56:52 -0700 | [diff] [blame] | 1196 | int prefixLen = trailingBinaryOpsStart(); |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 1197 | EvalContext ec = new EvalContext(degreeMode, prefixLen); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1198 | EvalRet res = evalExpr(0, ec); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 1199 | if (res.pos != prefixLen) { |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 1200 | throw new SyntaxException("Failed to parse full expression"); |
Hans Boehm | fbcef70 | 2015-04-27 18:07:47 -0700 | [diff] [blame] | 1201 | } |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 1202 | return new EvalResult(res.val, res.ratVal); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1203 | } catch (IndexOutOfBoundsException e) { |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 1204 | throw new SyntaxException("Unexpected expression end"); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1205 | } |
| 1206 | } |
| 1207 | |
| 1208 | // Produce a string representation of the expression itself |
Hans Boehm | 8a4f81c | 2015-07-09 10:41:25 -0700 | [diff] [blame] | 1209 | SpannableStringBuilder toSpannableStringBuilder(Context context) { |
| 1210 | SpannableStringBuilder ssb = new SpannableStringBuilder(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1211 | for (Token t: mExpr) { |
Hans Boehm | 8a4f81c | 2015-07-09 10:41:25 -0700 | [diff] [blame] | 1212 | ssb.append(t.toCharSequence(context)); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1213 | } |
Hans Boehm | 8a4f81c | 2015-07-09 10:41:25 -0700 | [diff] [blame] | 1214 | return ssb; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1215 | } |
| 1216 | } |