Browse Source

Very simple nrepl has been added

master
Sameer Rahmani 2 years ago
parent
commit
426d19ccff
  1. 3
      Makefile
  2. 76
      src/main/java/serene/simple/Main.java

3
Makefile

@ -5,6 +5,9 @@ all: @@ -5,6 +5,9 @@ all:
repl:
./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:
./gradlew compileJava && java -cp build/classes/java/main/ serene.simple.Main ${PWD}/test.srns

76
src/main/java/serene/simple/Main.java

@ -23,6 +23,12 @@ import java.io.IOException; @@ -23,6 +23,12 @@ import java.io.IOException;
import java.io.ByteArrayInputStream;
import java.io.Console;
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:
@ -43,6 +49,9 @@ import java.io.FileInputStream; @@ -43,6 +49,9 @@ import java.io.FileInputStream;
* sacrificed quality for time.
*/
public class Main {
private static int port = 5544;
private static String host = "localhost";
private static String licenseHeader = "\nSerene(simple), Copyright (C) 2019-2020 " +
"Sameer Rahmani <lxsameer@gnu.org>\n" +
"Serene(simple) comes with ABSOLUTELY NO WARRANTY;\n" +
@ -56,6 +65,12 @@ public class Main { @@ -56,6 +65,12 @@ public class Main {
startRepl();
return;
}
if (args[0].equals("nrepl")) {
nrepl();
return;
}
runSerene(args[0]);
}
@ -100,6 +115,67 @@ public class Main { @@ -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 {
IScope rootScope = new RootScope();
FileInputStream input = new FileInputStream(filePath);

Loading…
Cancel
Save