Skip to content

Commit 13656ef

Browse files
committed
Add interpreter and notebook support
1 parent 6808f4e commit 13656ef

File tree

8 files changed

+729
-43
lines changed

8 files changed

+729
-43
lines changed

examples/average.loxnb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"cells":[{"kind":1,"language":"markdown","value":"## This is a vscode notebook for the Lox language\r\n\r\nIn the following we will show how to calculate the average of two number:"},{"kind":2,"language":"lox","value":"var min = 14;\r\nvar max = 22;\r\n\r\n// Can reassign an existing variable\r\nmin = 5;\r\n\r\n// Printing\r\nprint average(min, max);\r\nprint average(1, 3);\r\nprint average(2, 42);\r\n\r\nfun average(min: number, max: number): number {\r\n return (min + max) / 2;\r\n}"}]}

package-lock.json

Lines changed: 226 additions & 40 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@
2020
"language": "lox",
2121
"scopeName": "source.lox",
2222
"path": "./syntaxes/lox.tmLanguage.json"
23+
}],
24+
"notebooks": [{
25+
"type": "lox-notebook",
26+
"displayName": "Lox Notebook",
27+
"selector": [
28+
{
29+
"filenamePattern": "*.loxnb"
30+
}
31+
]
2332
}]
2433
},
2534
"activationEvents": [
@@ -47,18 +56,20 @@
4756
"chevrotain": "^9.1.0",
4857
"colors": "^1.4.0",
4958
"commander": "^8.0.0",
50-
"langium": "0.5.0",
59+
"langium": "1.0.1",
60+
"uuid": "^9.0.0",
5161
"vscode-languageclient": "8.0.2",
5262
"vscode-languageserver": "8.0.2",
5363
"vscode-uri": "^3.0.2"
5464
},
5565
"devDependencies": {
5666
"@types/node": "^14.17.3",
67+
"@types/uuid": "^9.0.0",
5768
"@types/vscode": "^1.56.0",
5869
"@typescript-eslint/eslint-plugin": "^4.14.1",
5970
"@typescript-eslint/parser": "^4.14.1",
6071
"eslint": "^7.19.0",
61-
"langium-cli": "0.5.0",
72+
"langium-cli": "1.0.0",
6273
"typescript": "^4.6.2"
6374
}
6475
}

src/extension.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,21 @@ import * as path from 'path';
33
import {
44
LanguageClient, LanguageClientOptions, ServerOptions, TransportKind
55
} from 'vscode-languageclient/node';
6+
import { LoxNotebookSerializer } from './notebook/lox-notebook-serializer';
7+
import { LoxNotebookKernel } from './notebook/lox-notebook-kernel';
68

79
let client: LanguageClient;
810

911
// This function is called when the extension is activated.
1012
export function activate(context: vscode.ExtensionContext): void {
1113
client = startLanguageClient(context);
14+
15+
context.subscriptions.push(
16+
vscode.workspace.registerNotebookSerializer(
17+
'lox-notebook', new LoxNotebookSerializer(), { transientOutputs: true }
18+
),
19+
new LoxNotebookKernel()
20+
);
1221
}
1322

1423
// This function is called when the extension is deactivated.
@@ -38,7 +47,15 @@ function startLanguageClient(context: vscode.ExtensionContext): LanguageClient {
3847

3948
// Options to control the language client
4049
const clientOptions: LanguageClientOptions = {
41-
documentSelector: [{ scheme: 'file', language: 'lox' }],
50+
documentSelector: [
51+
{
52+
scheme: "file",
53+
language: "lox"
54+
}, {
55+
scheme: 'vscode-notebook-cell', // only notebook cells
56+
language: 'lox'
57+
}
58+
],
4259
synchronize: {
4360
// Notify the server about file changes to files contained in the workspace
4461
fileEvents: fileSystemWatcher

src/interpreter/cli.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Command } from 'commander';
2+
import fs from 'fs';
3+
import { runInterpreter } from './runner';
4+
5+
const program = new Command();
6+
7+
program
8+
.version(require('../../package.json').version);
9+
10+
program
11+
.command('run')
12+
.argument('<file>')
13+
.action(runCommand);
14+
15+
program.parse(process.argv);
16+
17+
async function runCommand(file: string): Promise<void> {
18+
const now = Date.now();
19+
const content = await fs.promises.readFile(file, 'utf-8');
20+
await runInterpreter(content, {
21+
log: value => console.log(`${value}`)
22+
});
23+
console.log(`Lox program finished running in ${Date.now() - now}ms`);
24+
}

0 commit comments

Comments
 (0)