From a153b27fac0607e2c87fb18da0d1325d24e6ae1e Mon Sep 17 00:00:00 2001 From: Johannes Meier Date: Thu, 19 Dec 2024 11:22:20 +0100 Subject: [PATCH] improvements according to the review --- CHANGELOG.md | 4 ++-- README.md | 9 +++++++-- examples/lox/src/language/lox-type-checking.ts | 8 +++++--- packages/typir/src/graph/type-node.ts | 3 ++- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a5cba9a..7f42758 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,11 +20,11 @@ It serves as first version to experiment with Typir and to gather feedback to gu - Caching - [Predefined types](/packages/typir/src/kinds/) to reuse: - Primitives - - Functions + - Functions (with overloading) - Classes (nominally typed) - Top, bottom - (some more are under development) - - Operators (which are mapped to Functions) + - Operators (which are mapped to Functions, with overloading) - Application examples: - LOX (without lambdas) - OX diff --git a/README.md b/README.md index 003dfbc..7c7b070 100644 --- a/README.md +++ b/README.md @@ -21,8 +21,13 @@ As a stand-alone library, Typir provides a TypeScript-API for language engineers Typir provides these core features: -- Predefined types: primitives, functions, classes, top, bottom (more are planned) -- Solutions for: circular type definitions, caching +- Predefined types: + - primitives + - functions (with overloading) + - classes + - top, bottom + - (more are planned) +- Solutions for: circular type definitions, caching, operators - Meaningful and customizable error messages - The provided default implementations are customizable by dependency injection diff --git a/examples/lox/src/language/lox-type-checking.ts b/examples/lox/src/language/lox-type-checking.ts index c6a7aaa..cabdc62 100644 --- a/examples/lox/src/language/lox-type-checking.ts +++ b/examples/lox/src/language/lox-type-checking.ts @@ -86,9 +86,11 @@ export class LoxTypeCreator extends AbstractLangiumTypeCreator { // show a warning to the user, if something like "3 == false" is compared, since different types already indicate, that the IF condition will be evaluated to false validationRule: (node, _operatorName, _operatorType, typir) => typir.validation.Constraints.ensureNodeIsEquals(node.left, node.right, (actual, expected) => { message: `This comparison will always return '${node.operator === '==' ? 'false' : 'true'}' as '${node.left.$cstNode?.text}' and '${node.right.$cstNode?.text}' have the different types '${actual.name}' and '${expected.name}'.`, - domainElement: node, // mark the 'operator' property! (note that "node.right" and "node.left" are the input for Typir) - domainProperty: 'operator', + domainElement: node, // inside the BinaryExpression ... + domainProperty: 'operator', // ... mark the '==' or '!=' token, i.e. the 'operator' property severity: 'warning' }), + // (The use of "node.right" and "node.left" without casting is possible, since the type checks of the given 'inferenceRule' are reused for the 'validationRule'. + // This approach saves the duplication of checks for inference and validation, but makes the validation rules depending on the inference rule.) }); } // = for SuperType = SubType (Note that this implementation of LOX realized assignments as operators!) @@ -131,7 +133,7 @@ export class LoxTypeCreator extends AbstractLangiumTypeCreator { if (domainElement.type) { return domainElement.type; // the user declared this variable with a type } else if (domainElement.value) { - return domainElement.value; // the user didn't declared a type for this variable => do type inference of the assigned value instead! + return domainElement.value; // the user didn't declare a type for this variable => do type inference of the assigned value instead! } else { return InferenceRuleNotApplicable; // this case is impossible, there is a validation in the Langium LOX validator for this case } diff --git a/packages/typir/src/graph/type-node.ts b/packages/typir/src/graph/type-node.ts index b69e14a..1e0e13f 100644 --- a/packages/typir/src/graph/type-node.ts +++ b/packages/typir/src/graph/type-node.ts @@ -64,9 +64,10 @@ export abstract class Type { protected readonly edgesOutgoing: Map = new Map(); /** - * An element from the domain might be associated with this type, e.g. the declaration node in the AST. + * The current type might be associated with an element from the domain, e.g. the corresponding declaration node in the AST. * This domain element is _not_ used for managing the lifecycles of this type, * since it should be usable for any domain-specific purpose. + * Therefore, the use and update of this feature is under the responsibility of the user of Typir. */ readonly associatedDomainElement: unknown | undefined;