-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultisl.js
More file actions
87 lines (83 loc) · 2.74 KB
/
multisl.js
File metadata and controls
87 lines (83 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
84
85
86
87
import { ISLInterpreter, ISLError } from "https://cdn.jsdelivr.net/gh/LightningLaser8/ISL@latest/core/interpreter.js";
import { ISLExtension } from "https://cdn.jsdelivr.net/gh/LightningLaser8/ISL@latest/core/extensions.js";
class MultISLExtension extends ISLExtension {
threads = {};
options = {};
constructor(interpreter) {
super("multisl");
this.options = interpreter.options;
this.addLabel("create", ["thread"]);
this.addLabel("addto", ["thread"]);
this.addLabel("in", ["thread"]);
this.addLabel("start", ["thread"]);
this.addLabel("stop", ["thread"]);
this.addLabel("pause", ["thread"]);
this.addLabel("delete", ["thread"]);
this.addKeyword(
"thread",
(interpreter, labels, threadName, ...isl) => {
this.#isl_thread(labels, threadName.value, ...isl.map(x => x.value))
},
[
{type: "identifier", name: "thread"},
{type: "any", name: "code", optional: true, recurring: true}
]
);
}
#isl_thread(labels, threadName, ...isl) {
if (labels.length !== 1) {
throw new ISLError(
"Cannot perform multiple thread operations at once, please only use one label at a time.", SyntaxError
);
}
if (labels.includes("create")) {
this.threads[threadName] = {
name: threadName,
isl: [],
interpreter: new ISLInterpreter(this.#getThreadOpts(threadName)),
};
} else {
if (!this.threads[threadName]) {
throw new ISLError(
"Cannot " +
labels[0] +
" a nonexistent thread '" +
threadName +
"'", ReferenceError
);
}
}
if (labels.includes("add-to")) {
this.threads[threadName].isl.push(
isl.map((x) => '"' + x + '"').join(" ")
);
}
if (labels.includes("in")) {
this.threads[threadName].interpreter.executeLine(
isl.map((x) => '"' + x + '"').join(" ")
);
}
if (labels.includes("start")) {
this.threads[threadName].interpreter.loadISL(
this.threads[threadName].isl
);
this.threads[threadName].interpreter.startExecution();
}
if (labels.includes("stop")) {
this.threads[threadName].interpreter.stopExecution();
}
if (labels.includes("pause")) {
this.threads[threadName].interpreter.pauseExecution();
}
if (labels.includes("delete")) {
this.threads[threadName].interpreter.stopExecution();
delete this.threads[threadName];
}
}
#getThreadOpts(threadName) {
let options = structuredClone(this.options);
options.name = this.options.name + " (thread '" + threadName + "')";
return options;
}
}
export { MultISLExtension };