From 426d19ccfffbb8db8f8308e86722a7cec34adfcc Mon Sep 17 00:00:00 2001 From: Sameer Rahmani Date: Fri, 24 Jan 2020 17:11:56 +0000 Subject: [PATCH] Very simple nrepl has been added --- Makefile | 3 ++ src/main/java/serene/simple/Main.java | 76 +++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) diff --git a/Makefile b/Makefile index 440e540..7b2c714 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/src/main/java/serene/simple/Main.java b/src/main/java/serene/simple/Main.java index 1d292d0..cdf0d3e 100644 --- a/src/main/java/serene/simple/Main.java +++ b/src/main/java/serene/simple/Main.java @@ -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; * 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 \n" + "Serene(simple) comes with ABSOLUTELY NO WARRANTY;\n" + @@ -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 { } } + 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 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);