-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrepl.js
79 lines (67 loc) · 2.21 KB
/
repl.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
const dgram = require('dgram');
const readline = require('readline');
const server = dgram.createSocket('udp4');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const arcadiaPort = process.argv[2] || 11211;
const arcadiaHost = process.argv[3] || "localhost";
var input = "";
rl.on('line', function(cmd) {
// always grow input
input += cmd + "\n";
if(parenthesesAreBalanced(input)) {
// send balanced form to server
server.send(input, arcadiaPort, arcadiaHost);
// reset input
input = "";
// pause prompt until message event
rl.pause();
}
});
rl.on('close', function() {
process.exit(0);
});
server.on('message', function(msg, rinfo) {
var msg = msg.toString();
var msgLines = msg.split("\n");
// set last line as prompt
rl.setPrompt(msgLines.pop());
// display all other lines
console.log(msgLines.join("\n"));
// result prompt
rl.prompt();
});
// send header form to start prompt
server.send('(binding [*warn-on-reflection* false] (do (println "; Arcadia REPL") (println (str "; Clojure " (clojure-version))) (println (str "; Unity " (UnityEditorInternal.InternalEditorUtility/GetFullUnityVersion))) (println (str "; Mono " (.Invoke (.GetMethod Mono.Runtime "GetDisplayName" (enum-or BindingFlags/NonPublic BindingFlags/Static)) nil nil)))))', arcadiaPort, arcadiaHost)
// http://codereview.stackexchange.com/questions/45991/balanced-parentheses
function parenthesesAreBalanced(s)
{
var parentheses = "[]{}()",
stack = [], //Parentheses stack
i, //Index in the string
c; //Character in the string
for (i = 0; c = s[i++];)
{
var bracePosition = parentheses.indexOf(c),
braceType;
//~ is truthy for any number but -1
if (!~bracePosition)
continue;
braceType = bracePosition % 2 ? 'closed' : 'open';
if (braceType === 'closed')
{
//If there is no open parenthese at all, return false OR
//if the opening parenthese does not match ( they should be neighbours )
if (!stack.length || parentheses.indexOf(stack.pop()) != bracePosition - 1)
return false;
}
else
{
stack.push(c);
}
}
//If anything is left on the stack <- not balanced
return !stack.length;
}