package com.lousylynx.asciidots;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.nio.file.Paths;

public class Interpreter {

public static void main(String[] args) throws IOException {
        System.out.println();
        Interpreter interpreter = new Interpreter(".-$#", ".");
}

public Interpreter(String program, String programDir) throws IOException {
        String path = Paths.get(".").toAbsolutePath().normalize().toString();
        String path_to_python3 = "/usr/local/bin/python3";

        Process process = Runtime.getRuntime ().exec (new String[] {
                        path_to_python3,
                        path+"/interpreter.py"
                });

        Reader stdOut = new InputStreamReader (process.getInputStream (), "UTF-8");
        OutputStream stdIn = process.getOutputStream ();


        char[] passRequest = new char[1024];
        int len = 0;
        while (true)
        {
                int chars_read = stdOut.read (passRequest, len, passRequest.length - len);

                if (chars_read == -1) {
                        break;
                }

                len += chars_read;

                if (new String (passRequest, len-chars_read, len).contains ("?: ")) {
                        stdIn.write("6\n".getBytes ("utf-8"));
                        stdIn.flush();
                } else if (new String (passRequest, len-chars_read, len).contains ("$ ")) {
                        stdIn.write((program+"\n%EOF\n").getBytes("utf-8"));
                        stdIn.flush();
                } else {
                        System.out.print(new String (passRequest, len-chars_read, len));
                }

                if (len > 512) {
                        passRequest = new char[1024];
                        len = 0;
                }
        }

        process.destroy();
}
}

@@ -198,16 +198,20 @@
 eter %7B%0A%0A
+    
 public s
@@ -356,19 +356,27 @@
 , %22.%22);%0A
-%7D%0A%0A
+    %7D%0A%0A    
 public I
@@ -539,27 +539,24 @@
 ing path
-_to_p
+ToP
 ython
-3
  = %22/usr
@@ -625,17 +625,15 @@
 time
- 
 ().exec
- 
 (new
@@ -641,17 +641,16 @@
 String%5B%5D
- 
 %7B%0A      
@@ -663,31 +663,20 @@
     
-       
- 
 path
-_to_p
+ToP
 ython
-3
 ,%0A  
@@ -693,21 +693,15 @@
     
-     
-   
 path
-+
+ + 
 %22/in
@@ -722,24 +722,16 @@
         
-       
- 
 %7D);%0A%0A   
@@ -772,17 +772,16 @@
 amReader
- 
 (process
@@ -795,17 +795,16 @@
 utStream
- 
 (), %22UTF
@@ -861,17 +861,16 @@
 utStream
- 
 ();%0A%0A%0A  
@@ -953,24 +953,16 @@
 e (true)
-%0A       
  %7B%0A     
@@ -964,28 +964,24 @@
             
- 
-   
 int chars_re
@@ -973,26 +973,25 @@
    int chars
-_r
+R
 ead = stdOut
@@ -995,17 +995,16 @@
 Out.read
- 
 (passReq
@@ -1050,28 +1050,24 @@
             
-    
 if (chars_re
@@ -1063,18 +1063,17 @@
 f (chars
-_r
+R
 ead == -
@@ -1093,24 +1093,16 @@
         
-        
 break;%0A 
@@ -1108,35 +1108,27 @@
             
-   
- 
 %7D%0A%0A
-    
             
@@ -1143,20 +1143,15 @@
 hars
-_r
+R
 ead;%0A%0A
-    
     
@@ -1164,33 +1164,32 @@
   if (new String
- 
 (passRequest, le
@@ -1185,32 +1185,33 @@
 Request, len
--
+ - 
 chars
-_r
+R
 ead, len).co
@@ -1216,17 +1216,16 @@
 contains
- 
 (%22?: %22))
@@ -1239,32 +1239,24 @@
             
-      
-  
 stdIn.write(
@@ -1269,17 +1269,16 @@
 getBytes
- 
 (%22utf-8%22
@@ -1293,32 +1293,24 @@
             
-   
-     
 stdIn.flush(
@@ -1320,28 +1320,24 @@
             
- 
-   
 %7D else if (n
@@ -1337,33 +1337,32 @@
 e if (new String
- 
 (passRequest, le
@@ -1358,32 +1358,33 @@
 Request, len
--
+ - 
 chars
-_r
+R
 ead, len).co
@@ -1389,17 +1389,16 @@
 contains
- 
 (%22$ %22)) 
@@ -1411,32 +1411,24 @@
             
-   
-     
 stdIn.write(
@@ -1435,17 +1435,19 @@
 (program
-+
+ + 
 %22%5Cn%25EOF%5C
@@ -1482,32 +1482,24 @@
             
-     
-   
 stdIn.flush(
@@ -1509,28 +1509,24 @@
             
-    
 %7D else %7B%0A   
@@ -1538,24 +1538,16 @@
         
-      
-  
 System.o
@@ -1565,17 +1565,16 @@
 w String
- 
 (passReq
@@ -1586,16 +1586,17 @@
  len
--
+ - 
 chars
-_r
+R
 ead,
@@ -1599,28 +1599,24 @@
 ead, len));%0A
-    
             
@@ -1626,28 +1626,24 @@
             
-    
 if (len %3E 51
@@ -1643,32 +1643,24 @@
 en %3E 512) %7B%0A
-        
             
@@ -1705,32 +1705,24 @@
             
-        
 len = 0;%0A   
@@ -1714,28 +1714,24 @@
    len = 0;%0A
-    
             
@@ -1770,12 +1770,16 @@
 troy();%0A
+    
 %7D%0A%7D%0A

Update formatting