added interactive command prompt

This commit is contained in:
User 2017-07-22 15:47:16 +02:00
parent 87b859a70c
commit f9110893c6
4 changed files with 100 additions and 1 deletions

View File

@ -22,5 +22,10 @@
<attribute name="javadoc_location" value="https://commons.apache.org/proper/commons-lang/javadocs/api-3.5/"/> <attribute name="javadoc_location" value="https://commons.apache.org/proper/commons-lang/javadocs/api-3.5/"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="lib" path="lib/commons-cli-1.4.jar">
<attributes>
<attribute name="javadoc_location" value="http://commons.apache.org/proper/commons-cli/javadocs/api-release/"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="bin"/> <classpathentry kind="output" path="bin"/>
</classpath> </classpath>

BIN
lib/commons-cli-1.4.jar Normal file

Binary file not shown.

View File

@ -0,0 +1,93 @@
package pp.s1184725.boppi.util;
import java.util.Scanner;
import java.util.logging.Logger;
import org.apache.commons.cli.*;
import pp.iloc.model.Program;
import pp.s1184725.boppi.ToolChain;
/**
* Provides an interactive command line for Boppi.
*
* @author Frank Wibbelink
*/
public class InteractivePrompt {
private static CommandLine cmd;
private static Scanner sc;
/**
* Runs the interactive command line.
*
* @param args
* unused
*/
public static void main(String[] args) {
Options options = new Options();
options.addOption("s", "stateless", false, "run stateless (every command will run in a fresh environment)");
options.addOption("n", "empty-line", false, "keep reading a single command until the user enters an empty line");
options.addOption("h", "help", false, "display this message");
options.addOption("p", "print-program", false, "prints the program before executing it");
options.addOption("D", "debug", false, "runs a program in debug mode");
options.addOption("k", "keep", false, "appends every new command to previous input (implies --stateless)");
try {
cmd = new DefaultParser().parse(options, args);
}
catch (ParseException e) {
System.err.println(e.getLocalizedMessage());
return;
}
if (cmd.hasOption("h")) {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("boppi", "", options, "Exit a program by typing 'exit' on a line.");
return;
}
sc = new Scanner(System.in);
if (cmd.hasOption("n"))
sc.useDelimiter("\r?\n\r?\n");
else
sc.useDelimiter("\r?\n");
if (cmd.hasOption("s") || cmd.hasOption("k"))
stateless();
else
System.err.println("Stateful interactive mode is not supported.");
sc.close();
}
private static void stateless() {
Logger logger = Logger.getAnonymousLogger();
String saved = "";
while (sc.hasNext()) {
String command = sc.next().trim();
if(command.equals(""))
continue;
if (command.equals("exit"))
break;
String code = saved+command;
if (cmd.hasOption("k"))
saved = code+";\n";
Program program = ToolChain.compile(ToolChain.getCharStream(code), logger);
if (cmd.hasOption("p")) {
for (String line : program.prettyPrint().split("\n"))
System.out.println("--- "+line);
}
for (String str : ToolChain.execute(program, logger, "").split("\n"))
System.out.println("<<< "+str);
}
}
}

View File

@ -1,12 +1,13 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project default="javadoc"> <project default="javadoc">
<target name="javadoc"> <target name="javadoc">
<javadoc access="public" author="true" classpath="../lib/commons-lang3-3.5.jar;../lib/antlr-4.7-complete.jar;../lib/junit-4.12.jar;../lib/hamcrest-all-1.3.jar" destdir="../doc/javadoc" nodeprecated="false" nodeprecatedlist="false" noindex="false" nonavbar="false" notree="false" packagenames="pp.*" source="1.8" sourcepath="../src" splitindex="false" use="true" version="true"> <javadoc access="public" author="true" classpath="../lib/commons-lang3-3.5.jar;../lib/commons-cli-1.4.jar;../lib/antlr-4.7-complete.jar;../lib/junit-4.12.jar;../lib/hamcrest-all-1.3.jar" destdir="../doc/javadoc" nodeprecated="false" nodeprecatedlist="false" noindex="false" nonavbar="false" notree="false" packagenames="pp.*" source="1.8" sourcepath="../src" splitindex="false" use="true" version="true">
<link href="http://www.antlr.org/api/Java/"/> <link href="http://www.antlr.org/api/Java/"/>
<link href="http://hamcrest.org/JavaHamcrest/javadoc/1.3/"/> <link href="http://hamcrest.org/JavaHamcrest/javadoc/1.3/"/>
<link href="https://docs.oracle.com/javase/8/docs/api/"/> <link href="https://docs.oracle.com/javase/8/docs/api/"/>
<link href="http://junit.org/junit4/javadoc/4.12/"/> <link href="http://junit.org/junit4/javadoc/4.12/"/>
<link href="https://commons.apache.org/proper/commons-lang/javadocs/api-3.5/"/> <link href="https://commons.apache.org/proper/commons-lang/javadocs/api-3.5/"/>
<link href="http://commons.apache.org/proper/commons-cli/javadocs/api-release/"/>
</javadoc> </javadoc>
</target> </target>
</project> </project>