diff --git a/examples/ox/examples/LangDev2024.ox b/examples/ox/examples/LangDev2024.ox new file mode 100644 index 0000000..8baba1a --- /dev/null +++ b/examples/ox/examples/LangDev2024.ox @@ -0,0 +1,77 @@ +// variables: numbers, booleans (void) +var n: number = 12; +var b: boolean = true; + +// Arithmetics +var add: number = 23 + 41; +var subtract: number = 13 - 4; +var multiply: number = 13 * 4; +var divide: number = 62 / 2; +var fractional: number = 61 / 3; + +var negateMe: number = -add; + +// Comparison and equality +var less: boolean = add < subtract; +var more: boolean = multiply > divide; + +var equality: boolean = add == subtract; +var inequality: boolean = multiply != divide; + +// Unary logical operator +var isTrue: boolean = !false; +var isFalse: boolean = !true; + +// Binary logical operator +var andTrue: boolean = isTrue and !isFalse; +var orFalse: boolean = !isTrue or isFalse; + +// Precedence and grouping +var min: number = 14; +var max: number = 22; +var average: number = (min + max) / 2; + +// Variables +// Can reassign an existing variable +min = 5; + +// If branching +var kk: number = average * 5; +if (average > 5) { + print 23; + if (max < 30 and min > 3) { + min = min + 15; + } +} else { + print -12; +} + +// While loops +var a: number = 1; +while (a < 10) { + print a; + a = a + 1; +} + +// Functions +fun inc(a: number): number { + return a + 1; +} +fun inc(a: boolean): boolean { + return !a; +} + +fun printSum(a: number, b: number): void { + print inc(true) + inc(inc(b)); +} + +fun returnSum(a: number, b: number): number { + return a + b; +} + +printSum(3, 2); + +print returnSum(32.3, 123.5); + +print 12; +print true; diff --git a/examples/ox/src/language/ox-type-checking.ts b/examples/ox/src/language/ox-type-checking.ts index 8573e01..5a121ba 100644 --- a/examples/ox/src/language/ox-type-checking.ts +++ b/examples/ox/src/language/ox-type-checking.ts @@ -9,7 +9,7 @@ import { LangiumSharedServices } from 'langium/lsp'; import { FUNCTION_MISSING_NAME, FunctionKind, InferOperatorWithMultipleOperands, InferOperatorWithSingleOperand, InferenceRuleNotApplicable, OperatorManager, ParameterDetails, PrimitiveKind, TypirServices, UniqueFunctionValidation } from 'typir'; import { AbstractLangiumTypeCreator, LangiumServicesForTypirBinding, PartialTypirLangiumServices } from 'typir-langium'; import { ValidationMessageDetails } from '../../../../packages/typir/lib/features/validation.js'; -import { BinaryExpression, MemberCall, UnaryExpression, isAssignmentStatement, isBinaryExpression, isBooleanLiteral, isForStatement, isFunctionDeclaration, isIfStatement, isMemberCall, isNumberLiteral, isParameter, isPrintStatement, isReturnStatement, isTypeReference, isUnaryExpression, isVariableDeclaration, isWhileStatement } from './generated/ast.js'; +import { BinaryExpression, MemberCall, UnaryExpression, isAssignmentStatement, isBinaryExpression, isBooleanLiteral, isForStatement, isFunctionDeclaration, isIfStatement, isMemberCall, isNumberLiteral, isParameter, isReturnStatement, isTypeReference, isUnaryExpression, isVariableDeclaration, isWhileStatement } from './generated/ast.js'; export class OxTypeCreator extends AbstractLangiumTypeCreator { protected readonly typir: TypirServices; @@ -104,8 +104,8 @@ export class OxTypeCreator extends AbstractLangiumTypeCreator { // use parameters inside expressions return ref.type; } else if (isFunctionDeclaration(ref)) { - // there is already an inference rule for function calls (see above for FunctionDeclaration)! - return 'N/A'; // as an alternative: use 'InferenceRuleNotApplicable' instead, what should we recommend? + // there is already an inference rule for function calls (see below for FunctionDeclaration)! + return InferenceRuleNotApplicable; } else if (ref === undefined) { return InferenceRuleNotApplicable; } else {