From 9694c40d49f7f5bf0e36bbcc35f1c142f34ba484 Mon Sep 17 00:00:00 2001 From: Lorenz Holzbauer <73286868+lorenzholzbauer@users.noreply.github.com> Date: Wed, 17 Jul 2024 08:38:44 +0000 Subject: [PATCH] minor (#663): simplified generation and added built in runtime types in the compiler --- .../semantics/symbol-table/universum-scope.ts | 4 +++- .../semantics/types/built-in/object.ts | 21 +++++++++++++++++++ kipper/target-js/src/built-in-types.ts | 20 ++++++++++++++++++ kipper/target-js/src/code-generator.ts | 17 +++++++-------- kipper/target-js/src/target.ts | 16 +++++++++++++- kipper/target-ts/src/code-generator.ts | 6 +++--- 6 files changed, 70 insertions(+), 14 deletions(-) create mode 100644 kipper/core/src/compiler/semantics/types/built-in/object.ts create mode 100644 kipper/target-js/src/built-in-types.ts diff --git a/kipper/core/src/compiler/semantics/symbol-table/universum-scope.ts b/kipper/core/src/compiler/semantics/symbol-table/universum-scope.ts index 5136f975f..2bdb96f25 100644 --- a/kipper/core/src/compiler/semantics/symbol-table/universum-scope.ts +++ b/kipper/core/src/compiler/semantics/symbol-table/universum-scope.ts @@ -15,8 +15,9 @@ import { BuiltInTypeType, BuiltInTypeUndefined, BuiltInTypeVoid, -} from "../types/built-in"; +} from "../types"; import { BuiltInTypeAny } from "../types/built-in/any"; +import { BuiltInTypeObject } from "../types/built-in/object"; const any = new BuiltInTypeAny(); @@ -35,6 +36,7 @@ export const BuiltInTypes = { str: new BuiltInTypeStr(), Func: new BuiltInTypeFunc(), Array: new BuiltInTypeArray(any), + obj: new BuiltInTypeObject(), } satisfies Record; /** diff --git a/kipper/core/src/compiler/semantics/types/built-in/object.ts b/kipper/core/src/compiler/semantics/types/built-in/object.ts new file mode 100644 index 000000000..c031a7daf --- /dev/null +++ b/kipper/core/src/compiler/semantics/types/built-in/object.ts @@ -0,0 +1,21 @@ +import { BuiltInType, type CompilableType } from "../index"; + +/** + * Represents the built-in type `void`. + * @since 0.12.0 + */ +export class BuiltInTypeObject extends BuiltInType implements CompilableType { + constructor() { + super("obj"); + } + + /** + * Returns whether the type is a compilable type. + * + * This is always true for the built-in type `void`. + * @since 0.12.0 + */ + public get isCompilable(): true { + return true; + } +} diff --git a/kipper/target-js/src/built-in-types.ts b/kipper/target-js/src/built-in-types.ts new file mode 100644 index 000000000..32d48dba8 --- /dev/null +++ b/kipper/target-js/src/built-in-types.ts @@ -0,0 +1,20 @@ +export class BuiltInRuntimeType { + name: string; + value: string; + + constructor(name: string, value: string) { + this.name = name; + this.value = value; + } +} + +export const builtInTypes: Array = [ + new BuiltInRuntimeType("num", 'new Type("num", undefined, undefined)'), + new BuiltInRuntimeType("str", 'new Type("str", undefined, undefined)'), + new BuiltInRuntimeType("bool", 'new Type("bool", undefined, undefined)'), + new BuiltInRuntimeType("void", 'new Type("void", undefined, undefined)'), + new BuiltInRuntimeType("undefined", 'new Type("undefined", undefined, undefined)'), + new BuiltInRuntimeType("null", 'new Type("null", undefined, undefined)'), + new BuiltInRuntimeType("obj", 'new Type("obj", undefined, undefined)'), + new BuiltInRuntimeType("array", 'new Type("array", undefined, undefined)'), +]; diff --git a/kipper/target-js/src/code-generator.ts b/kipper/target-js/src/code-generator.ts index 145ee013b..fdf17a603 100644 --- a/kipper/target-js/src/code-generator.ts +++ b/kipper/target-js/src/code-generator.ts @@ -70,6 +70,7 @@ import { } from "@kipper/core"; import { createJSFunctionSignature, getJSFunctionSignature, indentLines, removeBraces } from "./tools"; import { TargetJS, version } from "./index"; +import { builtInTypes } from "./built-in-types"; function removeBrackets(lines: Array) { return lines.slice(1, lines.length - 1); @@ -136,15 +137,13 @@ export class JavaScriptTargetCodeGenerator extends KipperTargetCodeGenerator { // The following objects are built-in types and functions that are used internally by Kipper and should not be // modified by the user. [ - "__kipper.builtIn = __kipper.builtIn || {};" + - "__kipper.builtIn.int = __kipper.builtIn.int || new Type('int', undefined, undefined);" + - "__kipper.builtIn.str = __kipper.builtIn.str || new Type('str', undefined, undefined);" + - "__kipper.builtIn.bool = __kipper.builtIn.bool || new Type('bool', undefined, undefined);" + - "__kipper.builtIn.void = __kipper.builtIn.void || new Type('void', undefined, undefined);" + - "__kipper.builtIn.undefined = __kipper.builtIn.undefined || new Type('undefined', undefined, undefined);" + - "__kipper.builtIn.null = __kipper.builtIn.null || new Type('null', undefined, undefined);" + - "__kipper.builtIn.obj = __kipper.builtIn.obj || new Type('obj', undefined, undefined);" + - "__kipper.builtIn.array = __kipper.builtIn.array || new Type('array', undefined, undefined);", + `__kipper.builtIn = ${TargetJS.internalObjectIdentifier}.builtIn || {};` + + builtInTypes + .map( + (b) => + `${TargetJS.internalObjectIdentifier}.builtIn.${b.name} = ${TargetJS.internalObjectIdentifier}.builtIn.${b.name} || ${b.value}`, + ) + .join(";"), ], ]; }; diff --git a/kipper/target-js/src/target.ts b/kipper/target-js/src/target.ts index 451b02e68..7b492e255 100644 --- a/kipper/target-js/src/target.ts +++ b/kipper/target-js/src/target.ts @@ -4,11 +4,14 @@ * @copyright 2021-2022 Luna Klatzer * @since 0.10.0 */ -import type { BuiltInFunction, BuiltInType } from "@kipper/core"; +import type { BuiltInFunction, ProcessedType } from "@kipper/core"; +import { BuiltInType } from "@kipper/core"; import { BuiltInVariable, KipperCompileTarget } from "@kipper/core"; import { JavaScriptTargetSemanticAnalyser } from "./semantic-analyser"; import { JavaScriptTargetCodeGenerator } from "./code-generator"; import { JavaScriptTargetBuiltInGenerator } from "./built-in-generator"; +import type { BuiltInRuntimeType } from "./built-in-types"; +import { builtInTypes } from "./built-in-types"; /** * The JavaScript translation target for the Kipper language. @@ -112,6 +115,17 @@ export class KipperJavaScriptTarget extends KipperCompileTarget { ? signature.identifier : this.getBuiltInIdentifier(signature.identifier); } + + /** + * Gets the builtin type for a Kipper Compiler Type + * @since 0.12.0 + */ + public static getRuntimeType(type: ProcessedType): BuiltInRuntimeType | string { + if (type instanceof BuiltInType) { + return builtInTypes.find((t) => t.name === type.identifier) ?? type.identifier; + } + return type.identifier; + } } /** diff --git a/kipper/target-ts/src/code-generator.ts b/kipper/target-ts/src/code-generator.ts index e98d21386..850a062c7 100644 --- a/kipper/target-ts/src/code-generator.ts +++ b/kipper/target-ts/src/code-generator.ts @@ -16,7 +16,7 @@ import type { } from "@kipper/core"; import { InterfacePropertyDeclaration } from "@kipper/core"; import { createTSFunctionSignature, getTSFunctionSignature } from "./tools"; -import { indentLines, JavaScriptTargetCodeGenerator } from "@kipper/target-js"; +import { indentLines, JavaScriptTargetCodeGenerator, TargetJS } from "@kipper/target-js"; import { TargetTS } from "./target"; import type { InterfaceMemberDeclarationTypeSemantics } from "@kipper/core/lib/compiler/ast/nodes/declarations/type-declaration/interface-declaration/interface-member-declaration/interface-member-declaration-type-semantics"; @@ -79,7 +79,7 @@ export class TypeScriptTargetCodeGenerator extends JavaScriptTargetCodeGenerator if (member instanceof InterfacePropertyDeclaration) { let property = member.getSemanticData(); let type = member.getTypeSemanticData(); - propertiesWithTypes += `"${property.identifier}": ${TargetTS.getTypeScriptType(type.type)}, `; + propertiesWithTypes += `"${property.identifier}": ${TargetJS.getRuntimeType(type.type)}, `; } } @@ -92,7 +92,7 @@ export class TypeScriptTargetCodeGenerator extends JavaScriptTargetCodeGenerator " = ", "new ", "Type(", - interfaceName, + '"' + interfaceName + '"', ",", "[{", propertiesWithTypes,