Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reworked the folder and file structure #41

Merged
merged 5 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions examples/lox/src/language/type-system/lox-type-checking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@

import { AstNode, AstUtils, Module, assertUnreachable } from 'langium';
import { LangiumSharedServices } from 'langium/lsp';
import { ClassKind, CreateFieldDetails, CreateFunctionTypeDetails, CreateParameterDetails, FunctionKind, InferOperatorWithMultipleOperands, InferOperatorWithSingleOperand, InferenceRuleNotApplicable, NO_PARAMETER_NAME, OperatorManager, PrimitiveKind, TopKind, TypirServices, UniqueClassValidation, UniqueFunctionValidation, UniqueMethodValidation, createNoSuperClassCyclesValidation } from 'typir';
import { ClassKind, CreateFieldDetails, CreateFunctionTypeDetails, CreateParameterDetails, FunctionKind, InferOperatorWithMultipleOperands, InferOperatorWithSingleOperand, InferenceRuleNotApplicable, NO_PARAMETER_NAME, OperatorManager, PrimitiveKind, TopKind, TypirServices, UniqueClassValidation, UniqueFunctionValidation, UniqueMethodValidation, createNoSuperClassCyclesValidation, ValidationMessageDetails } from 'typir';
import { AbstractLangiumTypeCreator, LangiumServicesForTypirBinding, PartialTypirLangiumServices } from 'typir-langium';
import { ValidationMessageDetails } from '../../../../../packages/typir/lib/features/validation.js';
import { BinaryExpression, FunctionDeclaration, MemberCall, MethodMember, TypeReference, UnaryExpression, isBinaryExpression, isBooleanLiteral, isClass, isClassMember, isFieldMember, isForStatement, isFunctionDeclaration, isIfStatement, isMemberCall, isMethodMember, isNilLiteral, isNumberLiteral, isParameter, isPrintStatement, isReturnStatement, isStringLiteral, isTypeReference, isUnaryExpression, isVariableDeclaration, isWhileStatement } from '../generated/ast.js';

/* eslint-disable @typescript-eslint/no-unused-vars */
Expand Down
174 changes: 174 additions & 0 deletions examples/lox/test/lox-type-checking-classes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/******************************************************************************
* Copyright 2024 TypeFox GmbH
* This program and the accompanying materials are made available under the
* terms of the MIT License, which is available in the project root.
******************************************************************************/

import { describe, test } from 'vitest';
import { loxServices, validateLox } from './lox-type-checking-utils.js';
import { expectTypirTypes } from '../../../packages/typir/lib/utils/test-utils.js';
import { isClassType } from '../../../packages/typir/lib/kinds/class/class-type.js';

describe('Test type checking for classes', () => {

test('Class inheritance for assignments: correct', async () => {
await validateLox(`
class MyClass1 { name: string age: number }
class MyClass2 < MyClass1 {}
var v1: MyClass1 = MyClass2();
`, 0);
expectTypirTypes(loxServices, isClassType, 'MyClass1', 'MyClass2');
});

test('Class inheritance for assignments: wrong', async () => {
await validateLox(`
class MyClass1 { name: string age: number }
class MyClass2 < MyClass1 {}
var v1: MyClass2 = MyClass1();
`, 1);
expectTypirTypes(loxServices, isClassType, 'MyClass1', 'MyClass2');
});

test('Class fields: correct values', async () => {
await validateLox(`
class MyClass1 { name: string age: number }
var v1: MyClass1 = MyClass1();
v1.name = "Bob";
v1.age = 42;
`, 0);
expectTypirTypes(loxServices, isClassType, 'MyClass1');
});

test('Class fields: wrong values', async () => {
await validateLox(`
class MyClass1 { name: string age: number }
var v1: MyClass1 = MyClass1();
v1.name = 42;
v1.age = "Bob";
`, 2);
expectTypirTypes(loxServices, isClassType, 'MyClass1');
});

test('Classes must be unique by name 2', async () => {
await validateLox(`
class MyClass1 { }
class MyClass1 { }
`, [
'Declared classes need to be unique (MyClass1).',
'Declared classes need to be unique (MyClass1).',
]);
expectTypirTypes(loxServices, isClassType, 'MyClass1');
});

test('Classes must be unique by name 3', async () => {
await validateLox(`
class MyClass2 { }
class MyClass2 { }
class MyClass2 { }
`, [
'Declared classes need to be unique (MyClass2).',
'Declared classes need to be unique (MyClass2).',
'Declared classes need to be unique (MyClass2).',
]);
expectTypirTypes(loxServices, isClassType, 'MyClass2');
});

test('Class methods: OK', async () => {
await validateLox(`
class MyClass1 {
method1(input: number): number {
return 123;
}
}
var v1: MyClass1 = MyClass1();
var v2: number = v1.method1(456);
`, []);
expectTypirTypes(loxServices, isClassType, 'MyClass1');
});

test('Class methods: wrong return value', async () => {
await validateLox(`
class MyClass1 {
method1(input: number): number {
return true;
}
}
var v1: MyClass1 = MyClass1();
var v2: number = v1.method1(456);
`, 1);
expectTypirTypes(loxServices, isClassType, 'MyClass1');
});

test('Class methods: method return type does not fit to variable type', async () => {
await validateLox(`
class MyClass1 {
method1(input: number): number {
return 123;
}
}
var v1: MyClass1 = MyClass1();
var v2: boolean = v1.method1(456);
`, 1);
expectTypirTypes(loxServices, isClassType, 'MyClass1');
});

test('Class methods: value for input parameter does not fit to the type of the input parameter', async () => {
await validateLox(`
class MyClass1 {
method1(input: number): number {
return 123;
}
}
var v1: MyClass1 = MyClass1();
var v2: number = v1.method1(true);
`, 1);
expectTypirTypes(loxServices, isClassType, 'MyClass1');
});

test('Class methods: methods are not distinguishable', async () => {
await validateLox(`
class MyClass1 {
method1(input: number): number {
return 123;
}
method1(another: number): boolean {
return true;
}
}
`, [ // both methods need to be marked:
'Declared methods need to be unique (class-MyClass1.method1(number)).',
'Declared methods need to be unique (class-MyClass1.method1(number)).',
]);
expectTypirTypes(loxServices, isClassType, 'MyClass1');
});

});

describe('Class literals', () => {

test('Class literals 1', async () => {
await validateLox(`
class MyClass { name: string age: number }
var v1 = MyClass(); // constructor call
`, []);
expectTypirTypes(loxServices, isClassType, 'MyClass');
});

test('Class literals 2', async () => {
await validateLox(`
class MyClass { name: string age: number }
var v1: MyClass = MyClass(); // constructor call
`, []);
expectTypirTypes(loxServices, isClassType, 'MyClass');
});

test('Class literals 3', async () => {
await validateLox(`
class MyClass1 {}
class MyClass2 {}
var v1: boolean = MyClass1() == MyClass2(); // comparing objects with each other
`, [], 1);
expectTypirTypes(loxServices, isClassType, 'MyClass1', 'MyClass2');
});

});
Loading