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: |
| 376 | return new PreEval(in); |
| 377 | default: throw new IOException("Bad save file format"); |
| 378 | } |
| 379 | } else { |
| 380 | return new Operator(kindByte); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 381 | } |
| 382 | } |
| 383 | |
| 384 | CalculatorExpr() { |
| 385 | mExpr = new ArrayList<Token>(); |
| 386 | } |
| 387 | |
| 388 | private CalculatorExpr(ArrayList<Token> expr) { |
| 389 | mExpr = expr; |
| 390 | } |
| 391 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 392 | /** |
| 393 | * Construct CalculatorExpr, by reading it from in. |
| 394 | */ |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 395 | CalculatorExpr(DataInput in) throws IOException { |
| 396 | mExpr = new ArrayList<Token>(); |
| 397 | int size = in.readInt(); |
| 398 | for (int i = 0; i < size; ++i) { |
| 399 | mExpr.add(newToken(in)); |
| 400 | } |
| 401 | } |
| 402 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 403 | /** |
| 404 | * Write this expression to out. |
| 405 | */ |
| 406 | public void write(DataOutput out) throws IOException { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 407 | int size = mExpr.size(); |
| 408 | out.writeInt(size); |
| 409 | for (int i = 0; i < size; ++i) { |
| 410 | mExpr.get(i).write(out); |
| 411 | } |
| 412 | } |
| 413 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 414 | /** |
Hans Boehm | f442477 | 2016-12-05 10:35:16 -0800 | [diff] [blame] | 415 | * Use write() above to generate a byte array containing a serialized representation of |
| 416 | * this expression. |
| 417 | */ |
| 418 | public byte[] toBytes() { |
| 419 | ByteArrayOutputStream byteArrayStream = new ByteArrayOutputStream(); |
| 420 | try (DataOutputStream out = new DataOutputStream(byteArrayStream)) { |
| 421 | write(out); |
| 422 | } catch (IOException e) { |
| 423 | // Impossible; No IO involved. |
| 424 | throw new AssertionError("Impossible IO exception", e); |
| 425 | } |
| 426 | return byteArrayStream.toByteArray(); |
| 427 | } |
| 428 | |
| 429 | /** |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 430 | * Does this expression end with a numeric constant? |
| 431 | * As opposed to an operator or preevaluated expression. |
| 432 | */ |
Hans Boehm | 0b9806f | 2015-06-29 16:07:15 -0700 | [diff] [blame] | 433 | boolean hasTrailingConstant() { |
| 434 | int s = mExpr.size(); |
| 435 | if (s == 0) { |
| 436 | return false; |
| 437 | } |
| 438 | Token t = mExpr.get(s-1); |
| 439 | return t instanceof Constant; |
| 440 | } |
| 441 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 442 | /** |
| 443 | * Does this expression end with a binary operator? |
| 444 | */ |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame] | 445 | boolean hasTrailingBinary() { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 446 | int s = mExpr.size(); |
| 447 | if (s == 0) return false; |
| 448 | Token t = mExpr.get(s-1); |
| 449 | if (!(t instanceof Operator)) return false; |
| 450 | Operator o = (Operator)t; |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 451 | return (KeyMaps.isBinary(o.id)); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 452 | } |
| 453 | |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 454 | /** |
| 455 | * Append press of button with given id to expression. |
| 456 | * If the insertion would clearly result in a syntax error, either just return false |
| 457 | * and do nothing, or make an adjustment to avoid the problem. We do the latter only |
| 458 | * for unambiguous consecutive binary operators, in which case we delete the first |
| 459 | * operator. |
| 460 | */ |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 461 | boolean add(int id) { |
| 462 | int s = mExpr.size(); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 463 | final int d = KeyMaps.digVal(id); |
| 464 | final boolean binary = KeyMaps.isBinary(id); |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 465 | Token lastTok = s == 0 ? null : mExpr.get(s-1); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 466 | int lastOp = lastTok instanceof Operator ? ((Operator) lastTok).id : 0; |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 467 | // Quietly replace a trailing binary operator with another one, unless the second |
| 468 | // operator is minus, in which case we just allow it as a unary minus. |
| 469 | if (binary && !KeyMaps.isPrefix(id)) { |
| 470 | if (s == 0 || lastOp == R.id.lparen || KeyMaps.isFunc(lastOp) |
| 471 | || KeyMaps.isPrefix(lastOp) && lastOp != R.id.op_sub) { |
| 472 | return false; |
| 473 | } |
| 474 | while (hasTrailingBinary()) { |
| 475 | delete(); |
| 476 | } |
| 477 | // s invalid and not used below. |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 478 | } |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 479 | final boolean isConstPiece = (d != KeyMaps.NOT_DIGIT || id == R.id.dec_point); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 480 | if (isConstPiece) { |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 481 | // Since we treat juxtaposition as multiplication, a constant can appear anywhere. |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 482 | if (s == 0) { |
| 483 | mExpr.add(new Constant()); |
| 484 | s++; |
| 485 | } else { |
| 486 | Token last = mExpr.get(s-1); |
| 487 | if(!(last instanceof Constant)) { |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 488 | if (last instanceof PreEval) { |
| 489 | // Add explicit multiplication to avoid confusing display. |
| 490 | mExpr.add(new Operator(R.id.op_mul)); |
| 491 | s++; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 492 | } |
| 493 | mExpr.add(new Constant()); |
| 494 | s++; |
| 495 | } |
| 496 | } |
| 497 | return ((Constant)(mExpr.get(s-1))).add(id); |
| 498 | } else { |
| 499 | mExpr.add(new Operator(id)); |
| 500 | return true; |
| 501 | } |
| 502 | } |
| 503 | |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 504 | /** |
Hans Boehm | 0b9806f | 2015-06-29 16:07:15 -0700 | [diff] [blame] | 505 | * Add exponent to the constant at the end of the expression. |
| 506 | * Assumes there is a constant at the end of the expression. |
| 507 | */ |
| 508 | void addExponent(int exp) { |
| 509 | Token lastTok = mExpr.get(mExpr.size() - 1); |
| 510 | ((Constant) lastTok).addExponent(exp); |
| 511 | } |
| 512 | |
| 513 | /** |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 514 | * Remove trailing op_add and op_sub operators. |
| 515 | */ |
| 516 | void removeTrailingAdditiveOperators() { |
| 517 | while (true) { |
| 518 | int s = mExpr.size(); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 519 | if (s == 0) { |
| 520 | break; |
| 521 | } |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 522 | Token lastTok = mExpr.get(s-1); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 523 | if (!(lastTok instanceof Operator)) { |
| 524 | break; |
| 525 | } |
| 526 | int lastOp = ((Operator) lastTok).id; |
| 527 | if (lastOp != R.id.op_add && lastOp != R.id.op_sub) { |
| 528 | break; |
| 529 | } |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 530 | delete(); |
| 531 | } |
| 532 | } |
| 533 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 534 | /** |
| 535 | * Append the contents of the argument expression. |
| 536 | * It is assumed that the argument expression will not change, and thus its pieces can be |
| 537 | * reused directly. |
| 538 | */ |
| 539 | public void append(CalculatorExpr expr2) { |
Hans Boehm | fbcef70 | 2015-04-27 18:07:47 -0700 | [diff] [blame] | 540 | int s = mExpr.size(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 541 | int s2 = expr2.mExpr.size(); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 542 | // Check that we're not concatenating Constant or PreEval tokens, since the result would |
| 543 | // look like a single constant, with very mysterious results for the user. |
Hans Boehm | fbcef70 | 2015-04-27 18:07:47 -0700 | [diff] [blame] | 544 | if (s != 0 && s2 != 0) { |
| 545 | Token last = mExpr.get(s-1); |
| 546 | Token first = expr2.mExpr.get(0); |
| 547 | if (!(first instanceof Operator) && !(last instanceof Operator)) { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 548 | // Fudge it by adding an explicit multiplication. We would have interpreted it as |
| 549 | // such anyway, and this makes it recognizable to the user. |
Hans Boehm | fbcef70 | 2015-04-27 18:07:47 -0700 | [diff] [blame] | 550 | mExpr.add(new Operator(R.id.op_mul)); |
| 551 | } |
| 552 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 553 | for (int i = 0; i < s2; ++i) { |
| 554 | mExpr.add(expr2.mExpr.get(i)); |
| 555 | } |
| 556 | } |
| 557 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 558 | /** |
| 559 | * Undo the last key addition, if any. |
| 560 | * Or possibly remove a trailing exponent digit. |
| 561 | */ |
| 562 | public void delete() { |
| 563 | final int s = mExpr.size(); |
| 564 | if (s == 0) { |
| 565 | return; |
| 566 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 567 | Token last = mExpr.get(s-1); |
| 568 | if (last instanceof Constant) { |
| 569 | Constant c = (Constant)last; |
| 570 | c.delete(); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 571 | if (!c.isEmpty()) { |
| 572 | return; |
| 573 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 574 | } |
| 575 | mExpr.remove(s-1); |
| 576 | } |
| 577 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 578 | /** |
| 579 | * Remove all tokens from the expression. |
| 580 | */ |
| 581 | public void clear() { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 582 | mExpr.clear(); |
| 583 | } |
| 584 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 585 | public boolean isEmpty() { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 586 | return mExpr.isEmpty(); |
| 587 | } |
| 588 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 589 | /** |
| 590 | * Returns a logical deep copy of the CalculatorExpr. |
| 591 | * Operator and PreEval tokens are immutable, and thus aren't really copied. |
| 592 | */ |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 593 | public Object clone() { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 594 | CalculatorExpr result = new CalculatorExpr(); |
Hans Boehm | 9c160b4 | 2016-12-02 11:55:12 -0800 | [diff] [blame] | 595 | for (Token t : mExpr) { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 596 | if (t instanceof Constant) { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 597 | result.mExpr.add((Token)(((Constant)t).clone())); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 598 | } else { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 599 | result.mExpr.add(t); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 600 | } |
| 601 | } |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 602 | return result; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | // Am I just a constant? |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 606 | public boolean isConstant() { |
| 607 | if (mExpr.size() != 1) { |
| 608 | return false; |
| 609 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 610 | return mExpr.get(0) instanceof Constant; |
| 611 | } |
| 612 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 613 | /** |
| 614 | * Return a new expression consisting of a single token representing the current pre-evaluated |
| 615 | * expression. |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame] | 616 | * The caller supplies the expression index and short string representation. |
| 617 | * The expression must have been previously evaluated. |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 618 | */ |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame] | 619 | public CalculatorExpr abbreviate(long index, String sr) { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 620 | CalculatorExpr result = new CalculatorExpr(); |
Justin Klaassen | 0ace4eb | 2016-02-05 11:38:12 -0800 | [diff] [blame] | 621 | @SuppressWarnings("unchecked") |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame] | 622 | Token t = new PreEval(index, sr); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 623 | result.mExpr.add(t); |
| 624 | return result; |
| 625 | } |
| 626 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 627 | /** |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 628 | * Internal evaluation functions return an EvalRet pair. |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 629 | * We compute rational (BoundedRational) results when possible, both as a performance |
| 630 | * optimization, and to detect errors exactly when we can. |
| 631 | */ |
| 632 | private static class EvalRet { |
| 633 | public int pos; // Next position (expression index) to be parsed. |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 634 | public final UnifiedReal val; // Constructive Real result of evaluating subexpression. |
| 635 | EvalRet(int p, UnifiedReal v) { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 636 | pos = p; |
| 637 | val = v; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 638 | } |
| 639 | } |
| 640 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 641 | /** |
| 642 | * Internal evaluation functions take an EvalContext argument. |
| 643 | */ |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 644 | private static class EvalContext { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 645 | public final int mPrefixLength; // Length of prefix to evaluate. Not explicitly saved. |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 646 | public final boolean mDegreeMode; |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame] | 647 | public final ExprResolver mExprResolver; // Reconstructed, not saved. |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 648 | // If we add any other kinds of evaluation modes, they go here. |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame] | 649 | EvalContext(boolean degreeMode, int len, ExprResolver er) { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 650 | mDegreeMode = degreeMode; |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 651 | mPrefixLength = len; |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame] | 652 | mExprResolver = er; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 653 | } |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame] | 654 | EvalContext(DataInput in, int len, ExprResolver er) throws IOException { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 655 | mDegreeMode = in.readBoolean(); |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 656 | mPrefixLength = len; |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame] | 657 | mExprResolver = er; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 658 | } |
| 659 | void write(DataOutput out) throws IOException { |
| 660 | out.writeBoolean(mDegreeMode); |
| 661 | } |
| 662 | } |
| 663 | |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 664 | private UnifiedReal toRadians(UnifiedReal x, EvalContext ec) { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 665 | if (ec.mDegreeMode) { |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 666 | return x.multiply(UnifiedReal.RADIANS_PER_DEGREE); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 667 | } else { |
| 668 | return x; |
| 669 | } |
| 670 | } |
| 671 | |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 672 | private UnifiedReal fromRadians(UnifiedReal x, EvalContext ec) { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 673 | if (ec.mDegreeMode) { |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 674 | return x.divide(UnifiedReal.RADIANS_PER_DEGREE); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 675 | } else { |
| 676 | return x; |
| 677 | } |
| 678 | } |
| 679 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 680 | // The following methods can all throw IndexOutOfBoundsException in the event of a syntax |
| 681 | // error. We expect that to be caught in eval below. |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 682 | |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 683 | private boolean isOperatorUnchecked(int i, int op) { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 684 | Token t = mExpr.get(i); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 685 | if (!(t instanceof Operator)) { |
| 686 | return false; |
| 687 | } |
| 688 | return ((Operator)(t)).id == op; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 689 | } |
| 690 | |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 691 | private boolean isOperator(int i, int op, EvalContext ec) { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 692 | if (i >= ec.mPrefixLength) { |
| 693 | return false; |
| 694 | } |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 695 | return isOperatorUnchecked(i, op); |
| 696 | } |
| 697 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 698 | public static class SyntaxException extends Exception { |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 699 | public SyntaxException() { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 700 | super(); |
| 701 | } |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 702 | public SyntaxException(String s) { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 703 | super(s); |
| 704 | } |
| 705 | } |
| 706 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 707 | // The following functions all evaluate some kind of expression starting at position i in |
| 708 | // mExpr in a specified evaluation context. They return both the expression value (as |
| 709 | // constructive real and, if applicable, as BoundedRational) and the position of the next token |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 710 | // that was not used as part of the evaluation. |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 711 | // This is essentially a simple recursive descent parser combined with expression evaluation. |
| 712 | |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 713 | private EvalRet evalUnary(int i, EvalContext ec) throws SyntaxException { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 714 | final Token t = mExpr.get(i); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 715 | if (t instanceof Constant) { |
| 716 | Constant c = (Constant)t; |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 717 | return new EvalRet(i+1,new UnifiedReal(c.toRational())); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 718 | } |
| 719 | if (t instanceof PreEval) { |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame] | 720 | final long index = ((PreEval)t).mIndex; |
| 721 | UnifiedReal res = ec.mExprResolver.getResult(index); |
| 722 | if (res == null) { |
Hans Boehm | 9c160b4 | 2016-12-02 11:55:12 -0800 | [diff] [blame] | 723 | // We try to minimize this recursive evaluation case, but currently don't |
| 724 | // completely avoid it. |
| 725 | res = nestedEval(index, ec.mExprResolver); |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame] | 726 | } |
| 727 | return new EvalRet(i+1, res); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 728 | } |
| 729 | EvalRet argVal; |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 730 | switch(((Operator)(t)).id) { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 731 | case R.id.const_pi: |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 732 | return new EvalRet(i+1, UnifiedReal.PI); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 733 | case R.id.const_e: |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 734 | return new EvalRet(i+1, UnifiedReal.E); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 735 | case R.id.op_sqrt: |
Hans Boehm | fbcef70 | 2015-04-27 18:07:47 -0700 | [diff] [blame] | 736 | // Seems to have highest precedence. |
| 737 | // Does not add implicit paren. |
| 738 | // Does seem to accept a leading minus. |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 739 | if (isOperator(i+1, R.id.op_sub, ec)) { |
Hans Boehm | fbcef70 | 2015-04-27 18:07:47 -0700 | [diff] [blame] | 740 | argVal = evalUnary(i+2, ec); |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 741 | return new EvalRet(argVal.pos, argVal.val.negate().sqrt()); |
Hans Boehm | fbcef70 | 2015-04-27 18:07:47 -0700 | [diff] [blame] | 742 | } else { |
| 743 | argVal = evalUnary(i+1, ec); |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 744 | return new EvalRet(argVal.pos, argVal.val.sqrt()); |
Hans Boehm | fbcef70 | 2015-04-27 18:07:47 -0700 | [diff] [blame] | 745 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 746 | case R.id.lparen: |
| 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, argVal.val); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 752 | case R.id.fun_sin: |
| 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 | return new EvalRet(argVal.pos, toRadians(argVal.val, ec).sin()); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 758 | case R.id.fun_cos: |
| 759 | argVal = evalExpr(i+1, ec); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 760 | if (isOperator(argVal.pos, R.id.rparen, ec)) { |
| 761 | argVal.pos++; |
| 762 | } |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 763 | return new EvalRet(argVal.pos, toRadians(argVal.val,ec).cos()); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 764 | case R.id.fun_tan: |
| 765 | argVal = evalExpr(i+1, ec); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 766 | if (isOperator(argVal.pos, R.id.rparen, ec)) { |
| 767 | argVal.pos++; |
| 768 | } |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 769 | UnifiedReal arg = toRadians(argVal.val, ec); |
| 770 | return new EvalRet(argVal.pos, arg.sin().divide(arg.cos())); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 771 | case R.id.fun_ln: |
| 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()); |
Hans Boehm | 4db31b4 | 2015-05-31 12:19:05 -0700 | [diff] [blame] | 777 | case R.id.fun_exp: |
| 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, argVal.val.exp()); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 783 | case R.id.fun_log: |
| 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 | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 788 | return new EvalRet(argVal.pos, argVal.val.ln().divide(UnifiedReal.TEN.ln())); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 789 | case R.id.fun_arcsin: |
| 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.asin(), ec)); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 795 | case R.id.fun_arccos: |
| 796 | argVal = evalExpr(i+1, ec); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 797 | if (isOperator(argVal.pos, R.id.rparen, ec)) { |
| 798 | argVal.pos++; |
| 799 | } |
Hans Boehm | 79f273c | 2016-06-09 11:00:27 -0700 | [diff] [blame] | 800 | return new EvalRet(argVal.pos, fromRadians(argVal.val.acos(), ec)); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 801 | case R.id.fun_arctan: |
| 802 | argVal = evalExpr(i+1, ec); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 803 | if (isOperator(argVal.pos, R.id.rparen, ec)) { |
| 804 | argVal.pos++; |
| 805 | } |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 806 | return new EvalRet(argVal.pos, fromRadians(argVal.val.atan(),ec)); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 807 | default: |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 808 | throw new SyntaxException("Unrecognized token in expression"); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 809 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 810 | } |
| 811 | |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 812 | private static final UnifiedReal ONE_HUNDREDTH = new UnifiedReal(100).inverse(); |
Hans Boehm | 682ff5e | 2015-03-09 14:40:25 -0700 | [diff] [blame] | 813 | |
Hans Boehm | 4db31b4 | 2015-05-31 12:19:05 -0700 | [diff] [blame] | 814 | private EvalRet evalSuffix(int i, EvalContext ec) throws SyntaxException { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 815 | final EvalRet tmp = evalUnary(i, ec); |
| 816 | int cpos = tmp.pos; |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 817 | UnifiedReal val = tmp.val; |
| 818 | |
Hans Boehm | 4db31b4 | 2015-05-31 12:19:05 -0700 | [diff] [blame] | 819 | boolean isFact; |
| 820 | boolean isSquared = false; |
| 821 | while ((isFact = isOperator(cpos, R.id.op_fact, ec)) || |
| 822 | (isSquared = isOperator(cpos, R.id.op_sqr, ec)) || |
| 823 | isOperator(cpos, R.id.op_pct, ec)) { |
| 824 | if (isFact) { |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 825 | val = val.fact(); |
Hans Boehm | 4db31b4 | 2015-05-31 12:19:05 -0700 | [diff] [blame] | 826 | } else if (isSquared) { |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 827 | val = val.multiply(val); |
Hans Boehm | 4db31b4 | 2015-05-31 12:19:05 -0700 | [diff] [blame] | 828 | } else /* percent */ { |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 829 | val = val.multiply(ONE_HUNDREDTH); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 830 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 831 | ++cpos; |
| 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 evalFactor(int i, EvalContext ec) throws SyntaxException { |
Hans Boehm | 4db31b4 | 2015-05-31 12:19:05 -0700 | [diff] [blame] | 837 | final EvalRet result1 = evalSuffix(i, ec); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 838 | int cpos = result1.pos; // current position |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 839 | UnifiedReal val = result1.val; // value so far |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 840 | if (isOperator(cpos, R.id.op_pow, ec)) { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 841 | final EvalRet exp = evalSignedFactor(cpos + 1, ec); |
| 842 | cpos = exp.pos; |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 843 | val = val.pow(exp.val); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 844 | } |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 845 | return new EvalRet(cpos, val); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 846 | } |
| 847 | |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 848 | private EvalRet evalSignedFactor(int i, EvalContext ec) throws SyntaxException { |
| 849 | final boolean negative = isOperator(i, R.id.op_sub, ec); |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 850 | int cpos = negative ? i + 1 : i; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 851 | EvalRet tmp = evalFactor(cpos, ec); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 852 | cpos = tmp.pos; |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 853 | final UnifiedReal result = negative ? tmp.val.negate() : tmp.val; |
| 854 | return new EvalRet(cpos, result); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | private boolean canStartFactor(int i) { |
| 858 | if (i >= mExpr.size()) return false; |
| 859 | Token t = mExpr.get(i); |
| 860 | if (!(t instanceof Operator)) return true; |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 861 | int id = ((Operator)(t)).id; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 862 | if (KeyMaps.isBinary(id)) return false; |
| 863 | switch (id) { |
| 864 | case R.id.op_fact: |
| 865 | case R.id.rparen: |
| 866 | return false; |
| 867 | default: |
| 868 | return true; |
| 869 | } |
| 870 | } |
| 871 | |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 872 | private EvalRet evalTerm(int i, EvalContext ec) throws SyntaxException { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 873 | EvalRet tmp = evalSignedFactor(i, ec); |
| 874 | boolean is_mul = false; |
| 875 | boolean is_div = false; |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 876 | int cpos = tmp.pos; // Current position in expression. |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 877 | UnifiedReal val = tmp.val; // Current value. |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 878 | while ((is_mul = isOperator(cpos, R.id.op_mul, ec)) |
| 879 | || (is_div = isOperator(cpos, R.id.op_div, ec)) |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 880 | || canStartFactor(cpos)) { |
| 881 | if (is_mul || is_div) ++cpos; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 882 | tmp = evalSignedFactor(cpos, ec); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 883 | if (is_div) { |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 884 | val = val.divide(tmp.val); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 885 | } else { |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 886 | val = val.multiply(tmp.val); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 887 | } |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 888 | cpos = tmp.pos; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 889 | is_mul = is_div = false; |
| 890 | } |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 891 | return new EvalRet(cpos, val); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 892 | } |
| 893 | |
Hans Boehm | 8afd0f8 | 2015-07-30 17:00:33 -0700 | [diff] [blame] | 894 | /** |
| 895 | * Is the subexpression starting at pos a simple percent constant? |
| 896 | * This is used to recognize exppressions like 200+10%, which we handle specially. |
| 897 | * This is defined as a Constant or PreEval token, followed by a percent sign, and followed |
| 898 | * by either nothing or an additive operator. |
| 899 | * Note that we are intentionally far more restrictive in recognizing such expressions than |
| 900 | * e.g. http://blogs.msdn.com/b/oldnewthing/archive/2008/01/10/7047497.aspx . |
| 901 | * When in doubt, we fall back to the the naive interpretation of % as 1/100. |
| 902 | * Note that 100+(10)% yields 100.1 while 100+10% yields 110. This may be controversial, |
| 903 | * but is consistent with Google web search. |
| 904 | */ |
| 905 | private boolean isPercent(int pos) { |
| 906 | if (mExpr.size() < pos + 2 || !isOperatorUnchecked(pos + 1, R.id.op_pct)) { |
| 907 | return false; |
| 908 | } |
| 909 | Token number = mExpr.get(pos); |
| 910 | if (number instanceof Operator) { |
| 911 | return false; |
| 912 | } |
| 913 | if (mExpr.size() == pos + 2) { |
| 914 | return true; |
| 915 | } |
| 916 | if (!(mExpr.get(pos + 2) instanceof Operator)) { |
| 917 | return false; |
| 918 | } |
| 919 | Operator op = (Operator) mExpr.get(pos + 2); |
Hans Boehm | 64d1cdb | 2016-03-08 19:14:07 -0800 | [diff] [blame] | 920 | 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] | 921 | } |
| 922 | |
| 923 | /** |
| 924 | * Compute the multiplicative factor corresponding to an N% addition or subtraction. |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 925 | * @param pos position of Constant or PreEval expression token corresponding to N. |
| 926 | * @param isSubtraction this is a subtraction, as opposed to addition. |
| 927 | * @param ec usable evaluation contex; only length matters. |
| 928 | * @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] | 929 | */ |
| 930 | private EvalRet getPercentFactor(int pos, boolean isSubtraction, EvalContext ec) |
| 931 | throws SyntaxException { |
| 932 | EvalRet tmp = evalUnary(pos, ec); |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 933 | UnifiedReal val = isSubtraction ? tmp.val.negate() : tmp.val; |
| 934 | val = UnifiedReal.ONE.add(val.multiply(ONE_HUNDREDTH)); |
| 935 | return new EvalRet(pos + 2 /* after percent sign */, val); |
Hans Boehm | 8afd0f8 | 2015-07-30 17:00:33 -0700 | [diff] [blame] | 936 | } |
| 937 | |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 938 | private EvalRet evalExpr(int i, EvalContext ec) throws SyntaxException { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 939 | EvalRet tmp = evalTerm(i, ec); |
| 940 | boolean is_plus; |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 941 | int cpos = tmp.pos; |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 942 | UnifiedReal val = tmp.val; |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 943 | while ((is_plus = isOperator(cpos, R.id.op_add, ec)) |
| 944 | || isOperator(cpos, R.id.op_sub, ec)) { |
Hans Boehm | 8afd0f8 | 2015-07-30 17:00:33 -0700 | [diff] [blame] | 945 | if (isPercent(cpos + 1)) { |
| 946 | tmp = getPercentFactor(cpos + 1, !is_plus, ec); |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 947 | val = val.multiply(tmp.val); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 948 | } else { |
Hans Boehm | 8afd0f8 | 2015-07-30 17:00:33 -0700 | [diff] [blame] | 949 | tmp = evalTerm(cpos + 1, ec); |
| 950 | if (is_plus) { |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 951 | val = val.add(tmp.val); |
Hans Boehm | 682ff5e | 2015-03-09 14:40:25 -0700 | [diff] [blame] | 952 | } else { |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 953 | val = val.subtract(tmp.val); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 954 | } |
| 955 | } |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 956 | cpos = tmp.pos; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 957 | } |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 958 | return new EvalRet(cpos, val); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 959 | } |
| 960 | |
Hans Boehm | e855376 | 2015-06-26 17:56:52 -0700 | [diff] [blame] | 961 | /** |
| 962 | * Return the starting position of the sequence of trailing binary operators. |
| 963 | */ |
| 964 | private int trailingBinaryOpsStart() { |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 965 | int result = mExpr.size(); |
| 966 | while (result > 0) { |
| 967 | Token last = mExpr.get(result - 1); |
| 968 | if (!(last instanceof Operator)) break; |
| 969 | Operator o = (Operator)last; |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 970 | if (!KeyMaps.isBinary(o.id)) break; |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 971 | --result; |
| 972 | } |
| 973 | return result; |
| 974 | } |
| 975 | |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 976 | /** |
| 977 | * Is the current expression worth evaluating? |
| 978 | */ |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 979 | public boolean hasInterestingOps() { |
Hans Boehm | 83f278e | 2016-12-19 16:20:03 -0800 | [diff] [blame^] | 980 | final int last = trailingBinaryOpsStart(); |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 981 | int first = 0; |
| 982 | if (last > first && isOperatorUnchecked(first, R.id.op_sub)) { |
| 983 | // Leading minus is not by itself interesting. |
| 984 | first++; |
| 985 | } |
| 986 | for (int i = first; i < last; ++i) { |
| 987 | Token t1 = mExpr.get(i); |
Hans Boehm | 187d3e9 | 2015-06-09 18:04:26 -0700 | [diff] [blame] | 988 | if (t1 instanceof Operator |
| 989 | || t1 instanceof PreEval && ((PreEval)t1).hasEllipsis()) { |
| 990 | return true; |
| 991 | } |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 992 | } |
| 993 | return false; |
| 994 | } |
| 995 | |
Hans Boehm | e855376 | 2015-06-26 17:56:52 -0700 | [diff] [blame] | 996 | /** |
Hans Boehm | 52d477a | 2016-04-01 17:42:50 -0700 | [diff] [blame] | 997 | * Does the expression contain trig operations? |
| 998 | */ |
| 999 | public boolean hasTrigFuncs() { |
Hans Boehm | 9c160b4 | 2016-12-02 11:55:12 -0800 | [diff] [blame] | 1000 | for (Token t : mExpr) { |
Hans Boehm | 52d477a | 2016-04-01 17:42:50 -0700 | [diff] [blame] | 1001 | if (t instanceof Operator) { |
| 1002 | Operator o = (Operator)t; |
| 1003 | if (KeyMaps.isTrigFunc(o.id)) { |
| 1004 | return true; |
| 1005 | } |
| 1006 | } |
| 1007 | } |
| 1008 | return false; |
| 1009 | } |
| 1010 | |
| 1011 | /** |
Hans Boehm | 9c160b4 | 2016-12-02 11:55:12 -0800 | [diff] [blame] | 1012 | * Add the indices of unevaluated PreEval expressions embedded in the current expression to |
| 1013 | * argument. This includes only directly referenced expressions e, not those indirectly |
| 1014 | * referenced by e. If the index was already present, it is not added. If the argument |
| 1015 | * contained no duplicates, the result will not either. New indices are added to the end of |
| 1016 | * the list. |
| 1017 | */ |
| 1018 | private void addReferencedExprs(ArrayList<Long> list, ExprResolver er) { |
| 1019 | for (Token t : mExpr) { |
| 1020 | if (t instanceof PreEval) { |
| 1021 | Long index = ((PreEval) t).mIndex; |
| 1022 | if (er.getResult(index) == null && !list.contains(index)) { |
| 1023 | list.add(index); |
| 1024 | } |
| 1025 | } |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | /** |
| 1030 | * Return a list of unevaluated expressions transitively referenced by the current one. |
| 1031 | * All expressions in the resulting list will have had er.getExpr() called on them. |
| 1032 | * The resulting list is ordered such that evaluating expressions in list order |
| 1033 | * should trigger few recursive evaluations. |
| 1034 | */ |
| 1035 | public ArrayList<Long> getTransitivelyReferencedExprs(ExprResolver er) { |
| 1036 | // We could avoid triggering any recursive evaluations by actually building the |
| 1037 | // dependency graph and topologically sorting it. Note that sorting by index works |
| 1038 | // for positive and negative indices separately, but not their union. Currently we |
| 1039 | // just settle for reverse breadth-first-search order, which handles the common case |
| 1040 | // of simple dependency chains well. |
| 1041 | ArrayList<Long> list = new ArrayList<Long>(); |
| 1042 | int scanned = 0; // We've added expressions referenced by [0, scanned) to the list |
| 1043 | addReferencedExprs(list, er); |
| 1044 | while (scanned != list.size()) { |
| 1045 | er.getExpr(list.get(scanned++)).addReferencedExprs(list, er); |
| 1046 | } |
| 1047 | Collections.reverse(list); |
| 1048 | return list; |
| 1049 | } |
| 1050 | |
| 1051 | /** |
| 1052 | * Evaluate the expression at the given index to a UnifiedReal. |
| 1053 | * Both saves and returns the result. |
| 1054 | */ |
| 1055 | UnifiedReal nestedEval(long index, ExprResolver er) throws SyntaxException { |
| 1056 | CalculatorExpr nestedExpr = er.getExpr(index); |
| 1057 | EvalContext newEc = new EvalContext(er.getDegreeMode(index), |
| 1058 | nestedExpr.trailingBinaryOpsStart(), er); |
| 1059 | EvalRet new_res = nestedExpr.evalExpr(0, newEc); |
| 1060 | return er.putResultIfAbsent(index, new_res.val); |
| 1061 | } |
| 1062 | |
| 1063 | /** |
Hans Boehm | e855376 | 2015-06-26 17:56:52 -0700 | [diff] [blame] | 1064 | * Evaluate the expression excluding trailing binary operators. |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 1065 | * Errors result in exceptions, most of which are unchecked. Should not be called |
| 1066 | * concurrently with modification of the expression. May take a very long time; avoid calling |
| 1067 | * from UI thread. |
Hans Boehm | e855376 | 2015-06-26 17:56:52 -0700 | [diff] [blame] | 1068 | * |
| 1069 | * @param degreeMode use degrees rather than radians |
| 1070 | */ |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame] | 1071 | UnifiedReal eval(boolean degreeMode, ExprResolver er) throws SyntaxException |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 1072 | // And unchecked exceptions thrown by UnifiedReal, CR, |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 1073 | // and BoundedRational. |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1074 | { |
Hans Boehm | 9c160b4 | 2016-12-02 11:55:12 -0800 | [diff] [blame] | 1075 | // First evaluate all indirectly referenced expressions in increasing index order. |
| 1076 | // This ensures that subsequent evaluation never encounters an embedded PreEval |
| 1077 | // expression that has not been previously evaluated. |
| 1078 | // We could do the embedded evaluations recursively, but that risks running out of |
| 1079 | // stack space. |
| 1080 | ArrayList<Long> referenced = getTransitivelyReferencedExprs(er); |
| 1081 | for (long index : referenced) { |
| 1082 | nestedEval(index, er); |
| 1083 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1084 | try { |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 1085 | // We currently never include trailing binary operators, but include other trailing |
| 1086 | // operators. Thus we usually, but not always, display results for prefixes of valid |
| 1087 | // expressions, and don't generate an error where we previously displayed an instant |
| 1088 | // result. This reflects the Android L design. |
Hans Boehm | e855376 | 2015-06-26 17:56:52 -0700 | [diff] [blame] | 1089 | int prefixLen = trailingBinaryOpsStart(); |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame] | 1090 | EvalContext ec = new EvalContext(degreeMode, prefixLen, er); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1091 | EvalRet res = evalExpr(0, ec); |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 1092 | if (res.pos != prefixLen) { |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 1093 | throw new SyntaxException("Failed to parse full expression"); |
Hans Boehm | fbcef70 | 2015-04-27 18:07:47 -0700 | [diff] [blame] | 1094 | } |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 1095 | return res.val; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1096 | } catch (IndexOutOfBoundsException e) { |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 1097 | throw new SyntaxException("Unexpected expression end"); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1098 | } |
| 1099 | } |
| 1100 | |
| 1101 | // Produce a string representation of the expression itself |
Hans Boehm | 8a4f81c | 2015-07-09 10:41:25 -0700 | [diff] [blame] | 1102 | SpannableStringBuilder toSpannableStringBuilder(Context context) { |
| 1103 | SpannableStringBuilder ssb = new SpannableStringBuilder(); |
Hans Boehm | 9c160b4 | 2016-12-02 11:55:12 -0800 | [diff] [blame] | 1104 | for (Token t : mExpr) { |
Hans Boehm | 8a4f81c | 2015-07-09 10:41:25 -0700 | [diff] [blame] | 1105 | ssb.append(t.toCharSequence(context)); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1106 | } |
Hans Boehm | 8a4f81c | 2015-07-09 10:41:25 -0700 | [diff] [blame] | 1107 | return ssb; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1108 | } |
| 1109 | } |