|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
4 | 4 | *
|
5 | 5 | * The Universal Permissive License (UPL), Version 1.0
|
@@ -225,21 +225,27 @@ public Collection<? extends Snippet> createExpressions(Context context) {
|
225 | 225 | List<Snippet> snippets = new ArrayList<>();
|
226 | 226 |
|
227 | 227 | // @formatter:off
|
228 |
| - addExpressionSnippet(context, snippets, "+", "lambda x, y: x + y", union(STRING, NUMBER, array(ANY)), AddVerifier.INSTANCE, union(STRING, BOOLEAN, NUMBER, array(ANY)), union(STRING, BOOLEAN, NUMBER, array(ANY))); |
229 |
| - addExpressionSnippet(context, snippets, "*", "lambda x, y: x * y", union(STRING, NUMBER, array(ANY)), MulVerifier.INSTANCE, union(STRING, BOOLEAN, NUMBER, array(ANY)), union(STRING, BOOLEAN, NUMBER, array(ANY))); |
| 228 | + addExpressionSnippet(context, snippets, "+", "lambda x, y: x + y", NUMBER, new NonPrimitiveNumberParameterThrows(AddVerifier.INSTANCE), union(BOOLEAN, NUMBER), union(BOOLEAN, NUMBER)); |
| 229 | + addExpressionSnippet(context, snippets, "+", "lambda x, y: x + y", union(STRING, array(ANY)), AddVerifier.INSTANCE, union(BOOLEAN, NUMBER), union(STRING, array(ANY))); |
| 230 | + addExpressionSnippet(context, snippets, "+", "lambda x, y: x + y", union(STRING, array(ANY)), AddVerifier.INSTANCE, union(STRING, array(ANY)), union(BOOLEAN, NUMBER)); |
| 231 | + addExpressionSnippet(context, snippets, "+", "lambda x, y: x + y", union(STRING, array(ANY)), AddVerifier.INSTANCE, union(STRING, array(ANY)), union(STRING, array(ANY))); |
| 232 | + addExpressionSnippet(context, snippets, "*", "lambda x, y: x * y", NUMBER, new NonPrimitiveNumberParameterThrows(MulVerifier.INSTANCE), union(BOOLEAN, NUMBER), union(BOOLEAN, NUMBER)); |
| 233 | + addExpressionSnippet(context, snippets, "*", "lambda x, y: x * y", union(STRING, array(ANY)), MulVerifier.INSTANCE, union(BOOLEAN, NUMBER), union(STRING, array(ANY))); |
| 234 | + addExpressionSnippet(context, snippets, "*", "lambda x, y: x * y", union(STRING, array(ANY)), MulVerifier.INSTANCE, union(STRING, array(ANY)), union(BOOLEAN, NUMBER)); |
| 235 | + addExpressionSnippet(context, snippets, "*", "lambda x, y: x * y", union(STRING, array(ANY)), MulVerifier.INSTANCE, union(STRING, array(ANY)), union(STRING, array(ANY))); |
230 | 236 |
|
231 |
| - addExpressionSnippet(context, snippets, "-", "lambda x, y: x - y", NUMBER, union(BOOLEAN, NUMBER), union(BOOLEAN, NUMBER)); |
| 237 | + addExpressionSnippet(context, snippets, "-", "lambda x, y: x - y", NUMBER, NonPrimitiveNumberParameterThrows.INSTANCE, union(BOOLEAN, NUMBER), union(BOOLEAN, NUMBER)); |
232 | 238 |
|
233 |
| - addExpressionSnippet(context, snippets, "/", "lambda x, y: x / y", NUMBER, PDivByZeroVerifier.INSTANCE, union(BOOLEAN, NUMBER), union(BOOLEAN, NUMBER)); |
| 239 | + addExpressionSnippet(context, snippets, "/", "lambda x, y: x / y", NUMBER, new NonPrimitiveNumberParameterThrows(PDivByZeroVerifier.INSTANCE), union(BOOLEAN, NUMBER), union(BOOLEAN, NUMBER)); |
234 | 240 |
|
235 | 241 | addExpressionSnippet(context, snippets, "list-from-foreign", "lambda x: list(x)", array(ANY), union(STRING, iterable(ANY), iterator(ANY), array(ANY), hash(ANY, ANY)));
|
236 | 242 |
|
237 | 243 | addExpressionSnippet(context, snippets, "==", "lambda x, y: x == y", BOOLEAN, ANY, ANY);
|
238 | 244 | addExpressionSnippet(context, snippets, "!=", "lambda x, y: x != y", BOOLEAN, ANY, ANY);
|
239 |
| - addExpressionSnippet(context, snippets, ">", "lambda x, y: x > y", BOOLEAN, union(BOOLEAN, NUMBER), union(BOOLEAN, NUMBER)); |
240 |
| - addExpressionSnippet(context, snippets, ">=", "lambda x, y: x >= y", BOOLEAN, union(BOOLEAN, NUMBER), union(BOOLEAN, NUMBER)); |
241 |
| - addExpressionSnippet(context, snippets, "<", "lambda x, y: x < y", BOOLEAN, union(BOOLEAN, NUMBER), union(BOOLEAN, NUMBER)); |
242 |
| - addExpressionSnippet(context, snippets, "<=", "lambda x, y: x <= y", BOOLEAN, union(BOOLEAN, NUMBER), union(BOOLEAN, NUMBER)); |
| 245 | + addExpressionSnippet(context, snippets, ">", "lambda x, y: x > y", BOOLEAN, NonPrimitiveNumberParameterThrows.INSTANCE, union(BOOLEAN, NUMBER), union(BOOLEAN, NUMBER)); |
| 246 | + addExpressionSnippet(context, snippets, ">=", "lambda x, y: x >= y", BOOLEAN, NonPrimitiveNumberParameterThrows.INSTANCE, union(BOOLEAN, NUMBER), union(BOOLEAN, NUMBER)); |
| 247 | + addExpressionSnippet(context, snippets, "<", "lambda x, y: x < y", BOOLEAN, NonPrimitiveNumberParameterThrows.INSTANCE, union(BOOLEAN, NUMBER), union(BOOLEAN, NUMBER)); |
| 248 | + addExpressionSnippet(context, snippets, "<=", "lambda x, y: x <= y", BOOLEAN, NonPrimitiveNumberParameterThrows.INSTANCE, union(BOOLEAN, NUMBER), union(BOOLEAN, NUMBER)); |
243 | 249 |
|
244 | 250 | addExpressionSnippet(context, snippets, "isinstance", "lambda x, y: isinstance(x, y)", BOOLEAN, ANY, META_OBJECT);
|
245 | 251 | addExpressionSnippet(context, snippets, "issubclass", "lambda x, y: issubclass(x, y)", BOOLEAN, META_OBJECT, META_OBJECT);
|
@@ -524,4 +530,35 @@ public void accept(SnippetRun snippetRun) throws PolyglotException {
|
524 | 530 |
|
525 | 531 | private static final PDivByZeroVerifier INSTANCE = new PDivByZeroVerifier();
|
526 | 532 | }
|
| 533 | + |
| 534 | + private static class NonPrimitiveNumberParameterThrows extends PResultVerifier { |
| 535 | + |
| 536 | + private final ResultVerifier next; |
| 537 | + |
| 538 | + public NonPrimitiveNumberParameterThrows(PResultVerifier next) { |
| 539 | + this.next = next != null ? next : ResultVerifier.getDefaultResultVerifier(); |
| 540 | + } |
| 541 | + |
| 542 | + public void accept(SnippetRun snippetRun) throws PolyglotException { |
| 543 | + boolean nonPrimitiveNumberParameter = false; |
| 544 | + boolean numberOrBooleanParameters = true; |
| 545 | + for (Value actualParameter : snippetRun.getParameters()) { |
| 546 | + if (!actualParameter.isBoolean() && !actualParameter.isNumber()) { |
| 547 | + numberOrBooleanParameters = false; |
| 548 | + } |
| 549 | + if (actualParameter.isNumber() && !actualParameter.fitsInLong() && !actualParameter.fitsInDouble()) { |
| 550 | + nonPrimitiveNumberParameter = true; |
| 551 | + } |
| 552 | + } |
| 553 | + if (numberOrBooleanParameters && nonPrimitiveNumberParameter) { |
| 554 | + if (snippetRun.getException() == null) { |
| 555 | + throw new AssertionError("TypeError expected but no error has been thrown."); |
| 556 | + } // else exception expected => ignore |
| 557 | + } else { |
| 558 | + next.accept(snippetRun); // no exception expected |
| 559 | + } |
| 560 | + } |
| 561 | + |
| 562 | + private static final NonPrimitiveNumberParameterThrows INSTANCE = new NonPrimitiveNumberParameterThrows(null); |
| 563 | + } |
527 | 564 | }
|
0 commit comments