Skip to content

Commit 9a824e3

Browse files
author
pluralia
committed
Add Ox grammar (subset of Lox) for experiments with type inferrence
1 parent 8f8f383 commit 9a824e3

24 files changed

+4105
-106
lines changed

.gitignore

+5-104
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,5 @@
1-
# Logs
2-
logs
3-
*.log
4-
npm-debug.log*
5-
yarn-debug.log*
6-
yarn-error.log*
7-
lerna-debug.log*
8-
9-
# Diagnostic reports (https://nodejs.org/api/report.html)
10-
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
11-
12-
# Runtime data
13-
pids
14-
*.pid
15-
*.seed
16-
*.pid.lock
17-
18-
# Directory for instrumented libs generated by jscoverage/JSCover
19-
lib-cov
20-
21-
# Coverage directory used by tools like istanbul
22-
coverage
23-
*.lcov
24-
25-
# nyc test coverage
26-
.nyc_output
27-
28-
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
29-
.grunt
30-
31-
# Bower dependency directory (https://bower.io/)
32-
bower_components
33-
34-
# node-waf configuration
35-
.lock-wscript
36-
37-
# Compiled binary addons (https://nodejs.org/api/addons.html)
38-
build/Release
39-
40-
# Dependency directories
41-
node_modules/
42-
jspm_packages/
43-
44-
# TypeScript v1 declaration files
45-
typings/
46-
47-
# TypeScript cache
48-
*.tsbuildinfo
49-
50-
# Optional npm cache directory
51-
.npm
52-
53-
# Optional eslint cache
54-
.eslintcache
55-
56-
# Microbundle cache
57-
.rpt2_cache/
58-
.rts2_cache_cjs/
59-
.rts2_cache_es/
60-
.rts2_cache_umd/
61-
62-
# Optional REPL history
63-
.node_repl_history
64-
65-
# Output of 'npm pack'
66-
*.tgz
67-
68-
# Yarn Integrity file
69-
.yarn-integrity
70-
71-
# dotenv environment variables file
72-
.env
73-
.env.test
74-
75-
# parcel-bundler cache (https://parceljs.org/)
76-
.cache
77-
78-
# Next.js build output
79-
.next
80-
81-
# Nuxt.js build / generate output
82-
.nuxt
83-
dist
84-
85-
# Gatsby files
86-
.cache/
87-
# Comment in the public line in if your project uses Gatsby and *not* Next.js
88-
# https://nextjs.org/blog/next-9-1#public-directory-support
89-
# public
90-
91-
# vuepress build output
92-
.vuepress/dist
93-
94-
# Serverless directories
95-
.serverless/
96-
97-
# FuseBox cache
98-
.fusebox/
99-
100-
# DynamoDB Local files
101-
.dynamodb/
102-
103-
# TernJS port file
104-
.tern-port
1+
/node_modules
2+
/out
3+
/**/generated
4+
.DS_Store
5+
*/dbg

.vscode/extensions.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
// See https://go.microsoft.com/fwlink/?LinkId=827846 to learn about workspace recommendations.
3+
// Extension identifier format: ${publisher}.${name}. Example: vscode.csharp
4+
5+
// List of extensions which should be recommended for users of this workspace.
6+
"recommendations": [
7+
"langium.langium-vscode"
8+
]
9+
}

.vscode/launch.json

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// A launch configuration that launches the extension inside a new window
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
{
6+
"version": "0.2.0",
7+
"configurations": [
8+
{
9+
"name": "Run Extension",
10+
"type": "extensionHost",
11+
"request": "launch",
12+
"args": [
13+
"--extensionDevelopmentPath=${workspaceFolder}",
14+
"${workspaceFolder}/examples"
15+
]
16+
},
17+
{
18+
"name": "Attach to Language Server",
19+
"type": "node",
20+
"port": 6009,
21+
"request": "attach",
22+
"skipFiles": [
23+
"<node_internals>/**"
24+
],
25+
"sourceMaps": true,
26+
"outFiles": [
27+
"${workspaceFolder}/out/**/*.js",
28+
"${workspaceFolder}/node_modules/langium/**/*.js"
29+
]
30+
}
31+
]
32+
}

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2023 Langium
3+
Copyright (c) 2024 Typir
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
# typesystem
1+
# Typir
2+
3+
A structure for generating validity checks associated with types.

bin/cli.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env node
2+
3+
import main from '../out/cli/main.js';
4+
main();

esbuild.mjs

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//@ts-check
2+
import * as esbuild from 'esbuild';
3+
4+
const watch = process.argv.includes('--watch');
5+
const minify = process.argv.includes('--minify');
6+
7+
const success = watch ? 'Watch build succeeded' : 'Build succeeded';
8+
9+
function getTime() {
10+
const date = new Date();
11+
return `[${`${padZeroes(date.getHours())}:${padZeroes(date.getMinutes())}:${padZeroes(date.getSeconds())}`}] `;
12+
}
13+
14+
function padZeroes(i) {
15+
return i.toString().padStart(2, '0');
16+
}
17+
18+
const plugins = [{
19+
name: 'watch-plugin',
20+
setup(build) {
21+
build.onEnd(result => {
22+
if (result.errors.length === 0) {
23+
console.log(getTime() + success);
24+
}
25+
});
26+
},
27+
}];
28+
29+
const ctx = await esbuild.context({
30+
// Entry points for the vscode extension and the language server
31+
entryPoints: ['src/extension/main.ts', 'src/language/main.ts'],
32+
outdir: 'out',
33+
bundle: true,
34+
target: "ES2017",
35+
// VSCode's extension host is still using cjs, so we need to transform the code
36+
format: 'cjs',
37+
// To prevent confusing node, we explicitly use the `.cjs` extension
38+
outExtension: {
39+
'.js': '.cjs'
40+
},
41+
loader: { '.ts': 'ts' },
42+
external: ['vscode'],
43+
platform: 'node',
44+
sourcemap: !minify,
45+
minify,
46+
plugins
47+
});
48+
49+
if (watch) {
50+
await ctx.watch();
51+
} else {
52+
await ctx.rebuild();
53+
ctx.dispose();
54+
}

examples/basic.ox

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Data types
2+
// Booleans
3+
true; // Not false.
4+
false; // Not *not* false.
5+
6+
// Numbers
7+
1234; // An integer.
8+
12.34; // A decimal number.
9+
10+
// Strings
11+
// "I am a string";
12+
// ""; // The empty string.
13+
// "123"; // This is a string, not a number.
14+
15+
// Expressions
16+
// Arithmetics
17+
var add: number = 23 + 41;
18+
var subtract: number = 13 - 4;
19+
var multiply: number = 13 * 4;
20+
var divide: number = 62 / 2;
21+
var fractional: number = 61 / 3;
22+
23+
var negateMe: number = -add;
24+
25+
// Comparison and equality
26+
var less: boolean = add < subtract;
27+
var more: boolean = multiply > divide;
28+
29+
var equality: boolean = add == subtract;
30+
var inequality: boolean = multiply != divide;
31+
32+
// Unary logical operator
33+
var isTrue: boolean = !false;
34+
var isFalse: boolean = !true;
35+
36+
// Binary logical operator
37+
var andTrue: boolean = isTrue and !isFalse;
38+
var orFalse: boolean = !isTrue or isFalse;
39+
40+
// Precedence and grouping
41+
var min: number = 14;
42+
var max: number = 22;
43+
var average: number = (min + max) / 2;
44+
45+
// Variables
46+
// Can reassign an existing variable
47+
min = 5;
48+
49+
// Printing
50+
print average;
51+
52+
var x: boolean = true;
53+
print x; // prints 1
54+
55+
// Blocks
56+
{
57+
// print "This is a new block";
58+
var x: number = 15;
59+
print x; // prints 15
60+
}
61+
62+
print x; // prints 1
63+
64+
// Control flow
65+
// If branching
66+
if (average > 5) {
67+
print 23;
68+
if (max < 30 and min > 3) {
69+
min = min + 15;
70+
}
71+
} else {
72+
print -12;
73+
}
74+
75+
// While loops
76+
var a: number = 1;
77+
while (a < 10) {
78+
print a;
79+
a = a + 1;
80+
}
81+
82+
// For loops
83+
for (var i: number = 1; i < 10; i = i + 1) {
84+
print i;
85+
}
86+
87+
fun inc(a: number): number {
88+
return a + 1;
89+
}
90+
91+
// Functions
92+
fun printSum(a: number, b: number): void {
93+
print inc(a) + inc(inc(b));
94+
}
95+
96+
fun returnSum(a: number, b: number): number {
97+
return a + b;
98+
}
99+
100+
printSum(3, 2);
101+
102+
print returnSum(32.3, 123.5);

examples/factorial.ox

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
fun fact(n: number): number {
2+
var res: number = 1;
3+
for (var i: number = 2; i <= n; i = i + 1) {
4+
print res;
5+
res = res * i;
6+
}
7+
return res;
8+
}
9+
10+
print fact(5);

examples/vars.ox

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var x: number = 12;
2+
var y: number = 32.32;
3+
var z: boolean = true;
4+
var w: number = 0;
5+
var a: boolean = false;
6+
7+
fun returnSum(a: number, b: number): number {
8+
print a;
9+
return a + b;
10+
}
11+
12+
var xx: number = returnSum(2, 3);
13+
print xx;

langium-config.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"projectName": "Ox",
3+
"languages": [{
4+
"id": "ox",
5+
"grammar": "src/language/ox.langium",
6+
"fileExtensions": [".ox"],
7+
"textMate": {
8+
"out": "syntaxes/ox.tmLanguage.json"
9+
}
10+
}],
11+
"out": "src/language/generated"
12+
}

0 commit comments

Comments
 (0)