Skip to content

Commit

Permalink
example for LangDev'24
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesMeierSE committed Oct 22, 2024
1 parent d0ca163 commit f1ae077
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 3 deletions.
77 changes: 77 additions & 0 deletions examples/ox/examples/LangDev2024.ox
Original file line number Diff line number Diff line change
@@ -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;
6 changes: 3 additions & 3 deletions examples/ox/src/language/ox-type-checking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit f1ae077

Please sign in to comment.