-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
executable file
·293 lines (274 loc) · 10.4 KB
/
main.js
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
284
285
286
287
288
289
290
291
292
293
var try_program = "";
var try_goal = "";
var try_goals = [""];
var try_stack = 0;
var session = null;
var code = null;
var query = null;
var MODE_PROGRAM = 1;
var MODE_DERIVATION = 2;
var mode = MODE_PROGRAM;
var reset = 0;
var styles = {};
window.addEventListener("load", function() {
var value = document.getElementById("program").innerHTML.replace(/</g,"<").replace(/>/g,">");
document.getElementById("program").innerHTML = "";
code = CodeMirror(document.getElementById("program"), {
value: value,
lineNumbers: true,
theme: "tau",
placeholder: "Your program here...",
mode: "prolog"
});
query = CodeMirror(document.getElementById("query"), {
lineNumbers: false,
theme: "tauout",
placeholder: "Type a Prolog goal in here and press ENTER",
mode: "prolog"
});
code.setSize("100%", "100%");
code.on("change", function(instance, change) {
document.getElementById("save-program").value = instance.getValue();
});
query.setSize("100%", query.defaultTextHeight() + 2 * 4);
query.on("beforeChange", function(instance, change) {
var newtext = change.text.join("").replace(/\n/g, "");
change.update(change.from, change.to, [newtext]);
return true;
});
query.on("keyHandled", try_tau_prolog);
document.getElementById("taupl-version").innerHTML = "Tau Prolog "
+ pl.version.major + "."
+ pl.version.minor + "."
+ pl.version.patch + " ("
+ pl.version.status + ")";
});
function getWriteOptions() {
return {
session: session,
ignore_ops: document.getElementById("ignore_ops").checked,
quoted: document.getElementById("quoted").checked,
numbervars: document.getElementById("numbervars").checked
};
}
function try_tau_prolog( cm, msg, e ) {
// Down
if( e.keyCode === 40 ) {
try_stack++;
if( try_stack >= try_goals.length ) try_stack = 0;
query.setValue(try_goals[try_stack]);
// Up
} else if( e.keyCode === 38 ) {
try_stack--;
if( try_stack < 0 ) try_stack = try_goals.length - 1;
query.setValue(try_goals[try_stack]);
// Enter
} else if( e.keyCode === 13 ) {
try {
var raw_program = code.getValue();
var raw_goal = query.getValue();
if( try_program !== raw_program || try_goal !== raw_goal || reset || mode == MODE_DERIVATION ) {
if( mode == MODE_PROGRAM ) {
new_block(raw_goal);
try_goals.push( raw_goal );
try_stack = try_goals.length - 1;
reset = 0;
} else if( mode == MODE_DERIVATION ) {
reset = 1;
}
if( session === null ) {
session = pl.create(parseInt(document.getElementById("limit").value));
session.streams.user_output = new pl.type.Stream({
put: function( text, _ ) {
new_message( text );
return true;
},
flush: function() {
return true;
}
}, "write", "user_output", "text", false, "eof_code");
session.standard_output = session.streams["user_output"];
session.current_output = session.streams["user_output"];
}
session.limit = parseInt(document.getElementById("limit").value);
session.query(raw_goal, {success: function(goal) {
try_answer("parsing query: " + goal.toString(getWriteOptions()) + " ok!", true);
if( mode == MODE_PROGRAM )
session.answer( try_answer );
else if( mode == MODE_DERIVATION ) {
var max_answers = parseInt(document.getElementById("max_answers").value);
session.draw(max_answers, "tau-canvas", styles, getWriteOptions());
}
}, error: function(err) {
try_answer("error parsing query:" + err.toString(getWriteOptions()), true);
}});
} else {
if( mode == MODE_PROGRAM )
session.answer( try_answer );
else if( mode == MODE_DERIVATION ) {
var max_answers = parseInt(document.getElementById("max_answers").value);
session.draw(max_answers, "tau-canvas", styles, getWriteOptions());
}
}
} catch( ex ) {
console.log(ex);
try_answer( 'javascript error: ' + ex.toString() + '<div class="report-github"><a href="https://github.com/jariazavalverde/tau-prolog/issues" target="_blank">Report error on GitHub</a></div>', true );
return;
}
}
try_program = raw_program;
try_goal = raw_goal;
}
function escapeHtml(unsafe) {
return unsafe.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
}
function new_block(last) {
var output = document.getElementById( "output" );
output.innerHTML = "<div class=\"last\">?- " + last + "</div><div class=\"goal\"><div class=\"write\"></div><div class=\"sep\"></div></div>" + output.innerHTML;
}
function new_message(msg) {
msg = msg.replace(/\n/g, "<br />");
var elem = document.getElementsByClassName( "write" )[0];
elem.style.display = "block";
elem.innerHTML += msg;
}
function try_answer( answer, format ) {
var stop = document.getElementById("stop").checked;
if(answer === null && stop === false) {
setTimeout(function() {
session.answer(try_answer);
}, 0);
} else {
var elem = document.getElementsByClassName( "goal" )[0];
elem.innerHTML = "<div class=\"answer\">" + (format ? answer : escapeHtml(pl.format_answer( answer, session, getWriteOptions() )) ) + "</div>" + elem.innerHTML;
elem.innerHTML = "<div class=\"write\"></div><div class=\"sep\"></div>" + elem.innerHTML;
}
}
function toggle(id) {
event.stopPropagation();
if( id == "save")
document.getElementById("help").style.display = 'none';
else
document.getElementById("save").style.display = 'none';
var elem = document.getElementById( id );
if( elem.style.display == 'none' ) {
elem.style.display = 'block';
} else {
elem.style.display = 'none';
}
}
function hide_dialogs() {
document.getElementById("help").style.display = 'none';
document.getElementById("save").style.display = 'none';
}
function add(text) {
code.setValue(text + "\n" + code.getValue());
}
function addClassName( elem, name ) {
var arr = elem.className.split(" ");
if( arr.indexOf(name) == -1 )
elem.className += " " + name;
}
function removeClassName( elem, name ) {
elem.className = elem.className.replace(name, "");
}
function show_program_tab() {
removeClassName(document.getElementById( "derivation-tab" ), "selected-section");
removeClassName(document.getElementById( "transformations-tab" ), "selected-section");
addClassName(document.getElementById( "program-tab" ), "selected-section");
document.getElementById( "query-container" ).style.display = "block";
document.getElementById( "canvas-container" ).style.display = "none";
document.getElementById( "transformations-container" ).style.display = "none";
document.getElementById( "program-container" ).style.display = "block";
document.getElementById( "output" ).style.display = "block";
document.getElementById( "max_answers-container" ).style.display = "none";
document.getElementById( "tree-options" ).style.display = "none";
mode = MODE_PROGRAM;
}
function show_derivation_tab() {
removeClassName(document.getElementById( "program-tab" ), "selected-section");
removeClassName(document.getElementById( "transformations-tab" ), "selected-section");
addClassName(document.getElementById( "derivation-tab" ), "selected-section");
document.getElementById( "query-container" ).style.display = "block";
document.getElementById( "program-container" ).style.display = "none";
document.getElementById( "transformations-container" ).style.display = "none";
document.getElementById( "canvas-container" ).style.display = "block";
document.getElementById( "output" ).style.display = "none";
document.getElementById( "max_answers-container" ).style.display = "inline";
document.getElementById( "tree-options" ).style.display = "block";
mode = MODE_DERIVATION;
}
function show_transformations_tab() {
removeClassName(document.getElementById( "program-tab" ), "selected-section");
removeClassName(document.getElementById( "derivation-tab" ), "selected-section");
addClassName(document.getElementById( "transformations-tab" ), "selected-section");
document.getElementById( "query-container" ).style.display = "none";
document.getElementById( "program-container" ).style.display = "none";
document.getElementById( "canvas-container" ).style.display = "none";
document.getElementById( "transformations-container" ).style.display = "block";
document.getElementById( "output" ).style.display = "none";
document.getElementById( "max_answers-container" ).style.display = "none";
document.getElementById( "tree-options" ).style.display = "none";
mode = MODE_PROGRAM;
}
function set_theme( theme ) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
styles = JSON.parse( this.responseText );
try_tau_prolog( query, null, {keyCode: 13} );
}
};
xhttp.open("GET", "../utils/draw-derivation-trees/themes/" + theme + ".json", true);
xhttp.send();
}
function reconsult() {
document.getElementById("reconsult").value = "Reconsult program";
var raw_program = code.getValue();
if( session == null ) {
session = pl.create(parseInt(document.getElementById("limit").value));
session.streams.user_output = new pl.type.Stream({
put: function( text, _ ) {
new_message( text );
return true;
},
flush: function() {
return true;
}
}, "write", "user_output", "text", false, "eof_code");
session.standard_output = session.streams["user_output"];
session.current_output = session.streams["user_output"];
}
new_block("consult");
session.consult(raw_program, {
success: function() {
try_answer( 'parsing program: ok!', true );
reset = 1;
var warnings = session.get_warnings();
for( var i = warnings.length-1; i >= 0; i-- )
try_answer( 'warning parsing program: ' + warnings[i].toString(getWriteOptions()), true );
update_transformation();
},
error: function(err) {
try_answer( 'error parsing program: ' + err.args[0], true );
}
});
}
function update_transformation() {
var t = document.getElementById("transformations-container");
var html = "<div><ul>";
for(var key in session.rules) {
html += "<li class=\"transformation-header\"><div></div><span>" + key + "</span></li><ul>";
for(var i = 0; i < session.rules[key].length; i++) {
html += "<li><input type=\"button\" class=\"transformation-button\" value=\"unfold\" onClick=\"unfold(session.rules['" + key + "'][" + i + "]);\" /> " + session.rules[key][i].toString( getWriteOptions() ) + "</li>";
}
html += "</ul>";
}
html += "</ul></div>";
t.innerHTML = html;
}
function unfold(rule) {
session.unfold(rule);
code.setValue(session.toString( getWriteOptions() ).trim());
update_transformation();
}