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 | /** |
| 220 | * Return BoundedRational representation of constant. |
| 221 | * Never null. |
| 222 | */ |
Hans Boehm | 682ff5e | 2015-03-09 14:40:25 -0700 | [diff] [blame] | 223 | public BoundedRational toRational() { |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 224 | String whole = mWhole; |
| 225 | if (whole.isEmpty()) whole = "0"; |
| 226 | BigInteger num = new BigInteger(whole + mFraction); |
Hans Boehm | 682ff5e | 2015-03-09 14:40:25 -0700 | [diff] [blame] | 227 | BigInteger den = BigInteger.TEN.pow(mFraction.length()); |
Hans Boehm | 0b9806f | 2015-06-29 16:07:15 -0700 | [diff] [blame] | 228 | if (mExponent > 0) { |
| 229 | num = num.multiply(BigInteger.TEN.pow(mExponent)); |
| 230 | } |
| 231 | if (mExponent < 0) { |
| 232 | den = den.multiply(BigInteger.TEN.pow(-mExponent)); |
| 233 | } |
Hans Boehm | 682ff5e | 2015-03-09 14:40:25 -0700 | [diff] [blame] | 234 | return new BoundedRational(num, den); |
| 235 | } |
| 236 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 237 | @Override |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 238 | public CharSequence toCharSequence(Context context) { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 239 | return toString(); |
| 240 | } |
| 241 | |
| 242 | @Override |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 243 | public TokenKind kind() { |
| 244 | return TokenKind.CONSTANT; |
| 245 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 246 | |
| 247 | // Override clone to make it public |
| 248 | @Override |
| 249 | public Object clone() { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 250 | Constant result = new Constant(); |
| 251 | result.mWhole = mWhole; |
| 252 | result.mFraction = mFraction; |
| 253 | result.mSawDecimal = mSawDecimal; |
| 254 | result.mExponent = mExponent; |
| 255 | return result; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 259 | // Hash maps used to detect duplicate subexpressions when we write out CalculatorExprs and |
| 260 | // read them back in. |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 261 | private static final ThreadLocal<IdentityHashMap<CR,Integer>>outMap = |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 262 | new ThreadLocal<IdentityHashMap<CR,Integer>>(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 263 | // Maps expressions to indices on output |
| 264 | private static final ThreadLocal<HashMap<Integer,PreEval>>inMap = |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 265 | new ThreadLocal<HashMap<Integer,PreEval>>(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 266 | // Maps expressions to indices on output |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 267 | private static final ThreadLocal<Integer> exprIndex = new ThreadLocal<Integer>(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 268 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 269 | /** |
| 270 | * Prepare for expression output. |
| 271 | * Initializes map that will lbe used to avoid duplicating shared subexpressions. |
| 272 | * This avoids a potential exponential blow-up in the expression size. |
| 273 | */ |
| 274 | public static void initExprOutput() { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 275 | outMap.set(new IdentityHashMap<CR,Integer>()); |
| 276 | exprIndex.set(Integer.valueOf(0)); |
| 277 | } |
| 278 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 279 | /** |
| 280 | * Prepare for expression input. |
| 281 | * Initializes map that will be used to reconstruct shared subexpressions. |
| 282 | */ |
| 283 | public static void initExprInput() { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 284 | inMap.set(new HashMap<Integer,PreEval>()); |
| 285 | } |
| 286 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 287 | /** |
| 288 | * The "token" class for previously evaluated subexpressions. |
| 289 | * We treat previously evaluated subexpressions as tokens. These are inserted when we either |
| 290 | * continue an expression after evaluating some of it, or copy an expression and paste it back |
| 291 | * in. |
| 292 | * The representation includes both CR and possibly BoundedRational values. In order to |
| 293 | * support saving and restoring, we also include the underlying expression itself, and the |
| 294 | * context (currently just degree mode) used to evaluate it. The short string representation |
| 295 | * is also stored in order to avoid potentially expensive recomputation in the UI thread. |
| 296 | */ |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 297 | private static class PreEval extends Token { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 298 | public final CR value; |
| 299 | public final BoundedRational ratValue; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 300 | private final CalculatorExpr mExpr; |
| 301 | private final EvalContext mContext; |
Hans Boehm | 50ed320 | 2015-06-09 14:35:49 -0700 | [diff] [blame] | 302 | private final String mShortRep; // Not internationalized. |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 303 | PreEval(CR val, BoundedRational ratVal, CalculatorExpr expr, |
| 304 | EvalContext ec, String shortRep) { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 305 | value = val; |
| 306 | ratValue = ratVal; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 307 | mExpr = expr; |
| 308 | mContext = ec; |
| 309 | mShortRep = shortRep; |
| 310 | } |
| 311 | // In writing out PreEvals, we are careful to avoid writing |
| 312 | // out duplicates. We assume that two expressions are |
| 313 | // duplicates if they have the same mVal. This avoids a |
| 314 | // potential exponential blow up in certain off cases and |
| 315 | // redundant evaluation after reading them back in. |
| 316 | // The parameter hash map maps expressions we've seen |
| 317 | // before to their index. |
| 318 | @Override |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 319 | public void write(DataOutput out) throws IOException { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 320 | out.writeByte(TokenKind.PRE_EVAL.ordinal()); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 321 | Integer index = outMap.get().get(value); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 322 | if (index == null) { |
| 323 | int nextIndex = exprIndex.get() + 1; |
| 324 | exprIndex.set(nextIndex); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 325 | outMap.get().put(value, nextIndex); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 326 | out.writeInt(nextIndex); |
| 327 | mExpr.write(out); |
| 328 | mContext.write(out); |
| 329 | out.writeUTF(mShortRep); |
| 330 | } else { |
| 331 | // Just write out the index |
| 332 | out.writeInt(index); |
| 333 | } |
| 334 | } |
| 335 | PreEval(DataInput in) throws IOException { |
| 336 | int index = in.readInt(); |
| 337 | PreEval prev = inMap.get().get(index); |
| 338 | if (prev == null) { |
| 339 | mExpr = new CalculatorExpr(in); |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 340 | mContext = new EvalContext(in, mExpr.mExpr.size()); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 341 | // Recompute other fields We currently do this in the UI thread, but we only |
| 342 | // create PreEval expressions that were previously successfully evaluated, and |
| 343 | // thus don't diverge. We also only evaluate to a constructive real, which |
| 344 | // involves substantial work only in fairly contrived circumstances. |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 345 | // TODO: Deal better with slow evaluations. |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 346 | EvalRet res = null; |
| 347 | try { |
| 348 | res = mExpr.evalExpr(0, mContext); |
| 349 | } catch (SyntaxException e) { |
| 350 | // Should be impossible, since we only write out |
| 351 | // expressions that can be evaluated. |
| 352 | Log.e("Calculator", "Unexpected syntax exception" + e); |
| 353 | } |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 354 | value = res.val; |
| 355 | ratValue = res.ratVal; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 356 | mShortRep = in.readUTF(); |
| 357 | inMap.get().put(index, this); |
| 358 | } else { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 359 | value = prev.value; |
| 360 | ratValue = prev.ratValue; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 361 | mExpr = prev.mExpr; |
| 362 | mContext = prev.mContext; |
| 363 | mShortRep = prev.mShortRep; |
| 364 | } |
| 365 | } |
| 366 | @Override |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 367 | public CharSequence toCharSequence(Context context) { |
Hans Boehm | 50ed320 | 2015-06-09 14:35:49 -0700 | [diff] [blame] | 368 | return KeyMaps.translateResult(mShortRep); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 369 | } |
| 370 | @Override |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 371 | public TokenKind kind() { |
Hans Boehm | 187d3e9 | 2015-06-09 18:04:26 -0700 | [diff] [blame] | 372 | return TokenKind.PRE_EVAL; |
| 373 | } |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 374 | public boolean hasEllipsis() { |
Hans Boehm | 187d3e9 | 2015-06-09 18:04:26 -0700 | [diff] [blame] | 375 | return mShortRep.lastIndexOf(KeyMaps.ELLIPSIS) != -1; |
| 376 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 377 | } |
| 378 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 379 | /** |
| 380 | * Read token from in. |
| 381 | */ |
| 382 | public static Token newToken(DataInput in) throws IOException { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 383 | TokenKind kind = tokenKindValues[in.readByte()]; |
| 384 | switch(kind) { |
| 385 | case CONSTANT: |
| 386 | return new Constant(in); |
| 387 | case OPERATOR: |
| 388 | return new Operator(in); |
| 389 | case PRE_EVAL: |
| 390 | return new PreEval(in); |
| 391 | default: throw new IOException("Bad save file format"); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | CalculatorExpr() { |
| 396 | mExpr = new ArrayList<Token>(); |
| 397 | } |
| 398 | |
| 399 | private CalculatorExpr(ArrayList<Token> expr) { |
| 400 | mExpr = expr; |
| 401 | } |
| 402 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 403 | /** |
| 404 | * Construct CalculatorExpr, by reading it from in. |
| 405 | */ |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 406 | CalculatorExpr(DataInput in) throws IOException { |
| 407 | mExpr = new ArrayList<Token>(); |
| 408 | int size = in.readInt(); |
| 409 | for (int i = 0; i < size; ++i) { |
| 410 | mExpr.add(newToken(in)); |
| 411 | } |
| 412 | } |
| 413 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 414 | /** |
| 415 | * Write this expression to out. |
| 416 | */ |
| 417 | public void write(DataOutput out) throws IOException { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 418 | int size = mExpr.size(); |
| 419 | out.writeInt(size); |
| 420 | for (int i = 0; i < size; ++i) { |
| 421 | mExpr.get(i).write(out); |
| 422 | } |
| 423 | } |
| 424 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 425 | /** |
| 426 | * Does this expression end with a numeric constant? |
| 427 | * As opposed to an operator or preevaluated expression. |
| 428 | */ |
Hans Boehm | 0b9806f | 2015-06-29 16:07:15 -0700 | [diff] [blame] | 429 | boolean hasTrailingConstant() { |
| 430 | int s = mExpr.size(); |
| 431 | if (s == 0) { |
| 432 | return false; |
| 433 | } |
| 434 | Token t = mExpr.get(s-1); |
| 435 | return t instanceof Constant; |
| 436 | } |
| 437 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 438 | /** |
| 439 | * Does this expression end with a binary operator? |
| 440 | */ |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 441 | private boolean hasTrailingBinary() { |
| 442 | int s = mExpr.size(); |
| 443 | if (s == 0) return false; |
| 444 | Token t = mExpr.get(s-1); |
| 445 | if (!(t instanceof Operator)) return false; |
| 446 | Operator o = (Operator)t; |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 447 | return (KeyMaps.isBinary(o.id)); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 448 | } |
| 449 | |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 450 | /** |
| 451 | * Append press of button with given id to expression. |
| 452 | * If the insertion would clearly result in a syntax error, either just return false |
| 453 | * and do nothing, or make an adjustment to avoid the problem. We do the latter only |
| 454 | * for unambiguous consecutive binary operators, in which case we delete the first |
| 455 | * operator. |
| 456 | */ |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 457 | boolean add(int id) { |
| 458 | int s = mExpr.size(); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 459 | final int d = KeyMaps.digVal(id); |
| 460 | final boolean binary = KeyMaps.isBinary(id); |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 461 | Token lastTok = s == 0 ? null : mExpr.get(s-1); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 462 | int lastOp = lastTok instanceof Operator ? ((Operator) lastTok).id : 0; |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 463 | // Quietly replace a trailing binary operator with another one, unless the second |
| 464 | // operator is minus, in which case we just allow it as a unary minus. |
| 465 | if (binary && !KeyMaps.isPrefix(id)) { |
| 466 | if (s == 0 || lastOp == R.id.lparen || KeyMaps.isFunc(lastOp) |
| 467 | || KeyMaps.isPrefix(lastOp) && lastOp != R.id.op_sub) { |
| 468 | return false; |
| 469 | } |
| 470 | while (hasTrailingBinary()) { |
| 471 | delete(); |
| 472 | } |
| 473 | // s invalid and not used below. |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 474 | } |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 475 | final boolean isConstPiece = (d != KeyMaps.NOT_DIGIT || id == R.id.dec_point); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 476 | if (isConstPiece) { |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 477 | // Since we treat juxtaposition as multiplication, a constant can appear anywhere. |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 478 | if (s == 0) { |
| 479 | mExpr.add(new Constant()); |
| 480 | s++; |
| 481 | } else { |
| 482 | Token last = mExpr.get(s-1); |
| 483 | if(!(last instanceof Constant)) { |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 484 | if (last instanceof PreEval) { |
| 485 | // Add explicit multiplication to avoid confusing display. |
| 486 | mExpr.add(new Operator(R.id.op_mul)); |
| 487 | s++; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 488 | } |
| 489 | mExpr.add(new Constant()); |
| 490 | s++; |
| 491 | } |
| 492 | } |
| 493 | return ((Constant)(mExpr.get(s-1))).add(id); |
| 494 | } else { |
| 495 | mExpr.add(new Operator(id)); |
| 496 | return true; |
| 497 | } |
| 498 | } |
| 499 | |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 500 | /** |
Hans Boehm | 0b9806f | 2015-06-29 16:07:15 -0700 | [diff] [blame] | 501 | * Add exponent to the constant at the end of the expression. |
| 502 | * Assumes there is a constant at the end of the expression. |
| 503 | */ |
| 504 | void addExponent(int exp) { |
| 505 | Token lastTok = mExpr.get(mExpr.size() - 1); |
| 506 | ((Constant) lastTok).addExponent(exp); |
| 507 | } |
| 508 | |
| 509 | /** |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 510 | * Remove trailing op_add and op_sub operators. |
| 511 | */ |
| 512 | void removeTrailingAdditiveOperators() { |
| 513 | while (true) { |
| 514 | int s = mExpr.size(); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 515 | if (s == 0) { |
| 516 | break; |
| 517 | } |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 518 | Token lastTok = mExpr.get(s-1); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 519 | if (!(lastTok instanceof Operator)) { |
| 520 | break; |
| 521 | } |
| 522 | int lastOp = ((Operator) lastTok).id; |
| 523 | if (lastOp != R.id.op_add && lastOp != R.id.op_sub) { |
| 524 | break; |
| 525 | } |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 526 | delete(); |
| 527 | } |
| 528 | } |
| 529 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 530 | /** |
| 531 | * Append the contents of the argument expression. |
| 532 | * It is assumed that the argument expression will not change, and thus its pieces can be |
| 533 | * reused directly. |
| 534 | */ |
| 535 | public void append(CalculatorExpr expr2) { |
Hans Boehm | fbcef70 | 2015-04-27 18:07:47 -0700 | [diff] [blame] | 536 | int s = mExpr.size(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 537 | int s2 = expr2.mExpr.size(); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 538 | // Check that we're not concatenating Constant or PreEval tokens, since the result would |
| 539 | // look like a single constant, with very mysterious results for the user. |
Hans Boehm | fbcef70 | 2015-04-27 18:07:47 -0700 | [diff] [blame] | 540 | if (s != 0 && s2 != 0) { |
| 541 | Token last = mExpr.get(s-1); |
| 542 | Token first = expr2.mExpr.get(0); |
| 543 | if (!(first instanceof Operator) && !(last instanceof Operator)) { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 544 | // Fudge it by adding an explicit multiplication. We would have interpreted it as |
| 545 | // such anyway, and this makes it recognizable to the user. |
Hans Boehm | fbcef70 | 2015-04-27 18:07:47 -0700 | [diff] [blame] | 546 | mExpr.add(new Operator(R.id.op_mul)); |
| 547 | } |
| 548 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 549 | for (int i = 0; i < s2; ++i) { |
| 550 | mExpr.add(expr2.mExpr.get(i)); |
| 551 | } |
| 552 | } |
| 553 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 554 | /** |
| 555 | * Undo the last key addition, if any. |
| 556 | * Or possibly remove a trailing exponent digit. |
| 557 | */ |
| 558 | public void delete() { |
| 559 | final int s = mExpr.size(); |
| 560 | if (s == 0) { |
| 561 | return; |
| 562 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 563 | Token last = mExpr.get(s-1); |
| 564 | if (last instanceof Constant) { |
| 565 | Constant c = (Constant)last; |
| 566 | c.delete(); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 567 | if (!c.isEmpty()) { |
| 568 | return; |
| 569 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 570 | } |
| 571 | mExpr.remove(s-1); |
| 572 | } |
| 573 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 574 | /** |
| 575 | * Remove all tokens from the expression. |
| 576 | */ |
| 577 | public void clear() { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 578 | mExpr.clear(); |
| 579 | } |
| 580 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 581 | public boolean isEmpty() { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 582 | return mExpr.isEmpty(); |
| 583 | } |
| 584 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 585 | /** |
| 586 | * Returns a logical deep copy of the CalculatorExpr. |
| 587 | * Operator and PreEval tokens are immutable, and thus aren't really copied. |
| 588 | */ |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 589 | public Object clone() { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 590 | CalculatorExpr result = new CalculatorExpr(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 591 | for (Token t: mExpr) { |
| 592 | if (t instanceof Constant) { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 593 | result.mExpr.add((Token)(((Constant)t).clone())); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 594 | } else { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 595 | result.mExpr.add(t); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 596 | } |
| 597 | } |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 598 | return result; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | // Am I just a constant? |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 602 | public boolean isConstant() { |
| 603 | if (mExpr.size() != 1) { |
| 604 | return false; |
| 605 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 606 | return mExpr.get(0) instanceof Constant; |
| 607 | } |
| 608 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 609 | /** |
| 610 | * Return a new expression consisting of a single token representing the current pre-evaluated |
| 611 | * expression. |
| 612 | * The caller supplies the value, degree mode, and short string representation, which must |
| 613 | * have been previously computed. Thus this is guaranteed to terminate reasonably quickly. |
| 614 | */ |
| 615 | public CalculatorExpr abbreviate(CR val, BoundedRational ratVal, |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 616 | boolean dm, String sr) { |
| 617 | CalculatorExpr result = new CalculatorExpr(); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 618 | Token t = new PreEval(val, ratVal, new CalculatorExpr((ArrayList<Token>) mExpr.clone()), |
| 619 | new EvalContext(dm, mExpr.size()), sr); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 620 | result.mExpr.add(t); |
| 621 | return result; |
| 622 | } |
| 623 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 624 | /** |
| 625 | * Internal evaluation functions return an EvalRet triple. |
| 626 | * We compute rational (BoundedRational) results when possible, both as a performance |
| 627 | * optimization, and to detect errors exactly when we can. |
| 628 | */ |
| 629 | private static class EvalRet { |
| 630 | public int pos; // Next position (expression index) to be parsed. |
| 631 | public final CR val; // Constructive Real result of evaluating subexpression. |
| 632 | public final BoundedRational ratVal; // Exact Rational value or null. |
Hans Boehm | 682ff5e | 2015-03-09 14:40:25 -0700 | [diff] [blame] | 633 | EvalRet(int p, CR v, BoundedRational r) { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 634 | pos = p; |
| 635 | val = v; |
| 636 | ratVal = r; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 637 | } |
| 638 | } |
| 639 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 640 | /** |
| 641 | * Internal evaluation functions take an EvalContext argument. |
| 642 | */ |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 643 | private static class EvalContext { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 644 | public final int mPrefixLength; // Length of prefix to evaluate. Not explicitly saved. |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 645 | public final boolean mDegreeMode; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 646 | // If we add any other kinds of evaluation modes, they go here. |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 647 | EvalContext(boolean degreeMode, int len) { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 648 | mDegreeMode = degreeMode; |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 649 | mPrefixLength = len; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 650 | } |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 651 | EvalContext(DataInput in, int len) throws IOException { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 652 | mDegreeMode = in.readBoolean(); |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 653 | mPrefixLength = len; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 654 | } |
| 655 | void write(DataOutput out) throws IOException { |
| 656 | out.writeBoolean(mDegreeMode); |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | private final CR RADIANS_PER_DEGREE = CR.PI.divide(CR.valueOf(180)); |
| 661 | |
| 662 | private final CR DEGREES_PER_RADIAN = CR.valueOf(180).divide(CR.PI); |
| 663 | |
| 664 | private CR toRadians(CR x, EvalContext ec) { |
| 665 | if (ec.mDegreeMode) { |
| 666 | return x.multiply(RADIANS_PER_DEGREE); |
| 667 | } else { |
| 668 | return x; |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | private CR fromRadians(CR x, EvalContext ec) { |
| 673 | if (ec.mDegreeMode) { |
| 674 | return x.multiply(DEGREES_PER_RADIAN); |
| 675 | } else { |
| 676 | return x; |
| 677 | } |
| 678 | } |
| 679 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 680 | // The following methods can all throw IndexOutOfBoundsException in the event of a syntax |
| 681 | // error. We expect that to be caught in eval below. |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 682 | |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 683 | private boolean isOperatorUnchecked(int i, int op) { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 684 | Token t = mExpr.get(i); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 685 | if (!(t instanceof Operator)) { |
| 686 | return false; |
| 687 | } |
| 688 | return ((Operator)(t)).id == op; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 689 | } |
| 690 | |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 691 | private boolean isOperator(int i, int op, EvalContext ec) { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 692 | if (i >= ec.mPrefixLength) { |
| 693 | return false; |
| 694 | } |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 695 | return isOperatorUnchecked(i, op); |
| 696 | } |
| 697 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 698 | public static class SyntaxException extends Exception { |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 699 | public SyntaxException() { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 700 | super(); |
| 701 | } |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 702 | public SyntaxException(String s) { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 703 | super(s); |
| 704 | } |
| 705 | } |
| 706 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 707 | // The following functions all evaluate some kind of expression starting at position i in |
| 708 | // mExpr in a specified evaluation context. They return both the expression value (as |
| 709 | // 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] | 710 | // that was not used as part of the evaluation. |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 711 | // This is essentially a simple recursive descent parser combined with expression evaluation. |
| 712 | |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 713 | private EvalRet evalUnary(int i, EvalContext ec) throws SyntaxException { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 714 | final Token t = mExpr.get(i); |
Hans Boehm | 0b9806f | 2015-06-29 16:07:15 -0700 | [diff] [blame] | 715 | BoundedRational ratVal; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 716 | if (t instanceof Constant) { |
| 717 | Constant c = (Constant)t; |
Hans Boehm | 0b9806f | 2015-06-29 16:07:15 -0700 | [diff] [blame] | 718 | ratVal = c.toRational(); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 719 | return new EvalRet(i+1, ratVal.CRValue(), ratVal); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 720 | } |
| 721 | if (t instanceof PreEval) { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 722 | final PreEval p = (PreEval)t; |
| 723 | return new EvalRet(i+1, p.value, p.ratValue); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 724 | } |
| 725 | EvalRet argVal; |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 726 | switch(((Operator)(t)).id) { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 727 | case R.id.const_pi: |
| 728 | return new EvalRet(i+1, CR.PI, null); |
| 729 | case R.id.const_e: |
Hans Boehm | 4db31b4 | 2015-05-31 12:19:05 -0700 | [diff] [blame] | 730 | return new EvalRet(i+1, REAL_E, null); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 731 | case R.id.op_sqrt: |
Hans Boehm | fbcef70 | 2015-04-27 18:07:47 -0700 | [diff] [blame] | 732 | // Seems to have highest precedence. |
| 733 | // Does not add implicit paren. |
| 734 | // Does seem to accept a leading minus. |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 735 | if (isOperator(i+1, R.id.op_sub, ec)) { |
Hans Boehm | fbcef70 | 2015-04-27 18:07:47 -0700 | [diff] [blame] | 736 | argVal = evalUnary(i+2, ec); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 737 | ratVal = BoundedRational.sqrt(BoundedRational.negate(argVal.ratVal)); |
| 738 | if (ratVal != null) { |
| 739 | break; |
| 740 | } |
| 741 | return new EvalRet(argVal.pos, |
| 742 | argVal.val.negate().sqrt(), null); |
Hans Boehm | fbcef70 | 2015-04-27 18:07:47 -0700 | [diff] [blame] | 743 | } else { |
| 744 | argVal = evalUnary(i+1, ec); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 745 | ratVal = BoundedRational.sqrt(argVal.ratVal); |
| 746 | if (ratVal != null) { |
| 747 | break; |
| 748 | } |
| 749 | return new EvalRet(argVal.pos, argVal.val.sqrt(), null); |
Hans Boehm | fbcef70 | 2015-04-27 18:07:47 -0700 | [diff] [blame] | 750 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 751 | case R.id.lparen: |
| 752 | argVal = evalExpr(i+1, ec); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 753 | if (isOperator(argVal.pos, R.id.rparen, ec)) { |
| 754 | argVal.pos++; |
| 755 | } |
| 756 | return new EvalRet(argVal.pos, argVal.val, argVal.ratVal); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 757 | case R.id.fun_sin: |
| 758 | argVal = evalExpr(i+1, ec); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 759 | if (isOperator(argVal.pos, R.id.rparen, ec)) { |
| 760 | argVal.pos++; |
| 761 | } |
| 762 | ratVal = ec.mDegreeMode ? BoundedRational.degreeSin(argVal.ratVal) |
| 763 | : BoundedRational.sin(argVal.ratVal); |
| 764 | if (ratVal != null) { |
| 765 | break; |
| 766 | } |
| 767 | return new EvalRet(argVal.pos, toRadians(argVal.val,ec).sin(), null); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 768 | case R.id.fun_cos: |
| 769 | argVal = evalExpr(i+1, ec); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 770 | if (isOperator(argVal.pos, R.id.rparen, ec)) { |
| 771 | argVal.pos++; |
| 772 | } |
| 773 | ratVal = ec.mDegreeMode ? BoundedRational.degreeCos(argVal.ratVal) |
| 774 | : BoundedRational.cos(argVal.ratVal); |
| 775 | if (ratVal != null) { |
| 776 | break; |
| 777 | } |
| 778 | return new EvalRet(argVal.pos, toRadians(argVal.val,ec).cos(), null); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 779 | case R.id.fun_tan: |
| 780 | argVal = evalExpr(i+1, ec); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 781 | if (isOperator(argVal.pos, R.id.rparen, ec)) { |
| 782 | argVal.pos++; |
| 783 | } |
| 784 | ratVal = ec.mDegreeMode ? BoundedRational.degreeTan(argVal.ratVal) |
| 785 | : BoundedRational.tan(argVal.ratVal); |
| 786 | if (ratVal != null) { |
| 787 | break; |
| 788 | } |
| 789 | CR argCR = toRadians(argVal.val, ec); |
| 790 | return new EvalRet(argVal.pos, argCR.sin().divide(argCR.cos()), null); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 791 | case R.id.fun_ln: |
| 792 | argVal = evalExpr(i+1, ec); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 793 | if (isOperator(argVal.pos, R.id.rparen, ec)) { |
| 794 | argVal.pos++; |
| 795 | } |
| 796 | ratVal = BoundedRational.ln(argVal.ratVal); |
| 797 | if (ratVal != null) { |
| 798 | break; |
| 799 | } |
| 800 | return new EvalRet(argVal.pos, argVal.val.ln(), null); |
Hans Boehm | 4db31b4 | 2015-05-31 12:19:05 -0700 | [diff] [blame] | 801 | case R.id.fun_exp: |
| 802 | argVal = evalExpr(i+1, ec); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 803 | if (isOperator(argVal.pos, R.id.rparen, ec)) { |
| 804 | argVal.pos++; |
| 805 | } |
| 806 | ratVal = BoundedRational.exp(argVal.ratVal); |
| 807 | if (ratVal != null) { |
| 808 | break; |
| 809 | } |
| 810 | return new EvalRet(argVal.pos, argVal.val.exp(), null); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 811 | case R.id.fun_log: |
| 812 | argVal = evalExpr(i+1, ec); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 813 | if (isOperator(argVal.pos, R.id.rparen, ec)) { |
| 814 | argVal.pos++; |
| 815 | } |
| 816 | ratVal = BoundedRational.log(argVal.ratVal); |
| 817 | if (ratVal != null) { |
| 818 | break; |
| 819 | } |
| 820 | 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] | 821 | case R.id.fun_arcsin: |
| 822 | argVal = evalExpr(i+1, ec); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 823 | if (isOperator(argVal.pos, R.id.rparen, ec)) { |
| 824 | argVal.pos++; |
| 825 | } |
| 826 | ratVal = ec.mDegreeMode ? BoundedRational.degreeAsin(argVal.ratVal) |
| 827 | : BoundedRational.asin(argVal.ratVal); |
| 828 | if (ratVal != null) { |
| 829 | break; |
| 830 | } |
| 831 | return new EvalRet(argVal.pos, |
| 832 | fromRadians(UnaryCRFunction.asinFunction.execute(argVal.val),ec), null); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 833 | case R.id.fun_arccos: |
| 834 | argVal = evalExpr(i+1, ec); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 835 | if (isOperator(argVal.pos, R.id.rparen, ec)) { |
| 836 | argVal.pos++; |
| 837 | } |
| 838 | ratVal = ec.mDegreeMode ? BoundedRational.degreeAcos(argVal.ratVal) |
| 839 | : BoundedRational.acos(argVal.ratVal); |
| 840 | if (ratVal != null) { |
| 841 | break; |
| 842 | } |
| 843 | return new EvalRet(argVal.pos, |
| 844 | fromRadians(UnaryCRFunction.acosFunction.execute(argVal.val),ec), null); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 845 | case R.id.fun_arctan: |
| 846 | argVal = evalExpr(i+1, ec); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 847 | if (isOperator(argVal.pos, R.id.rparen, ec)) { |
| 848 | argVal.pos++; |
| 849 | } |
| 850 | ratVal = ec.mDegreeMode ? BoundedRational.degreeAtan(argVal.ratVal) |
| 851 | : BoundedRational.atan(argVal.ratVal); |
| 852 | if (ratVal != null) { |
| 853 | break; |
| 854 | } |
| 855 | return new EvalRet(argVal.pos, |
| 856 | fromRadians(UnaryCRFunction.atanFunction.execute(argVal.val),ec), null); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 857 | default: |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 858 | throw new SyntaxException("Unrecognized token in expression"); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 859 | } |
Hans Boehm | 682ff5e | 2015-03-09 14:40:25 -0700 | [diff] [blame] | 860 | // We have a rational value. |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 861 | return new EvalRet(argVal.pos, ratVal.CRValue(), ratVal); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 862 | } |
| 863 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 864 | /** |
| 865 | * Compute an integral power of a constructive real. |
| 866 | * Unlike the "general" case using logarithms, this handles a negative base. |
| 867 | */ |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 868 | private static CR pow(CR base, BigInteger exp) { |
| 869 | if (exp.compareTo(BigInteger.ZERO) < 0) { |
| 870 | return pow(base, exp.negate()).inverse(); |
| 871 | } |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 872 | if (exp.equals(BigInteger.ONE)) { |
| 873 | return base; |
| 874 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 875 | if (exp.and(BigInteger.ONE).intValue() == 1) { |
| 876 | return pow(base, exp.subtract(BigInteger.ONE)).multiply(base); |
| 877 | } |
| 878 | if (exp.equals(BigInteger.ZERO)) { |
| 879 | return CR.valueOf(1); |
| 880 | } |
| 881 | CR tmp = pow(base, exp.shiftRight(1)); |
| 882 | return tmp.multiply(tmp); |
| 883 | } |
| 884 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 885 | // Number of bits past binary point to test for integer-ness. |
Hans Boehm | 682ff5e | 2015-03-09 14:40:25 -0700 | [diff] [blame] | 886 | private static final int TEST_PREC = -100; |
Hans Boehm | 682ff5e | 2015-03-09 14:40:25 -0700 | [diff] [blame] | 887 | private static final BigInteger MASK = |
| 888 | BigInteger.ONE.shiftLeft(-TEST_PREC).subtract(BigInteger.ONE); |
Hans Boehm | 4db31b4 | 2015-05-31 12:19:05 -0700 | [diff] [blame] | 889 | private static final CR REAL_E = CR.valueOf(1).exp(); |
| 890 | private static final CR REAL_ONE_HUNDREDTH = CR.valueOf(100).inverse(); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 891 | private static final BoundedRational RATIONAL_ONE_HUNDREDTH = new BoundedRational(1,100); |
Hans Boehm | 682ff5e | 2015-03-09 14:40:25 -0700 | [diff] [blame] | 892 | private static boolean isApprInt(CR x) { |
| 893 | BigInteger appr = x.get_appr(TEST_PREC); |
| 894 | return appr.and(MASK).signum() == 0; |
| 895 | } |
| 896 | |
Hans Boehm | 4db31b4 | 2015-05-31 12:19:05 -0700 | [diff] [blame] | 897 | private EvalRet evalSuffix(int i, EvalContext ec) throws SyntaxException { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 898 | final EvalRet tmp = evalUnary(i, ec); |
| 899 | int cpos = tmp.pos; |
| 900 | CR crVal = tmp.val; |
| 901 | BoundedRational ratVal = tmp.ratVal; |
Hans Boehm | 4db31b4 | 2015-05-31 12:19:05 -0700 | [diff] [blame] | 902 | boolean isFact; |
| 903 | boolean isSquared = false; |
| 904 | while ((isFact = isOperator(cpos, R.id.op_fact, ec)) || |
| 905 | (isSquared = isOperator(cpos, R.id.op_sqr, ec)) || |
| 906 | isOperator(cpos, R.id.op_pct, ec)) { |
| 907 | if (isFact) { |
| 908 | if (ratVal == null) { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 909 | // Assume it was an integer, but we didn't figure it out. |
Hans Boehm | 4db31b4 | 2015-05-31 12:19:05 -0700 | [diff] [blame] | 910 | // KitKat may have used the Gamma function. |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 911 | if (!isApprInt(crVal)) { |
Hans Boehm | 4db31b4 | 2015-05-31 12:19:05 -0700 | [diff] [blame] | 912 | throw new ArithmeticException("factorial(non-integer)"); |
| 913 | } |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 914 | ratVal = new BoundedRational(crVal.BigIntegerValue()); |
Hans Boehm | 682ff5e | 2015-03-09 14:40:25 -0700 | [diff] [blame] | 915 | } |
Hans Boehm | 4db31b4 | 2015-05-31 12:19:05 -0700 | [diff] [blame] | 916 | ratVal = BoundedRational.fact(ratVal); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 917 | crVal = ratVal.CRValue(); |
Hans Boehm | 4db31b4 | 2015-05-31 12:19:05 -0700 | [diff] [blame] | 918 | } else if (isSquared) { |
| 919 | ratVal = BoundedRational.multiply(ratVal, ratVal); |
| 920 | if (ratVal == null) { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 921 | crVal = crVal.multiply(crVal); |
Hans Boehm | 4db31b4 | 2015-05-31 12:19:05 -0700 | [diff] [blame] | 922 | } else { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 923 | crVal = ratVal.CRValue(); |
Hans Boehm | 4db31b4 | 2015-05-31 12:19:05 -0700 | [diff] [blame] | 924 | } |
| 925 | } else /* percent */ { |
| 926 | ratVal = BoundedRational.multiply(ratVal, RATIONAL_ONE_HUNDREDTH); |
| 927 | if (ratVal == null) { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 928 | crVal = crVal.multiply(REAL_ONE_HUNDREDTH); |
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 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 932 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 933 | ++cpos; |
| 934 | } |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 935 | return new EvalRet(cpos, crVal, ratVal); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 936 | } |
| 937 | |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 938 | private EvalRet evalFactor(int i, EvalContext ec) throws SyntaxException { |
Hans Boehm | 4db31b4 | 2015-05-31 12:19:05 -0700 | [diff] [blame] | 939 | final EvalRet result1 = evalSuffix(i, ec); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 940 | int cpos = result1.pos; // current position |
| 941 | CR crVal = result1.val; // value so far |
| 942 | BoundedRational ratVal = result1.ratVal; // int value so far |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 943 | if (isOperator(cpos, R.id.op_pow, ec)) { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 944 | final EvalRet exp = evalSignedFactor(cpos + 1, ec); |
| 945 | cpos = exp.pos; |
Hans Boehm | 682ff5e | 2015-03-09 14:40:25 -0700 | [diff] [blame] | 946 | // Try completely rational evaluation first. |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 947 | ratVal = BoundedRational.pow(ratVal, exp.ratVal); |
Hans Boehm | 682ff5e | 2015-03-09 14:40:25 -0700 | [diff] [blame] | 948 | if (ratVal != null) { |
| 949 | return new EvalRet(cpos, ratVal.CRValue(), ratVal); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 950 | } |
Hans Boehm | 682ff5e | 2015-03-09 14:40:25 -0700 | [diff] [blame] | 951 | // Power with integer exponent is defined for negative base. |
| 952 | // Thus we handle that case separately. |
| 953 | // We punt if the exponent is an integer computed from irrational |
| 954 | // values. That wouldn't work reliably with floating point either. |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 955 | BigInteger int_exp = BoundedRational.asBigInteger(exp.ratVal); |
Hans Boehm | 682ff5e | 2015-03-09 14:40:25 -0700 | [diff] [blame] | 956 | if (int_exp != null) { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 957 | crVal = pow(crVal, int_exp); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 958 | } else { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 959 | crVal = crVal.ln().multiply(exp.val).exp(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 960 | } |
Hans Boehm | 682ff5e | 2015-03-09 14:40:25 -0700 | [diff] [blame] | 961 | ratVal = null; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 962 | } |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 963 | return new EvalRet(cpos, crVal, ratVal); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 964 | } |
| 965 | |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 966 | private EvalRet evalSignedFactor(int i, EvalContext ec) throws SyntaxException { |
| 967 | final boolean negative = isOperator(i, R.id.op_sub, ec); |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 968 | int cpos = negative ? i + 1 : i; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 969 | EvalRet tmp = evalFactor(cpos, ec); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 970 | cpos = tmp.pos; |
| 971 | CR crVal = negative ? tmp.val.negate() : tmp.val; |
| 972 | BoundedRational ratVal = negative ? BoundedRational.negate(tmp.ratVal) |
| 973 | : tmp.ratVal; |
| 974 | return new EvalRet(cpos, crVal, ratVal); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | private boolean canStartFactor(int i) { |
| 978 | if (i >= mExpr.size()) return false; |
| 979 | Token t = mExpr.get(i); |
| 980 | if (!(t instanceof Operator)) return true; |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 981 | int id = ((Operator)(t)).id; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 982 | if (KeyMaps.isBinary(id)) return false; |
| 983 | switch (id) { |
| 984 | case R.id.op_fact: |
| 985 | case R.id.rparen: |
| 986 | return false; |
| 987 | default: |
| 988 | return true; |
| 989 | } |
| 990 | } |
| 991 | |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 992 | private EvalRet evalTerm(int i, EvalContext ec) throws SyntaxException { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 993 | EvalRet tmp = evalSignedFactor(i, ec); |
| 994 | boolean is_mul = false; |
| 995 | boolean is_div = false; |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 996 | int cpos = tmp.pos; // Current position in expression. |
| 997 | CR crVal = tmp.val; // Current value. |
| 998 | BoundedRational ratVal = tmp.ratVal; // Current rational value. |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 999 | while ((is_mul = isOperator(cpos, R.id.op_mul, ec)) |
| 1000 | || (is_div = isOperator(cpos, R.id.op_div, ec)) |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1001 | || canStartFactor(cpos)) { |
| 1002 | if (is_mul || is_div) ++cpos; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 1003 | tmp = evalSignedFactor(cpos, ec); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1004 | if (is_div) { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 1005 | ratVal = BoundedRational.divide(ratVal, tmp.ratVal); |
Hans Boehm | 682ff5e | 2015-03-09 14:40:25 -0700 | [diff] [blame] | 1006 | if (ratVal == null) { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 1007 | crVal = crVal.divide(tmp.val); |
Hans Boehm | 682ff5e | 2015-03-09 14:40:25 -0700 | [diff] [blame] | 1008 | } else { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 1009 | crVal = ratVal.CRValue(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1010 | } |
| 1011 | } else { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 1012 | ratVal = BoundedRational.multiply(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.multiply(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 | } |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 1019 | cpos = tmp.pos; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1020 | is_mul = is_div = false; |
| 1021 | } |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 1022 | return new EvalRet(cpos, crVal, ratVal); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1023 | } |
| 1024 | |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 1025 | private EvalRet evalExpr(int i, EvalContext ec) throws SyntaxException { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1026 | EvalRet tmp = evalTerm(i, ec); |
| 1027 | boolean is_plus; |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 1028 | int cpos = tmp.pos; |
| 1029 | CR crVal = tmp.val; |
| 1030 | BoundedRational ratVal = tmp.ratVal; |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 1031 | while ((is_plus = isOperator(cpos, R.id.op_add, ec)) |
| 1032 | || isOperator(cpos, R.id.op_sub, ec)) { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1033 | tmp = evalTerm(cpos+1, ec); |
| 1034 | if (is_plus) { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 1035 | ratVal = BoundedRational.add(ratVal, tmp.ratVal); |
Hans Boehm | 682ff5e | 2015-03-09 14:40:25 -0700 | [diff] [blame] | 1036 | if (ratVal == null) { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 1037 | crVal = crVal.add(tmp.val); |
Hans Boehm | 682ff5e | 2015-03-09 14:40:25 -0700 | [diff] [blame] | 1038 | } else { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 1039 | crVal = ratVal.CRValue(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1040 | } |
| 1041 | } else { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 1042 | ratVal = BoundedRational.subtract(ratVal, tmp.ratVal); |
Hans Boehm | 682ff5e | 2015-03-09 14:40:25 -0700 | [diff] [blame] | 1043 | if (ratVal == null) { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 1044 | crVal = crVal.subtract(tmp.val); |
Hans Boehm | 682ff5e | 2015-03-09 14:40:25 -0700 | [diff] [blame] | 1045 | } else { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 1046 | crVal = ratVal.CRValue(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1047 | } |
| 1048 | } |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 1049 | cpos = tmp.pos; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1050 | } |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 1051 | return new EvalRet(cpos, crVal, ratVal); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1052 | } |
| 1053 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 1054 | /** |
| 1055 | * Externally visible evaluation result. |
| 1056 | */ |
| 1057 | public static class EvalResult { |
| 1058 | public final CR val; |
| 1059 | public final BoundedRational ratVal; |
| 1060 | EvalResult (CR v, BoundedRational rv) { |
| 1061 | val = v; |
| 1062 | ratVal = rv; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1063 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1064 | } |
| 1065 | |
Hans Boehm | e855376 | 2015-06-26 17:56:52 -0700 | [diff] [blame] | 1066 | /** |
| 1067 | * Return the starting position of the sequence of trailing binary operators. |
| 1068 | */ |
| 1069 | private int trailingBinaryOpsStart() { |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 1070 | int result = mExpr.size(); |
| 1071 | while (result > 0) { |
| 1072 | Token last = mExpr.get(result - 1); |
| 1073 | if (!(last instanceof Operator)) break; |
| 1074 | Operator o = (Operator)last; |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 1075 | if (!KeyMaps.isBinary(o.id)) break; |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 1076 | --result; |
| 1077 | } |
| 1078 | return result; |
| 1079 | } |
| 1080 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 1081 | /** |
| 1082 | * Is the current expression worth evaluating? |
| 1083 | */ |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 1084 | public boolean hasInterestingOps() { |
Hans Boehm | e855376 | 2015-06-26 17:56:52 -0700 | [diff] [blame] | 1085 | int last = trailingBinaryOpsStart(); |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 1086 | int first = 0; |
| 1087 | if (last > first && isOperatorUnchecked(first, R.id.op_sub)) { |
| 1088 | // Leading minus is not by itself interesting. |
| 1089 | first++; |
| 1090 | } |
| 1091 | for (int i = first; i < last; ++i) { |
| 1092 | Token t1 = mExpr.get(i); |
Hans Boehm | 187d3e9 | 2015-06-09 18:04:26 -0700 | [diff] [blame] | 1093 | if (t1 instanceof Operator |
| 1094 | || t1 instanceof PreEval && ((PreEval)t1).hasEllipsis()) { |
| 1095 | return true; |
| 1096 | } |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 1097 | } |
| 1098 | return false; |
| 1099 | } |
| 1100 | |
Hans Boehm | e855376 | 2015-06-26 17:56:52 -0700 | [diff] [blame] | 1101 | /** |
| 1102 | * Evaluate the expression excluding trailing binary operators. |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 1103 | * Errors result in exceptions, most of which are unchecked. Should not be called |
| 1104 | * concurrently with modification of the expression. May take a very long time; avoid calling |
| 1105 | * from UI thread. |
Hans Boehm | e855376 | 2015-06-26 17:56:52 -0700 | [diff] [blame] | 1106 | * |
| 1107 | * @param degreeMode use degrees rather than radians |
| 1108 | */ |
| 1109 | EvalResult eval(boolean degreeMode) throws SyntaxException |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 1110 | // And unchecked exceptions thrown by CR |
| 1111 | // and BoundedRational. |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1112 | { |
| 1113 | try { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 1114 | // We currently never include trailing binary operators, but include other trailing |
| 1115 | // operators. Thus we usually, but not always, display results for prefixes of valid |
| 1116 | // expressions, and don't generate an error where we previously displayed an instant |
| 1117 | // result. This reflects the Android L design. |
Hans Boehm | e855376 | 2015-06-26 17:56:52 -0700 | [diff] [blame] | 1118 | int prefixLen = trailingBinaryOpsStart(); |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 1119 | EvalContext ec = new EvalContext(degreeMode, prefixLen); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1120 | EvalRet res = evalExpr(0, ec); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 1121 | if (res.pos != prefixLen) { |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 1122 | throw new SyntaxException("Failed to parse full expression"); |
Hans Boehm | fbcef70 | 2015-04-27 18:07:47 -0700 | [diff] [blame] | 1123 | } |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame^] | 1124 | return new EvalResult(res.val, res.ratVal); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1125 | } catch (IndexOutOfBoundsException e) { |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 1126 | throw new SyntaxException("Unexpected expression end"); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1127 | } |
| 1128 | } |
| 1129 | |
| 1130 | // Produce a string representation of the expression itself |
Hans Boehm | 8a4f81c | 2015-07-09 10:41:25 -0700 | [diff] [blame] | 1131 | SpannableStringBuilder toSpannableStringBuilder(Context context) { |
| 1132 | SpannableStringBuilder ssb = new SpannableStringBuilder(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1133 | for (Token t: mExpr) { |
Hans Boehm | 8a4f81c | 2015-07-09 10:41:25 -0700 | [diff] [blame] | 1134 | ssb.append(t.toCharSequence(context)); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1135 | } |
Hans Boehm | 8a4f81c | 2015-07-09 10:41:25 -0700 | [diff] [blame] | 1136 | return ssb; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1137 | } |
| 1138 | } |