OLD | NEW |
(Empty) | |
| 1 // This code was auto-generated, is not intended to be edited, and is subject to |
| 2 // significant change. Please see the README file for more information. |
| 3 |
| 4 library engine.constant; |
| 5 |
| 6 import 'java_core.dart'; |
| 7 import 'source.dart' show Source; |
| 8 import 'error.dart' show AnalysisError, ErrorCode, CompileTimeErrorCode; |
| 9 import 'scanner.dart' show TokenType; |
| 10 import 'ast.dart'; |
| 11 import 'element.dart'; |
| 12 |
| 13 /** |
| 14 * Instances of the class {@code ConstantEvaluator} evaluate constant expression
s to produce their |
| 15 * compile-time value. According to the Dart Language Specification: <blockquote
> A constant |
| 16 * expression is one of the following: |
| 17 * <ul> |
| 18 * <li>A literal number.</li> |
| 19 * <li>A literal boolean.</li> |
| 20 * <li>A literal string where any interpolated expression is a compile-time cons
tant that evaluates |
| 21 * to a numeric, string or boolean value or to {@code null}.</li> |
| 22 * <li>{@code null}.</li> |
| 23 * <li>A reference to a static constant variable.</li> |
| 24 * <li>An identifier expression that denotes a constant variable, a class or a t
ype variable.</li> |
| 25 * <li>A constant constructor invocation.</li> |
| 26 * <li>A constant list literal.</li> |
| 27 * <li>A constant map literal.</li> |
| 28 * <li>A simple or qualified identifier denoting a top-level function or a stati
c method.</li> |
| 29 * <li>A parenthesized expression {@code (e)} where {@code e} is a constant expr
ession.</li> |
| 30 * <li>An expression of one of the forms {@code identical(e1, e2)}, {@code e1 ==
e2},{@code e1 != e2} where {@code e1} and {@code e2} are constant expressions t
hat evaluate to a |
| 31 * numeric, string or boolean value or to {@code null}.</li> |
| 32 * <li>An expression of one of the forms {@code !e}, {@code e1 && e2} or {@code
e1 || e2}, where{@code e}, {@code e1} and {@code e2} are constant expressions th
at evaluate to a boolean value or |
| 33 * to {@code null}.</li> |
| 34 * <li>An expression of one of the forms {@code ~e}, {@code e1 ^ e2}, {@code e1
& e2},{@code e1 | e2}, {@code e1 >> e2} or {@code e1 << e2}, where {@code e}, {@
code e1} and {@code e2}are constant expressions that evaluate to an integer valu
e or to {@code null}.</li> |
| 35 * <li>An expression of one of the forms {@code -e}, {@code e1 + e2}, {@code e1
- e2},{@code e1 * e2}, {@code e1 / e2}, {@code e1 ~/ e2}, {@code e1 > e2}, {@cod
e e1 < e2},{@code e1 >= e2}, {@code e1 <= e2} or {@code e1 % e2}, where {@code e
}, {@code e1} and {@code e2}are constant expressions that evaluate to a numeric
value or to {@code null}.</li> |
| 36 * </ul> |
| 37 * </blockquote> The values returned by instances of this class are therefore {@
code null} and |
| 38 * instances of the classes {@code Boolean}, {@code BigInteger}, {@code Double},
{@code String}, and{@code DartObject}. |
| 39 * <p> |
| 40 * In addition, this class defines several values that can be returned to indica
te various |
| 41 * conditions encountered during evaluation. These are documented with the stati
c field that define |
| 42 * those values. |
| 43 */ |
| 44 class ConstantEvaluator { |
| 45 /** |
| 46 * The source containing the expression(s) that will be evaluated. |
| 47 */ |
| 48 Source _source; |
| 49 /** |
| 50 * Initialize a newly created evaluator to evaluate expressions in the given s
ource. |
| 51 * @param source the source containing the expression(s) that will be evaluate
d |
| 52 */ |
| 53 ConstantEvaluator(Source source) { |
| 54 this._source = source; |
| 55 } |
| 56 EvaluationResult evaluate(Expression expression) { |
| 57 EvaluationResultImpl result = expression.accept(new ConstantVisitor()); |
| 58 if (result is ValidResult) { |
| 59 return EvaluationResult.forValue(((result as ValidResult)).value); |
| 60 } |
| 61 List<AnalysisError> errors = new List<AnalysisError>(); |
| 62 for (ErrorResult_ErrorData data in ((result as ErrorResult)).errorData) { |
| 63 ASTNode node2 = data.node; |
| 64 errors.add(new AnalysisError.con2(_source, node2.offset, node2.length, dat
a.errorCode, [])); |
| 65 } |
| 66 return EvaluationResult.forErrors(new List.from(errors)); |
| 67 } |
| 68 } |
| 69 /** |
| 70 * Instances of the class {@code EvaluationResult} represent the result of attem
pting to evaluate an |
| 71 * expression. |
| 72 */ |
| 73 class EvaluationResult { |
| 74 /** |
| 75 * Return an evaluation result representing the result of evaluating an expres
sion that is not a |
| 76 * compile-time constant because of the given errors. |
| 77 * @param errors the errors that should be reported for the expression(s) that
were evaluated |
| 78 * @return the result of evaluating an expression that is not a compile-time c
onstant |
| 79 */ |
| 80 static EvaluationResult forErrors(List<AnalysisError> errors) => new Evaluatio
nResult(null, errors); |
| 81 /** |
| 82 * Return an evaluation result representing the result of evaluating an expres
sion that is a |
| 83 * compile-time constant that evaluates to the given value. |
| 84 * @param value the value of the expression |
| 85 * @return the result of evaluating an expression that is a compile-time const
ant |
| 86 */ |
| 87 static EvaluationResult forValue(Object value) => new EvaluationResult(value,
null); |
| 88 /** |
| 89 * The value of the expression. |
| 90 */ |
| 91 Object _value; |
| 92 /** |
| 93 * The errors that should be reported for the expression(s) that were evaluate
d. |
| 94 */ |
| 95 List<AnalysisError> _errors; |
| 96 /** |
| 97 * Initialize a newly created result object with the given state. Clients shou
ld use one of the |
| 98 * factory methods: {@link #forErrors(AnalysisError[])} and {@link #forValue(O
bject)}. |
| 99 * @param value the value of the expression |
| 100 * @param errors the errors that should be reported for the expression(s) that
were evaluated |
| 101 */ |
| 102 EvaluationResult(Object value, List<AnalysisError> errors) { |
| 103 this._value = value; |
| 104 this._errors = errors; |
| 105 } |
| 106 /** |
| 107 * Return an array containing the errors that should be reported for the expre
ssion(s) that were |
| 108 * evaluated. If there are no such errors, the array will be empty. The array
can be empty even if |
| 109 * the expression is not a valid compile time constant if the errors would hav
e been reported by |
| 110 * other parts of the analysis engine. |
| 111 */ |
| 112 List<AnalysisError> get errors => _errors == null ? AnalysisError.NO_ERRORS :
_errors; |
| 113 /** |
| 114 * Return the value of the expression, or {@code null} if the expression evalu
ated to {@code null}or if the expression could not be evaluated, either because
it was not a compile-time constant |
| 115 * expression or because it would throw an exception when evaluated. |
| 116 * @return the value of the expression |
| 117 */ |
| 118 Object get value => _value; |
| 119 /** |
| 120 * Return {@code true} if the expression is a compile-time constant expression
that would not |
| 121 * throw an exception when evaluated. |
| 122 * @return {@code true} if the expression is a valid compile-time constant exp
ression |
| 123 */ |
| 124 bool isValid() => _errors == null; |
| 125 } |
| 126 /** |
| 127 * Instances of the class {@code ConstantVisitor} evaluate constant expressions
to produce their |
| 128 * compile-time value. According to the Dart Language Specification: <blockquote
> A constant |
| 129 * expression is one of the following: |
| 130 * <ul> |
| 131 * <li>A literal number.</li> |
| 132 * <li>A literal boolean.</li> |
| 133 * <li>A literal string where any interpolated expression is a compile-time cons
tant that evaluates |
| 134 * to a numeric, string or boolean value or to {@code null}.</li> |
| 135 * <li>{@code null}.</li> |
| 136 * <li>A reference to a static constant variable.</li> |
| 137 * <li>An identifier expression that denotes a constant variable, a class or a t
ype variable.</li> |
| 138 * <li>A constant constructor invocation.</li> |
| 139 * <li>A constant list literal.</li> |
| 140 * <li>A constant map literal.</li> |
| 141 * <li>A simple or qualified identifier denoting a top-level function or a stati
c method.</li> |
| 142 * <li>A parenthesized expression {@code (e)} where {@code e} is a constant expr
ession.</li> |
| 143 * <li>An expression of one of the forms {@code identical(e1, e2)}, {@code e1 ==
e2},{@code e1 != e2} where {@code e1} and {@code e2} are constant expressions t
hat evaluate to a |
| 144 * numeric, string or boolean value or to {@code null}.</li> |
| 145 * <li>An expression of one of the forms {@code !e}, {@code e1 && e2} or {@code
e1 || e2}, where{@code e}, {@code e1} and {@code e2} are constant expressions th
at evaluate to a boolean value or |
| 146 * to {@code null}.</li> |
| 147 * <li>An expression of one of the forms {@code ~e}, {@code e1 ^ e2}, {@code e1
& e2},{@code e1 | e2}, {@code e1 >> e2} or {@code e1 << e2}, where {@code e}, {@
code e1} and {@code e2}are constant expressions that evaluate to an integer valu
e or to {@code null}.</li> |
| 148 * <li>An expression of one of the forms {@code -e}, {@code e1 + e2}, {@code e1
- e2},{@code e1 * e2}, {@code e1 / e2}, {@code e1 ~/ e2}, {@code e1 > e2}, {@cod
e e1 < e2},{@code e1 >= e2}, {@code e1 <= e2} or {@code e1 % e2}, where {@code e
}, {@code e1} and {@code e2}are constant expressions that evaluate to a numeric
value or to {@code null}.</li> |
| 149 * </ul> |
| 150 * </blockquote> |
| 151 */ |
| 152 class ConstantVisitor extends GeneralizingASTVisitor<EvaluationResultImpl> { |
| 153 /** |
| 154 * Initialize a newly created constant visitor. |
| 155 */ |
| 156 ConstantVisitor() : super() { |
| 157 } |
| 158 EvaluationResultImpl visitAdjacentStrings(AdjacentStrings node) { |
| 159 EvaluationResultImpl result = null; |
| 160 for (StringLiteral string in node.strings) { |
| 161 if (result == null) { |
| 162 result = string.accept(this); |
| 163 } else { |
| 164 result = result.concatenate(node, string.accept(this)); |
| 165 } |
| 166 } |
| 167 return result; |
| 168 } |
| 169 EvaluationResultImpl visitBinaryExpression(BinaryExpression node) { |
| 170 EvaluationResultImpl leftResult = node.leftOperand.accept(this); |
| 171 EvaluationResultImpl rightResult = node.rightOperand.accept(this); |
| 172 while (true) { |
| 173 if (node.operator.type == TokenType.AMPERSAND) { |
| 174 return leftResult.bitAnd(node, rightResult); |
| 175 } else if (node.operator.type == TokenType.AMPERSAND_AMPERSAND) { |
| 176 return leftResult.logicalAnd(node, rightResult); |
| 177 } else if (node.operator.type == TokenType.BANG_EQ) { |
| 178 return leftResult.notEqual(node, rightResult); |
| 179 } else if (node.operator.type == TokenType.BAR) { |
| 180 return leftResult.bitOr(node, rightResult); |
| 181 } else if (node.operator.type == TokenType.BAR_BAR) { |
| 182 return leftResult.logicalOr(node, rightResult); |
| 183 } else if (node.operator.type == TokenType.CARET) { |
| 184 return leftResult.bitXor(node, rightResult); |
| 185 } else if (node.operator.type == TokenType.EQ_EQ) { |
| 186 return leftResult.equalEqual(node, rightResult); |
| 187 } else if (node.operator.type == TokenType.GT) { |
| 188 return leftResult.greaterThan(node, rightResult); |
| 189 } else if (node.operator.type == TokenType.GT_EQ) { |
| 190 return leftResult.greaterThanOrEqual(node, rightResult); |
| 191 } else if (node.operator.type == TokenType.GT_GT) { |
| 192 return leftResult.shiftRight(node, rightResult); |
| 193 } else if (node.operator.type == TokenType.LT) { |
| 194 return leftResult.lessThan(node, rightResult); |
| 195 } else if (node.operator.type == TokenType.LT_EQ) { |
| 196 return leftResult.lessThanOrEqual(node, rightResult); |
| 197 } else if (node.operator.type == TokenType.LT_LT) { |
| 198 return leftResult.shiftLeft(node, rightResult); |
| 199 } else if (node.operator.type == TokenType.MINUS) { |
| 200 return leftResult.minus(node, rightResult); |
| 201 } else if (node.operator.type == TokenType.PERCENT) { |
| 202 return leftResult.remainder(node, rightResult); |
| 203 } else if (node.operator.type == TokenType.PLUS) { |
| 204 return leftResult.add(node, rightResult); |
| 205 } else if (node.operator.type == TokenType.STAR) { |
| 206 return leftResult.times(node, rightResult); |
| 207 } else if (node.operator.type == TokenType.SLASH) { |
| 208 return leftResult.divide(node, rightResult); |
| 209 } else if (node.operator.type == TokenType.TILDE_SLASH) { |
| 210 return leftResult.integerDivide(node, rightResult); |
| 211 } |
| 212 break; |
| 213 } |
| 214 return error(node, null); |
| 215 } |
| 216 EvaluationResultImpl visitBooleanLiteral(BooleanLiteral node) => node.value ?
ValidResult.RESULT_TRUE : ValidResult.RESULT_FALSE; |
| 217 EvaluationResultImpl visitDoubleLiteral(DoubleLiteral node) => new ValidResult
(node.value); |
| 218 EvaluationResultImpl visitInstanceCreationExpression(InstanceCreationExpressio
n node) { |
| 219 ConstructorElement constructor = node.element; |
| 220 if (constructor != null && constructor.isConst()) { |
| 221 node.argumentList.accept(this); |
| 222 return ValidResult.RESULT_OBJECT; |
| 223 } |
| 224 return error(node, null); |
| 225 } |
| 226 EvaluationResultImpl visitIntegerLiteral(IntegerLiteral node) => new ValidResu
lt(node.value); |
| 227 EvaluationResultImpl visitInterpolationExpression(InterpolationExpression node
) { |
| 228 EvaluationResultImpl result = node.expression.accept(this); |
| 229 return result.performToString(node); |
| 230 } |
| 231 EvaluationResultImpl visitInterpolationString(InterpolationString node) => new
ValidResult(node.value); |
| 232 EvaluationResultImpl visitListLiteral(ListLiteral node) { |
| 233 ErrorResult result = null; |
| 234 for (Expression element in node.elements) { |
| 235 result = union(result, element.accept(this)); |
| 236 } |
| 237 if (result != null) { |
| 238 return result; |
| 239 } |
| 240 return ValidResult.RESULT_OBJECT; |
| 241 } |
| 242 EvaluationResultImpl visitMapLiteral(MapLiteral node) { |
| 243 ErrorResult result = null; |
| 244 for (MapLiteralEntry entry in node.entries) { |
| 245 result = union(result, entry.key.accept(this)); |
| 246 result = union(result, entry.value.accept(this)); |
| 247 } |
| 248 if (result != null) { |
| 249 return result; |
| 250 } |
| 251 return ValidResult.RESULT_OBJECT; |
| 252 } |
| 253 EvaluationResultImpl visitMethodInvocation(MethodInvocation node) { |
| 254 Element element21 = node.methodName.element; |
| 255 if (element21 is FunctionElement) { |
| 256 FunctionElement function = element21 as FunctionElement; |
| 257 if (function.name == "identical") { |
| 258 NodeList<Expression> arguments3 = node.argumentList.arguments; |
| 259 if (arguments3.length == 2) { |
| 260 Element enclosingElement2 = function.enclosingElement; |
| 261 if (enclosingElement2 is CompilationUnitElement) { |
| 262 LibraryElement library20 = ((enclosingElement2 as CompilationUnitEle
ment)).library; |
| 263 if (library20.isDartCore()) { |
| 264 EvaluationResultImpl leftArgument = arguments3[0].accept(this); |
| 265 EvaluationResultImpl rightArgument = arguments3[1].accept(this); |
| 266 return leftArgument.equalEqual(node, rightArgument); |
| 267 } |
| 268 } |
| 269 } |
| 270 } |
| 271 } |
| 272 return error(node, null); |
| 273 } |
| 274 EvaluationResultImpl visitNode(ASTNode node) => error(node, null); |
| 275 EvaluationResultImpl visitNullLiteral(NullLiteral node) => new ValidResult(nul
l); |
| 276 EvaluationResultImpl visitParenthesizedExpression(ParenthesizedExpression node
) => node.expression.accept(this); |
| 277 EvaluationResultImpl visitPrefixedIdentifier(PrefixedIdentifier node) => getCo
nstantValue(node, node.element); |
| 278 EvaluationResultImpl visitPrefixExpression(PrefixExpression node) { |
| 279 EvaluationResultImpl operand3 = node.operand.accept(this); |
| 280 while (true) { |
| 281 if (node.operator.type == TokenType.BANG) { |
| 282 return operand3.logicalNot(node); |
| 283 } else if (node.operator.type == TokenType.TILDE) { |
| 284 return operand3.bitNot(node); |
| 285 } else if (node.operator.type == TokenType.MINUS) { |
| 286 return operand3.negated(node); |
| 287 } |
| 288 break; |
| 289 } |
| 290 return error(node, null); |
| 291 } |
| 292 EvaluationResultImpl visitPropertyAccess(PropertyAccess node) => getConstantVa
lue(node, node.propertyName.element); |
| 293 EvaluationResultImpl visitSimpleIdentifier(SimpleIdentifier node) => getConsta
ntValue(node, node.element); |
| 294 EvaluationResultImpl visitSimpleStringLiteral(SimpleStringLiteral node) => new
ValidResult(node.value); |
| 295 EvaluationResultImpl visitStringInterpolation(StringInterpolation node) { |
| 296 EvaluationResultImpl result = null; |
| 297 for (InterpolationElement element in node.elements) { |
| 298 if (result == null) { |
| 299 result = element.accept(this); |
| 300 } else { |
| 301 result = result.concatenate(node, element.accept(this)); |
| 302 } |
| 303 } |
| 304 return result; |
| 305 } |
| 306 /** |
| 307 * Return a result object representing an error associated with the given node
. |
| 308 * @param node the AST node associated with the error |
| 309 * @param code the error code indicating the nature of the error |
| 310 * @return a result object representing an error associated with the given nod
e |
| 311 */ |
| 312 ErrorResult error(ASTNode node, ErrorCode code) => new ErrorResult.con1(node,
code == null ? CompileTimeErrorCode.INVALID_CONSTANT : code); |
| 313 /** |
| 314 * Return the constant value of the static constant represented by the given e
lement. |
| 315 * @param node the node to be used if an error needs to be reported |
| 316 * @param element the element whose value is to be returned |
| 317 * @return the constant value of the static constant |
| 318 */ |
| 319 EvaluationResultImpl getConstantValue(ASTNode node, Element element) { |
| 320 if (element is PropertyAccessorElementImpl) { |
| 321 element = ((element as PropertyAccessorElementImpl)).variable; |
| 322 } |
| 323 if (element is VariableElementImpl) { |
| 324 EvaluationResultImpl value = ((element as VariableElementImpl)).evaluation
Result; |
| 325 if (value != null) { |
| 326 return value; |
| 327 } |
| 328 } |
| 329 return error(node, null); |
| 330 } |
| 331 /** |
| 332 * Return the union of the errors encoded in the given results. |
| 333 * @param leftResult the first set of errors, or {@code null} if there was no
previous collection |
| 334 * of errors |
| 335 * @param rightResult the errors to be added to the collection, or a valid res
ult if there are no |
| 336 * errors to be added |
| 337 * @return the union of the errors encoded in the given results |
| 338 */ |
| 339 ErrorResult union(ErrorResult leftResult, EvaluationResultImpl rightResult) { |
| 340 if (rightResult is ErrorResult) { |
| 341 if (leftResult != null) { |
| 342 return new ErrorResult.con2(leftResult, (rightResult as ErrorResult)); |
| 343 } else { |
| 344 return rightResult as ErrorResult; |
| 345 } |
| 346 } |
| 347 return leftResult; |
| 348 } |
| 349 } |
| 350 /** |
| 351 * Instances of the class {@code ErrorResult} represent the result of evaluating
an expression that |
| 352 * is not a valid compile time constant. |
| 353 */ |
| 354 class ErrorResult extends EvaluationResultImpl { |
| 355 /** |
| 356 * The errors that prevent the expression from being a valid compile time cons
tant. |
| 357 */ |
| 358 List<ErrorResult_ErrorData> _errors = new List<ErrorResult_ErrorData>(); |
| 359 /** |
| 360 * Initialize a newly created result representing the error with the given cod
e reported against |
| 361 * the given node. |
| 362 * @param node the node against which the error should be reported |
| 363 * @param errorCode the error code for the error to be generated |
| 364 */ |
| 365 ErrorResult.con1(ASTNode node, ErrorCode errorCode) { |
| 366 _jtd_constructor_156_impl(node, errorCode); |
| 367 } |
| 368 _jtd_constructor_156_impl(ASTNode node, ErrorCode errorCode) { |
| 369 _errors.add(new ErrorResult_ErrorData(node, errorCode)); |
| 370 } |
| 371 /** |
| 372 * Initialize a newly created result to represent the union of the errors in t
he given result |
| 373 * objects. |
| 374 * @param firstResult the first set of results being merged |
| 375 * @param secondResult the second set of results being merged |
| 376 */ |
| 377 ErrorResult.con2(ErrorResult firstResult, ErrorResult secondResult) { |
| 378 _jtd_constructor_157_impl(firstResult, secondResult); |
| 379 } |
| 380 _jtd_constructor_157_impl(ErrorResult firstResult, ErrorResult secondResult) { |
| 381 _errors.addAll(firstResult._errors); |
| 382 _errors.addAll(secondResult._errors); |
| 383 } |
| 384 EvaluationResultImpl add(BinaryExpression node, EvaluationResultImpl rightOper
and) => rightOperand.addToError(node, this); |
| 385 EvaluationResultImpl bitAnd(BinaryExpression node, EvaluationResultImpl rightO
perand) => rightOperand.bitAndError(node, this); |
| 386 EvaluationResultImpl bitNot(Expression node) => this; |
| 387 EvaluationResultImpl bitOr(BinaryExpression node, EvaluationResultImpl rightOp
erand) => rightOperand.bitOrError(node, this); |
| 388 EvaluationResultImpl bitXor(BinaryExpression node, EvaluationResultImpl rightO
perand) => rightOperand.bitXorError(node, this); |
| 389 EvaluationResultImpl concatenate(Expression node, EvaluationResultImpl rightOp
erand) => rightOperand.concatenateError(node, this); |
| 390 EvaluationResultImpl divide(BinaryExpression node, EvaluationResultImpl rightO
perand) => rightOperand.divideError(node, this); |
| 391 EvaluationResultImpl equalEqual(Expression node, EvaluationResultImpl rightOpe
rand) => rightOperand.equalEqualError(node, this); |
| 392 List<ErrorResult_ErrorData> get errorData => _errors; |
| 393 List<AnalysisError> get errors => new List.from(_errors); |
| 394 EvaluationResultImpl greaterThan(BinaryExpression node, EvaluationResultImpl r
ightOperand) => rightOperand.greaterThanError(node, this); |
| 395 EvaluationResultImpl greaterThanOrEqual(BinaryExpression node, EvaluationResul
tImpl rightOperand) => rightOperand.greaterThanOrEqualError(node, this); |
| 396 EvaluationResultImpl integerDivide(BinaryExpression node, EvaluationResultImpl
rightOperand) => rightOperand.integerDivideError(node, this); |
| 397 EvaluationResultImpl integerDivideValid(BinaryExpression node, ValidResult lef
tOperand) => this; |
| 398 EvaluationResultImpl lessThan(BinaryExpression node, EvaluationResultImpl righ
tOperand) => rightOperand.lessThanError(node, this); |
| 399 EvaluationResultImpl lessThanOrEqual(BinaryExpression node, EvaluationResultIm
pl rightOperand) => rightOperand.lessThanOrEqualError(node, this); |
| 400 EvaluationResultImpl logicalAnd(BinaryExpression node, EvaluationResultImpl ri
ghtOperand) => rightOperand.logicalAndError(node, this); |
| 401 EvaluationResultImpl logicalNot(Expression node) => this; |
| 402 EvaluationResultImpl logicalOr(BinaryExpression node, EvaluationResultImpl rig
htOperand) => rightOperand.logicalOrError(node, this); |
| 403 EvaluationResultImpl minus(BinaryExpression node, EvaluationResultImpl rightOp
erand) => rightOperand.minusError(node, this); |
| 404 EvaluationResultImpl negated(Expression node) => this; |
| 405 EvaluationResultImpl notEqual(BinaryExpression node, EvaluationResultImpl righ
tOperand) => rightOperand.notEqualError(node, this); |
| 406 EvaluationResultImpl performToString(ASTNode node) => this; |
| 407 EvaluationResultImpl remainder(BinaryExpression node, EvaluationResultImpl rig
htOperand) => rightOperand.remainderError(node, this); |
| 408 EvaluationResultImpl shiftLeft(BinaryExpression node, EvaluationResultImpl rig
htOperand) => rightOperand.shiftLeftError(node, this); |
| 409 EvaluationResultImpl shiftRight(BinaryExpression node, EvaluationResultImpl ri
ghtOperand) => rightOperand.shiftRightError(node, this); |
| 410 EvaluationResultImpl times(BinaryExpression node, EvaluationResultImpl rightOp
erand) => rightOperand.timesError(node, this); |
| 411 EvaluationResultImpl addToError(BinaryExpression node, ErrorResult leftOperand
) => new ErrorResult.con2(this, leftOperand); |
| 412 EvaluationResultImpl addToValid(BinaryExpression node, ValidResult leftOperand
) => this; |
| 413 EvaluationResultImpl bitAndError(BinaryExpression node, ErrorResult leftOperan
d) => new ErrorResult.con2(this, leftOperand); |
| 414 EvaluationResultImpl bitAndValid(BinaryExpression node, ValidResult leftOperan
d) => this; |
| 415 EvaluationResultImpl bitOrError(BinaryExpression node, ErrorResult leftOperand
) => new ErrorResult.con2(this, leftOperand); |
| 416 EvaluationResultImpl bitOrValid(BinaryExpression node, ValidResult leftOperand
) => this; |
| 417 EvaluationResultImpl bitXorError(BinaryExpression node, ErrorResult leftOperan
d) => new ErrorResult.con2(this, leftOperand); |
| 418 EvaluationResultImpl bitXorValid(BinaryExpression node, ValidResult leftOperan
d) => this; |
| 419 EvaluationResultImpl concatenateError(Expression node, ErrorResult leftOperand
) => new ErrorResult.con2(this, leftOperand); |
| 420 EvaluationResultImpl concatenateValid(Expression node, ValidResult leftOperand
) => this; |
| 421 EvaluationResultImpl divideError(BinaryExpression node, ErrorResult leftOperan
d) => new ErrorResult.con2(this, leftOperand); |
| 422 EvaluationResultImpl divideValid(BinaryExpression node, ValidResult leftOperan
d) => this; |
| 423 EvaluationResultImpl equalEqualError(Expression node, ErrorResult leftOperand)
=> new ErrorResult.con2(this, leftOperand); |
| 424 EvaluationResultImpl equalEqualValid(Expression node, ValidResult leftOperand)
=> this; |
| 425 EvaluationResultImpl greaterThanError(BinaryExpression node, ErrorResult leftO
perand) => new ErrorResult.con2(this, leftOperand); |
| 426 EvaluationResultImpl greaterThanOrEqualError(BinaryExpression node, ErrorResul
t leftOperand) => new ErrorResult.con2(this, leftOperand); |
| 427 EvaluationResultImpl greaterThanOrEqualValid(BinaryExpression node, ValidResul
t leftOperand) => this; |
| 428 EvaluationResultImpl greaterThanValid(BinaryExpression node, ValidResult leftO
perand) => this; |
| 429 EvaluationResultImpl integerDivideError(BinaryExpression node, ErrorResult lef
tOperand) => new ErrorResult.con2(this, leftOperand); |
| 430 EvaluationResultImpl lessThanError(BinaryExpression node, ErrorResult leftOper
and) => new ErrorResult.con2(this, leftOperand); |
| 431 EvaluationResultImpl lessThanOrEqualError(BinaryExpression node, ErrorResult l
eftOperand) => new ErrorResult.con2(this, leftOperand); |
| 432 EvaluationResultImpl lessThanOrEqualValid(BinaryExpression node, ValidResult l
eftOperand) => this; |
| 433 EvaluationResultImpl lessThanValid(BinaryExpression node, ValidResult leftOper
and) => this; |
| 434 EvaluationResultImpl logicalAndError(BinaryExpression node, ErrorResult leftOp
erand) => new ErrorResult.con2(this, leftOperand); |
| 435 EvaluationResultImpl logicalAndValid(BinaryExpression node, ValidResult leftOp
erand) => this; |
| 436 EvaluationResultImpl logicalOrError(BinaryExpression node, ErrorResult leftOpe
rand) => new ErrorResult.con2(this, leftOperand); |
| 437 EvaluationResultImpl logicalOrValid(BinaryExpression node, ValidResult leftOpe
rand) => this; |
| 438 EvaluationResultImpl minusError(BinaryExpression node, ErrorResult leftOperand
) => new ErrorResult.con2(this, leftOperand); |
| 439 EvaluationResultImpl minusValid(BinaryExpression node, ValidResult leftOperand
) => this; |
| 440 EvaluationResultImpl notEqualError(BinaryExpression node, ErrorResult leftOper
and) => new ErrorResult.con2(this, leftOperand); |
| 441 EvaluationResultImpl notEqualValid(BinaryExpression node, ValidResult leftOper
and) => this; |
| 442 EvaluationResultImpl remainderError(BinaryExpression node, ErrorResult leftOpe
rand) => new ErrorResult.con2(this, leftOperand); |
| 443 EvaluationResultImpl remainderValid(BinaryExpression node, ValidResult leftOpe
rand) => this; |
| 444 EvaluationResultImpl shiftLeftError(BinaryExpression node, ErrorResult leftOpe
rand) => new ErrorResult.con2(this, leftOperand); |
| 445 EvaluationResultImpl shiftLeftValid(BinaryExpression node, ValidResult leftOpe
rand) => this; |
| 446 EvaluationResultImpl shiftRightError(BinaryExpression node, ErrorResult leftOp
erand) => new ErrorResult.con2(this, leftOperand); |
| 447 EvaluationResultImpl shiftRightValid(BinaryExpression node, ValidResult leftOp
erand) => this; |
| 448 EvaluationResultImpl timesError(BinaryExpression node, ErrorResult leftOperand
) => new ErrorResult.con2(this, leftOperand); |
| 449 EvaluationResultImpl timesValid(BinaryExpression node, ValidResult leftOperand
) => this; |
| 450 } |
| 451 class ErrorResult_ErrorData { |
| 452 /** |
| 453 * The node against which the error should be reported. |
| 454 */ |
| 455 ASTNode _node; |
| 456 /** |
| 457 * The error code for the error to be generated. |
| 458 */ |
| 459 ErrorCode _errorCode; |
| 460 /** |
| 461 * Initialize a newly created data holder to represent the error with the give
n code reported |
| 462 * against the given node. |
| 463 * @param node the node against which the error should be reported |
| 464 * @param errorCode the error code for the error to be generated |
| 465 */ |
| 466 ErrorResult_ErrorData(ASTNode node, ErrorCode errorCode) { |
| 467 this._node = node; |
| 468 this._errorCode = errorCode; |
| 469 } |
| 470 /** |
| 471 * Return the error code for the error to be generated. |
| 472 * @return the error code for the error to be generated |
| 473 */ |
| 474 ErrorCode get errorCode => _errorCode; |
| 475 /** |
| 476 * Return the node against which the error should be reported. |
| 477 * @return the node against which the error should be reported |
| 478 */ |
| 479 ASTNode get node => _node; |
| 480 } |
| 481 /** |
| 482 * Instances of the class {@code InternalResult} represent the result of attempt
ing to evaluate a |
| 483 * expression. |
| 484 */ |
| 485 abstract class EvaluationResultImpl { |
| 486 EvaluationResultImpl add(BinaryExpression node, EvaluationResultImpl rightOper
and); |
| 487 EvaluationResultImpl bitAnd(BinaryExpression node, EvaluationResultImpl rightO
perand); |
| 488 EvaluationResultImpl bitNot(Expression node); |
| 489 EvaluationResultImpl bitOr(BinaryExpression node, EvaluationResultImpl rightOp
erand); |
| 490 EvaluationResultImpl bitXor(BinaryExpression node, EvaluationResultImpl rightO
perand); |
| 491 EvaluationResultImpl concatenate(Expression node, EvaluationResultImpl rightOp
erand); |
| 492 EvaluationResultImpl divide(BinaryExpression node, EvaluationResultImpl rightO
perand); |
| 493 EvaluationResultImpl equalEqual(Expression node, EvaluationResultImpl rightOpe
rand); |
| 494 EvaluationResultImpl greaterThan(BinaryExpression node, EvaluationResultImpl r
ightOperand); |
| 495 EvaluationResultImpl greaterThanOrEqual(BinaryExpression node, EvaluationResul
tImpl rightOperand); |
| 496 EvaluationResultImpl integerDivide(BinaryExpression node, EvaluationResultImpl
rightOperand); |
| 497 EvaluationResultImpl lessThan(BinaryExpression node, EvaluationResultImpl righ
tOperand); |
| 498 EvaluationResultImpl lessThanOrEqual(BinaryExpression node, EvaluationResultIm
pl rightOperand); |
| 499 EvaluationResultImpl logicalAnd(BinaryExpression node, EvaluationResultImpl ri
ghtOperand); |
| 500 EvaluationResultImpl logicalNot(Expression node); |
| 501 EvaluationResultImpl logicalOr(BinaryExpression node, EvaluationResultImpl rig
htOperand); |
| 502 EvaluationResultImpl minus(BinaryExpression node, EvaluationResultImpl rightOp
erand); |
| 503 EvaluationResultImpl negated(Expression node); |
| 504 EvaluationResultImpl notEqual(BinaryExpression node, EvaluationResultImpl righ
tOperand); |
| 505 EvaluationResultImpl performToString(ASTNode node); |
| 506 EvaluationResultImpl remainder(BinaryExpression node, EvaluationResultImpl rig
htOperand); |
| 507 EvaluationResultImpl shiftLeft(BinaryExpression node, EvaluationResultImpl rig
htOperand); |
| 508 EvaluationResultImpl shiftRight(BinaryExpression node, EvaluationResultImpl ri
ghtOperand); |
| 509 EvaluationResultImpl times(BinaryExpression node, EvaluationResultImpl rightOp
erand); |
| 510 EvaluationResultImpl addToError(BinaryExpression node, ErrorResult leftOperand
); |
| 511 EvaluationResultImpl addToValid(BinaryExpression node, ValidResult leftOperand
); |
| 512 EvaluationResultImpl bitAndError(BinaryExpression node, ErrorResult leftOperan
d); |
| 513 EvaluationResultImpl bitAndValid(BinaryExpression node, ValidResult leftOperan
d); |
| 514 EvaluationResultImpl bitOrError(BinaryExpression node, ErrorResult leftOperand
); |
| 515 EvaluationResultImpl bitOrValid(BinaryExpression node, ValidResult leftOperand
); |
| 516 EvaluationResultImpl bitXorError(BinaryExpression node, ErrorResult leftOperan
d); |
| 517 EvaluationResultImpl bitXorValid(BinaryExpression node, ValidResult leftOperan
d); |
| 518 EvaluationResultImpl concatenateError(Expression node, ErrorResult leftOperand
); |
| 519 EvaluationResultImpl concatenateValid(Expression node, ValidResult leftOperand
); |
| 520 EvaluationResultImpl divideError(BinaryExpression node, ErrorResult leftOperan
d); |
| 521 EvaluationResultImpl divideValid(BinaryExpression node, ValidResult leftOperan
d); |
| 522 EvaluationResultImpl equalEqualError(Expression node, ErrorResult leftOperand)
; |
| 523 EvaluationResultImpl equalEqualValid(Expression node, ValidResult leftOperand)
; |
| 524 EvaluationResultImpl greaterThanError(BinaryExpression node, ErrorResult leftO
perand); |
| 525 EvaluationResultImpl greaterThanOrEqualError(BinaryExpression node, ErrorResul
t leftOperand); |
| 526 EvaluationResultImpl greaterThanOrEqualValid(BinaryExpression node, ValidResul
t leftOperand); |
| 527 EvaluationResultImpl greaterThanValid(BinaryExpression node, ValidResult leftO
perand); |
| 528 EvaluationResultImpl integerDivideError(BinaryExpression node, ErrorResult lef
tOperand); |
| 529 EvaluationResultImpl integerDivideValid(BinaryExpression node, ValidResult lef
tOperand); |
| 530 EvaluationResultImpl lessThanError(BinaryExpression node, ErrorResult leftOper
and); |
| 531 EvaluationResultImpl lessThanOrEqualError(BinaryExpression node, ErrorResult l
eftOperand); |
| 532 EvaluationResultImpl lessThanOrEqualValid(BinaryExpression node, ValidResult l
eftOperand); |
| 533 EvaluationResultImpl lessThanValid(BinaryExpression node, ValidResult leftOper
and); |
| 534 EvaluationResultImpl logicalAndError(BinaryExpression node, ErrorResult leftOp
erand); |
| 535 EvaluationResultImpl logicalAndValid(BinaryExpression node, ValidResult leftOp
erand); |
| 536 EvaluationResultImpl logicalOrError(BinaryExpression node, ErrorResult leftOpe
rand); |
| 537 EvaluationResultImpl logicalOrValid(BinaryExpression node, ValidResult leftOpe
rand); |
| 538 EvaluationResultImpl minusError(BinaryExpression node, ErrorResult leftOperand
); |
| 539 EvaluationResultImpl minusValid(BinaryExpression node, ValidResult leftOperand
); |
| 540 EvaluationResultImpl notEqualError(BinaryExpression node, ErrorResult leftOper
and); |
| 541 EvaluationResultImpl notEqualValid(BinaryExpression node, ValidResult leftOper
and); |
| 542 EvaluationResultImpl remainderError(BinaryExpression node, ErrorResult leftOpe
rand); |
| 543 EvaluationResultImpl remainderValid(BinaryExpression node, ValidResult leftOpe
rand); |
| 544 EvaluationResultImpl shiftLeftError(BinaryExpression node, ErrorResult leftOpe
rand); |
| 545 EvaluationResultImpl shiftLeftValid(BinaryExpression node, ValidResult leftOpe
rand); |
| 546 EvaluationResultImpl shiftRightError(BinaryExpression node, ErrorResult leftOp
erand); |
| 547 EvaluationResultImpl shiftRightValid(BinaryExpression node, ValidResult leftOp
erand); |
| 548 EvaluationResultImpl timesError(BinaryExpression node, ErrorResult leftOperand
); |
| 549 EvaluationResultImpl timesValid(BinaryExpression node, ValidResult leftOperand
); |
| 550 } |
| 551 /** |
| 552 * Instances of the class {@code ValidResult} represent the result of attempting
to evaluate a valid |
| 553 * compile time constant expression. |
| 554 */ |
| 555 class ValidResult extends EvaluationResultImpl { |
| 556 /** |
| 557 * A result object representing the value 'false'. |
| 558 */ |
| 559 static ValidResult RESULT_FALSE = new ValidResult(false); |
| 560 /** |
| 561 * A result object representing the an arbitrary object on which no further op
erations can be |
| 562 * performed. |
| 563 */ |
| 564 static ValidResult RESULT_OBJECT = new ValidResult(new Object()); |
| 565 /** |
| 566 * A result object representing the value 'true'. |
| 567 */ |
| 568 static ValidResult RESULT_TRUE = new ValidResult(true); |
| 569 /** |
| 570 * The value of the expression. |
| 571 */ |
| 572 Object _value; |
| 573 /** |
| 574 * Initialize a newly created result to represent the given value. |
| 575 * @param value the value of the expression |
| 576 */ |
| 577 ValidResult(Object value) { |
| 578 this._value = value; |
| 579 } |
| 580 EvaluationResultImpl add(BinaryExpression node, EvaluationResultImpl rightOper
and) => rightOperand.addToValid(node, this); |
| 581 EvaluationResultImpl bitAnd(BinaryExpression node, EvaluationResultImpl rightO
perand) => rightOperand.bitAndValid(node, this); |
| 582 EvaluationResultImpl bitNot(Expression node) { |
| 583 if (_value == null) { |
| 584 return error(node); |
| 585 } else if (_value is int) { |
| 586 return valueOf(~((_value as int))); |
| 587 } |
| 588 return error(node); |
| 589 } |
| 590 EvaluationResultImpl bitOr(BinaryExpression node, EvaluationResultImpl rightOp
erand) => rightOperand.bitOrValid(node, this); |
| 591 EvaluationResultImpl bitXor(BinaryExpression node, EvaluationResultImpl rightO
perand) => rightOperand.bitXorValid(node, this); |
| 592 EvaluationResultImpl concatenate(Expression node, EvaluationResultImpl rightOp
erand) => rightOperand.concatenateValid(node, this); |
| 593 EvaluationResultImpl divide(BinaryExpression node, EvaluationResultImpl rightO
perand) => rightOperand.divideValid(node, this); |
| 594 EvaluationResultImpl equalEqual(Expression node, EvaluationResultImpl rightOpe
rand) => rightOperand.equalEqualValid(node, this); |
| 595 Object get value => _value; |
| 596 EvaluationResultImpl greaterThan(BinaryExpression node, EvaluationResultImpl r
ightOperand) => rightOperand.greaterThanValid(node, this); |
| 597 EvaluationResultImpl greaterThanOrEqual(BinaryExpression node, EvaluationResul
tImpl rightOperand) => rightOperand.greaterThanOrEqualValid(node, this); |
| 598 EvaluationResultImpl integerDivide(BinaryExpression node, EvaluationResultImpl
rightOperand) => rightOperand.integerDivideValid(node, this); |
| 599 EvaluationResultImpl lessThan(BinaryExpression node, EvaluationResultImpl righ
tOperand) => rightOperand.lessThanValid(node, this); |
| 600 EvaluationResultImpl lessThanOrEqual(BinaryExpression node, EvaluationResultIm
pl rightOperand) => rightOperand.lessThanOrEqualValid(node, this); |
| 601 EvaluationResultImpl logicalAnd(BinaryExpression node, EvaluationResultImpl ri
ghtOperand) => rightOperand.logicalAndValid(node, this); |
| 602 EvaluationResultImpl logicalNot(Expression node) { |
| 603 if (_value == null) { |
| 604 return RESULT_TRUE; |
| 605 } else if (_value is bool) { |
| 606 return ((_value as bool)) ? RESULT_FALSE : RESULT_TRUE; |
| 607 } |
| 608 return error(node); |
| 609 } |
| 610 EvaluationResultImpl logicalOr(BinaryExpression node, EvaluationResultImpl rig
htOperand) => rightOperand.logicalOrValid(node, this); |
| 611 EvaluationResultImpl minus(BinaryExpression node, EvaluationResultImpl rightOp
erand) => rightOperand.minusValid(node, this); |
| 612 EvaluationResultImpl negated(Expression node) { |
| 613 if (_value == null) { |
| 614 return error(node); |
| 615 } else if (_value is int) { |
| 616 return valueOf(-((_value as int))); |
| 617 } else if (_value is double) { |
| 618 return valueOf3(-((_value as double))); |
| 619 } |
| 620 return error(node); |
| 621 } |
| 622 EvaluationResultImpl notEqual(BinaryExpression node, EvaluationResultImpl righ
tOperand) => rightOperand.notEqualValid(node, this); |
| 623 EvaluationResultImpl performToString(ASTNode node) { |
| 624 if (_value == null) { |
| 625 return valueOf4("null"); |
| 626 } else if (_value is bool) { |
| 627 return valueOf4(((_value as bool)).toString()); |
| 628 } else if (_value is int) { |
| 629 return valueOf4(((_value as int)).toString()); |
| 630 } else if (_value is double) { |
| 631 return valueOf4(((_value as double)).toString()); |
| 632 } else if (_value is String) { |
| 633 return this; |
| 634 } |
| 635 return error(node); |
| 636 } |
| 637 EvaluationResultImpl remainder(BinaryExpression node, EvaluationResultImpl rig
htOperand) => rightOperand.remainderValid(node, this); |
| 638 EvaluationResultImpl shiftLeft(BinaryExpression node, EvaluationResultImpl rig
htOperand) => rightOperand.shiftLeftValid(node, this); |
| 639 EvaluationResultImpl shiftRight(BinaryExpression node, EvaluationResultImpl ri
ghtOperand) => rightOperand.shiftRightValid(node, this); |
| 640 EvaluationResultImpl times(BinaryExpression node, EvaluationResultImpl rightOp
erand) => rightOperand.timesValid(node, this); |
| 641 String toString() { |
| 642 if (_value == null) { |
| 643 return "null"; |
| 644 } |
| 645 return _value.toString(); |
| 646 } |
| 647 EvaluationResultImpl addToError(BinaryExpression node, ErrorResult leftOperand
) => leftOperand; |
| 648 EvaluationResultImpl addToValid(BinaryExpression node, ValidResult leftOperand
3) { |
| 649 Object leftValue = leftOperand3.value; |
| 650 if (leftValue == null) { |
| 651 return error(node.leftOperand); |
| 652 } else if (_value == null) { |
| 653 return error(node.rightOperand); |
| 654 } else if (leftValue is int) { |
| 655 if (_value is int) { |
| 656 return valueOf(((leftValue as int)) + (_value as int)); |
| 657 } else if (_value is double) { |
| 658 return valueOf3(((leftValue as int)).toDouble() + ((_value as double))); |
| 659 } |
| 660 } else if (leftValue is double) { |
| 661 if (_value is int) { |
| 662 return valueOf3(((leftValue as double)) + ((_value as int)).toDouble()); |
| 663 } else if (_value is double) { |
| 664 return valueOf3(((leftValue as double)) + ((_value as double))); |
| 665 } |
| 666 } else if (leftValue is String) { |
| 667 if (_value is String) { |
| 668 return valueOf4("${((leftValue as String))}${((_value as String))}"); |
| 669 } |
| 670 } |
| 671 return error(node); |
| 672 } |
| 673 EvaluationResultImpl bitAndError(BinaryExpression node, ErrorResult leftOperan
d) => leftOperand; |
| 674 EvaluationResultImpl bitAndValid(BinaryExpression node, ValidResult leftOperan
d4) { |
| 675 Object leftValue = leftOperand4.value; |
| 676 if (leftValue == null) { |
| 677 return error(node.leftOperand); |
| 678 } else if (_value == null) { |
| 679 return error(node.rightOperand); |
| 680 } else if (leftValue is int) { |
| 681 if (_value is int) { |
| 682 return valueOf(((leftValue as int)) & (_value as int)); |
| 683 } |
| 684 return error(node.leftOperand); |
| 685 } |
| 686 if (_value is int) { |
| 687 return error(node.rightOperand); |
| 688 } |
| 689 return union(error(node.leftOperand), error(node.rightOperand)); |
| 690 } |
| 691 EvaluationResultImpl bitOrError(BinaryExpression node, ErrorResult leftOperand
) => leftOperand; |
| 692 EvaluationResultImpl bitOrValid(BinaryExpression node, ValidResult leftOperand
5) { |
| 693 Object leftValue = leftOperand5.value; |
| 694 if (leftValue == null) { |
| 695 return error(node.leftOperand); |
| 696 } else if (_value == null) { |
| 697 return error(node.rightOperand); |
| 698 } else if (leftValue is int) { |
| 699 if (_value is int) { |
| 700 return valueOf(((leftValue as int)) | (_value as int)); |
| 701 } |
| 702 return error(node.leftOperand); |
| 703 } |
| 704 if (_value is int) { |
| 705 return error(node.rightOperand); |
| 706 } |
| 707 return union(error(node.leftOperand), error(node.rightOperand)); |
| 708 } |
| 709 EvaluationResultImpl bitXorError(BinaryExpression node, ErrorResult leftOperan
d) => leftOperand; |
| 710 EvaluationResultImpl bitXorValid(BinaryExpression node, ValidResult leftOperan
d6) { |
| 711 Object leftValue = leftOperand6.value; |
| 712 if (leftValue == null) { |
| 713 return error(node.leftOperand); |
| 714 } else if (_value == null) { |
| 715 return error(node.rightOperand); |
| 716 } else if (leftValue is int) { |
| 717 if (_value is int) { |
| 718 return valueOf(((leftValue as int)) ^ (_value as int)); |
| 719 } |
| 720 return error(node.leftOperand); |
| 721 } |
| 722 if (_value is int) { |
| 723 return error(node.rightOperand); |
| 724 } |
| 725 return union(error(node.leftOperand), error(node.rightOperand)); |
| 726 } |
| 727 EvaluationResultImpl concatenateError(Expression node, ErrorResult leftOperand
) => leftOperand; |
| 728 EvaluationResultImpl concatenateValid(Expression node, ValidResult leftOperand
) { |
| 729 Object leftValue = leftOperand.value; |
| 730 if (leftValue is String && _value is String) { |
| 731 return valueOf4("${((leftValue as String))}${((_value as String))}"); |
| 732 } |
| 733 return error(node); |
| 734 } |
| 735 EvaluationResultImpl divideError(BinaryExpression node, ErrorResult leftOperan
d) => leftOperand; |
| 736 EvaluationResultImpl divideValid(BinaryExpression node, ValidResult leftOperan
d7) { |
| 737 Object leftValue = leftOperand7.value; |
| 738 if (leftValue == null) { |
| 739 return error(node.leftOperand); |
| 740 } else if (_value == null) { |
| 741 return error(node.rightOperand); |
| 742 } else if (leftValue is int) { |
| 743 if (_value is int) { |
| 744 if (((_value as int)) == 0) { |
| 745 return error2(node.rightOperand, CompileTimeErrorCode.COMPILE_TIME_CON
STANT_RAISES_EXCEPTION_DIVIDE_BY_ZERO); |
| 746 } |
| 747 return valueOf(((leftValue as int)) ~/ (_value as int)); |
| 748 } else if (_value is double) { |
| 749 return valueOf3(((leftValue as int)).toDouble() / ((_value as double))); |
| 750 } |
| 751 } else if (leftValue is double) { |
| 752 if (_value is int) { |
| 753 return valueOf3(((leftValue as double)) / ((_value as int)).toDouble()); |
| 754 } else if (_value is double) { |
| 755 return valueOf3(((leftValue as double)) / ((_value as double))); |
| 756 } |
| 757 } |
| 758 return error(node); |
| 759 } |
| 760 EvaluationResultImpl equalEqualError(Expression node, ErrorResult leftOperand)
=> leftOperand; |
| 761 EvaluationResultImpl equalEqualValid(Expression node, ValidResult leftOperand)
{ |
| 762 Object leftValue = leftOperand.value; |
| 763 if (leftValue == null) { |
| 764 return valueOf2(_value == null); |
| 765 } else if (leftValue is bool) { |
| 766 if (_value is bool) { |
| 767 return valueOf2(identical(((leftValue as bool)), ((_value as bool)))); |
| 768 } |
| 769 return RESULT_FALSE; |
| 770 } else if (leftValue is int) { |
| 771 if (_value is int) { |
| 772 return valueOf2(((leftValue as int)) == _value); |
| 773 } else if (_value is double) { |
| 774 return valueOf2(toDouble((leftValue as int)) == _value); |
| 775 } |
| 776 return RESULT_FALSE; |
| 777 } else if (leftValue is double) { |
| 778 if (_value is int) { |
| 779 return valueOf2(((leftValue as double)) == toDouble((_value as int))); |
| 780 } else if (_value is double) { |
| 781 return valueOf2(((leftValue as double)) == _value); |
| 782 } |
| 783 return RESULT_FALSE; |
| 784 } else if (leftValue is String) { |
| 785 if (_value is String) { |
| 786 return valueOf2(((leftValue as String)) == _value); |
| 787 } |
| 788 return RESULT_FALSE; |
| 789 } |
| 790 return RESULT_FALSE; |
| 791 } |
| 792 EvaluationResultImpl greaterThanError(BinaryExpression node, ErrorResult leftO
perand) => leftOperand; |
| 793 EvaluationResultImpl greaterThanOrEqualError(BinaryExpression node, ErrorResul
t leftOperand) => leftOperand; |
| 794 EvaluationResultImpl greaterThanOrEqualValid(BinaryExpression node, ValidResul
t leftOperand8) { |
| 795 Object leftValue = leftOperand8.value; |
| 796 if (leftValue == null) { |
| 797 return error(node.leftOperand); |
| 798 } else if (_value == null) { |
| 799 return error(node.rightOperand); |
| 800 } else if (leftValue is int) { |
| 801 if (_value is int) { |
| 802 return valueOf2(((leftValue as int)).compareTo((_value as int)) >= 0); |
| 803 } else if (_value is double) { |
| 804 return valueOf2(((leftValue as int)).toDouble() >= ((_value as double)))
; |
| 805 } |
| 806 } else if (leftValue is double) { |
| 807 if (_value is int) { |
| 808 return valueOf2(((leftValue as double)) >= ((_value as int)).toDouble())
; |
| 809 } else if (_value is double) { |
| 810 return valueOf2(((leftValue as double)) >= ((_value as double))); |
| 811 } |
| 812 } |
| 813 return error(node); |
| 814 } |
| 815 EvaluationResultImpl greaterThanValid(BinaryExpression node, ValidResult leftO
perand9) { |
| 816 Object leftValue = leftOperand9.value; |
| 817 if (leftValue == null) { |
| 818 return error(node.leftOperand); |
| 819 } else if (_value == null) { |
| 820 return error(node.rightOperand); |
| 821 } else if (leftValue is int) { |
| 822 if (_value is int) { |
| 823 return valueOf2(((leftValue as int)).compareTo((_value as int)) > 0); |
| 824 } else if (_value is double) { |
| 825 return valueOf2(((leftValue as int)).toDouble() > ((_value as double))); |
| 826 } |
| 827 } else if (leftValue is double) { |
| 828 if (_value is int) { |
| 829 return valueOf2(((leftValue as double)) > ((_value as int)).toDouble()); |
| 830 } else if (_value is double) { |
| 831 return valueOf2(((leftValue as double)) > ((_value as double))); |
| 832 } |
| 833 } |
| 834 return error(node); |
| 835 } |
| 836 EvaluationResultImpl integerDivideError(BinaryExpression node, ErrorResult lef
tOperand) => leftOperand; |
| 837 EvaluationResultImpl integerDivideValid(BinaryExpression node, ValidResult lef
tOperand10) { |
| 838 Object leftValue = leftOperand10.value; |
| 839 if (leftValue == null) { |
| 840 return error(node.leftOperand); |
| 841 } else if (_value == null) { |
| 842 return error(node.rightOperand); |
| 843 } else if (leftValue is int) { |
| 844 if (_value is int) { |
| 845 if (((_value as int)) == 0) { |
| 846 return error2(node.rightOperand, CompileTimeErrorCode.COMPILE_TIME_CON
STANT_RAISES_EXCEPTION_DIVIDE_BY_ZERO); |
| 847 } |
| 848 return valueOf(((leftValue as int)) ~/ (_value as int)); |
| 849 } else if (_value is double) { |
| 850 double result = ((leftValue as int)).toDouble() / ((_value as double)); |
| 851 return valueOf((result as int)); |
| 852 } |
| 853 } else if (leftValue is double) { |
| 854 if (_value is int) { |
| 855 double result = ((leftValue as double)) / ((_value as int)).toDouble(); |
| 856 return valueOf((result as int)); |
| 857 } else if (_value is double) { |
| 858 double result = ((leftValue as double)) / ((_value as double)); |
| 859 return valueOf((result as int)); |
| 860 } |
| 861 } |
| 862 return error(node); |
| 863 } |
| 864 EvaluationResultImpl lessThanError(BinaryExpression node, ErrorResult leftOper
and) => leftOperand; |
| 865 EvaluationResultImpl lessThanOrEqualError(BinaryExpression node, ErrorResult l
eftOperand) => leftOperand; |
| 866 EvaluationResultImpl lessThanOrEqualValid(BinaryExpression node, ValidResult l
eftOperand11) { |
| 867 Object leftValue = leftOperand11.value; |
| 868 if (leftValue == null) { |
| 869 return error(node.leftOperand); |
| 870 } else if (_value == null) { |
| 871 return error(node.rightOperand); |
| 872 } else if (leftValue is int) { |
| 873 if (_value is int) { |
| 874 return valueOf2(((leftValue as int)).compareTo((_value as int)) <= 0); |
| 875 } else if (_value is double) { |
| 876 return valueOf2(((leftValue as int)).toDouble() <= ((_value as double)))
; |
| 877 } |
| 878 } else if (leftValue is double) { |
| 879 if (_value is int) { |
| 880 return valueOf2(((leftValue as double)) <= ((_value as int)).toDouble())
; |
| 881 } else if (_value is double) { |
| 882 return valueOf2(((leftValue as double)) <= ((_value as double))); |
| 883 } |
| 884 } |
| 885 return error(node); |
| 886 } |
| 887 EvaluationResultImpl lessThanValid(BinaryExpression node, ValidResult leftOper
and12) { |
| 888 Object leftValue = leftOperand12.value; |
| 889 if (leftValue == null) { |
| 890 return error(node.leftOperand); |
| 891 } else if (_value == null) { |
| 892 return error(node.rightOperand); |
| 893 } else if (leftValue is int) { |
| 894 if (_value is int) { |
| 895 return valueOf2(((leftValue as int)).compareTo((_value as int)) < 0); |
| 896 } else if (_value is double) { |
| 897 return valueOf2(((leftValue as int)).toDouble() < ((_value as double))); |
| 898 } |
| 899 } else if (leftValue is double) { |
| 900 if (_value is int) { |
| 901 return valueOf2(((leftValue as double)) < ((_value as int)).toDouble()); |
| 902 } else if (_value is double) { |
| 903 return valueOf2(((leftValue as double)) < ((_value as double))); |
| 904 } |
| 905 } |
| 906 return error(node); |
| 907 } |
| 908 EvaluationResultImpl logicalAndError(BinaryExpression node, ErrorResult leftOp
erand) => leftOperand; |
| 909 EvaluationResultImpl logicalAndValid(BinaryExpression node, ValidResult leftOp
erand) { |
| 910 Object leftValue = leftOperand.value; |
| 911 if (leftValue is bool && ((leftValue as bool))) { |
| 912 return booleanConversion(node.rightOperand, _value); |
| 913 } |
| 914 return RESULT_FALSE; |
| 915 } |
| 916 EvaluationResultImpl logicalOrError(BinaryExpression node, ErrorResult leftOpe
rand) => leftOperand; |
| 917 EvaluationResultImpl logicalOrValid(BinaryExpression node, ValidResult leftOpe
rand) { |
| 918 Object leftValue = leftOperand.value; |
| 919 if (leftValue is bool && ((leftValue as bool))) { |
| 920 return RESULT_TRUE; |
| 921 } |
| 922 return booleanConversion(node.rightOperand, _value); |
| 923 } |
| 924 EvaluationResultImpl minusError(BinaryExpression node, ErrorResult leftOperand
) => leftOperand; |
| 925 EvaluationResultImpl minusValid(BinaryExpression node, ValidResult leftOperand
13) { |
| 926 Object leftValue = leftOperand13.value; |
| 927 if (leftValue == null) { |
| 928 return error(node.leftOperand); |
| 929 } else if (_value == null) { |
| 930 return error(node.rightOperand); |
| 931 } else if (leftValue is int) { |
| 932 if (_value is int) { |
| 933 return valueOf(((leftValue as int)) - (_value as int)); |
| 934 } else if (_value is double) { |
| 935 return valueOf3(((leftValue as int)).toDouble() - ((_value as double))); |
| 936 } |
| 937 } else if (leftValue is double) { |
| 938 if (_value is int) { |
| 939 return valueOf3(((leftValue as double)) - ((_value as int)).toDouble()); |
| 940 } else if (_value is double) { |
| 941 return valueOf3(((leftValue as double)) - ((_value as double))); |
| 942 } |
| 943 } |
| 944 return error(node); |
| 945 } |
| 946 EvaluationResultImpl notEqualError(BinaryExpression node, ErrorResult leftOper
and) => leftOperand; |
| 947 EvaluationResultImpl notEqualValid(BinaryExpression node, ValidResult leftOper
and) { |
| 948 Object leftValue = leftOperand.value; |
| 949 if (leftValue == null) { |
| 950 return valueOf2(_value != null); |
| 951 } else if (leftValue is bool) { |
| 952 if (_value is bool) { |
| 953 return valueOf2(((leftValue as bool)) != ((_value as bool))); |
| 954 } |
| 955 return RESULT_TRUE; |
| 956 } else if (leftValue is int) { |
| 957 if (_value is int) { |
| 958 return valueOf2(((leftValue as int)) != _value); |
| 959 } else if (_value is double) { |
| 960 return valueOf2(toDouble((leftValue as int)) != _value); |
| 961 } |
| 962 return RESULT_TRUE; |
| 963 } else if (leftValue is double) { |
| 964 if (_value is int) { |
| 965 return valueOf2(((leftValue as double)) != toDouble((_value as int))); |
| 966 } else if (_value is double) { |
| 967 return valueOf2(((leftValue as double)) != _value); |
| 968 } |
| 969 return RESULT_TRUE; |
| 970 } else if (leftValue is String) { |
| 971 if (_value is String) { |
| 972 return valueOf2(((leftValue as String)) != _value); |
| 973 } |
| 974 return RESULT_TRUE; |
| 975 } |
| 976 return RESULT_TRUE; |
| 977 } |
| 978 EvaluationResultImpl remainderError(BinaryExpression node, ErrorResult leftOpe
rand) => leftOperand; |
| 979 EvaluationResultImpl remainderValid(BinaryExpression node, ValidResult leftOpe
rand14) { |
| 980 Object leftValue = leftOperand14.value; |
| 981 if (leftValue == null) { |
| 982 return error(node.leftOperand); |
| 983 } else if (_value == null) { |
| 984 return error(node.rightOperand); |
| 985 } else if (leftValue is int) { |
| 986 if (((_value as int)) == 0) { |
| 987 return error2(node.rightOperand, CompileTimeErrorCode.COMPILE_TIME_CONST
ANT_RAISES_EXCEPTION_DIVIDE_BY_ZERO); |
| 988 } |
| 989 if (_value is int) { |
| 990 return valueOf(((leftValue as int)).remainder((_value as int))); |
| 991 } else if (_value is double) { |
| 992 return valueOf3(((leftValue as int)).toDouble() % ((_value as double))); |
| 993 } |
| 994 } else if (leftValue is double) { |
| 995 if (_value is int) { |
| 996 return valueOf3(((leftValue as double)) % ((_value as int)).toDouble()); |
| 997 } else if (_value is double) { |
| 998 return valueOf3(((leftValue as double)) % ((_value as double))); |
| 999 } |
| 1000 } |
| 1001 return error(node); |
| 1002 } |
| 1003 EvaluationResultImpl shiftLeftError(BinaryExpression node, ErrorResult leftOpe
rand) => leftOperand; |
| 1004 EvaluationResultImpl shiftLeftValid(BinaryExpression node, ValidResult leftOpe
rand15) { |
| 1005 Object leftValue = leftOperand15.value; |
| 1006 if (leftValue == null) { |
| 1007 return error(node.leftOperand); |
| 1008 } else if (_value == null) { |
| 1009 return error(node.rightOperand); |
| 1010 } else if (leftValue is int) { |
| 1011 if (_value is int) { |
| 1012 return valueOf(((leftValue as int)) << ((_value as int))); |
| 1013 } |
| 1014 return error(node.rightOperand); |
| 1015 } |
| 1016 if (_value is int) { |
| 1017 return error(node.leftOperand); |
| 1018 } |
| 1019 return union(error(node.leftOperand), error(node.rightOperand)); |
| 1020 } |
| 1021 EvaluationResultImpl shiftRightError(BinaryExpression node, ErrorResult leftOp
erand) => leftOperand; |
| 1022 EvaluationResultImpl shiftRightValid(BinaryExpression node, ValidResult leftOp
erand16) { |
| 1023 Object leftValue = leftOperand16.value; |
| 1024 if (leftValue == null) { |
| 1025 return error(node.leftOperand); |
| 1026 } else if (_value == null) { |
| 1027 return error(node.rightOperand); |
| 1028 } else if (leftValue is int) { |
| 1029 if (_value is int) { |
| 1030 return valueOf(((leftValue as int)) >> ((_value as int))); |
| 1031 } |
| 1032 return error(node.rightOperand); |
| 1033 } |
| 1034 if (_value is int) { |
| 1035 return error(node.leftOperand); |
| 1036 } |
| 1037 return union(error(node.leftOperand), error(node.rightOperand)); |
| 1038 } |
| 1039 EvaluationResultImpl timesError(BinaryExpression node, ErrorResult leftOperand
) => leftOperand; |
| 1040 EvaluationResultImpl timesValid(BinaryExpression node, ValidResult leftOperand
17) { |
| 1041 Object leftValue = leftOperand17.value; |
| 1042 if (leftValue == null) { |
| 1043 return error(node.leftOperand); |
| 1044 } else if (_value == null) { |
| 1045 return error(node.rightOperand); |
| 1046 } else if (leftValue is int) { |
| 1047 if (_value is int) { |
| 1048 return valueOf(((leftValue as int)) * (_value as int)); |
| 1049 } else if (_value is double) { |
| 1050 return valueOf3(((leftValue as int)).toDouble() * ((_value as double))); |
| 1051 } |
| 1052 } else if (leftValue is double) { |
| 1053 if (_value is int) { |
| 1054 return valueOf3(((leftValue as double)) * ((_value as int)).toDouble()); |
| 1055 } else if (_value is double) { |
| 1056 return valueOf3(((leftValue as double)) * ((_value as double))); |
| 1057 } |
| 1058 } |
| 1059 return error(node); |
| 1060 } |
| 1061 /** |
| 1062 * Return the result of applying boolean conversion to the given value. |
| 1063 * @param node the node against which errors should be reported |
| 1064 * @param value the value to be converted to a boolean |
| 1065 * @return the result of applying boolean conversion to the given value |
| 1066 */ |
| 1067 EvaluationResultImpl booleanConversion(ASTNode node, Object value) { |
| 1068 if (value == null) { |
| 1069 return error(node); |
| 1070 } else if (value is bool && ((value as bool))) { |
| 1071 return RESULT_TRUE; |
| 1072 } |
| 1073 return RESULT_FALSE; |
| 1074 } |
| 1075 ErrorResult error(ASTNode node) => error2(node, CompileTimeErrorCode.INVALID_C
ONSTANT); |
| 1076 /** |
| 1077 * Return a result object representing an error associated with the given node
. |
| 1078 * @param node the AST node associated with the error |
| 1079 * @param code the error code indicating the nature of the error |
| 1080 * @return a result object representing an error associated with the given nod
e |
| 1081 */ |
| 1082 ErrorResult error2(ASTNode node, ErrorCode code) => new ErrorResult.con1(node,
code); |
| 1083 double toDouble(int value) => value.toDouble(); |
| 1084 /** |
| 1085 * Return an error result that is the union of the two given error results. |
| 1086 * @param firstError the first error to be combined |
| 1087 * @param secondError the second error to be combined |
| 1088 * @return an error result that is the union of the two given error results |
| 1089 */ |
| 1090 ErrorResult union(ErrorResult firstError, ErrorResult secondError) => new Erro
rResult.con2(firstError, secondError); |
| 1091 /** |
| 1092 * Return a result object representing the given value. |
| 1093 * @param value the value to be represented as a result object |
| 1094 * @return a result object representing the given value |
| 1095 */ |
| 1096 ValidResult valueOf(int value) => new ValidResult(value); |
| 1097 /** |
| 1098 * Return a result object representing the given value. |
| 1099 * @param value the value to be represented as a result object |
| 1100 * @return a result object representing the given value |
| 1101 */ |
| 1102 ValidResult valueOf2(bool value) => value ? RESULT_TRUE : RESULT_FALSE; |
| 1103 /** |
| 1104 * Return a result object representing the given value. |
| 1105 * @param value the value to be represented as a result object |
| 1106 * @return a result object representing the given value |
| 1107 */ |
| 1108 ValidResult valueOf3(double value) => new ValidResult(value); |
| 1109 /** |
| 1110 * Return a result object representing the given value. |
| 1111 * @param value the value to be represented as a result object |
| 1112 * @return a result object representing the given value |
| 1113 */ |
| 1114 ValidResult valueOf4(String value) => new ValidResult(value); |
| 1115 } |
OLD | NEW |