Skip to content

Commit

Permalink
Add a first test
Browse files Browse the repository at this point in the history
  • Loading branch information
Lotes committed Feb 4, 2025
1 parent 1962c41 commit fe5f3d8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
22 changes: 17 additions & 5 deletions examples/expression/src/type-system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* This program and the accompanying materials are made available under the
* terms of the MIT License, which is available in the project root.
******************************************************************************/
import { createTypirServices, InferenceRuleNotApplicable, InferOperatorWithMultipleOperands, InferOperatorWithSingleOperand } from 'typir';
import { createTypirServices, InferenceRuleNotApplicable, InferOperatorWithMultipleOperands, InferOperatorWithSingleOperand, NO_PARAMETER_NAME } from 'typir';
import { BinaryExpression, isAstNode, isBinaryExpression, isNumeric, isPrintout, isUnaryExpression, isVariableDeclaration, isVariableUsage, UnaryExpression } from './ast.js';

export function initializeTypir() {
Expand All @@ -15,10 +15,10 @@ export function initializeTypir() {
]
});
const typeString = typir.factory.Primitives.create({
primitiveName: 'void', inferenceRules:
primitiveName: 'string', inferenceRules:
(node: unknown) => isAstNode(node) && node.type === 'string'
});
//const typeVoid = typir.factory.Primitives.create({ primitiveName: 'void' });
const typeVoid = typir.factory.Primitives.create({ primitiveName: 'void' });

const binaryInferenceRule: InferOperatorWithMultipleOperands<BinaryExpression> = {
filter: isBinaryExpression,
Expand All @@ -40,15 +40,27 @@ export function initializeTypir() {
typir.factory.Operators.createUnary({ name: '+', signature: { operand: typeNumber, return: typeNumber }, inferenceRule: unaryInferenceRule });
typir.factory.Operators.createUnary({ name: '-', signature: { operand: typeNumber, return: typeNumber }, inferenceRule: unaryInferenceRule });

typir.factory.Functions.create({
functionName: 'print',
inputParameters: [{
name: 'input',
type: typeString,
}],
outputParameter: { name: NO_PARAMETER_NAME, type: typeVoid },
inferenceRuleForCalls: {
filter: isPrintout,
matching: () => true,
inputArguments: (node) => [node.value],
}
});

typir.Conversion.markAsConvertible(typeNumber, typeString, 'IMPLICIT_EXPLICIT');

typir.Inference.addInferenceRule((languageNode) => {
if (isVariableDeclaration(languageNode)) {
return languageNode.value;
} else if (isVariableUsage(languageNode)) {
return languageNode.ref;
} else if (isPrintout(languageNode)) {
return typeString;
}
return InferenceRuleNotApplicable;
});
Expand Down
2 changes: 1 addition & 1 deletion examples/expression/test/validator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const typir = initializeTypir();

describe('Validator', () => {
test('quak', () => {
expectValidationMessages('PRINT 1+2;');
expectValidationMessages('PRINT 1;');
});
});

Expand Down

0 comments on commit fe5f3d8

Please sign in to comment.