Created
October 18, 2016 19:23
-
-
Save QuantumFractal/d122c0c07dab6ddde83ddf764e7915df to your computer and use it in GitHub Desktop.
342 FuncLang Console
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
/** Replace the first two print() methods with this **/ | |
public void print(Value v) { | |
if(v.tostring() != "") | |
System.out.println("=> "+v.tostring()); | |
} | |
public void print(Exception e) { | |
System.out.println("Error: "+e.toString()); | |
} |
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
package funclang; | |
import java.io.BufferedReader; | |
import java.io.InputStreamReader; | |
import java.io.IOException; | |
import java.io.FileReader; | |
import funclang.AST.Program; | |
import funclang.parser.FuncLangLexer; | |
import funclang.parser.FuncLangParser; | |
public class Reader { | |
private final static String TAB = " "; | |
Program read() throws IOException { | |
String programText = readNextProgram(); | |
return parse(programText); | |
} | |
Program parse(String programText) { | |
FuncLangLexer l = new FuncLangLexer(new org.antlr.v4.runtime.ANTLRInputStream(programText)); | |
FuncLangParser p = new FuncLangParser(new org.antlr.v4.runtime.CommonTokenStream(l)); | |
Program program = p.program().ast; | |
return program; | |
} | |
static String readFile(String fileName) throws IOException { | |
try (BufferedReader br = new BufferedReader( | |
new FileReader(fileName))) { | |
StringBuilder sb = new StringBuilder(); | |
String line = br.readLine(); | |
while (line != null) { | |
sb.append(line); | |
sb.append(System.lineSeparator()); | |
line = br.readLine(); | |
} | |
return sb.toString(); | |
} | |
} | |
private String readNextProgram() throws IOException { | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
System.out.print(" > "); | |
String programText = br.readLine(); | |
boolean confirmed = false; | |
String indents = ""; | |
while (!checkParens(programText) && !confirmed) { | |
indents = genTabs(countOpenParens(programText)); | |
System.out.print(".. "+indents); | |
String line = br.readLine(); | |
programText += line; | |
if (line.trim().length() == 0){ | |
confirmed = true; | |
} | |
} | |
return runFile(programText); | |
} | |
private String genTabs(int num) { | |
StringBuilder sb = new StringBuilder(); | |
for (int i=0; i<num; i++) | |
sb.append(TAB); | |
return sb.toString(); | |
} | |
private boolean checkParens(String program) { | |
int count = 0; | |
for (char c: program.toCharArray()) { | |
if (c == '(') | |
count++; | |
if (c == ')') | |
count--; | |
if (count < 0) | |
return false; | |
} | |
return (count == 0); | |
} | |
private int countOpenParens(String program) { | |
int count = 0; | |
for (char c: program.toCharArray()){ | |
if (c == '(') | |
count++; | |
if (c == ')') | |
count--; | |
} | |
return count; | |
} | |
private String runFile(String programText) throws IOException { | |
if(programText.startsWith("run ")){ | |
programText = readFile("build/funclang/examples/" + programText.substring(4)); | |
} | |
return programText; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment