Created
November 9, 2018 10:32
-
-
Save sogaiu/fcb8674b24bbb6ebc6f09639e28864a1 to your computer and use it in GitHub Desktop.
sketch of tcp socket repl - modification of original udp repl-client.javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// based on original udp repl-client.javascript | |
const net = require('net'); | |
const readline = require('readline'); | |
const client = new net.Socket(); | |
const rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout | |
}); | |
const arcadiaPort = process.argv[2] || 5555; | |
// XXX: Unity Editor on Linuxen listens on tcp4 5555, so | |
// 127.0.0.1 is not a good idea there... | |
//const arcadiaHost = process.argv[3] || "127.0.0.1"; | |
const arcadiaHost = process.argv[3] || "::1"; | |
var input = ""; | |
client.connect(arcadiaPort, arcadiaHost, () => { | |
rl.on('line', (cmd) => { | |
// always grow input | |
input += cmd + "\n"; | |
if (parenthesesAreBalanced(input)) { | |
// send balanced form to server | |
client.write(input); | |
// reset input | |
input = ""; | |
// pause prompt until message event | |
rl.pause(); | |
} | |
}); | |
rl.on('close', () => { | |
process.exit(0); | |
}); | |
client.on('data', (data) => { | |
var msg = data.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(); | |
}); | |
client.on('ready', () => { | |
// send header form to start prompt | |
client.write('(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 System.Reflection.BindingFlags/NonPublic System.Reflection.BindingFlags/Static)) nil nil)))))'); | |
}); | |
}); | |
// 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment