-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclips_wrap.js
More file actions
283 lines (241 loc) · 7.24 KB
/
clips_wrap.js
File metadata and controls
283 lines (241 loc) · 7.24 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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// ################
// Global constants
// ################
const spawn = require("child_process").spawn;
const Clips_Prompt = "CLIPS> ";
// made it global because otherwise it would be generated for each datachunks
const Clips_Subs = Clips_Prompt.split("")
.map((char, index, chars) =>
chars.slice(0, index+1).join("")
)
// global constants:
const completions = [
'load', 'save', 'bload', 'bsave', 'load-facts', 'save-facts', 'clear', 'run', 'reset', 'exit', 'assert', 'retract', 'agenda', 'ppdefrule', 'declare', 'salience', 'defrule', 'deffacts',
// everything you can watch
'watch', 'unwatch', 'facts', 'instances', 'slots', 'rules', 'activations', 'messages', 'message-handlers', 'generic-functions', 'methods', 'deffunctions', 'compilations', 'statistics', 'globals', 'focus', 'all',
// macros
'init', ':q', 'runfast'
];
// #########################################
// defines the global state of the programm.
// #########################################
const global_state = {
clips_process: null,
line_reader: require("readline").createInterface({
input: process.stdin,
output: process.stdout,
completer: onTab,
removeHistoryDuplicates: true,
}),
run_cmd_activated: false,
data_read_since_last_prompt: false,
display_clips_output: true,
}
// start the clips function and wire the handlers
function start() {
// start the clips process
global_state.clips_process = spawn("clips");
// wire handlers for clips process
global_state.clips_process.on('exit', () => {
process.stdout.write("\x1b[0m\n");
process.exit()
});
global_state.clips_process.stdout.on('data', ondata_handler());
// wire handlers for the line reader
global_state.line_reader.on("line", sendClips);
global_state.line_reader.on('SIGINT', () => {
if (global_state.run_cmd_activated) {
global_state.run_cmd_activated = false;
return;
}
global_state.clips_process.kill('SIGINT');
});
}
// handle clips data output and decide when to send back data to clips
function ondata_handler(){
let buffer = "";
const sendNextLine = inputDispatcher();
return chunk => {
buffer += chunk;
buffer = consumeClipsOutput(buffer);
if (buffer == Clips_Prompt) {
buffer = "";
sendNextLine();
}
};
}
// send outputs from commands back to the user
// TODO: add coloring
function consumeClipsOutput(rawoutput) {
const lines = rawoutput.split("\n");
const lastLine = lines.pop();
if (lines.length > 0) {
global_state.data_read_since_last_prompt = true;
if (global_state.display_clips_output)
lines.forEach(line => process.stdout.write(line + "\n"));
}
const sub_of_clp_prpt = endsWithPartOfClipsPrompt(lastLine);
if (sub_of_clp_prpt) {
const data_before_len = lastLine.length - sub_of_clp_prpt.length;
if (data_before_len > 0) {
global_state.data_read_since_last_prompt = true;
if (global_state.display_clips_output)
process.stdin.write(lastLine.slice(0, data_before_len));
}
return sub_of_clp_prpt;
}
// if last line is intended to be a prompt then on edits it will be seen as a prompt
// try editing a line when asked for data in a clips programm with and without this line to see the effect
if (global_state.display_clips_output) {
global_state.line_reader.setPrompt(lastLine);
process.stdin.write(lastLine);
}
global_state.data_read_since_last_prompt = true;
return "";
}
function endsWithPartOfClipsPrompt(str) {
for (sub of Clips_Subs) {
if (str.endsWith(sub)) return sub;
}
return null;
}
// choose between sending user specified commands or expands macros
function inputDispatcher() {
const queue = [];
// this way everything the user types when not asked for input is sent to clips
return async () => {
// if we don't already know what to do then find something
if (queue.length == 0) {
global_state.display_clips_output = true;
// if the run cmd is activated and should be continued then continue it
if (global_state.run_cmd_activated && global_state.data_read_since_last_prompt) {
runCmdIter(queue);
} else {
global_state.run_cmd_activated = false;
while (queue.length == 0) {
const userLine = await userInput();
// remove unnecessary caracters from the userLine
const userCmd = cleanLine(userLine);
expandMacros(queue, userCmd);
}
}
} else if (global_state.run_cmd_activated) {
global_state.display_clips_output = false;
}
// send to clips the oldest element from the queue
global_state.data_read_since_last_prompt = false;
sendClips(queue.shift());
};
}
function userInput() {
return new Promise((resolve, reject) => {
global_state.line_reader.question("\x1b[36m\n#> ", userline => {
process.stdin.write("\x1b[0m");
resolve(userline);
});
});
}
function cleanLine(line) {
line = line.trim();
if (line[0] == "(") {
line = line.slice(1, line.length);
if (line[line.length - 1] == ")") line = line.slice(0, line.length - 1);
line = line.trim();
}
return line;
}
function expandMacros(queue, line) {
let words = line.split(" ");
if (words.length == 0) return;
const error = msg => process.stderr.write("\x1b[35m" + msg + "\x1b[0m\n");
// "init" macros
const fst = words[0];
switch (true) {
case fst.endsWith(".clp"): words.push(fst);
case fst == "init": {
if (words.length == 2) {
const filename = words[1].replace(/"/g, "");
queue.push("(clear)");
queue.push('(load "' + filename + '")');
queue.push("(reset)");
} else {
error("usage: init <filename>");
error(" <=> (clear) (load <filename>) (reset)");
}
return;
}
case fst == "load": {
// useful if you are working whith a clips program whithout the .clp extension
if (words.length == 2) {
const filename = words[1].replace(/"/g, "");
queue.push('(load "' + filename + '")');
}
return;
}
case fst == ":q" : {
queue.push("(exit)");
return;
}
case fst == "run": {
if (words.length != 1) break;
global_state.run_cmd_activated = true;
runCmdIter(queue);
return;
}
case fst == "forest":
case fst == "gump":
case fst == "forestgump":
case fst == "runfast":
queue.push("(run)");
return;
}
addToCompletion(words);
words = words.map(w => {
if (w.indexOf(".clp") != -1) {
const filename = w.replace(/"/g, "");
return '"'+filename+'"';
}
return w;
});
// basic commands
queue.push("(" + words.join(" ") + ")");
}
function runCmdIter(queue) {
queue.push("(run 100)");
queue.push("(agenda)");
}
function sendClips(line) {
// console.log("sending: " + line + " to clips");
global_state.clips_process.stdin.write(line + "\n");
}
// completion helpers:
function onTab(line) {
const lastw = line.split(" ").pop();
const hits = completions.filter((c) => c.startsWith(lastw));
//const quoteHits = completions.filter((c) => c.startsWith('"'+lastw));
// show all completions if none found
return [hits, lastw];
// return [hits.concat(quoteHits), lastw];
}
function addToCompletion(words) {
for (w of words) {
if (completions.indexOf(w) == -1) completions.push(w);
}
}
function addCurDirClipsFiles() {
const fs = require("fs");
fs.readdir(".", (err, files) => {
if (!err) {
for (f of files) {
if (f.endsWith(".clp") && completions.indexOf(f) == -1) {
completions.push(f);
completions.push('"'+f+'"');
}
}
}
});
}
// begining of the program
addCurDirClipsFiles();
start();
/* vim: set nowrap: */