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