Skip to content

Commit

Permalink
improvements according to the review
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesMeierSE committed Dec 19, 2024
1 parent 33c454b commit a153b27
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 5 additions & 3 deletions examples/lox/src/language/lox-type-checking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => <ValidationMessageDetails>{
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!)
Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion packages/typir/src/graph/type-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@ export abstract class Type {
protected readonly edgesOutgoing: Map<string, TypeEdge[]> = 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;

Expand Down

0 comments on commit a153b27

Please sign in to comment.