Skip to content

Commit d683c4e

Browse files
committed
Corrected faulty typescript annotations in Readme.
1 parent fd90f42 commit d683c4e

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

README.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -64,80 +64,80 @@ This repository contains the following stand-alone applications, which demonstra
6464
Both the LOX and OX examples have been created with Langium. Here is a very small example for using Typir with a tiny expression language, which is independent from any language workbench like Langium. We show how to use the Typir API for type checking of Tiny Typir. You can also find the example in the repository, implemented in form of an executable [test case](/packages/typir/test/api-example.test.ts).
6565
Our Tiny Typir language has only a few concepts (all are realized as `AstElement`s), namely numbers (`NumberLiteral`), strings (`StringLiteral`), binary expressions (`BinaryExpression`), variables (`Variable`), and assignments (`AssignmentStatement`). They are implemented in a very simple way, see for example our `BinaryExpression`:
6666

67-
```
67+
```typescript
6868
class BinaryExpression extends AstElement {
6969
constructor(
7070
public left: AstElement,
7171
public operator: string,
7272
public right: AstElement,
7373
) { super(); }
7474
}
75-
```typescript
75+
```
7676

7777
Feel free to check out the others in the [test code](/packages/typir/test/api-example.test.ts), but a little spoiler: no surprises there.
7878

7979
Let's head into setting up the Typir type system and creating the primitive types for our NumberLiteral and StringLiteral, which is a one line of code job each, as we use the Typir's predefined Primitives factory service:
8080

81-
```
81+
```typescript
8282
const typir = createTypirServices();
8383

8484
const numberType = typir.factory.Primitives.create({ primitiveName: 'number', inferenceRules: node => node instanceof NumberLiteral });
8585

8686
const stringType = typir.factory.Primitives.create({ primitiveName: 'string', inferenceRules: node => node instanceof StringLiteral });
87-
```typescript
87+
```
8888

8989
Note that the inference rules are included in this. For the operators this is a bit longer, as we have to take care of the left and right operand and the operator of the binary expression, so we extract it and will resuse it later for both the `+` and `-` operators:
9090

91-
```
91+
```typescript
9292
const inferenceRule: InferOperatorWithMultipleOperands<BinaryExpression> = {
9393
filter: node => node instanceof BinaryExpression,
9494
matching: (node, operatorName) => node.operator === operatorName,
9595
operands: node => [node.left, node.right],
9696
};
97-
```typescript
97+
```
9898

9999
We wish to have two operators, the `+` operator, which should be overloaded to accept either two numbers to add or two strings to concatenate. This can be expressed with an array of signatures with different types for the operands and the return type of the operator. Furthermore, there is going to be a `-` operator with only one signature, since there is only subtraction of numbers. Both operators refer to the inferenceRule we defined above. `numberType` and `stringType` are the primitive types we defined above.
100100

101-
```
101+
```typescript
102102
typir.factory.Operators.createBinary({ name: '+', signatures: [
103103
{ left: numberType, right: numberType, return: numberType },
104104
{ left: stringType, right: stringType, return: stringType },
105105
], inferenceRule });
106106
typir.factory.Operators.createBinary({ name: '-', signatures: [{ left: numberType, right: numberType, return: numberType }], inferenceRule });
107-
```typescript
107+
```
108108

109109
As we'd like to be able to convert numbers to strings implicitly, we add the following line. Note that this will for example make it possible to concatenate numbers and strings with the `+` operator, though it has no signature for a number and a string parameter in the operator definition above.
110110

111-
```
112-
typir.Conversion.markAsConvertible(numberType, stringType,'IMPLICIT_EXPLICIT');
113111
```typescript
112+
typir.Conversion.markAsConvertible(numberType, stringType,'IMPLICIT_EXPLICIT');
113+
```
114114

115115
Furthermore we can specify how Typir should infer the variable type. We decided that the type of the variable should be the type of its initial value. Typir internally considers the inference rules for primitives and operators as well, when recursively inferring the given AstElement.
116116

117-
```
117+
```typescript
118118
typir.Inference.addInferenceRule(node => {
119119
if (node instanceof Variable) {
120120
return node.initialValue; // the type of the variable is the type of its initial value
121121
}
122122
return InferenceRuleNotApplicable;
123123
});
124-
```typescript
124+
```
125125

126126
Finally, we add a type related validation rule for our small example: In case we have an AssignmentStatement, we check whether the type to be assigned is an assignable match for the variable type. We can do that with a custom message. An error with this message will show up for example when we try to assign the string literal "hello" to a number variable. It will not show up in case we assign the number literal 123 to a string variable, as we have defined the implicit conversion above.
127127

128-
```
128+
```typescript
129129
typir.validation.Collector.addValidationRule(node => {
130130
if (node instanceof AssignmentStatement) {
131131
return typir.validation.Constraints.ensureNodeIsAssignable(node.right, node.left, (actual, expected) => <ValidationMessageDetails>{ message:
132132
`The type '${actual.name}' is not assignable to the type '${expected.name}'.` });
133133
}
134134
return [];
135135
});
136-
```typescript
136+
```
137137

138138
Wrapping this up, these are the test examples for the language usage with the expected type checking outcome:
139139

140-
```
140+
```typescript
141141
// 2 + 3 => OK
142142
const example1 = new BinaryExpression(new NumberLiteral(2), '+', new NumberLiteral(3));
143143
expect(typir.validation.Collector.validate(example1)).toHaveLength(0);
@@ -163,7 +163,7 @@ const varNumber = new Variable('v2', new NumberLiteral(456));
163163
const assignStringToNumber = new AssignmentStatement(varNumber, new StringLiteral('123'));
164164
const errors2 = typir.validation.Collector.validate(assignStringToNumber);
165165
expect(errors2[0].message).toBe("The type 'string' is not assignable to the type 'number'.");
166-
```typescript
166+
```
167167

168168
## Resources
169169

0 commit comments

Comments
 (0)