Skip to content

Commit

Permalink
minor (#663): simplified generation and added built in runtime types …
Browse files Browse the repository at this point in the history
…in the compiler
  • Loading branch information
lorenzholzbauer committed Jul 17, 2024
1 parent 83a8cb6 commit 9694c40
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -35,6 +36,7 @@ export const BuiltInTypes = {
str: new BuiltInTypeStr(),
Func: new BuiltInTypeFunc(),
Array: new BuiltInTypeArray(any),
obj: new BuiltInTypeObject(),
} satisfies Record<KipperBuiltInTypeLiteral, BuiltInType>;

/**
Expand Down
21 changes: 21 additions & 0 deletions kipper/core/src/compiler/semantics/types/built-in/object.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
20 changes: 20 additions & 0 deletions kipper/target-js/src/built-in-types.ts
Original file line number Diff line number Diff line change
@@ -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<BuiltInRuntimeType> = [
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)'),
];
17 changes: 8 additions & 9 deletions kipper/target-js/src/code-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TranslatedCodeLine>) {
return lines.slice(1, lines.length - 1);
Expand Down Expand Up @@ -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(";"),
],
];
};
Expand Down
16 changes: 15 additions & 1 deletion kipper/target-js/src/target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
}

/**
Expand Down
6 changes: 3 additions & 3 deletions kipper/target-ts/src/code-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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)}, `;
}
}

Expand All @@ -92,7 +92,7 @@ export class TypeScriptTargetCodeGenerator extends JavaScriptTargetCodeGenerator
" = ",
"new ",
"Type(",
interfaceName,
'"' + interfaceName + '"',
",",
"[{",
propertiesWithTypes,
Expand Down

0 comments on commit 9694c40

Please sign in to comment.