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 | import android.content.Context; |
Hans Boehm | 8a4f81c | 2015-07-09 10:41:25 -0700 | [diff] [blame] | 20 | import android.text.SpannableString; |
| 21 | import android.text.SpannableStringBuilder; |
| 22 | import android.text.Spanned; |
| 23 | import android.text.style.TtsSpan; |
| 24 | import android.text.style.TtsSpan.TextBuilder; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 25 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 26 | import java.math.BigInteger; |
| 27 | import java.io.DataInput; |
| 28 | import java.io.DataOutput; |
| 29 | import java.io.IOException; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 30 | import java.util.ArrayList; |
| 31 | import java.util.HashMap; |
| 32 | import java.util.IdentityHashMap; |
| 33 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 34 | /** |
| 35 | * A mathematical expression represented as a sequence of "tokens". |
| 36 | * Many tokens are represented by button ids for the corresponding operator. |
| 37 | * A token may also represent the result of a previously evaluated expression. |
| 38 | * The add() method adds a token to the end of the expression. The delete method() removes one. |
| 39 | * Clear() deletes the entire expression contents. Eval() evaluates the expression, |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 40 | * producing a UnifiedReal result. |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 41 | * Expressions are parsed only during evaluation; no explicit parse tree is maintained. |
| 42 | * |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 43 | * The write() method is used to save the current expression. Note that neither UnifiedReal |
| 44 | * nor the underlying CR provide a serialization facility. Thus we save all previously |
| 45 | * computed values by writing out the expression that was used to compute them, and reevaluate |
| 46 | * when reading it back in. |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 47 | */ |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 48 | class CalculatorExpr { |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame^] | 49 | /** |
| 50 | * An interface for resolving expression indices in embedded subexpressions to |
| 51 | * the associated CalculatorExpr, and associating a UnifiedReal result with it. |
| 52 | * All methods are thread-safe in the strong sense; they may be called asynchronously |
| 53 | * at any time from any thread. |
| 54 | */ |
| 55 | public interface ExprResolver { |
| 56 | /* |
| 57 | * Retrieve the expression corresponding to index. |
| 58 | */ |
| 59 | CalculatorExpr getExpr(long index); |
| 60 | /* |
| 61 | * Retrive the degree mode associated with the expression at index i. |
| 62 | */ |
| 63 | boolean getDegreeMode(long index); |
| 64 | /* |
| 65 | * Retrieve the stored result for the expression at index, or return null. |
| 66 | */ |
| 67 | UnifiedReal getResult(long index); |
| 68 | /* |
| 69 | * Atomically test for an existing result, and set it if there was none. |
| 70 | * Return the prior result if there was one, or the new one if there was not. |
| 71 | * May only be called after getExpr. |
| 72 | */ |
| 73 | UnifiedReal putResultIfAbsent(long index, UnifiedReal result); |
| 74 | // FIXME: Check that long timeouts for embedded expressions are propagated |
| 75 | // correctly. |
| 76 | } |
| 77 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 78 | private ArrayList<Token> mExpr; // The actual representation |
| 79 | // as a list of tokens. Constant |
| 80 | // tokens are always nonempty. |
| 81 | |
| 82 | private static enum TokenKind { CONSTANT, OPERATOR, PRE_EVAL }; |
| 83 | private static TokenKind[] tokenKindValues = TokenKind.values(); |
| 84 | private final static BigInteger BIG_MILLION = BigInteger.valueOf(1000000); |
| 85 | private final static BigInteger BIG_BILLION = BigInteger.valueOf(1000000000); |
| 86 | |
| 87 | private static abstract class Token { |
| 88 | abstract TokenKind kind(); |
Hans Boehm | 8a4f81c | 2015-07-09 10:41:25 -0700 | [diff] [blame] | 89 | |
| 90 | /** |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame^] | 91 | * Write token as either a very small Byte containing the TokenKind, |
| 92 | * followed by data needed by subclass constructor, |
| 93 | * or as a byte >= 0x20 directly describing the OPERATOR token. |
Hans Boehm | 8a4f81c | 2015-07-09 10:41:25 -0700 | [diff] [blame] | 94 | */ |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 95 | abstract void write(DataOutput out) throws IOException; |
Hans Boehm | 8a4f81c | 2015-07-09 10:41:25 -0700 | [diff] [blame] | 96 | |
| 97 | /** |
| 98 | * Return a textual representation of the token. |
| 99 | * The result is suitable for either display as part od the formula or TalkBack use. |
| 100 | * It may be a SpannableString that includes added TalkBack information. |
| 101 | * @param context context used for converting button ids to strings |
| 102 | */ |
| 103 | abstract CharSequence toCharSequence(Context context); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 106 | /** |
| 107 | * Representation of an operator token |
| 108 | */ |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 109 | private static class Operator extends Token { |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame^] | 110 | // TODO: rename id. |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 111 | public final int id; // We use the button resource id |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 112 | Operator(int resId) { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 113 | id = resId; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 114 | } |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame^] | 115 | Operator(byte op) throws IOException { |
| 116 | id = KeyMaps.fromByte(op); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 117 | } |
| 118 | @Override |
| 119 | void write(DataOutput out) throws IOException { |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame^] | 120 | out.writeByte(KeyMaps.toByte(id)); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 121 | } |
| 122 | @Override |
Hans Boehm | 8a4f81c | 2015-07-09 10:41:25 -0700 | [diff] [blame] | 123 | public CharSequence toCharSequence(Context context) { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 124 | String desc = KeyMaps.toDescriptiveString(context, id); |
Hans Boehm | 8a4f81c | 2015-07-09 10:41:25 -0700 | [diff] [blame] | 125 | if (desc != null) { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 126 | SpannableString result = new SpannableString(KeyMaps.toString(context, id)); |
Hans Boehm | 8a4f81c | 2015-07-09 10:41:25 -0700 | [diff] [blame] | 127 | Object descSpan = new TtsSpan.TextBuilder(desc).build(); |
| 128 | result.setSpan(descSpan, 0, result.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
| 129 | return result; |
| 130 | } else { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 131 | return KeyMaps.toString(context, id); |
Hans Boehm | 8a4f81c | 2015-07-09 10:41:25 -0700 | [diff] [blame] | 132 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 133 | } |
| 134 | @Override |
| 135 | TokenKind kind() { return TokenKind.OPERATOR; } |
| 136 | } |
| 137 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 138 | /** |
| 139 | * Representation of a (possibly incomplete) numerical constant. |
| 140 | * Supports addition and removal of trailing characters; hence mutable. |
| 141 | */ |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 142 | private static class Constant extends Token implements Cloneable { |
| 143 | private boolean mSawDecimal; |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 144 | private String mWhole; // String preceding decimal point. |
Hans Boehm | 0b9806f | 2015-06-29 16:07:15 -0700 | [diff] [blame] | 145 | private String mFraction; // String after decimal point. |
| 146 | private int mExponent; // Explicit exponent, only generated through addExponent. |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame^] | 147 | private static int SAW_DECIMAL = 0x1; |
| 148 | private static int HAS_EXPONENT = 0x2; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 149 | |
| 150 | Constant() { |
| 151 | mWhole = ""; |
| 152 | mFraction = ""; |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame^] | 153 | // mSawDecimal = false; |
| 154 | // mExponent = 0; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | Constant(DataInput in) throws IOException { |
| 158 | mWhole = in.readUTF(); |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame^] | 159 | byte flags = in.readByte(); |
| 160 | if ((flags & SAW_DECIMAL) != 0) { |
| 161 | mSawDecimal = true; |
| 162 | mFraction = in.readUTF(); |
| 163 | } else { |
| 164 | // mSawDecimal = false; |
| 165 | mFraction = ""; |
| 166 | } |
| 167 | if ((flags & HAS_EXPONENT) != 0) { |
| 168 | mExponent = in.readInt(); |
| 169 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | @Override |
| 173 | void write(DataOutput out) throws IOException { |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame^] | 174 | byte flags = (byte)((mSawDecimal ? SAW_DECIMAL : 0) |
| 175 | | (mExponent != 0 ? HAS_EXPONENT : 0)); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 176 | out.writeByte(TokenKind.CONSTANT.ordinal()); |
| 177 | out.writeUTF(mWhole); |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame^] | 178 | out.writeByte(flags); |
| 179 | if (mSawDecimal) { |
| 180 | out.writeUTF(mFraction); |
| 181 | } |
| 182 | if (mExponent != 0) { |
| 183 | out.writeInt(mExponent); |
| 184 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | // Given a button press, append corresponding digit. |
| 188 | // We assume id is a digit or decimal point. |
| 189 | // Just return false if this was the second (or later) decimal point |
| 190 | // in this constant. |
Hans Boehm | 0b9806f | 2015-06-29 16:07:15 -0700 | [diff] [blame] | 191 | // Assumes that this constant does not have an exponent. |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 192 | public boolean add(int id) { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 193 | if (id == R.id.dec_point) { |
Hans Boehm | 0b9806f | 2015-06-29 16:07:15 -0700 | [diff] [blame] | 194 | if (mSawDecimal || mExponent != 0) return false; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 195 | mSawDecimal = true; |
| 196 | return true; |
| 197 | } |
| 198 | int val = KeyMaps.digVal(id); |
Hans Boehm | 0b9806f | 2015-06-29 16:07:15 -0700 | [diff] [blame] | 199 | if (mExponent != 0) { |
| 200 | if (Math.abs(mExponent) <= 10000) { |
| 201 | if (mExponent > 0) { |
| 202 | mExponent = 10 * mExponent + val; |
| 203 | } else { |
| 204 | mExponent = 10 * mExponent - val; |
| 205 | } |
| 206 | return true; |
| 207 | } else { // Too large; refuse |
| 208 | return false; |
| 209 | } |
| 210 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 211 | if (mSawDecimal) { |
| 212 | mFraction += val; |
| 213 | } else { |
| 214 | mWhole += val; |
| 215 | } |
| 216 | return true; |
| 217 | } |
| 218 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 219 | public void addExponent(int exp) { |
Hans Boehm | 0b9806f | 2015-06-29 16:07:15 -0700 | [diff] [blame] | 220 | // Note that adding a 0 exponent is a no-op. That's OK. |
| 221 | mExponent = exp; |
| 222 | } |
| 223 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 224 | /** |
| 225 | * Undo the last add or remove last exponent digit. |
| 226 | * Assumes the constant is nonempty. |
| 227 | */ |
| 228 | public void delete() { |
Hans Boehm | 0b9806f | 2015-06-29 16:07:15 -0700 | [diff] [blame] | 229 | if (mExponent != 0) { |
| 230 | mExponent /= 10; |
| 231 | // Once zero, it can only be added back with addExponent. |
| 232 | } else if (!mFraction.isEmpty()) { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 233 | mFraction = mFraction.substring(0, mFraction.length() - 1); |
| 234 | } else if (mSawDecimal) { |
| 235 | mSawDecimal = false; |
| 236 | } else { |
| 237 | mWhole = mWhole.substring(0, mWhole.length() - 1); |
| 238 | } |
| 239 | } |
| 240 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 241 | public boolean isEmpty() { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 242 | return (mSawDecimal == false && mWhole.isEmpty()); |
| 243 | } |
| 244 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 245 | /** |
| 246 | * Produce human-readable string representation of constant, as typed. |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 247 | * We do add digit grouping separators to the whole number, even if not typed. |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 248 | * Result is internationalized. |
| 249 | */ |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 250 | @Override |
| 251 | public String toString() { |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 252 | String result; |
| 253 | if (mExponent != 0) { |
| 254 | result = mWhole; |
| 255 | } else { |
| 256 | result = StringUtils.addCommas(mWhole, 0, mWhole.length()); |
| 257 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 258 | if (mSawDecimal) { |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 259 | result += '.'; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 260 | result += mFraction; |
| 261 | } |
Hans Boehm | 0b9806f | 2015-06-29 16:07:15 -0700 | [diff] [blame] | 262 | if (mExponent != 0) { |
| 263 | result += "E" + mExponent; |
| 264 | } |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 265 | return KeyMaps.translateResult(result); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 268 | /** |
Hans Boehm | fa5203c | 2015-08-17 16:14:52 -0700 | [diff] [blame] | 269 | * Return BoundedRational representation of constant, if well-formed. |
| 270 | * Result is never null. |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 271 | */ |
Hans Boehm | fa5203c | 2015-08-17 16:14:52 -0700 | [diff] [blame] | 272 | public BoundedRational toRational() throws SyntaxException { |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 273 | String whole = mWhole; |
Hans Boehm | fa5203c | 2015-08-17 16:14:52 -0700 | [diff] [blame] | 274 | if (whole.isEmpty()) { |
| 275 | if (mFraction.isEmpty()) { |
| 276 | // Decimal point without digits. |
| 277 | throw new SyntaxException(); |
| 278 | } else { |
| 279 | whole = "0"; |
| 280 | } |
| 281 | } |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 282 | BigInteger num = new BigInteger(whole + mFraction); |
Hans Boehm | 682ff5e | 2015-03-09 14:40:25 -0700 | [diff] [blame] | 283 | BigInteger den = BigInteger.TEN.pow(mFraction.length()); |
Hans Boehm | 0b9806f | 2015-06-29 16:07:15 -0700 | [diff] [blame] | 284 | if (mExponent > 0) { |
| 285 | num = num.multiply(BigInteger.TEN.pow(mExponent)); |
| 286 | } |
| 287 | if (mExponent < 0) { |
| 288 | den = den.multiply(BigInteger.TEN.pow(-mExponent)); |
| 289 | } |
Hans Boehm | 682ff5e | 2015-03-09 14:40:25 -0700 | [diff] [blame] | 290 | return new BoundedRational(num, den); |
| 291 | } |
| 292 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 293 | @Override |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 294 | public CharSequence toCharSequence(Context context) { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 295 | return toString(); |
| 296 | } |
| 297 | |
| 298 | @Override |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 299 | public TokenKind kind() { |
| 300 | return TokenKind.CONSTANT; |
| 301 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 302 | |
| 303 | // Override clone to make it public |
| 304 | @Override |
| 305 | public Object clone() { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 306 | Constant result = new Constant(); |
| 307 | result.mWhole = mWhole; |
| 308 | result.mFraction = mFraction; |
| 309 | result.mSawDecimal = mSawDecimal; |
| 310 | result.mExponent = mExponent; |
| 311 | return result; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 315 | /** |
| 316 | * The "token" class for previously evaluated subexpressions. |
| 317 | * We treat previously evaluated subexpressions as tokens. These are inserted when we either |
| 318 | * continue an expression after evaluating some of it, or copy an expression and paste it back |
| 319 | * in. |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame^] | 320 | * This only contains enough information to allow us to display the expression in a |
| 321 | * formula, or reevaluate the expression with the aid of an ExprResolver; we no longer |
| 322 | * cache the result. The expression corresponding to the index can be obtained through |
| 323 | * the ExprResolver, which looks it up in a subexpression database. |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 324 | * The representation includes a UnifiedReal value. In order to |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 325 | * support saving and restoring, we also include the underlying expression itself, and the |
| 326 | * context (currently just degree mode) used to evaluate it. The short string representation |
| 327 | * is also stored in order to avoid potentially expensive recomputation in the UI thread. |
| 328 | */ |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 329 | private static class PreEval extends Token { |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame^] | 330 | public final long mIndex; |
Hans Boehm | 50ed320 | 2015-06-09 14:35:49 -0700 | [diff] [blame] | 331 | private final String mShortRep; // Not internationalized. |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame^] | 332 | PreEval(long index, String shortRep) { |
| 333 | mIndex = index; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 334 | mShortRep = shortRep; |
| 335 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 336 | @Override |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame^] | 337 | // This writes out only a shallow representation of the result, without |
| 338 | // information about subexpressions. To write out a deep representation, we |
| 339 | // find referenced subexpressions, and iteratively write those as well. |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 340 | public void write(DataOutput out) throws IOException { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 341 | out.writeByte(TokenKind.PRE_EVAL.ordinal()); |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame^] | 342 | if (mIndex > Integer.MAX_VALUE || mIndex < Integer.MIN_VALUE) { |
| 343 | // This would be millions of expressions per day for the life of the device. |
| 344 | throw new AssertionError("Expression index too big"); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 345 | } |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame^] | 346 | out.writeInt((int)mIndex); |
| 347 | out.writeUTF(mShortRep); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 348 | } |
| 349 | PreEval(DataInput in) throws IOException { |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame^] | 350 | mIndex = in.readInt(); |
| 351 | mShortRep = in.readUTF(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 352 | } |
| 353 | @Override |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 354 | public CharSequence toCharSequence(Context context) { |
Hans Boehm | 50ed320 | 2015-06-09 14:35:49 -0700 | [diff] [blame] | 355 | return KeyMaps.translateResult(mShortRep); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 356 | } |
| 357 | @Override |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 358 | public TokenKind kind() { |
Hans Boehm | 187d3e9 | 2015-06-09 18:04:26 -0700 | [diff] [blame] | 359 | return TokenKind.PRE_EVAL; |
| 360 | } |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 361 | public boolean hasEllipsis() { |
Hans Boehm | 187d3e9 | 2015-06-09 18:04:26 -0700 | [diff] [blame] | 362 | return mShortRep.lastIndexOf(KeyMaps.ELLIPSIS) != -1; |
| 363 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 364 | } |
| 365 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 366 | /** |
| 367 | * Read token from in. |
| 368 | */ |
| 369 | public static Token newToken(DataInput in) throws IOException { |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame^] | 370 | byte kindByte = in.readByte(); |
| 371 | if (kindByte < 0x20) { |
| 372 | TokenKind kind = tokenKindValues[kindByte]; |
| 373 | switch(kind) { |
| 374 | case CONSTANT: |
| 375 | return new Constant(in); |
| 376 | case PRE_EVAL: |
| 377 | return new PreEval(in); |
| 378 | default: throw new IOException("Bad save file format"); |
| 379 | } |
| 380 | } else { |
| 381 | return new Operator(kindByte); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 382 | } |
| 383 | } |
| 384 | |
| 385 | CalculatorExpr() { |
| 386 | mExpr = new ArrayList<Token>(); |
| 387 | } |
| 388 | |
| 389 | private CalculatorExpr(ArrayList<Token> expr) { |
| 390 | mExpr = expr; |
| 391 | } |
| 392 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 393 | /** |
| 394 | * Construct CalculatorExpr, by reading it from in. |
| 395 | */ |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 396 | CalculatorExpr(DataInput in) throws IOException { |
| 397 | mExpr = new ArrayList<Token>(); |
| 398 | int size = in.readInt(); |
| 399 | for (int i = 0; i < size; ++i) { |
| 400 | mExpr.add(newToken(in)); |
| 401 | } |
| 402 | } |
| 403 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 404 | /** |
| 405 | * Write this expression to out. |
| 406 | */ |
| 407 | public void write(DataOutput out) throws IOException { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 408 | int size = mExpr.size(); |
| 409 | out.writeInt(size); |
| 410 | for (int i = 0; i < size; ++i) { |
| 411 | mExpr.get(i).write(out); |
| 412 | } |
| 413 | } |
| 414 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 415 | /** |
| 416 | * Does this expression end with a numeric constant? |
| 417 | * As opposed to an operator or preevaluated expression. |
| 418 | */ |
Hans Boehm | 0b9806f | 2015-06-29 16:07:15 -0700 | [diff] [blame] | 419 | boolean hasTrailingConstant() { |
| 420 | int s = mExpr.size(); |
| 421 | if (s == 0) { |
| 422 | return false; |
| 423 | } |
| 424 | Token t = mExpr.get(s-1); |
| 425 | return t instanceof Constant; |
| 426 | } |
| 427 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 428 | /** |
| 429 | * Does this expression end with a binary operator? |
| 430 | */ |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame^] | 431 | boolean hasTrailingBinary() { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 432 | int s = mExpr.size(); |
| 433 | if (s == 0) return false; |
| 434 | Token t = mExpr.get(s-1); |
| 435 | if (!(t instanceof Operator)) return false; |
| 436 | Operator o = (Operator)t; |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 437 | return (KeyMaps.isBinary(o.id)); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 438 | } |
| 439 | |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 440 | /** |
| 441 | * Append press of button with given id to expression. |
| 442 | * If the insertion would clearly result in a syntax error, either just return false |
| 443 | * and do nothing, or make an adjustment to avoid the problem. We do the latter only |
| 444 | * for unambiguous consecutive binary operators, in which case we delete the first |
| 445 | * operator. |
| 446 | */ |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 447 | boolean add(int id) { |
| 448 | int s = mExpr.size(); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 449 | final int d = KeyMaps.digVal(id); |
| 450 | final boolean binary = KeyMaps.isBinary(id); |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 451 | Token lastTok = s == 0 ? null : mExpr.get(s-1); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 452 | int lastOp = lastTok instanceof Operator ? ((Operator) lastTok).id : 0; |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 453 | // Quietly replace a trailing binary operator with another one, unless the second |
| 454 | // operator is minus, in which case we just allow it as a unary minus. |
| 455 | if (binary && !KeyMaps.isPrefix(id)) { |
| 456 | if (s == 0 || lastOp == R.id.lparen || KeyMaps.isFunc(lastOp) |
| 457 | || KeyMaps.isPrefix(lastOp) && lastOp != R.id.op_sub) { |
| 458 | return false; |
| 459 | } |
| 460 | while (hasTrailingBinary()) { |
| 461 | delete(); |
| 462 | } |
| 463 | // s invalid and not used below. |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 464 | } |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 465 | final boolean isConstPiece = (d != KeyMaps.NOT_DIGIT || id == R.id.dec_point); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 466 | if (isConstPiece) { |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 467 | // Since we treat juxtaposition as multiplication, a constant can appear anywhere. |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 468 | if (s == 0) { |
| 469 | mExpr.add(new Constant()); |
| 470 | s++; |
| 471 | } else { |
| 472 | Token last = mExpr.get(s-1); |
| 473 | if(!(last instanceof Constant)) { |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 474 | if (last instanceof PreEval) { |
| 475 | // Add explicit multiplication to avoid confusing display. |
| 476 | mExpr.add(new Operator(R.id.op_mul)); |
| 477 | s++; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 478 | } |
| 479 | mExpr.add(new Constant()); |
| 480 | s++; |
| 481 | } |
| 482 | } |
| 483 | return ((Constant)(mExpr.get(s-1))).add(id); |
| 484 | } else { |
| 485 | mExpr.add(new Operator(id)); |
| 486 | return true; |
| 487 | } |
| 488 | } |
| 489 | |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 490 | /** |
Hans Boehm | 0b9806f | 2015-06-29 16:07:15 -0700 | [diff] [blame] | 491 | * Add exponent to the constant at the end of the expression. |
| 492 | * Assumes there is a constant at the end of the expression. |
| 493 | */ |
| 494 | void addExponent(int exp) { |
| 495 | Token lastTok = mExpr.get(mExpr.size() - 1); |
| 496 | ((Constant) lastTok).addExponent(exp); |
| 497 | } |
| 498 | |
| 499 | /** |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 500 | * Remove trailing op_add and op_sub operators. |
| 501 | */ |
| 502 | void removeTrailingAdditiveOperators() { |
| 503 | while (true) { |
| 504 | int s = mExpr.size(); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 505 | if (s == 0) { |
| 506 | break; |
| 507 | } |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 508 | Token lastTok = mExpr.get(s-1); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 509 | if (!(lastTok instanceof Operator)) { |
| 510 | break; |
| 511 | } |
| 512 | int lastOp = ((Operator) lastTok).id; |
| 513 | if (lastOp != R.id.op_add && lastOp != R.id.op_sub) { |
| 514 | break; |
| 515 | } |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 516 | delete(); |
| 517 | } |
| 518 | } |
| 519 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 520 | /** |
| 521 | * Append the contents of the argument expression. |
| 522 | * It is assumed that the argument expression will not change, and thus its pieces can be |
| 523 | * reused directly. |
| 524 | */ |
| 525 | public void append(CalculatorExpr expr2) { |
Hans Boehm | fbcef70 | 2015-04-27 18:07:47 -0700 | [diff] [blame] | 526 | int s = mExpr.size(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 527 | int s2 = expr2.mExpr.size(); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 528 | // Check that we're not concatenating Constant or PreEval tokens, since the result would |
| 529 | // look like a single constant, with very mysterious results for the user. |
Hans Boehm | fbcef70 | 2015-04-27 18:07:47 -0700 | [diff] [blame] | 530 | if (s != 0 && s2 != 0) { |
| 531 | Token last = mExpr.get(s-1); |
| 532 | Token first = expr2.mExpr.get(0); |
| 533 | if (!(first instanceof Operator) && !(last instanceof Operator)) { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 534 | // Fudge it by adding an explicit multiplication. We would have interpreted it as |
| 535 | // such anyway, and this makes it recognizable to the user. |
Hans Boehm | fbcef70 | 2015-04-27 18:07:47 -0700 | [diff] [blame] | 536 | mExpr.add(new Operator(R.id.op_mul)); |
| 537 | } |
| 538 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 539 | for (int i = 0; i < s2; ++i) { |
| 540 | mExpr.add(expr2.mExpr.get(i)); |
| 541 | } |
| 542 | } |
| 543 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 544 | /** |
| 545 | * Undo the last key addition, if any. |
| 546 | * Or possibly remove a trailing exponent digit. |
| 547 | */ |
| 548 | public void delete() { |
| 549 | final int s = mExpr.size(); |
| 550 | if (s == 0) { |
| 551 | return; |
| 552 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 553 | Token last = mExpr.get(s-1); |
| 554 | if (last instanceof Constant) { |
| 555 | Constant c = (Constant)last; |
| 556 | c.delete(); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 557 | if (!c.isEmpty()) { |
| 558 | return; |
| 559 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 560 | } |
| 561 | mExpr.remove(s-1); |
| 562 | } |
| 563 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 564 | /** |
| 565 | * Remove all tokens from the expression. |
| 566 | */ |
| 567 | public void clear() { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 568 | mExpr.clear(); |
| 569 | } |
| 570 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 571 | public boolean isEmpty() { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 572 | return mExpr.isEmpty(); |
| 573 | } |
| 574 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 575 | /** |
| 576 | * Returns a logical deep copy of the CalculatorExpr. |
| 577 | * Operator and PreEval tokens are immutable, and thus aren't really copied. |
| 578 | */ |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 579 | public Object clone() { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 580 | CalculatorExpr result = new CalculatorExpr(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 581 | for (Token t: mExpr) { |
| 582 | if (t instanceof Constant) { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 583 | result.mExpr.add((Token)(((Constant)t).clone())); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 584 | } else { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 585 | result.mExpr.add(t); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 586 | } |
| 587 | } |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 588 | return result; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | // Am I just a constant? |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 592 | public boolean isConstant() { |
| 593 | if (mExpr.size() != 1) { |
| 594 | return false; |
| 595 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 596 | return mExpr.get(0) instanceof Constant; |
| 597 | } |
| 598 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 599 | /** |
| 600 | * Return a new expression consisting of a single token representing the current pre-evaluated |
| 601 | * expression. |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame^] | 602 | * The caller supplies the expression index and short string representation. |
| 603 | * The expression must have been previously evaluated. |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 604 | */ |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame^] | 605 | public CalculatorExpr abbreviate(long index, String sr) { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 606 | CalculatorExpr result = new CalculatorExpr(); |
Justin Klaassen | 0ace4eb | 2016-02-05 11:38:12 -0800 | [diff] [blame] | 607 | @SuppressWarnings("unchecked") |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame^] | 608 | Token t = new PreEval(index, sr); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 609 | result.mExpr.add(t); |
| 610 | return result; |
| 611 | } |
| 612 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 613 | /** |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 614 | * Internal evaluation functions return an EvalRet pair. |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 615 | * We compute rational (BoundedRational) results when possible, both as a performance |
| 616 | * optimization, and to detect errors exactly when we can. |
| 617 | */ |
| 618 | private static class EvalRet { |
| 619 | public int pos; // Next position (expression index) to be parsed. |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 620 | public final UnifiedReal val; // Constructive Real result of evaluating subexpression. |
| 621 | EvalRet(int p, UnifiedReal v) { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 622 | pos = p; |
| 623 | val = v; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 624 | } |
| 625 | } |
| 626 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 627 | /** |
| 628 | * Internal evaluation functions take an EvalContext argument. |
| 629 | */ |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 630 | private static class EvalContext { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 631 | public final int mPrefixLength; // Length of prefix to evaluate. Not explicitly saved. |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 632 | public final boolean mDegreeMode; |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame^] | 633 | public final ExprResolver mExprResolver; // Reconstructed, not saved. |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 634 | // If we add any other kinds of evaluation modes, they go here. |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame^] | 635 | EvalContext(boolean degreeMode, int len, ExprResolver er) { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 636 | mDegreeMode = degreeMode; |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 637 | mPrefixLength = len; |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame^] | 638 | mExprResolver = er; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 639 | } |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame^] | 640 | EvalContext(DataInput in, int len, ExprResolver er) throws IOException { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 641 | mDegreeMode = in.readBoolean(); |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 642 | mPrefixLength = len; |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame^] | 643 | mExprResolver = er; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 644 | } |
| 645 | void write(DataOutput out) throws IOException { |
| 646 | out.writeBoolean(mDegreeMode); |
| 647 | } |
| 648 | } |
| 649 | |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 650 | private UnifiedReal toRadians(UnifiedReal x, EvalContext ec) { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 651 | if (ec.mDegreeMode) { |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 652 | return x.multiply(UnifiedReal.RADIANS_PER_DEGREE); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 653 | } else { |
| 654 | return x; |
| 655 | } |
| 656 | } |
| 657 | |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 658 | private UnifiedReal fromRadians(UnifiedReal x, EvalContext ec) { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 659 | if (ec.mDegreeMode) { |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 660 | return x.divide(UnifiedReal.RADIANS_PER_DEGREE); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 661 | } else { |
| 662 | return x; |
| 663 | } |
| 664 | } |
| 665 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 666 | // The following methods can all throw IndexOutOfBoundsException in the event of a syntax |
| 667 | // error. We expect that to be caught in eval below. |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 668 | |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 669 | private boolean isOperatorUnchecked(int i, int op) { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 670 | Token t = mExpr.get(i); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 671 | if (!(t instanceof Operator)) { |
| 672 | return false; |
| 673 | } |
| 674 | return ((Operator)(t)).id == op; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 675 | } |
| 676 | |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 677 | private boolean isOperator(int i, int op, EvalContext ec) { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 678 | if (i >= ec.mPrefixLength) { |
| 679 | return false; |
| 680 | } |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 681 | return isOperatorUnchecked(i, op); |
| 682 | } |
| 683 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 684 | public static class SyntaxException extends Exception { |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 685 | public SyntaxException() { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 686 | super(); |
| 687 | } |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 688 | public SyntaxException(String s) { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 689 | super(s); |
| 690 | } |
| 691 | } |
| 692 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 693 | // The following functions all evaluate some kind of expression starting at position i in |
| 694 | // mExpr in a specified evaluation context. They return both the expression value (as |
| 695 | // 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] | 696 | // that was not used as part of the evaluation. |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 697 | // This is essentially a simple recursive descent parser combined with expression evaluation. |
| 698 | |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 699 | private EvalRet evalUnary(int i, EvalContext ec) throws SyntaxException { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 700 | final Token t = mExpr.get(i); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 701 | if (t instanceof Constant) { |
| 702 | Constant c = (Constant)t; |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 703 | return new EvalRet(i+1,new UnifiedReal(c.toRational())); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 704 | } |
| 705 | if (t instanceof PreEval) { |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame^] | 706 | final long index = ((PreEval)t).mIndex; |
| 707 | UnifiedReal res = ec.mExprResolver.getResult(index); |
| 708 | if (res == null) { |
| 709 | CalculatorExpr nestedExpr = ec.mExprResolver.getExpr(index); |
| 710 | EvalContext newEc = new EvalContext(ec.mExprResolver.getDegreeMode(index), |
| 711 | nestedExpr.trailingBinaryOpsStart(), ec.mExprResolver); |
| 712 | EvalRet new_res = nestedExpr.evalExpr(0, newEc); |
| 713 | res = ec.mExprResolver.putResultIfAbsent(index, new_res.val); |
| 714 | } |
| 715 | return new EvalRet(i+1, res); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 716 | } |
| 717 | EvalRet argVal; |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 718 | switch(((Operator)(t)).id) { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 719 | case R.id.const_pi: |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 720 | return new EvalRet(i+1, UnifiedReal.PI); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 721 | case R.id.const_e: |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 722 | return new EvalRet(i+1, UnifiedReal.E); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 723 | case R.id.op_sqrt: |
Hans Boehm | fbcef70 | 2015-04-27 18:07:47 -0700 | [diff] [blame] | 724 | // Seems to have highest precedence. |
| 725 | // Does not add implicit paren. |
| 726 | // Does seem to accept a leading minus. |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 727 | if (isOperator(i+1, R.id.op_sub, ec)) { |
Hans Boehm | fbcef70 | 2015-04-27 18:07:47 -0700 | [diff] [blame] | 728 | argVal = evalUnary(i+2, ec); |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 729 | return new EvalRet(argVal.pos, argVal.val.negate().sqrt()); |
Hans Boehm | fbcef70 | 2015-04-27 18:07:47 -0700 | [diff] [blame] | 730 | } else { |
| 731 | argVal = evalUnary(i+1, ec); |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 732 | return new EvalRet(argVal.pos, argVal.val.sqrt()); |
Hans Boehm | fbcef70 | 2015-04-27 18:07:47 -0700 | [diff] [blame] | 733 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 734 | case R.id.lparen: |
| 735 | argVal = evalExpr(i+1, ec); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 736 | if (isOperator(argVal.pos, R.id.rparen, ec)) { |
| 737 | argVal.pos++; |
| 738 | } |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 739 | return new EvalRet(argVal.pos, argVal.val); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 740 | case R.id.fun_sin: |
| 741 | argVal = evalExpr(i+1, ec); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 742 | if (isOperator(argVal.pos, R.id.rparen, ec)) { |
| 743 | argVal.pos++; |
| 744 | } |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 745 | return new EvalRet(argVal.pos, toRadians(argVal.val, ec).sin()); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 746 | case R.id.fun_cos: |
| 747 | argVal = evalExpr(i+1, ec); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 748 | if (isOperator(argVal.pos, R.id.rparen, ec)) { |
| 749 | argVal.pos++; |
| 750 | } |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 751 | return new EvalRet(argVal.pos, toRadians(argVal.val,ec).cos()); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 752 | case R.id.fun_tan: |
| 753 | argVal = evalExpr(i+1, ec); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 754 | if (isOperator(argVal.pos, R.id.rparen, ec)) { |
| 755 | argVal.pos++; |
| 756 | } |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 757 | UnifiedReal arg = toRadians(argVal.val, ec); |
| 758 | return new EvalRet(argVal.pos, arg.sin().divide(arg.cos())); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 759 | case R.id.fun_ln: |
| 760 | argVal = evalExpr(i+1, ec); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 761 | if (isOperator(argVal.pos, R.id.rparen, ec)) { |
| 762 | argVal.pos++; |
| 763 | } |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 764 | return new EvalRet(argVal.pos, argVal.val.ln()); |
Hans Boehm | 4db31b4 | 2015-05-31 12:19:05 -0700 | [diff] [blame] | 765 | case R.id.fun_exp: |
| 766 | argVal = evalExpr(i+1, ec); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 767 | if (isOperator(argVal.pos, R.id.rparen, ec)) { |
| 768 | argVal.pos++; |
| 769 | } |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 770 | return new EvalRet(argVal.pos, argVal.val.exp()); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 771 | case R.id.fun_log: |
| 772 | argVal = evalExpr(i+1, ec); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 773 | if (isOperator(argVal.pos, R.id.rparen, ec)) { |
| 774 | argVal.pos++; |
| 775 | } |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 776 | return new EvalRet(argVal.pos, argVal.val.ln().divide(UnifiedReal.TEN.ln())); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 777 | case R.id.fun_arcsin: |
| 778 | argVal = evalExpr(i+1, ec); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 779 | if (isOperator(argVal.pos, R.id.rparen, ec)) { |
| 780 | argVal.pos++; |
| 781 | } |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 782 | return new EvalRet(argVal.pos, fromRadians(argVal.val.asin(), ec)); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 783 | case R.id.fun_arccos: |
| 784 | argVal = evalExpr(i+1, ec); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 785 | if (isOperator(argVal.pos, R.id.rparen, ec)) { |
| 786 | argVal.pos++; |
| 787 | } |
Hans Boehm | 79f273c | 2016-06-09 11:00:27 -0700 | [diff] [blame] | 788 | return new EvalRet(argVal.pos, fromRadians(argVal.val.acos(), ec)); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 789 | case R.id.fun_arctan: |
| 790 | argVal = evalExpr(i+1, ec); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 791 | if (isOperator(argVal.pos, R.id.rparen, ec)) { |
| 792 | argVal.pos++; |
| 793 | } |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 794 | return new EvalRet(argVal.pos, fromRadians(argVal.val.atan(),ec)); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 795 | default: |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 796 | throw new SyntaxException("Unrecognized token in expression"); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 797 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 798 | } |
| 799 | |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 800 | private static final UnifiedReal ONE_HUNDREDTH = new UnifiedReal(100).inverse(); |
Hans Boehm | 682ff5e | 2015-03-09 14:40:25 -0700 | [diff] [blame] | 801 | |
Hans Boehm | 4db31b4 | 2015-05-31 12:19:05 -0700 | [diff] [blame] | 802 | private EvalRet evalSuffix(int i, EvalContext ec) throws SyntaxException { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 803 | final EvalRet tmp = evalUnary(i, ec); |
| 804 | int cpos = tmp.pos; |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 805 | UnifiedReal val = tmp.val; |
| 806 | |
Hans Boehm | 4db31b4 | 2015-05-31 12:19:05 -0700 | [diff] [blame] | 807 | boolean isFact; |
| 808 | boolean isSquared = false; |
| 809 | while ((isFact = isOperator(cpos, R.id.op_fact, ec)) || |
| 810 | (isSquared = isOperator(cpos, R.id.op_sqr, ec)) || |
| 811 | isOperator(cpos, R.id.op_pct, ec)) { |
| 812 | if (isFact) { |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 813 | val = val.fact(); |
Hans Boehm | 4db31b4 | 2015-05-31 12:19:05 -0700 | [diff] [blame] | 814 | } else if (isSquared) { |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 815 | val = val.multiply(val); |
Hans Boehm | 4db31b4 | 2015-05-31 12:19:05 -0700 | [diff] [blame] | 816 | } else /* percent */ { |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 817 | val = val.multiply(ONE_HUNDREDTH); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 818 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 819 | ++cpos; |
| 820 | } |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 821 | return new EvalRet(cpos, val); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 822 | } |
| 823 | |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 824 | private EvalRet evalFactor(int i, EvalContext ec) throws SyntaxException { |
Hans Boehm | 4db31b4 | 2015-05-31 12:19:05 -0700 | [diff] [blame] | 825 | final EvalRet result1 = evalSuffix(i, ec); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 826 | int cpos = result1.pos; // current position |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 827 | UnifiedReal val = result1.val; // value so far |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 828 | if (isOperator(cpos, R.id.op_pow, ec)) { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 829 | final EvalRet exp = evalSignedFactor(cpos + 1, ec); |
| 830 | cpos = exp.pos; |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 831 | val = val.pow(exp.val); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 832 | } |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 833 | return new EvalRet(cpos, val); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 834 | } |
| 835 | |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 836 | private EvalRet evalSignedFactor(int i, EvalContext ec) throws SyntaxException { |
| 837 | final boolean negative = isOperator(i, R.id.op_sub, ec); |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 838 | int cpos = negative ? i + 1 : i; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 839 | EvalRet tmp = evalFactor(cpos, ec); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 840 | cpos = tmp.pos; |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 841 | final UnifiedReal result = negative ? tmp.val.negate() : tmp.val; |
| 842 | return new EvalRet(cpos, result); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | private boolean canStartFactor(int i) { |
| 846 | if (i >= mExpr.size()) return false; |
| 847 | Token t = mExpr.get(i); |
| 848 | if (!(t instanceof Operator)) return true; |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 849 | int id = ((Operator)(t)).id; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 850 | if (KeyMaps.isBinary(id)) return false; |
| 851 | switch (id) { |
| 852 | case R.id.op_fact: |
| 853 | case R.id.rparen: |
| 854 | return false; |
| 855 | default: |
| 856 | return true; |
| 857 | } |
| 858 | } |
| 859 | |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 860 | private EvalRet evalTerm(int i, EvalContext ec) throws SyntaxException { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 861 | EvalRet tmp = evalSignedFactor(i, ec); |
| 862 | boolean is_mul = false; |
| 863 | boolean is_div = false; |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 864 | int cpos = tmp.pos; // Current position in expression. |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 865 | UnifiedReal val = tmp.val; // Current value. |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 866 | while ((is_mul = isOperator(cpos, R.id.op_mul, ec)) |
| 867 | || (is_div = isOperator(cpos, R.id.op_div, ec)) |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 868 | || canStartFactor(cpos)) { |
| 869 | if (is_mul || is_div) ++cpos; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 870 | tmp = evalSignedFactor(cpos, ec); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 871 | if (is_div) { |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 872 | val = val.divide(tmp.val); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 873 | } else { |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 874 | val = val.multiply(tmp.val); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 875 | } |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 876 | cpos = tmp.pos; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 877 | is_mul = is_div = false; |
| 878 | } |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 879 | return new EvalRet(cpos, val); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 880 | } |
| 881 | |
Hans Boehm | 8afd0f8 | 2015-07-30 17:00:33 -0700 | [diff] [blame] | 882 | /** |
| 883 | * Is the subexpression starting at pos a simple percent constant? |
| 884 | * This is used to recognize exppressions like 200+10%, which we handle specially. |
| 885 | * This is defined as a Constant or PreEval token, followed by a percent sign, and followed |
| 886 | * by either nothing or an additive operator. |
| 887 | * Note that we are intentionally far more restrictive in recognizing such expressions than |
| 888 | * e.g. http://blogs.msdn.com/b/oldnewthing/archive/2008/01/10/7047497.aspx . |
| 889 | * When in doubt, we fall back to the the naive interpretation of % as 1/100. |
| 890 | * Note that 100+(10)% yields 100.1 while 100+10% yields 110. This may be controversial, |
| 891 | * but is consistent with Google web search. |
| 892 | */ |
| 893 | private boolean isPercent(int pos) { |
| 894 | if (mExpr.size() < pos + 2 || !isOperatorUnchecked(pos + 1, R.id.op_pct)) { |
| 895 | return false; |
| 896 | } |
| 897 | Token number = mExpr.get(pos); |
| 898 | if (number instanceof Operator) { |
| 899 | return false; |
| 900 | } |
| 901 | if (mExpr.size() == pos + 2) { |
| 902 | return true; |
| 903 | } |
| 904 | if (!(mExpr.get(pos + 2) instanceof Operator)) { |
| 905 | return false; |
| 906 | } |
| 907 | Operator op = (Operator) mExpr.get(pos + 2); |
Hans Boehm | 64d1cdb | 2016-03-08 19:14:07 -0800 | [diff] [blame] | 908 | return op.id == R.id.op_add || op.id == R.id.op_sub || op.id == R.id.rparen; |
Hans Boehm | 8afd0f8 | 2015-07-30 17:00:33 -0700 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | /** |
| 912 | * Compute the multiplicative factor corresponding to an N% addition or subtraction. |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 913 | * @param pos position of Constant or PreEval expression token corresponding to N. |
| 914 | * @param isSubtraction this is a subtraction, as opposed to addition. |
| 915 | * @param ec usable evaluation contex; only length matters. |
| 916 | * @return UnifiedReal value and position, which is pos + 2, i.e. after percent sign |
Hans Boehm | 8afd0f8 | 2015-07-30 17:00:33 -0700 | [diff] [blame] | 917 | */ |
| 918 | private EvalRet getPercentFactor(int pos, boolean isSubtraction, EvalContext ec) |
| 919 | throws SyntaxException { |
| 920 | EvalRet tmp = evalUnary(pos, ec); |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 921 | UnifiedReal val = isSubtraction ? tmp.val.negate() : tmp.val; |
| 922 | val = UnifiedReal.ONE.add(val.multiply(ONE_HUNDREDTH)); |
| 923 | return new EvalRet(pos + 2 /* after percent sign */, val); |
Hans Boehm | 8afd0f8 | 2015-07-30 17:00:33 -0700 | [diff] [blame] | 924 | } |
| 925 | |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 926 | private EvalRet evalExpr(int i, EvalContext ec) throws SyntaxException { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 927 | EvalRet tmp = evalTerm(i, ec); |
| 928 | boolean is_plus; |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 929 | int cpos = tmp.pos; |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 930 | UnifiedReal val = tmp.val; |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 931 | while ((is_plus = isOperator(cpos, R.id.op_add, ec)) |
| 932 | || isOperator(cpos, R.id.op_sub, ec)) { |
Hans Boehm | 8afd0f8 | 2015-07-30 17:00:33 -0700 | [diff] [blame] | 933 | if (isPercent(cpos + 1)) { |
| 934 | tmp = getPercentFactor(cpos + 1, !is_plus, ec); |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 935 | val = val.multiply(tmp.val); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 936 | } else { |
Hans Boehm | 8afd0f8 | 2015-07-30 17:00:33 -0700 | [diff] [blame] | 937 | tmp = evalTerm(cpos + 1, ec); |
| 938 | if (is_plus) { |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 939 | val = val.add(tmp.val); |
Hans Boehm | 682ff5e | 2015-03-09 14:40:25 -0700 | [diff] [blame] | 940 | } else { |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 941 | val = val.subtract(tmp.val); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 942 | } |
| 943 | } |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 944 | cpos = tmp.pos; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 945 | } |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 946 | return new EvalRet(cpos, val); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 947 | } |
| 948 | |
Hans Boehm | e855376 | 2015-06-26 17:56:52 -0700 | [diff] [blame] | 949 | /** |
| 950 | * Return the starting position of the sequence of trailing binary operators. |
| 951 | */ |
| 952 | private int trailingBinaryOpsStart() { |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 953 | int result = mExpr.size(); |
| 954 | while (result > 0) { |
| 955 | Token last = mExpr.get(result - 1); |
| 956 | if (!(last instanceof Operator)) break; |
| 957 | Operator o = (Operator)last; |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 958 | if (!KeyMaps.isBinary(o.id)) break; |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 959 | --result; |
| 960 | } |
| 961 | return result; |
| 962 | } |
| 963 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 964 | /** |
| 965 | * Is the current expression worth evaluating? |
| 966 | */ |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 967 | public boolean hasInterestingOps() { |
Hans Boehm | e855376 | 2015-06-26 17:56:52 -0700 | [diff] [blame] | 968 | int last = trailingBinaryOpsStart(); |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 969 | int first = 0; |
| 970 | if (last > first && isOperatorUnchecked(first, R.id.op_sub)) { |
| 971 | // Leading minus is not by itself interesting. |
| 972 | first++; |
| 973 | } |
| 974 | for (int i = first; i < last; ++i) { |
| 975 | Token t1 = mExpr.get(i); |
Hans Boehm | 187d3e9 | 2015-06-09 18:04:26 -0700 | [diff] [blame] | 976 | if (t1 instanceof Operator |
| 977 | || t1 instanceof PreEval && ((PreEval)t1).hasEllipsis()) { |
| 978 | return true; |
| 979 | } |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 980 | } |
| 981 | return false; |
| 982 | } |
| 983 | |
Hans Boehm | e855376 | 2015-06-26 17:56:52 -0700 | [diff] [blame] | 984 | /** |
Hans Boehm | 52d477a | 2016-04-01 17:42:50 -0700 | [diff] [blame] | 985 | * Does the expression contain trig operations? |
| 986 | */ |
| 987 | public boolean hasTrigFuncs() { |
| 988 | for (Token t: mExpr) { |
| 989 | if (t instanceof Operator) { |
| 990 | Operator o = (Operator)t; |
| 991 | if (KeyMaps.isTrigFunc(o.id)) { |
| 992 | return true; |
| 993 | } |
| 994 | } |
| 995 | } |
| 996 | return false; |
| 997 | } |
| 998 | |
| 999 | /** |
Hans Boehm | e855376 | 2015-06-26 17:56:52 -0700 | [diff] [blame] | 1000 | * Evaluate the expression excluding trailing binary operators. |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 1001 | * Errors result in exceptions, most of which are unchecked. Should not be called |
| 1002 | * concurrently with modification of the expression. May take a very long time; avoid calling |
| 1003 | * from UI thread. |
Hans Boehm | e855376 | 2015-06-26 17:56:52 -0700 | [diff] [blame] | 1004 | * |
| 1005 | * @param degreeMode use degrees rather than radians |
| 1006 | */ |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame^] | 1007 | UnifiedReal eval(boolean degreeMode, ExprResolver er) throws SyntaxException |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 1008 | // And unchecked exceptions thrown by UnifiedReal, CR, |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 1009 | // and BoundedRational. |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1010 | { |
| 1011 | try { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 1012 | // We currently never include trailing binary operators, but include other trailing |
| 1013 | // operators. Thus we usually, but not always, display results for prefixes of valid |
| 1014 | // expressions, and don't generate an error where we previously displayed an instant |
| 1015 | // result. This reflects the Android L design. |
Hans Boehm | e855376 | 2015-06-26 17:56:52 -0700 | [diff] [blame] | 1016 | int prefixLen = trailingBinaryOpsStart(); |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame^] | 1017 | EvalContext ec = new EvalContext(degreeMode, prefixLen, er); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1018 | EvalRet res = evalExpr(0, ec); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 1019 | if (res.pos != prefixLen) { |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 1020 | throw new SyntaxException("Failed to parse full expression"); |
Hans Boehm | fbcef70 | 2015-04-27 18:07:47 -0700 | [diff] [blame] | 1021 | } |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 1022 | return res.val; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1023 | } catch (IndexOutOfBoundsException e) { |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 1024 | throw new SyntaxException("Unexpected expression end"); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | // Produce a string representation of the expression itself |
Hans Boehm | 8a4f81c | 2015-07-09 10:41:25 -0700 | [diff] [blame] | 1029 | SpannableStringBuilder toSpannableStringBuilder(Context context) { |
| 1030 | SpannableStringBuilder ssb = new SpannableStringBuilder(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1031 | for (Token t: mExpr) { |
Hans Boehm | 8a4f81c | 2015-07-09 10:41:25 -0700 | [diff] [blame] | 1032 | ssb.append(t.toCharSequence(context)); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1033 | } |
Hans Boehm | 8a4f81c | 2015-07-09 10:41:25 -0700 | [diff] [blame] | 1034 | return ssb; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1035 | } |
| 1036 | } |