From 621ab868ee72a7c1ad4cf2b55a4fc201c770a363 Mon Sep 17 00:00:00 2001 From: Lars Reimann Date: Sat, 6 Apr 2024 19:37:51 +0200 Subject: [PATCH] fix: Python generation for type casts (#1000) ### Summary of Changes Previously, the Python generator threw an error, since type casts were not handled. Now, they are handled properly. --- .mega-linter.yml | 2 +- .../src/language/generation/safe-ds-python-generator.ts | 3 +++ .../tests/generation/expressions/typeCasts/gen_input.py | 4 ++++ .../tests/generation/expressions/typeCasts/gen_input.py.map | 1 + .../generation/python/expressions/type casts/input.sdstest | 5 +++++ 5 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 packages/safe-ds-lang/tests/resources/generation/python/expressions/type casts/generated/tests/generation/expressions/typeCasts/gen_input.py create mode 100644 packages/safe-ds-lang/tests/resources/generation/python/expressions/type casts/generated/tests/generation/expressions/typeCasts/gen_input.py.map create mode 100644 packages/safe-ds-lang/tests/resources/generation/python/expressions/type casts/input.sdstest diff --git a/.mega-linter.yml b/.mega-linter.yml index 4595cba51..b0b5ddb30 100644 --- a/.mega-linter.yml +++ b/.mega-linter.yml @@ -21,4 +21,4 @@ TSX_ESLINT_FILE_EXTENSIONS: # Commands PRE_COMMANDS: - - command: npm i @lars-reimann/eslint-config-svelte eslint-plugin-vitest @lars-reimann/prettier-config-svelte + - command: npm i @lars-reimann/eslint-config-svelte eslint-plugin-vitest@0.4.0 @lars-reimann/prettier-config-svelte diff --git a/packages/safe-ds-lang/src/language/generation/safe-ds-python-generator.ts b/packages/safe-ds-lang/src/language/generation/safe-ds-python-generator.ts index fb4508c7e..e775ed899 100644 --- a/packages/safe-ds-lang/src/language/generation/safe-ds-python-generator.ts +++ b/packages/safe-ds-lang/src/language/generation/safe-ds-python-generator.ts @@ -47,6 +47,7 @@ import { isSdsTemplateStringInner, isSdsTemplateStringPart, isSdsTemplateStringStart, + isSdsTypeCast, isSdsWildcard, isSdsYield, SdsArgument, @@ -907,6 +908,8 @@ export class SafeDsPythonGenerator { const referenceImport = this.createImportDataForReference(expression); frame.addImport(referenceImport); return traceToNode(expression)(referenceImport?.alias ?? this.getPythonNameOrDefault(declaration)); + } else if (isSdsTypeCast(expression)) { + return traceToNode(expression)(this.generateExpression(expression.expression, frame)); } /* c8 ignore next 2 */ throw new Error(`Unknown expression type: ${expression.$type}`); diff --git a/packages/safe-ds-lang/tests/resources/generation/python/expressions/type casts/generated/tests/generation/expressions/typeCasts/gen_input.py b/packages/safe-ds-lang/tests/resources/generation/python/expressions/type casts/generated/tests/generation/expressions/typeCasts/gen_input.py new file mode 100644 index 000000000..e3ecd097a --- /dev/null +++ b/packages/safe-ds-lang/tests/resources/generation/python/expressions/type casts/generated/tests/generation/expressions/typeCasts/gen_input.py @@ -0,0 +1,4 @@ +# Segments --------------------------------------------------------------------- + +def mySegment(p): + a = p diff --git a/packages/safe-ds-lang/tests/resources/generation/python/expressions/type casts/generated/tests/generation/expressions/typeCasts/gen_input.py.map b/packages/safe-ds-lang/tests/resources/generation/python/expressions/type casts/generated/tests/generation/expressions/typeCasts/gen_input.py.map new file mode 100644 index 000000000..360c197c0 --- /dev/null +++ b/packages/safe-ds-lang/tests/resources/generation/python/expressions/type casts/generated/tests/generation/expressions/typeCasts/gen_input.py.map @@ -0,0 +1 @@ +{"version":3,"sources":["input.sdstest"],"names":["mysegment","p"],"mappings":"AAAA;;AAEA,IAAQA,SAAS,CAACC,CAAC;IACf,IAAQA,CAAC","file":"gen_input.py"} \ No newline at end of file diff --git a/packages/safe-ds-lang/tests/resources/generation/python/expressions/type casts/input.sdstest b/packages/safe-ds-lang/tests/resources/generation/python/expressions/type casts/input.sdstest new file mode 100644 index 000000000..a264cede4 --- /dev/null +++ b/packages/safe-ds-lang/tests/resources/generation/python/expressions/type casts/input.sdstest @@ -0,0 +1,5 @@ +package tests.generation.expressions.typeCasts + +segment mySegment(p: Int) { + val a = p as Int; +}