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.
Files changed (167) hide show
  1. package/dist/compiler/compile.d.ts +25 -1
  2. package/dist/compiler/compile.d.ts.map +1 -1
  3. package/dist/compiler/compile.js +32 -2
  4. package/dist/compiler/compile.js.map +1 -1
  5. package/dist/compiler/evaluate.d.ts +11 -0
  6. package/dist/compiler/evaluate.d.ts.map +1 -1
  7. package/dist/compiler/evaluate.js +15 -11
  8. package/dist/compiler/evaluate.js.map +1 -1
  9. package/dist/compiler/format.d.ts +13 -0
  10. package/dist/compiler/format.d.ts.map +1 -1
  11. package/dist/compiler/format.js +23 -0
  12. package/dist/compiler/format.js.map +1 -1
  13. package/dist/compiler/index.d.ts.map +1 -1
  14. package/dist/compiler/index.js.map +1 -1
  15. package/dist/compiler/options.d.ts +79 -0
  16. package/dist/compiler/options.d.ts.map +1 -1
  17. package/dist/compiler/options.js +18 -11
  18. package/dist/compiler/options.js.map +1 -1
  19. package/dist/decompiler/decompile.d.ts +20 -0
  20. package/dist/decompiler/decompile.d.ts.map +1 -0
  21. package/dist/decompiler/decompile.js +32 -0
  22. package/dist/decompiler/decompile.js.map +1 -0
  23. package/dist/decompiler/emit.d.ts +10 -0
  24. package/dist/decompiler/emit.d.ts.map +1 -0
  25. package/dist/decompiler/emit.js +37 -0
  26. package/dist/decompiler/emit.js.map +1 -0
  27. package/dist/decompiler/extract.d.ts +14 -0
  28. package/dist/decompiler/extract.d.ts.map +1 -0
  29. package/dist/decompiler/extract.js +63 -0
  30. package/dist/decompiler/extract.js.map +1 -0
  31. package/dist/decompiler/format.d.ts +3 -0
  32. package/dist/decompiler/format.d.ts.map +1 -0
  33. package/dist/decompiler/format.js +14 -0
  34. package/dist/decompiler/format.js.map +1 -0
  35. package/dist/decompiler/index.d.ts +2 -0
  36. package/dist/decompiler/index.d.ts.map +1 -0
  37. package/dist/decompiler/index.js +2 -0
  38. package/dist/decompiler/index.js.map +1 -0
  39. package/dist/desmos.d.ts +3 -0
  40. package/dist/desmos.d.ts.map +1 -1
  41. package/dist/desmos.js +3 -0
  42. package/dist/desmos.js.map +1 -1
  43. package/dist/errors.d.ts +18 -4
  44. package/dist/errors.d.ts.map +1 -1
  45. package/dist/errors.js +13 -1
  46. package/dist/errors.js.map +1 -1
  47. package/dist/index.d.ts +1 -0
  48. package/dist/index.d.ts.map +1 -1
  49. package/dist/index.internal.d.ts +2 -1
  50. package/dist/index.internal.d.ts.map +1 -1
  51. package/dist/index.internal.js +2 -1
  52. package/dist/index.internal.js.map +1 -1
  53. package/dist/index.js +1 -0
  54. package/dist/index.js.map +1 -1
  55. package/dist/magic/expr/index.js.map +1 -1
  56. package/dist/magic/expr/latex.d.ts.map +1 -1
  57. package/dist/magic/expr/latex.js +1 -0
  58. package/dist/magic/expr/latex.js.map +1 -1
  59. package/dist/magic/expr/text.d.ts.map +1 -1
  60. package/dist/magic/expr/text.js +1 -0
  61. package/dist/magic/expr/text.js.map +1 -1
  62. package/dist/magic/global/dark-mode.d.ts.map +1 -1
  63. package/dist/magic/global/dark-mode.js.map +1 -1
  64. package/dist/magic/global/desmos.d.ts +3 -1
  65. package/dist/magic/global/desmos.d.ts.map +1 -1
  66. package/dist/magic/global/desmos.js.map +1 -1
  67. package/dist/magic/global/index.d.ts +3 -0
  68. package/dist/magic/global/index.d.ts.map +1 -1
  69. package/dist/magic/global/index.js +3 -0
  70. package/dist/magic/global/index.js.map +1 -1
  71. package/dist/magic/global/viewport.d.ts.map +1 -1
  72. package/dist/magic/global/viewport.js +0 -1
  73. package/dist/magic/global/viewport.js.map +1 -1
  74. package/dist/magic/incantation.d.ts +44 -0
  75. package/dist/magic/incantation.d.ts.map +1 -1
  76. package/dist/magic/incantation.js +38 -0
  77. package/dist/magic/incantation.js.map +1 -1
  78. package/dist/magic/index.d.ts +7 -0
  79. package/dist/magic/index.d.ts.map +1 -1
  80. package/dist/magic/index.js +7 -0
  81. package/dist/magic/index.js.map +1 -1
  82. package/dist/magic/local/anim.d.ts.map +1 -1
  83. package/dist/magic/local/anim.js +1 -0
  84. package/dist/magic/local/anim.js.map +1 -1
  85. package/dist/magic/local/colour.d.ts.map +1 -1
  86. package/dist/magic/local/colour.js.map +1 -1
  87. package/dist/magic/local/dashed.d.ts.map +1 -1
  88. package/dist/magic/local/dashed.js.map +1 -1
  89. package/dist/magic/local/fill.d.ts.map +1 -1
  90. package/dist/magic/local/fill.js.map +1 -1
  91. package/dist/magic/local/hide.d.ts.map +1 -1
  92. package/dist/magic/local/hide.js.map +1 -1
  93. package/dist/magic/local/index.js.map +1 -1
  94. package/dist/magic/local/label.d.ts.map +1 -1
  95. package/dist/magic/local/label.js +1 -0
  96. package/dist/magic/local/label.js.map +1 -1
  97. package/dist/magic/local/line.d.ts.map +1 -1
  98. package/dist/magic/local/line.js.map +1 -1
  99. package/dist/magic/local/no-line.d.ts.map +1 -1
  100. package/dist/magic/local/no-line.js.map +1 -1
  101. package/dist/magic/local/point.d.ts.map +1 -1
  102. package/dist/magic/local/point.js.map +1 -1
  103. package/dist/magic/local/secret.d.ts.map +1 -1
  104. package/dist/magic/local/secret.js.map +1 -1
  105. package/dist/magic/local/slider.d.ts.map +1 -1
  106. package/dist/magic/local/slider.js +1 -0
  107. package/dist/magic/local/slider.js.map +1 -1
  108. package/dist/parser/ast.d.ts +9 -0
  109. package/dist/parser/ast.d.ts.map +1 -1
  110. package/dist/parser/ast.js +1 -0
  111. package/dist/parser/ast.js.map +1 -1
  112. package/dist/parser/desmost-parser.d.ts +101 -1
  113. package/dist/parser/desmost-parser.d.ts.map +1 -1
  114. package/dist/parser/desmost-parser.js +129 -2
  115. package/dist/parser/desmost-parser.js.map +1 -1
  116. package/dist/parser/generic-parser.d.ts +47 -0
  117. package/dist/parser/generic-parser.d.ts.map +1 -1
  118. package/dist/parser/generic-parser.js +50 -0
  119. package/dist/parser/generic-parser.js.map +1 -1
  120. package/dist/parser/index.d.ts.map +1 -1
  121. package/dist/parser/index.js.map +1 -1
  122. package/dist/utils.d.ts.map +1 -1
  123. package/dist/utils.js.map +1 -1
  124. package/package.json +5 -2
  125. package/src/compiler/compile.ts +124 -124
  126. package/src/compiler/evaluate.ts +86 -118
  127. package/src/compiler/format.ts +39 -6
  128. package/src/compiler/index.ts +0 -7
  129. package/src/compiler/options.ts +83 -77
  130. package/src/compiler/readme.md +5 -0
  131. package/src/decompiler/decompile.ts +48 -0
  132. package/src/decompiler/emit.ts +47 -0
  133. package/src/decompiler/extract.ts +100 -0
  134. package/src/decompiler/format.ts +20 -0
  135. package/src/decompiler/index.ts +1 -0
  136. package/src/decompiler/readme.md +34 -0
  137. package/src/desmos.ts +6 -6
  138. package/src/errors.ts +19 -19
  139. package/src/index.internal.ts +3 -1
  140. package/src/index.ts +1 -0
  141. package/src/magic/expr/index.ts +2 -2
  142. package/src/magic/expr/latex.ts +16 -16
  143. package/src/magic/expr/text.ts +13 -13
  144. package/src/magic/global/dark-mode.ts +10 -10
  145. package/src/magic/global/desmos.ts +18 -15
  146. package/src/magic/global/index.ts +8 -3
  147. package/src/magic/global/viewport.ts +19 -20
  148. package/src/magic/incantation.ts +94 -94
  149. package/src/magic/local/anim.ts +10 -10
  150. package/src/magic/local/colour.ts +26 -26
  151. package/src/magic/local/dashed.ts +9 -9
  152. package/src/magic/local/fill.ts +12 -12
  153. package/src/magic/local/hide.ts +9 -9
  154. package/src/magic/local/index.ts +11 -11
  155. package/src/magic/local/label.ts +51 -51
  156. package/src/magic/local/line.ts +16 -16
  157. package/src/magic/local/no-line.ts +9 -9
  158. package/src/magic/local/point.ts +16 -16
  159. package/src/magic/local/secret.ts +9 -9
  160. package/src/magic/local/slider.ts +15 -15
  161. package/src/magic/readme.md +5 -0
  162. package/src/parser/ast.ts +46 -46
  163. package/src/parser/desmost-parser.ts +550 -550
  164. package/src/parser/generic-parser.ts +176 -176
  165. package/src/parser/index.ts +0 -4
  166. package/src/parser/readme.md +21 -0
  167. package/src/utils.ts +33 -33
@@ -7,9 +7,9 @@ import { DesmostParser, Ast } from "../parser";
7
7
  /** Debug diagnostics returned from `compile()`. */
8
8
  export interface DesmostDebug
9
9
  {
10
- duration: number;
11
- num_blocks: number;
12
- ast: Ast[];
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
- /** The Desmos calculator instance to compile into. */
35
- desmos: Desmos.Calculator,
34
+ /** The Desmos calculator instance to compile into. */
35
+ desmos: Desmos.Calculator,
36
36
 
37
- /** The Desmost source code to compile. */
38
- source: string,
37
+ /** The Desmost source code to compile. */
38
+ source: string,
39
39
 
40
- /** Compilation options. */
41
- options?: DesmostOptions,
40
+ /** Compilation options. */
41
+ options?: DesmostOptions,
42
42
  ): void | DesmostDebug
43
43
  {
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
- }
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
- for (let i = 0; i < count; i++) {
162
- desmos.setExpression({ latex: ` ` });
163
- }
161
+ for (let i = 0; i < count; i++) {
162
+ desmos.setExpression({ latex: ` ` });
163
+ }
164
164
  }
@@ -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
- invocation: Ast.IncantationInvocation,
13
- desmos: Desmos.Calculator,
14
- options: Required<DesmostOptions>,
12
+ invocation: Ast.IncantationInvocation,
13
+ desmos: Desmos.Calculator,
14
+ options: Required<DesmostOptions>,
15
15
  ): Unrecoverable<void | string>
16
16
  {
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);
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
- expr: Ast.Expression,
38
- desmos: Desmos.Calculator,
39
- options: Required<DesmostOptions>,
37
+ expr: Ast.Expression,
38
+ desmos: Desmos.Calculator,
39
+ options: Required<DesmostOptions>,
40
40
  ): Unrecoverable<void | string>
41
41
  {
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);
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
- error: UnrecoverableError,
94
- desmos: Desmos.Calculator,
95
- options: Required<DesmostOptions>,
93
+ error: UnrecoverableError,
94
+ desmos: Desmos.Calculator,
95
+ options: Required<DesmostOptions>,
96
96
  ): Unrecoverable<void | string>
97
97
  {
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
- }
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
  }
@@ -4,13 +4,46 @@ import { UnrecoverableError } from "../errors";
4
4
 
5
5
 
6
6
  export function format_error(
7
- e: UnrecoverableError,
8
- options: Required<DesmostOptions>,
7
+ e: UnrecoverableError,
8
+ options: Required<DesmostOptions>,
9
9
  ): string
10
10
  {
11
- let prefix = options.error_prefix;
12
- let sep = (prefix === "" || prefix.endsWith("\n")) ? "" : " ";
11
+ let prefix = options.error_prefix;
12
+ let sep = (prefix === "" || prefix.endsWith("\n")) ? "" : " ";
13
13
 
14
- return `${prefix}${sep}${e.name}: ${e.message}`;
15
- // TODO maybe include stack? (configurable?)
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
  }
@@ -1,9 +1,2 @@
1
- /**
2
- * Implements the top-level logic of the Desmost compiler.
3
- */
4
-
5
- // public entry point
6
1
  export { compile } from "./compile";
7
-
8
- // compilation options
9
2
  export type { DesmostOptions } from "./options";