desmost 0.7.3 → 0.8.1
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 +25 -1
- package/dist/compiler/compile.d.ts.map +1 -1
- package/dist/compiler/compile.js +32 -2
- package/dist/compiler/compile.js.map +1 -1
- package/dist/compiler/evaluate.d.ts +11 -0
- package/dist/compiler/evaluate.d.ts.map +1 -1
- package/dist/compiler/evaluate.js +15 -11
- package/dist/compiler/evaluate.js.map +1 -1
- package/dist/compiler/format.d.ts +13 -0
- package/dist/compiler/format.d.ts.map +1 -1
- package/dist/compiler/format.js +23 -0
- package/dist/compiler/format.js.map +1 -1
- package/dist/compiler/index.d.ts.map +1 -1
- package/dist/compiler/index.js.map +1 -1
- package/dist/compiler/options.d.ts +79 -0
- package/dist/compiler/options.d.ts.map +1 -1
- package/dist/compiler/options.js +18 -11
- package/dist/compiler/options.js.map +1 -1
- package/dist/decompiler/decompile.d.ts +20 -0
- package/dist/decompiler/decompile.d.ts.map +1 -0
- package/dist/decompiler/decompile.js +32 -0
- package/dist/decompiler/decompile.js.map +1 -0
- package/dist/decompiler/emit.d.ts +10 -0
- package/dist/decompiler/emit.d.ts.map +1 -0
- package/dist/decompiler/emit.js +37 -0
- package/dist/decompiler/emit.js.map +1 -0
- package/dist/decompiler/extract.d.ts +14 -0
- package/dist/decompiler/extract.d.ts.map +1 -0
- package/dist/decompiler/extract.js +63 -0
- package/dist/decompiler/extract.js.map +1 -0
- package/dist/decompiler/format.d.ts +3 -0
- package/dist/decompiler/format.d.ts.map +1 -0
- package/dist/decompiler/format.js +14 -0
- package/dist/decompiler/format.js.map +1 -0
- package/dist/decompiler/index.d.ts +2 -0
- package/dist/decompiler/index.d.ts.map +1 -0
- package/dist/decompiler/index.js +2 -0
- package/dist/decompiler/index.js.map +1 -0
- package/dist/desmos.d.ts +3 -0
- package/dist/desmos.d.ts.map +1 -1
- package/dist/desmos.js +3 -0
- package/dist/desmos.js.map +1 -1
- package/dist/errors.d.ts +18 -4
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +13 -1
- package/dist/errors.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.internal.d.ts +2 -1
- package/dist/index.internal.d.ts.map +1 -1
- package/dist/index.internal.js +2 -1
- package/dist/index.internal.js.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/magic/expr/index.js.map +1 -1
- package/dist/magic/expr/latex.d.ts.map +1 -1
- package/dist/magic/expr/latex.js +1 -0
- package/dist/magic/expr/latex.js.map +1 -1
- package/dist/magic/expr/text.d.ts.map +1 -1
- package/dist/magic/expr/text.js +1 -0
- package/dist/magic/expr/text.js.map +1 -1
- package/dist/magic/global/dark-mode.d.ts.map +1 -1
- package/dist/magic/global/dark-mode.js.map +1 -1
- package/dist/magic/global/desmos.d.ts +3 -1
- package/dist/magic/global/desmos.d.ts.map +1 -1
- package/dist/magic/global/desmos.js.map +1 -1
- package/dist/magic/global/index.d.ts +3 -0
- package/dist/magic/global/index.d.ts.map +1 -1
- package/dist/magic/global/index.js +3 -0
- package/dist/magic/global/index.js.map +1 -1
- package/dist/magic/global/viewport.d.ts.map +1 -1
- package/dist/magic/global/viewport.js +0 -1
- package/dist/magic/global/viewport.js.map +1 -1
- package/dist/magic/incantation.d.ts +44 -0
- package/dist/magic/incantation.d.ts.map +1 -1
- package/dist/magic/incantation.js +38 -0
- package/dist/magic/incantation.js.map +1 -1
- package/dist/magic/index.d.ts +7 -0
- package/dist/magic/index.d.ts.map +1 -1
- package/dist/magic/index.js +7 -0
- package/dist/magic/index.js.map +1 -1
- package/dist/magic/local/anim.d.ts.map +1 -1
- package/dist/magic/local/anim.js +1 -0
- package/dist/magic/local/anim.js.map +1 -1
- package/dist/magic/local/colour.d.ts.map +1 -1
- package/dist/magic/local/colour.js.map +1 -1
- package/dist/magic/local/dashed.d.ts.map +1 -1
- package/dist/magic/local/dashed.js.map +1 -1
- package/dist/magic/local/fill.d.ts.map +1 -1
- package/dist/magic/local/fill.js.map +1 -1
- package/dist/magic/local/hide.d.ts.map +1 -1
- package/dist/magic/local/hide.js.map +1 -1
- package/dist/magic/local/index.js.map +1 -1
- package/dist/magic/local/label.d.ts.map +1 -1
- package/dist/magic/local/label.js +1 -0
- package/dist/magic/local/label.js.map +1 -1
- package/dist/magic/local/line.d.ts.map +1 -1
- package/dist/magic/local/line.js.map +1 -1
- package/dist/magic/local/no-line.d.ts.map +1 -1
- package/dist/magic/local/no-line.js.map +1 -1
- package/dist/magic/local/point.d.ts.map +1 -1
- package/dist/magic/local/point.js.map +1 -1
- package/dist/magic/local/secret.d.ts.map +1 -1
- package/dist/magic/local/secret.js.map +1 -1
- package/dist/magic/local/slider.d.ts.map +1 -1
- package/dist/magic/local/slider.js +1 -0
- package/dist/magic/local/slider.js.map +1 -1
- package/dist/parser/ast.d.ts +9 -0
- package/dist/parser/ast.d.ts.map +1 -1
- package/dist/parser/ast.js +1 -0
- package/dist/parser/ast.js.map +1 -1
- package/dist/parser/desmost-parser.d.ts +101 -1
- package/dist/parser/desmost-parser.d.ts.map +1 -1
- package/dist/parser/desmost-parser.js +129 -2
- package/dist/parser/desmost-parser.js.map +1 -1
- package/dist/parser/generic-parser.d.ts +47 -0
- package/dist/parser/generic-parser.d.ts.map +1 -1
- package/dist/parser/generic-parser.js +50 -0
- package/dist/parser/generic-parser.js.map +1 -1
- package/dist/parser/index.d.ts.map +1 -1
- package/dist/parser/index.js.map +1 -1
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js.map +1 -1
- package/package.json +5 -2
- package/src/compiler/compile.ts +124 -124
- package/src/compiler/evaluate.ts +86 -118
- package/src/compiler/format.ts +39 -6
- package/src/compiler/index.ts +0 -7
- package/src/compiler/options.ts +83 -77
- package/src/compiler/readme.md +5 -0
- package/src/decompiler/decompile.ts +48 -0
- package/src/decompiler/emit.ts +47 -0
- package/src/decompiler/extract.ts +100 -0
- package/src/decompiler/format.ts +20 -0
- package/src/decompiler/index.ts +1 -0
- package/src/decompiler/readme.md +34 -0
- package/src/desmos.ts +6 -6
- package/src/errors.ts +19 -19
- package/src/index.internal.ts +3 -1
- package/src/index.ts +1 -0
- package/src/magic/expr/index.ts +2 -2
- package/src/magic/expr/latex.ts +16 -16
- package/src/magic/expr/text.ts +13 -13
- package/src/magic/global/dark-mode.ts +10 -10
- package/src/magic/global/desmos.ts +18 -15
- package/src/magic/global/index.ts +8 -3
- package/src/magic/global/viewport.ts +19 -20
- package/src/magic/incantation.ts +94 -94
- package/src/magic/local/anim.ts +10 -10
- package/src/magic/local/colour.ts +26 -26
- package/src/magic/local/dashed.ts +9 -9
- package/src/magic/local/fill.ts +12 -12
- package/src/magic/local/hide.ts +9 -9
- package/src/magic/local/index.ts +11 -11
- package/src/magic/local/label.ts +51 -51
- package/src/magic/local/line.ts +16 -16
- package/src/magic/local/no-line.ts +9 -9
- package/src/magic/local/point.ts +16 -16
- package/src/magic/local/secret.ts +9 -9
- package/src/magic/local/slider.ts +15 -15
- package/src/magic/readme.md +5 -0
- package/src/parser/ast.ts +46 -46
- package/src/parser/desmost-parser.ts +550 -550
- package/src/parser/generic-parser.ts +176 -176
- package/src/parser/index.ts +0 -4
- package/src/parser/readme.md +21 -0
- package/src/utils.ts +33 -33
package/src/compiler/compile.ts
CHANGED
|
@@ -7,9 +7,9 @@ import { DesmostParser, Ast } from "../parser";
|
|
|
7
7
|
/** Debug diagnostics returned from `compile()`. */
|
|
8
8
|
export interface DesmostDebug
|
|
9
9
|
{
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
duration: number;
|
|
11
|
+
num_blocks: number;
|
|
12
|
+
ast: Ast[];
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
|
|
@@ -31,134 +31,134 @@ export interface DesmostDebug
|
|
|
31
31
|
* ```
|
|
32
32
|
*/
|
|
33
33
|
export function compile(
|
|
34
|
-
|
|
35
|
-
|
|
34
|
+
/** The Desmos calculator instance to compile into. */
|
|
35
|
+
desmos: Desmos.Calculator,
|
|
36
36
|
|
|
37
|
-
|
|
38
|
-
|
|
37
|
+
/** The Desmost source code to compile. */
|
|
38
|
+
source: string,
|
|
39
39
|
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
/** Compilation options. */
|
|
41
|
+
options?: DesmostOptions,
|
|
42
42
|
): void | DesmostDebug
|
|
43
43
|
{
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
44
|
+
if (desmos == undefined) {
|
|
45
|
+
console.error(`Desmost: No \`Desmos.Calculator\` instance provided, aborting compilation!`);
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (source == undefined) {
|
|
50
|
+
console.error(`Desmost: No source code provided, aborting compilation!`);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
let opts = fill_defaults(options);
|
|
55
|
+
|
|
56
|
+
if (opts.debug) {
|
|
57
|
+
var t_init = performance.now();
|
|
58
|
+
var num_blocks = 0;
|
|
59
|
+
var ast: Ast[] = [];
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
let parser = new DesmostParser(source, opts);
|
|
63
|
+
|
|
64
|
+
let errors: string[] = [];
|
|
65
|
+
|
|
66
|
+
/* To aggregate errors at the start, we need a target to retroactively inject errors into */
|
|
67
|
+
if (opts.place_errors === "start") {
|
|
68
|
+
desmos.setExpression({ id: "deferred-start", latex: " " }); // FIXME check if ` ` needed
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/* Keep track of whether we're still evaluating leading blank expressions, which might be ignored. */
|
|
72
|
+
let seen_non_blank = false;
|
|
73
|
+
|
|
74
|
+
/** The number of pending blank expressions to add. */
|
|
75
|
+
let pending_blanks: number = 0;
|
|
76
|
+
|
|
77
|
+
/* The compiler is lazy, parsing and evaluating one block at a time (as opposed to first parsing the entire AST). We don't need the whole AST, so this saves memory. */
|
|
78
|
+
/* NOTE: It also doesn't sacrifice performance, since `.setExpressions()` internally just calls `.setExpression()` in a loop anyway. Benchmarks in the frontend produce similar times for both, so there's no performance improvement. */
|
|
79
|
+
try {
|
|
80
|
+
while (true) {
|
|
81
|
+
let r = parser.parse_next();
|
|
82
|
+
if (r === null) break;
|
|
83
|
+
|
|
84
|
+
if (opts.debug) {
|
|
85
|
+
num_blocks!++;
|
|
86
|
+
ast!.push(r);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** An error message to leave for aggregation later. */
|
|
90
|
+
let defer: string | void = undefined;
|
|
91
|
+
|
|
92
|
+
switch (r.kind) {
|
|
93
|
+
case Ast.Kind.INCANTATION_INVOCATION:
|
|
94
|
+
defer = evaluate_global_incantation(r, desmos, opts);
|
|
95
|
+
break;
|
|
96
|
+
|
|
97
|
+
case Ast.Kind.EXPRESSION:
|
|
98
|
+
// @ts-expect-error: outdated types
|
|
99
|
+
if (r.data.latex === " ") {
|
|
100
|
+
if (opts.ignore_all_blanks) break;
|
|
101
|
+
if (!seen_non_blank && !opts.keep_leading_blanks) break;
|
|
102
|
+
pending_blanks++;
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
flush_pending_blanks(desmos, pending_blanks);
|
|
107
|
+
pending_blanks = 0;
|
|
108
|
+
|
|
109
|
+
seen_non_blank = true;
|
|
110
|
+
defer = evaluate_expr(r, desmos, opts);
|
|
111
|
+
break;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
case Ast.Kind.INVALID_INCANTATION:
|
|
115
|
+
defer = evaluate_error(r.error, desmos, opts);
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (defer !== undefined) {
|
|
120
|
+
errors.push(defer);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
catch (e) {
|
|
125
|
+
errors.push((e as Error).message);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (opts.keep_trailing_blanks) {
|
|
129
|
+
flush_pending_blanks(desmos, pending_blanks);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (errors.length > 0) {
|
|
133
|
+
let expr: Desmos.ExpressionState = {
|
|
134
|
+
type: "text",
|
|
135
|
+
text: errors.join("\n\n"),
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
switch (opts.place_errors) {
|
|
139
|
+
case "start":
|
|
140
|
+
desmos.setExpression({ ...expr, id: "deferred-start" });
|
|
141
|
+
break;
|
|
142
|
+
case "end":
|
|
143
|
+
case "inline":
|
|
144
|
+
desmos.setExpression(expr);
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (opts.debug) {
|
|
150
|
+
return {
|
|
151
|
+
duration: performance.now() - t_init!,
|
|
152
|
+
num_blocks: num_blocks!,
|
|
153
|
+
ast: ast!,
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
156
|
}
|
|
157
157
|
|
|
158
158
|
|
|
159
159
|
function flush_pending_blanks(desmos: Desmos.Calculator, count: number)
|
|
160
160
|
{
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
161
|
+
for (let i = 0; i < count; i++) {
|
|
162
|
+
desmos.setExpression({ latex: ` ` });
|
|
163
|
+
}
|
|
164
164
|
}
|
package/src/compiler/evaluate.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { format_error } from "./format";
|
|
1
|
+
import { format_error, normalise_latex, prettify_latex } from "./format";
|
|
2
2
|
import type { DesmostOptions } from "./options";
|
|
3
3
|
|
|
4
4
|
import { Ast } from "../parser";
|
|
@@ -9,24 +9,24 @@ import { UnrecoverableError, type Unrecoverable } from "../errors";
|
|
|
9
9
|
* Evaluate arguments (if any) to a global incantation `invocation`, then apply the incantation to `desmos`.
|
|
10
10
|
*/
|
|
11
11
|
export function evaluate_global_incantation(
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
invocation: Ast.IncantationInvocation,
|
|
13
|
+
desmos: Desmos.Calculator,
|
|
14
|
+
options: Required<DesmostOptions>,
|
|
15
15
|
): Unrecoverable<void | string>
|
|
16
16
|
{
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
17
|
+
let data = undefined;
|
|
18
|
+
|
|
19
|
+
if ("arg_raw" in invocation && invocation.arg_raw != undefined) {
|
|
20
|
+
try {
|
|
21
|
+
data = invocation.incantation.evaluate_arg(invocation.arg_raw);
|
|
22
|
+
}
|
|
23
|
+
catch (e) {
|
|
24
|
+
let msg = evaluate_error(e as Error, desmos, options);
|
|
25
|
+
return msg;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
invocation.incantation.apply(desmos, data);
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
|
|
@@ -34,53 +34,53 @@ export function evaluate_global_incantation(
|
|
|
34
34
|
* Evaluate arguments (if any) to local incantation invocations on `expr`, then add `expr` to `desmos`.
|
|
35
35
|
*/
|
|
36
36
|
export function evaluate_expr(
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
37
|
+
expr: Ast.Expression,
|
|
38
|
+
desmos: Desmos.Calculator,
|
|
39
|
+
options: Required<DesmostOptions>,
|
|
40
40
|
): Unrecoverable<void | string>
|
|
41
41
|
{
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
42
|
+
let errors: string[] = [];
|
|
43
|
+
|
|
44
|
+
for (let invocation of expr.incantations) {
|
|
45
|
+
if (invocation.kind === Ast.Kind.INVALID_INCANTATION) {
|
|
46
|
+
let msg = evaluate_error(invocation.error, desmos, { ...options, place_errors: "start" });
|
|
47
|
+
if (msg != undefined) {
|
|
48
|
+
errors.push(msg);
|
|
49
|
+
}
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
let data = undefined;
|
|
54
|
+
|
|
55
|
+
if ("arg_raw" in invocation && invocation.arg_raw != undefined) {
|
|
56
|
+
try {
|
|
57
|
+
data = invocation.incantation.evaluate_arg(invocation.arg_raw);
|
|
58
|
+
}
|
|
59
|
+
catch (e) {
|
|
60
|
+
errors.push(format_error(e as Error, options));
|
|
61
|
+
if (invocation.incantation.requires_arg) continue;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
invocation.incantation.apply(expr.data, data);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (errors.length > 0) {
|
|
69
|
+
desmos.setExpression({ type: "text", text: errors.join("\n\n") });
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// @ts-expect-error: outdated types
|
|
73
|
+
if (expr.data.latex != undefined) {
|
|
74
|
+
// @ts-expect-error: outdated types
|
|
75
|
+
expr.data.latex = normalise_latex(expr.data.latex);
|
|
76
|
+
|
|
77
|
+
if (options.prettify) {
|
|
78
|
+
// @ts-expect-error: outdated types
|
|
79
|
+
expr.data.latex = prettify_latex(expr.data.latex);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
desmos.setExpression(expr.data);
|
|
84
84
|
}
|
|
85
85
|
|
|
86
86
|
|
|
@@ -90,62 +90,30 @@ export function evaluate_expr(
|
|
|
90
90
|
* If the user set `place_errors: start` or `place_errors: end`, this returns the formatted error message for deferred aggregation.
|
|
91
91
|
*/
|
|
92
92
|
export function evaluate_error(
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
93
|
+
error: UnrecoverableError,
|
|
94
|
+
desmos: Desmos.Calculator,
|
|
95
|
+
options: Required<DesmostOptions>,
|
|
96
96
|
): Unrecoverable<void | string>
|
|
97
97
|
{
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* Remove line breaks from `latex` so Desmos can properly consume it.
|
|
124
|
-
*/
|
|
125
|
-
function normalise_latex(latex: string): string
|
|
126
|
-
{
|
|
127
|
-
latex = latex.replaceAll(/\s+/g, " ");
|
|
128
|
-
return latex;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
/**
|
|
133
|
-
* Prettify `latex` to render nicely in Desmos, reflecting how you would type directly into Desmos.
|
|
134
|
-
*
|
|
135
|
-
* This includes:
|
|
136
|
-
*
|
|
137
|
-
* - Replace `()`, `[]`, etc. with `\left(\right)`
|
|
138
|
-
* - Replace `min()`, `max()`, etc. with `\operatorname{min}()`
|
|
139
|
-
*/
|
|
140
|
-
function prettify_latex(latex: string): string
|
|
141
|
-
{
|
|
142
|
-
latex = latex.replaceAll(/(?<!\\left)(\(|\[|\\\{)/g, "\\left$1");
|
|
143
|
-
latex = latex.replaceAll(/(?<!\\right)(\)|\]|\\\})/g, "\\right$1");
|
|
144
|
-
|
|
145
|
-
latex = latex.replaceAll(
|
|
146
|
-
/(?<=[^\w]|^)\\?(mean|median|count|total|repeat|join|sort|shuffle|unique|mod|ceil|floor|round|sign) ?(?=\(|\\left\()/g,
|
|
147
|
-
"\\operatorname{$1}"
|
|
148
|
-
);
|
|
149
|
-
|
|
150
|
-
return latex;
|
|
98
|
+
switch (options.errors) {
|
|
99
|
+
case "crash":
|
|
100
|
+
throw error;
|
|
101
|
+
|
|
102
|
+
case "suppress":
|
|
103
|
+
console.error(error);
|
|
104
|
+
break;
|
|
105
|
+
|
|
106
|
+
default: switch (options.place_errors) {
|
|
107
|
+
case "end":
|
|
108
|
+
case "start":
|
|
109
|
+
return format_error(error, options);
|
|
110
|
+
|
|
111
|
+
default:
|
|
112
|
+
desmos.setExpression({
|
|
113
|
+
type: "text",
|
|
114
|
+
text: format_error(error, options),
|
|
115
|
+
});
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
151
119
|
}
|
package/src/compiler/format.ts
CHANGED
|
@@ -4,13 +4,46 @@ import { UnrecoverableError } from "../errors";
|
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
export function format_error(
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
e: UnrecoverableError,
|
|
8
|
+
options: Required<DesmostOptions>,
|
|
9
9
|
): string
|
|
10
10
|
{
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
let prefix = options.error_prefix;
|
|
12
|
+
let sep = (prefix === "" || prefix.endsWith("\n")) ? "" : " ";
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
return `${prefix}${sep}${e.name}: ${e.message}`;
|
|
15
|
+
// TODO maybe include stack? (configurable?)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Remove line breaks from `latex` so Desmos can properly consume it.
|
|
21
|
+
*/
|
|
22
|
+
export function normalise_latex(latex: string): string
|
|
23
|
+
{
|
|
24
|
+
latex = latex.replaceAll(/\s+/g, " ");
|
|
25
|
+
return latex;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Prettify `latex` to render nicely in Desmos, reflecting how you would type directly into Desmos.
|
|
31
|
+
*
|
|
32
|
+
* This includes:
|
|
33
|
+
*
|
|
34
|
+
* - Replace `()`, `[]`, etc. with `\left(\right)`
|
|
35
|
+
* - Replace `min()`, `max()`, etc. with `\operatorname{min}()`
|
|
36
|
+
*/
|
|
37
|
+
export function prettify_latex(latex: string): string
|
|
38
|
+
{
|
|
39
|
+
latex = latex.replaceAll(/(?<!\\left)(\(|\[|\\\{)/g, "\\left$1");
|
|
40
|
+
latex = latex.replaceAll(/(?<!\\right)(\)|\]|\\\})/g, "\\right$1");
|
|
41
|
+
latex = latex.replaceAll(/(:|,)(?!\\ ) */g, "$1\\ ");
|
|
42
|
+
|
|
43
|
+
latex = latex.replaceAll(
|
|
44
|
+
/(?<=[^\w]|^)\\?(mean|median|count|total|repeat|join|sort|shuffle|unique|mod|ceil|floor|round|sign) ?(?=\(|\\left\()/g,
|
|
45
|
+
"\\operatorname{$1}"
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
return latex;
|
|
16
49
|
}
|