Very simple nrepl has been added

This commit is contained in:
Sameer Rahmani 2020-01-24 17:11:56 +00:00
parent bb595cbbe1
commit 426d19ccff
2 changed files with 79 additions and 0 deletions

View File

@ -5,6 +5,9 @@ all:
repl: repl:
./gradlew compileJava && rlwrap java -cp build/classes/java/main/ serene.simple.Main ./gradlew compileJava && rlwrap java -cp build/classes/java/main/ serene.simple.Main
nrepl:
./gradlew compileJava && rlwrap java -cp build/classes/java/main/ serene.simple.Main nrepl
run: run:
./gradlew compileJava && java -cp build/classes/java/main/ serene.simple.Main ${PWD}/test.srns ./gradlew compileJava && java -cp build/classes/java/main/ serene.simple.Main ${PWD}/test.srns

View File

@ -23,6 +23,12 @@ import java.io.IOException;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.Console; import java.io.Console;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.ServerSocket;
import java.net.Socket;
/** /**
* What is missing: * What is missing:
@ -43,6 +49,9 @@ import java.io.FileInputStream;
* sacrificed quality for time. * sacrificed quality for time.
*/ */
public class Main { public class Main {
private static int port = 5544;
private static String host = "localhost";
private static String licenseHeader = "\nSerene(simple), Copyright (C) 2019-2020 " + private static String licenseHeader = "\nSerene(simple), Copyright (C) 2019-2020 " +
"Sameer Rahmani <lxsameer@gnu.org>\n" + "Sameer Rahmani <lxsameer@gnu.org>\n" +
"Serene(simple) comes with ABSOLUTELY NO WARRANTY;\n" + "Serene(simple) comes with ABSOLUTELY NO WARRANTY;\n" +
@ -56,6 +65,12 @@ public class Main {
startRepl(); startRepl();
return; return;
} }
if (args[0].equals("nrepl")) {
nrepl();
return;
}
runSerene(args[0]); runSerene(args[0]);
} }
@ -100,6 +115,67 @@ public class Main {
} }
} }
public static void nrepl() throws IOException {
RootScope rootScope = new RootScope();
ServerSocket socket = new ServerSocket(Main.port);;
System.out.println("Serene 'simple' v0.1.0");
System.out.println(licenseHeader);
System.out.println(
String.format("nRepl has been started on tcp://%s:%s", Main.host, Main.port));
try {
Socket clientSocket = socket.accept();
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
while(true) {
String inputData = in.readLine();
if (inputData == null) break;
ByteArrayInputStream inputStream = new ByteArrayInputStream(inputData.getBytes());
try {
ListNode<Node> nodes = Reader.read(inputStream);
Object result = ListNode.EMPTY;
for (Node n : nodes) {
result = n.eval(rootScope);
}
if (result == null) {
out.println("0nil");
}
else {
out.println(
String.format("0%s", result.toString()));
}
}
catch(Exception e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
String exceptionAsString = sw.toString();
out.println(
String.format("1%s", exceptionAsString));
}
}
}
catch (IOException e) {
System.out.println("Exception caught when trying to listen on port "
+ Main.port + " or listening for a connection");
System.out.println(e.getMessage());
}
finally {
socket.close();
}
}
private static void runSerene(String filePath) throws IOException { private static void runSerene(String filePath) throws IOException {
IScope rootScope = new RootScope(); IScope rootScope = new RootScope();
FileInputStream input = new FileInputStream(filePath); FileInputStream input = new FileInputStream(filePath);