desmost 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/compiler/compile.d.ts +32 -0
- package/dist/compiler/compile.d.ts.map +1 -0
- package/dist/compiler/compile.js +101 -0
- package/dist/compiler/compile.js.map +1 -0
- package/dist/compiler/evaluate.d.ts +18 -0
- package/dist/compiler/evaluate.d.ts.map +1 -0
- package/dist/compiler/evaluate.js +83 -0
- package/dist/compiler/evaluate.js.map +1 -0
- package/dist/compiler/index.d.ts +3 -0
- package/dist/compiler/index.d.ts.map +1 -0
- package/dist/compiler/index.js +2 -0
- package/dist/compiler/index.js.map +1 -0
- package/dist/compiler/options.d.ts +54 -0
- package/dist/compiler/options.d.ts.map +1 -0
- package/dist/compiler/options.js +15 -0
- package/dist/compiler/options.js.map +1 -0
- package/dist/desmos.d.ts +12 -0
- package/dist/desmos.d.ts.map +1 -0
- package/dist/desmos.js +13 -0
- package/dist/desmos.js.map +1 -0
- package/dist/errors.d.ts +45 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +38 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/magic/expr/index.d.ts +3 -0
- package/dist/magic/expr/index.d.ts.map +1 -0
- package/dist/magic/expr/index.js +8 -0
- package/dist/magic/expr/index.js.map +1 -0
- package/dist/magic/expr/latex.d.ts +10 -0
- package/dist/magic/expr/latex.d.ts.map +1 -0
- package/dist/magic/expr/latex.js +16 -0
- package/dist/magic/expr/latex.js.map +1 -0
- package/dist/magic/expr/text.d.ts +9 -0
- package/dist/magic/expr/text.d.ts.map +1 -0
- package/dist/magic/expr/text.js +13 -0
- package/dist/magic/expr/text.js.map +1 -0
- package/dist/magic/global/dark-mode.d.ts +7 -0
- package/dist/magic/global/dark-mode.d.ts.map +1 -0
- package/dist/magic/global/dark-mode.js +11 -0
- package/dist/magic/global/dark-mode.js.map +1 -0
- package/dist/magic/global/desmos.d.ts +9 -0
- package/dist/magic/global/desmos.d.ts.map +1 -0
- package/dist/magic/global/desmos.js +11 -0
- package/dist/magic/global/desmos.js.map +1 -0
- package/dist/magic/global/index.d.ts +3 -0
- package/dist/magic/global/index.d.ts.map +1 -0
- package/dist/magic/global/index.js +10 -0
- package/dist/magic/global/index.js.map +1 -0
- package/dist/magic/global/viewport.d.ts +16 -0
- package/dist/magic/global/viewport.d.ts.map +1 -0
- package/dist/magic/global/viewport.js +12 -0
- package/dist/magic/global/viewport.js.map +1 -0
- package/dist/magic/incantation.d.ts +74 -0
- package/dist/magic/incantation.d.ts.map +1 -0
- package/dist/magic/incantation.js +81 -0
- package/dist/magic/incantation.js.map +1 -0
- package/dist/magic/index.d.ts +6 -0
- package/dist/magic/index.d.ts.map +1 -0
- package/dist/magic/index.js +5 -0
- package/dist/magic/index.js.map +1 -0
- package/dist/magic/local/anim.d.ts +7 -0
- package/dist/magic/local/anim.d.ts.map +1 -0
- package/dist/magic/local/anim.js +11 -0
- package/dist/magic/local/anim.js.map +1 -0
- package/dist/magic/local/colour.d.ts +12 -0
- package/dist/magic/local/colour.d.ts.map +1 -0
- package/dist/magic/local/colour.js +27 -0
- package/dist/magic/local/colour.js.map +1 -0
- package/dist/magic/local/dashed.d.ts +7 -0
- package/dist/magic/local/dashed.d.ts.map +1 -0
- package/dist/magic/local/dashed.js +10 -0
- package/dist/magic/local/dashed.js.map +1 -0
- package/dist/magic/local/fill.d.ts +13 -0
- package/dist/magic/local/fill.d.ts.map +1 -0
- package/dist/magic/local/fill.js +12 -0
- package/dist/magic/local/fill.js.map +1 -0
- package/dist/magic/local/hide.d.ts +7 -0
- package/dist/magic/local/hide.d.ts.map +1 -0
- package/dist/magic/local/hide.js +10 -0
- package/dist/magic/local/hide.js.map +1 -0
- package/dist/magic/local/index.d.ts +3 -0
- package/dist/magic/local/index.d.ts.map +1 -0
- package/dist/magic/local/index.js +24 -0
- package/dist/magic/local/index.js.map +1 -0
- package/dist/magic/local/line.d.ts +15 -0
- package/dist/magic/local/line.d.ts.map +1 -0
- package/dist/magic/local/line.js +17 -0
- package/dist/magic/local/line.js.map +1 -0
- package/dist/magic/local/no-line.d.ts +7 -0
- package/dist/magic/local/no-line.d.ts.map +1 -0
- package/dist/magic/local/no-line.js +10 -0
- package/dist/magic/local/no-line.js.map +1 -0
- package/dist/magic/local/point.d.ts +15 -0
- package/dist/magic/local/point.d.ts.map +1 -0
- package/dist/magic/local/point.js +17 -0
- package/dist/magic/local/point.js.map +1 -0
- package/dist/magic/local/secret.d.ts +7 -0
- package/dist/magic/local/secret.d.ts.map +1 -0
- package/dist/magic/local/secret.js +10 -0
- package/dist/magic/local/secret.js.map +1 -0
- package/dist/magic/local/slider.d.ts +15 -0
- package/dist/magic/local/slider.d.ts.map +1 -0
- package/dist/magic/local/slider.js +13 -0
- package/dist/magic/local/slider.js.map +1 -0
- package/dist/parser/ast.d.ts +40 -0
- package/dist/parser/ast.d.ts.map +1 -0
- package/dist/parser/ast.js +13 -0
- package/dist/parser/ast.js.map +1 -0
- package/dist/parser/desmost-parser.d.ts +105 -0
- package/dist/parser/desmost-parser.d.ts.map +1 -0
- package/dist/parser/desmost-parser.js +379 -0
- package/dist/parser/desmost-parser.js.map +1 -0
- package/dist/parser/generic-parser.d.ts +62 -0
- package/dist/parser/generic-parser.d.ts.map +1 -0
- package/dist/parser/generic-parser.js +143 -0
- package/dist/parser/generic-parser.js.map +1 -0
- package/dist/parser/index.d.ts +8 -0
- package/dist/parser/index.d.ts.map +1 -0
- package/dist/parser/index.js +8 -0
- package/dist/parser/index.js.map +1 -0
- package/dist/src/compiler/compile.d.ts +32 -0
- package/dist/src/compiler/compile.d.ts.map +1 -0
- package/dist/src/compiler/compile.js +101 -0
- package/dist/src/compiler/compile.js.map +1 -0
- package/dist/src/compiler/evaluate.d.ts +18 -0
- package/dist/src/compiler/evaluate.d.ts.map +1 -0
- package/dist/src/compiler/evaluate.js +83 -0
- package/dist/src/compiler/evaluate.js.map +1 -0
- package/dist/src/compiler/index.d.ts +3 -0
- package/dist/src/compiler/index.d.ts.map +1 -0
- package/dist/src/compiler/index.js +2 -0
- package/dist/src/compiler/index.js.map +1 -0
- package/dist/src/compiler/options.d.ts +54 -0
- package/dist/src/compiler/options.d.ts.map +1 -0
- package/dist/src/compiler/options.js +15 -0
- package/dist/src/compiler/options.js.map +1 -0
- package/dist/src/desmos.d.ts +12 -0
- package/dist/src/desmos.d.ts.map +1 -0
- package/dist/src/desmos.js +13 -0
- package/dist/src/desmos.js.map +1 -0
- package/dist/src/errors.d.ts +45 -0
- package/dist/src/errors.d.ts.map +1 -0
- package/dist/src/errors.js +38 -0
- package/dist/src/errors.js.map +1 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +2 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/magic/expr/index.d.ts +3 -0
- package/dist/src/magic/expr/index.d.ts.map +1 -0
- package/dist/src/magic/expr/index.js +8 -0
- package/dist/src/magic/expr/index.js.map +1 -0
- package/dist/src/magic/expr/latex.d.ts +10 -0
- package/dist/src/magic/expr/latex.d.ts.map +1 -0
- package/dist/src/magic/expr/latex.js +16 -0
- package/dist/src/magic/expr/latex.js.map +1 -0
- package/dist/src/magic/expr/text.d.ts +9 -0
- package/dist/src/magic/expr/text.d.ts.map +1 -0
- package/dist/src/magic/expr/text.js +13 -0
- package/dist/src/magic/expr/text.js.map +1 -0
- package/dist/src/magic/global/dark-mode.d.ts +7 -0
- package/dist/src/magic/global/dark-mode.d.ts.map +1 -0
- package/dist/src/magic/global/dark-mode.js +11 -0
- package/dist/src/magic/global/dark-mode.js.map +1 -0
- package/dist/src/magic/global/desmos.d.ts +9 -0
- package/dist/src/magic/global/desmos.d.ts.map +1 -0
- package/dist/src/magic/global/desmos.js +11 -0
- package/dist/src/magic/global/desmos.js.map +1 -0
- package/dist/src/magic/global/index.d.ts +3 -0
- package/dist/src/magic/global/index.d.ts.map +1 -0
- package/dist/src/magic/global/index.js +10 -0
- package/dist/src/magic/global/index.js.map +1 -0
- package/dist/src/magic/global/viewport.d.ts +16 -0
- package/dist/src/magic/global/viewport.d.ts.map +1 -0
- package/dist/src/magic/global/viewport.js +12 -0
- package/dist/src/magic/global/viewport.js.map +1 -0
- package/dist/src/magic/incantation.d.ts +74 -0
- package/dist/src/magic/incantation.d.ts.map +1 -0
- package/dist/src/magic/incantation.js +81 -0
- package/dist/src/magic/incantation.js.map +1 -0
- package/dist/src/magic/index.d.ts +6 -0
- package/dist/src/magic/index.d.ts.map +1 -0
- package/dist/src/magic/index.js +5 -0
- package/dist/src/magic/index.js.map +1 -0
- package/dist/src/magic/local/anim.d.ts +7 -0
- package/dist/src/magic/local/anim.d.ts.map +1 -0
- package/dist/src/magic/local/anim.js +11 -0
- package/dist/src/magic/local/anim.js.map +1 -0
- package/dist/src/magic/local/colour.d.ts +12 -0
- package/dist/src/magic/local/colour.d.ts.map +1 -0
- package/dist/src/magic/local/colour.js +27 -0
- package/dist/src/magic/local/colour.js.map +1 -0
- package/dist/src/magic/local/dashed.d.ts +7 -0
- package/dist/src/magic/local/dashed.d.ts.map +1 -0
- package/dist/src/magic/local/dashed.js +10 -0
- package/dist/src/magic/local/dashed.js.map +1 -0
- package/dist/src/magic/local/fill.d.ts +13 -0
- package/dist/src/magic/local/fill.d.ts.map +1 -0
- package/dist/src/magic/local/fill.js +12 -0
- package/dist/src/magic/local/fill.js.map +1 -0
- package/dist/src/magic/local/hide.d.ts +7 -0
- package/dist/src/magic/local/hide.d.ts.map +1 -0
- package/dist/src/magic/local/hide.js +10 -0
- package/dist/src/magic/local/hide.js.map +1 -0
- package/dist/src/magic/local/index.d.ts +3 -0
- package/dist/src/magic/local/index.d.ts.map +1 -0
- package/dist/src/magic/local/index.js +24 -0
- package/dist/src/magic/local/index.js.map +1 -0
- package/dist/src/magic/local/line.d.ts +15 -0
- package/dist/src/magic/local/line.d.ts.map +1 -0
- package/dist/src/magic/local/line.js +17 -0
- package/dist/src/magic/local/line.js.map +1 -0
- package/dist/src/magic/local/no-line.d.ts +7 -0
- package/dist/src/magic/local/no-line.d.ts.map +1 -0
- package/dist/src/magic/local/no-line.js +10 -0
- package/dist/src/magic/local/no-line.js.map +1 -0
- package/dist/src/magic/local/point.d.ts +15 -0
- package/dist/src/magic/local/point.d.ts.map +1 -0
- package/dist/src/magic/local/point.js +17 -0
- package/dist/src/magic/local/point.js.map +1 -0
- package/dist/src/magic/local/secret.d.ts +7 -0
- package/dist/src/magic/local/secret.d.ts.map +1 -0
- package/dist/src/magic/local/secret.js +10 -0
- package/dist/src/magic/local/secret.js.map +1 -0
- package/dist/src/magic/local/slider.d.ts +15 -0
- package/dist/src/magic/local/slider.d.ts.map +1 -0
- package/dist/src/magic/local/slider.js +13 -0
- package/dist/src/magic/local/slider.js.map +1 -0
- package/dist/src/parser/ast.d.ts +40 -0
- package/dist/src/parser/ast.d.ts.map +1 -0
- package/dist/src/parser/ast.js +13 -0
- package/dist/src/parser/ast.js.map +1 -0
- package/dist/src/parser/desmost-parser.d.ts +105 -0
- package/dist/src/parser/desmost-parser.d.ts.map +1 -0
- package/dist/src/parser/desmost-parser.js +379 -0
- package/dist/src/parser/desmost-parser.js.map +1 -0
- package/dist/src/parser/generic-parser.d.ts +62 -0
- package/dist/src/parser/generic-parser.d.ts.map +1 -0
- package/dist/src/parser/generic-parser.js +143 -0
- package/dist/src/parser/generic-parser.js.map +1 -0
- package/dist/src/parser/index.d.ts +8 -0
- package/dist/src/parser/index.d.ts.map +1 -0
- package/dist/src/parser/index.js +8 -0
- package/dist/src/parser/index.js.map +1 -0
- package/package.json +58 -0
- package/src/compiler/compile.ts +127 -0
- package/src/compiler/evaluate.ts +121 -0
- package/src/compiler/index.ts +2 -0
- package/src/compiler/options.ts +77 -0
- package/src/desmos.ts +13 -0
- package/src/errors.ts +46 -0
- package/src/index.ts +1 -0
- package/src/magic/.template.ts +22 -0
- package/src/magic/expr/index.ts +11 -0
- package/src/magic/expr/latex.ts +24 -0
- package/src/magic/expr/text.ts +19 -0
- package/src/magic/global/dark-mode.ts +17 -0
- package/src/magic/global/desmos.ts +20 -0
- package/src/magic/global/index.ts +13 -0
- package/src/magic/global/viewport.ts +25 -0
- package/src/magic/incantation.ts +142 -0
- package/src/magic/index.ts +6 -0
- package/src/magic/local/anim.ts +17 -0
- package/src/magic/local/colour.ts +38 -0
- package/src/magic/local/dashed.ts +16 -0
- package/src/magic/local/fill.ts +24 -0
- package/src/magic/local/hide.ts +16 -0
- package/src/magic/local/index.ts +27 -0
- package/src/magic/local/line.ts +29 -0
- package/src/magic/local/no-line.ts +16 -0
- package/src/magic/local/point.ts +29 -0
- package/src/magic/local/secret.ts +16 -0
- package/src/magic/local/slider.ts +27 -0
- package/src/parser/ast.ts +60 -0
- package/src/parser/desmost-parser.ts +486 -0
- package/src/parser/generic-parser.ts +191 -0
- package/src/parser/index.ts +8 -0
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "desmost",
|
|
3
|
+
"author": "Sup#2.0",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
|
|
7
|
+
"description": "A tiny DSL for compiling LaTeX to Desmos",
|
|
8
|
+
"keywords": [
|
|
9
|
+
"desmos", "latex", "desmost"
|
|
10
|
+
],
|
|
11
|
+
|
|
12
|
+
"homepage": "https://github.com/Sup2point0/desmost",
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/Sup2point0/desmost.git"
|
|
16
|
+
},
|
|
17
|
+
"bugs": {
|
|
18
|
+
"url": "https://github.com/Sup2point0/desmost/issues"
|
|
19
|
+
},
|
|
20
|
+
|
|
21
|
+
"type": "module",
|
|
22
|
+
"files": [
|
|
23
|
+
"./src/",
|
|
24
|
+
"./dist/",
|
|
25
|
+
"package.json"
|
|
26
|
+
],
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"import": "./dist/index.js",
|
|
30
|
+
"types": "./dist/index.d.ts"
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "tsc -p ./tsconfig.build.json",
|
|
36
|
+
|
|
37
|
+
"test": "vitest run",
|
|
38
|
+
"testw": "vitest",
|
|
39
|
+
|
|
40
|
+
"check": "npx oxlint src"
|
|
41
|
+
},
|
|
42
|
+
|
|
43
|
+
"dependencies":
|
|
44
|
+
{
|
|
45
|
+
"json5": "^2.2"
|
|
46
|
+
},
|
|
47
|
+
|
|
48
|
+
"devDependencies":
|
|
49
|
+
{
|
|
50
|
+
"@eslint/js": "^10.0",
|
|
51
|
+
"@types/desmos": "1.11",
|
|
52
|
+
"@types/node": "^26.0",
|
|
53
|
+
"oxlint": "^1.78",
|
|
54
|
+
"oxlint-tsgolint": "^7.0",
|
|
55
|
+
"typescript": "^7.0",
|
|
56
|
+
"vitest": "^4.0"
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { type DesmostOptions, set_default_options } from "./options";
|
|
2
|
+
import { evaluate_global_incantation, evaluate_expr, evaluate_error } from "./evaluate";
|
|
3
|
+
|
|
4
|
+
import { DesmostParser, Ast } from "../parser";
|
|
5
|
+
import { UnrecoverableError } from "../errors";
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Compile Desmost into Desmos.
|
|
10
|
+
*
|
|
11
|
+
* This calls the Desmost compiler: parsing and evaluating `source`, then injecting the results into an existing `desmos` instance. Pass in `options` to customise compilation.
|
|
12
|
+
*
|
|
13
|
+
* ## Example
|
|
14
|
+
*
|
|
15
|
+
* ```ts
|
|
16
|
+
* let calc = Desmos.GraphingCalculator();
|
|
17
|
+
*
|
|
18
|
+
* compile(calc, `/text{ sup world! }`);
|
|
19
|
+
*
|
|
20
|
+
* compile(calc, `/slider{ error! } :: t = 0`, {
|
|
21
|
+
* errors: "crash",
|
|
22
|
+
* });
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export function compile(
|
|
26
|
+
/** The Desmos calculator instance to compile into. */
|
|
27
|
+
desmos: Desmos.Calculator,
|
|
28
|
+
|
|
29
|
+
/** The Desmost source code to compile. */
|
|
30
|
+
source: string,
|
|
31
|
+
|
|
32
|
+
/** Compilation options. */
|
|
33
|
+
options?: DesmostOptions,
|
|
34
|
+
): void | DesmostDebug
|
|
35
|
+
{
|
|
36
|
+
if (desmos == undefined) {
|
|
37
|
+
console.error(`Desmost: No \`Desmos.Calculator\` instance provided, aborting compilation!`);
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (source == undefined) {
|
|
42
|
+
console.error(`Desmost: No source code provided, aborting compilation!`);
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
let t_init = performance.now();
|
|
47
|
+
let ast = [];
|
|
48
|
+
|
|
49
|
+
let opts = set_default_options(options);
|
|
50
|
+
let parser = new DesmostParser(source, opts);
|
|
51
|
+
|
|
52
|
+
/* To aggregate errors at the start, we need a target to retroactively inject errors into */
|
|
53
|
+
if (opts.place_errors === "start") {
|
|
54
|
+
desmos.setExpression({ id: "deferred-start", latex: "" });
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
let errors = [];
|
|
58
|
+
|
|
59
|
+
try {
|
|
60
|
+
while (true) {
|
|
61
|
+
let r = parser.parse_next();
|
|
62
|
+
if (r === null) break;
|
|
63
|
+
|
|
64
|
+
if (opts.debug) {
|
|
65
|
+
ast.push(r);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
let defer: string | void;
|
|
69
|
+
|
|
70
|
+
switch (r.kind) {
|
|
71
|
+
case Ast.Kind.INCANTATION_INVOCATION:
|
|
72
|
+
defer = evaluate_global_incantation(r, desmos, opts);
|
|
73
|
+
break;
|
|
74
|
+
|
|
75
|
+
case Ast.Kind.EXPRESSION:
|
|
76
|
+
defer = evaluate_expr(r, desmos, opts);
|
|
77
|
+
break;
|
|
78
|
+
|
|
79
|
+
case Ast.Kind.INVALID_INCANTATION:
|
|
80
|
+
defer = evaluate_error(r.error, desmos, opts);
|
|
81
|
+
break;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (defer !== undefined) {
|
|
85
|
+
errors.push(defer);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
catch (e) {
|
|
90
|
+
if (e instanceof UnrecoverableError) {
|
|
91
|
+
errors = [e.message];
|
|
92
|
+
} else {
|
|
93
|
+
throw e;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (errors.length > 0) {
|
|
98
|
+
let expr: Desmos.ExpressionState = {
|
|
99
|
+
type: "text",
|
|
100
|
+
text: errors.join("\n\n"),
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
switch (opts.place_errors) {
|
|
104
|
+
case "start":
|
|
105
|
+
desmos.setExpression({ ...expr, id: "deferred-start" });
|
|
106
|
+
break;
|
|
107
|
+
case "end":
|
|
108
|
+
case "inline":
|
|
109
|
+
desmos.setExpression(expr);
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (opts.debug) {
|
|
115
|
+
return {
|
|
116
|
+
duration: performance.now() - t_init,
|
|
117
|
+
ast,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
interface DesmostDebug
|
|
124
|
+
{
|
|
125
|
+
duration: number;
|
|
126
|
+
ast: Ast[];
|
|
127
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import type { DesmostOptions } from "./options";
|
|
2
|
+
|
|
3
|
+
import { Ast } from "../parser";
|
|
4
|
+
import { UnrecoverableError, type Unrecoverable } from "../errors";
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Evaluate arguments (if any) to a global incantation `invocation`, then apply the incantation to `desmos`.
|
|
9
|
+
*/
|
|
10
|
+
export function evaluate_global_incantation(
|
|
11
|
+
invocation: Ast.IncantationInvocation,
|
|
12
|
+
desmos: Desmos.Calculator,
|
|
13
|
+
options: Required<DesmostOptions>,
|
|
14
|
+
): Unrecoverable<void | string>
|
|
15
|
+
{
|
|
16
|
+
let data = undefined;
|
|
17
|
+
|
|
18
|
+
if ("arg_raw" in invocation && invocation.arg_raw != undefined) {
|
|
19
|
+
try {
|
|
20
|
+
data = invocation.incantation.evaluate_arg(invocation.arg_raw);
|
|
21
|
+
}
|
|
22
|
+
catch (e) {
|
|
23
|
+
let msg = evaluate_error(e as Error, desmos, options);
|
|
24
|
+
if (msg != undefined) {
|
|
25
|
+
return msg;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
invocation.incantation.apply(desmos, data);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Evaluate arguments (if any) to local incantation invocations on `expr`, then add `expr` to `desmos`.
|
|
36
|
+
*/
|
|
37
|
+
export function evaluate_expr(
|
|
38
|
+
expr: Ast.Expression,
|
|
39
|
+
desmos: Desmos.Calculator,
|
|
40
|
+
options: Required<DesmostOptions>,
|
|
41
|
+
): Unrecoverable<void | string>
|
|
42
|
+
{
|
|
43
|
+
let errors: string[] = [];
|
|
44
|
+
|
|
45
|
+
for (let invocation of expr.incantations) {
|
|
46
|
+
if (invocation.kind === Ast.Kind.INVALID_INCANTATION) {
|
|
47
|
+
let msg = evaluate_error(invocation.error, desmos, { ...options, place_errors: "start" });
|
|
48
|
+
if (msg != undefined) {
|
|
49
|
+
errors.push(msg);
|
|
50
|
+
}
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
let data = undefined;
|
|
55
|
+
|
|
56
|
+
if ("arg_raw" in invocation && invocation.arg_raw != undefined) {
|
|
57
|
+
try {
|
|
58
|
+
data = invocation.incantation.evaluate_arg(invocation.arg_raw);
|
|
59
|
+
}
|
|
60
|
+
catch (e) {
|
|
61
|
+
errors.push(format_error(e as Error, options));
|
|
62
|
+
if (invocation.incantation.requires_arg) continue;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
invocation.incantation.apply(expr.data, data);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (errors.length > 0) {
|
|
70
|
+
desmos.setExpression({ type: "text", text: errors.join("\n\n") });
|
|
71
|
+
}
|
|
72
|
+
desmos.setExpression(expr.data);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Handle an `InvalidInvocation`, respecting the user's preferences in `options`.
|
|
78
|
+
*
|
|
79
|
+
* If the user set `place_errors: start` or `place_errors: end`, this returns the formatted error message for deferred aggregation.
|
|
80
|
+
*/
|
|
81
|
+
export function evaluate_error(
|
|
82
|
+
error: UnrecoverableError,
|
|
83
|
+
desmos: Desmos.Calculator,
|
|
84
|
+
options: Required<DesmostOptions>,
|
|
85
|
+
): Unrecoverable<void | string>
|
|
86
|
+
{
|
|
87
|
+
switch (options.errors) {
|
|
88
|
+
case "crash":
|
|
89
|
+
throw error;
|
|
90
|
+
|
|
91
|
+
case "suppress":
|
|
92
|
+
console.error(error);
|
|
93
|
+
break;
|
|
94
|
+
|
|
95
|
+
default: switch (options.place_errors) {
|
|
96
|
+
case "end":
|
|
97
|
+
case "start":
|
|
98
|
+
return format_error(error, options);
|
|
99
|
+
|
|
100
|
+
default:
|
|
101
|
+
desmos.setExpression({
|
|
102
|
+
type: "text",
|
|
103
|
+
text: format_error(error, options),
|
|
104
|
+
});
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
function format_error(
|
|
112
|
+
e: UnrecoverableError,
|
|
113
|
+
options: Required<DesmostOptions>,
|
|
114
|
+
): string
|
|
115
|
+
{
|
|
116
|
+
let prefix = options.error_prefix;
|
|
117
|
+
let sep = (prefix === "" || prefix.endsWith("\n")) ? "" : " ";
|
|
118
|
+
|
|
119
|
+
return `${prefix}${sep}${e.name}: ${e.message}`;
|
|
120
|
+
// TODO maybe include stack? (configurable?)
|
|
121
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/** Options to customise Desmost compilation. */
|
|
2
|
+
export interface DesmostOptions
|
|
3
|
+
{
|
|
4
|
+
/**
|
|
5
|
+
* How should errors be *surfaced*?
|
|
6
|
+
*
|
|
7
|
+
* Errors will always be logged to console for the developer; this setting affects how they visually reach the end user.
|
|
8
|
+
*
|
|
9
|
+
* - `surface` (default): Blocks that result in errors will become Desmos text expressions containing the error message, leaving other expressions unaffected.
|
|
10
|
+
* - `crash`: The entire compilation to Desmos will terminate with a single error message. This means you don't get any output at all, but errors are also immediately obvious.
|
|
11
|
+
* - `suppress`: Silently fail on the frontend (if you wish to give the illusion that everything is fine).
|
|
12
|
+
*/
|
|
13
|
+
errors?: "surface" | "crash" | "suppress"
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Where should errors be placed?
|
|
17
|
+
*
|
|
18
|
+
* - `inline` (default): Alongside or in place of the expression that produced it.
|
|
19
|
+
* - `end`: All aggregated at the end of the Desmos expressions list.
|
|
20
|
+
* - `start`: All aggregated at the start of the Desmos expressions list.
|
|
21
|
+
*/
|
|
22
|
+
place_errors?: "inline" | "end" | "start"
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The prefix to prepend to error blocks. Defaults to `[DESMOST ERROR]\n`. Provide a blank string if you wish for no prefix to be added.
|
|
26
|
+
*/
|
|
27
|
+
error_prefix?: string
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Should comments be ignored, instead of turned into text expressions?
|
|
31
|
+
*
|
|
32
|
+
* Defaults to `false`, meaning comments are kept.
|
|
33
|
+
*/
|
|
34
|
+
ignore_comments?: boolean
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Should all line breaks be ignored, instead of kept as blank expressions.
|
|
38
|
+
*
|
|
39
|
+
* Defaults to `false`, meaning all blank lines are kept.
|
|
40
|
+
*/
|
|
41
|
+
ignore_line_breaks?: boolean
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Should trailing blank lines at the end of the source be ignored, instead of kept as blank expressions?
|
|
45
|
+
*
|
|
46
|
+
* Defaults to `false`, meaning trailing blank lines are kept.
|
|
47
|
+
*/
|
|
48
|
+
ignore_trailing_blanks?: boolean
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Return debug diagnostics? This includes the unevaluated AST, and performance diagnostics.
|
|
52
|
+
*
|
|
53
|
+
* Defaults to `false`.
|
|
54
|
+
*/
|
|
55
|
+
debug?: boolean
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Fill in `options` with Desmost's defaults to produce a complete `DesmostOptions` config.
|
|
61
|
+
*/
|
|
62
|
+
export function set_default_options(options: Partial<DesmostOptions> | undefined): Required<DesmostOptions>
|
|
63
|
+
{
|
|
64
|
+
options ??= {};
|
|
65
|
+
|
|
66
|
+
options.errors ??= "surface";
|
|
67
|
+
options.place_errors ??= "inline";
|
|
68
|
+
options.error_prefix ??= "[DESMOST ERROR]\n";
|
|
69
|
+
|
|
70
|
+
options.ignore_comments ??= false;
|
|
71
|
+
options.ignore_line_breaks ??= false;
|
|
72
|
+
options.ignore_trailing_blanks ??= false;
|
|
73
|
+
|
|
74
|
+
options.debug ??= false;
|
|
75
|
+
|
|
76
|
+
return options as Required<DesmostOptions>;
|
|
77
|
+
}
|
package/src/desmos.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Desmos API (currently) is browser-only, so we polyfill the API objects we need here for the server-side.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export enum DesmosColour
|
|
6
|
+
{
|
|
7
|
+
RED = "#c74440",
|
|
8
|
+
BLUE = "#2d70b3",
|
|
9
|
+
GREEN = "#388c46",
|
|
10
|
+
PURPLE = "#6042a6",
|
|
11
|
+
ORANGE = "#fa7e19",
|
|
12
|
+
BLACK = "#000000",
|
|
13
|
+
}
|
package/src/errors.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The compiler encountered a fatal unrecoverable error, which will reach the end user.
|
|
3
|
+
*
|
|
4
|
+
* It's not *literally* unrecoverable in the sense that it will crash the compiler completely, but it means there are no further fallbacks to try.
|
|
5
|
+
*/
|
|
6
|
+
export class UnrecoverableError extends Error {}
|
|
7
|
+
|
|
8
|
+
export namespace UnrecoverableError
|
|
9
|
+
{
|
|
10
|
+
/** The parser unexpectedly reached the end of its entire input source code. */
|
|
11
|
+
export class UnexpectedEnd extends UnrecoverableError {}
|
|
12
|
+
|
|
13
|
+
/** The parser did not receive input that it expected. */
|
|
14
|
+
export class MissingInput extends UnrecoverableError {}
|
|
15
|
+
|
|
16
|
+
/** The parser received input that did not match what it expected. */
|
|
17
|
+
export class UnexpectedInput extends UnrecoverableError {}
|
|
18
|
+
|
|
19
|
+
/** The parser received excess input that it did not expect. */
|
|
20
|
+
export class ExcessInput extends UnrecoverableError {}
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
/** An incantation can't be applied here. */
|
|
24
|
+
export class IllegalIncantation extends UnrecoverableError {}
|
|
25
|
+
|
|
26
|
+
/** An incantation's argument couldn't be parsed. */
|
|
27
|
+
export class InvalidArgument extends UnrecoverableError {}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
/** The compiler encountered a non-fatal recoverable failure (usually during speculative parsing), which only results in internal backtracking. */
|
|
32
|
+
export class RecoverableFail {}
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
/** Indicates that a function may throw a `RecoverableFail`. */
|
|
36
|
+
export type Recoverable<Result> = Result | Result & { readonly __brand?: unique symbol };
|
|
37
|
+
|
|
38
|
+
/** Indicates that a function may throw an `UnrecoverableError`. */
|
|
39
|
+
export type Unrecoverable<Result> = Result | Result & { readonly __brand?: unique symbol };;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Indicates that a function may throw either a `RecoverableFail` *or* an `UnrecoverableError`.
|
|
43
|
+
*
|
|
44
|
+
* This is used in potentially ambiguous cases. For instance, the user might have invoked an unknown incantation, in which case we might want to `RecoverableFail` and fallback to parsing it as LaTeX; on the other hand, if they invoked a valid incantation, but did not supply the required argument, that's a definite `UnrecoverableError`.
|
|
45
|
+
*/
|
|
46
|
+
export type MaybeRecoverable<Result> = Recoverable<Result> | Unrecoverable<Result>;
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { compile, type DesmostOptions } from "./compiler";
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// import { Incantation, ArgIncantation, type GLOBAL, type LOCAL, type EXPR } from "../incantation";
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
// export class ?Incantation extends ArgIncantation<?>
|
|
5
|
+
// {
|
|
6
|
+
// override readonly description
|
|
7
|
+
// = ""
|
|
8
|
+
|
|
9
|
+
// override readonly identifier = ?
|
|
10
|
+
// override readonly requires_arg = ?
|
|
11
|
+
// override readonly arg_type = Incantation.ArgType.?
|
|
12
|
+
|
|
13
|
+
// override apply(target: ?, data: ?)
|
|
14
|
+
// {
|
|
15
|
+
// // TODO
|
|
16
|
+
// }
|
|
17
|
+
|
|
18
|
+
// override evaluate_arg(raw: ?): ?
|
|
19
|
+
// {
|
|
20
|
+
// // TODO
|
|
21
|
+
// }
|
|
22
|
+
// }
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ArgIncantation, type EXPR } from "../incantation";
|
|
2
|
+
|
|
3
|
+
import { LatexIncantation } from "./latex";
|
|
4
|
+
import { TextIncantation } from "./text";
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
export const EXPR_INCANTATIONS: ArgIncantation<EXPR>[] =
|
|
8
|
+
[
|
|
9
|
+
new LatexIncantation(),
|
|
10
|
+
new TextIncantation(),
|
|
11
|
+
];
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Incantation, ArgIncantation, type EXPR } from "../incantation";
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
export class LatexIncantation extends ArgIncantation<EXPR>
|
|
5
|
+
{
|
|
6
|
+
override readonly description
|
|
7
|
+
= "Produce a LaTeX expression. This allows the input to span multiple lines."
|
|
8
|
+
|
|
9
|
+
override readonly identifier = "latex"
|
|
10
|
+
override readonly requires_arg = true
|
|
11
|
+
override readonly arg_type = Incantation.ArgType.LATEX
|
|
12
|
+
|
|
13
|
+
apply(target: Desmos.ExpressionState, data: string): void
|
|
14
|
+
{
|
|
15
|
+
target.type = "expression";
|
|
16
|
+
// @ts-expect-error: outdated types
|
|
17
|
+
target.latex = data;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
override evaluate_arg(data: string): string
|
|
21
|
+
{
|
|
22
|
+
return data;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Incantation, ArgIncantation, type EXPR } from "../incantation";
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
export class TextIncantation extends ArgIncantation<EXPR>
|
|
5
|
+
{
|
|
6
|
+
override readonly description
|
|
7
|
+
= "Produce a text expression (“Add Note” in the Desmos GUI)."
|
|
8
|
+
|
|
9
|
+
override readonly identifier = "text"
|
|
10
|
+
override readonly requires_arg = true
|
|
11
|
+
override readonly arg_type = Incantation.ArgType.STRING
|
|
12
|
+
|
|
13
|
+
override apply(target: Desmos.ExpressionState, data: string)
|
|
14
|
+
{
|
|
15
|
+
target.type = "text";
|
|
16
|
+
// @ts-expect-error: outdated types
|
|
17
|
+
target.text = data;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Incantation, type GLOBAL } from "../incantation";
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
export class DarkModeIncantation extends Incantation<GLOBAL>
|
|
5
|
+
{
|
|
6
|
+
override readonly description
|
|
7
|
+
= "Enable dark mode for the calculator, which inverts all colours."
|
|
8
|
+
|
|
9
|
+
override readonly identifier = "dark"
|
|
10
|
+
|
|
11
|
+
apply(target: Desmos.Calculator)
|
|
12
|
+
{
|
|
13
|
+
target.updateSettings({
|
|
14
|
+
invertedColors: true,
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Incantation, ArgIncantation, type GLOBAL } from "../incantation";
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
export class DesmosIncantation extends ArgIncantation<GLOBAL>
|
|
5
|
+
{
|
|
6
|
+
override readonly description
|
|
7
|
+
= "Set the configuration of the calculator via `Calculator.updateSettings()`."
|
|
8
|
+
|
|
9
|
+
override readonly identifier = "desmos"
|
|
10
|
+
override readonly requires_arg = false
|
|
11
|
+
override readonly arg_type = Incantation.ArgType.OBJECT
|
|
12
|
+
|
|
13
|
+
override apply(
|
|
14
|
+
target: Desmos.Calculator,
|
|
15
|
+
data: Desmos.GraphConfiguration & Desmos.GraphSettings,
|
|
16
|
+
): void
|
|
17
|
+
{
|
|
18
|
+
target.updateSettings(data);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Incantation, type GLOBAL } from "../incantation";
|
|
2
|
+
|
|
3
|
+
import { DesmosIncantation } from "./desmos";
|
|
4
|
+
import { ViewportIncantation } from "./viewport";
|
|
5
|
+
import { DarkModeIncantation } from "./dark-mode";
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
export const GLOBAL_INCANTATIONS: Incantation<GLOBAL>[] =
|
|
9
|
+
[
|
|
10
|
+
new DesmosIncantation(),
|
|
11
|
+
new ViewportIncantation(),
|
|
12
|
+
new DarkModeIncantation(),
|
|
13
|
+
];
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Incantation, ArgIncantation, type GLOBAL } from "../incantation";
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
interface ViewportBounds
|
|
5
|
+
{
|
|
6
|
+
left: number; right: number;
|
|
7
|
+
bottom: number; top: number;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
export class ViewportIncantation extends ArgIncantation<GLOBAL>
|
|
12
|
+
{
|
|
13
|
+
override readonly description
|
|
14
|
+
= "Set the bounds of the viewport via `Calculator.setMathBounds()`."
|
|
15
|
+
|
|
16
|
+
override readonly identifier = "viewport"
|
|
17
|
+
override readonly requires_arg = true
|
|
18
|
+
override readonly arg_type = Incantation.ArgType.OBJECT
|
|
19
|
+
|
|
20
|
+
apply(target: Desmos.Calculator, data: ViewportBounds)
|
|
21
|
+
{
|
|
22
|
+
// TODO validate bounds
|
|
23
|
+
target.setMathBounds(data);
|
|
24
|
+
}
|
|
25
|
+
}
|