Commit 2f6da3c0 authored by Mart Lubbers's avatar Mart Lubbers
Browse files

add --haltonerror and set returncode when parsing fails

parent 20dd5e3e
Pipeline #48466 passed with stages
in 29 seconds
......@@ -18,5 +18,3 @@ test:
- unzip ssm.zip
script:
- "for test in tests/*.ssm; do java -jar ssm/ssm.jar --cli --file $test; done"
......@@ -5,4 +5,4 @@ test:
- curl -LSs https://gitlab.science.ru.nl/compilerconstruction/ssm/builds/artifacts/master/raw/ssm.zip?job=build > ssm.zip
- unzip ssm.zip
script:
- "for test in tests/*.ssm; do java -jar ssm/ssm.jar --cli --file $test; done"
- "for test in tests/*.ssm; do java -jar ssm/ssm.jar --cli --haltonerror --file $test; done"
......@@ -180,7 +180,7 @@ public class MachineState extends Model
{
return isHalted ;
}
public String toString()
{
return "state code=" + code + " instr-pc=" + instrPC + " n-inl=" + nInlineOpnds ;
......
......@@ -49,7 +49,7 @@ public class Memory
boolean res ;
if ( res = ( addr < 0 || addr >= cells.length ) )
{
messenger.println
messenger.error
( java.text.MessageFormat.format
( "attempt to access location {0} outside {1}"
, new Object[] { Utils.asHex(addr), ""+this }
......
......@@ -12,6 +12,8 @@ public interface Messenger
{
public void print(String s);
public void println(String s);
public void error(String s);
/**
* Asks the user for an integer value (i.e. via a dialog)
* @return Returns the integer value as provided by the user
......@@ -30,4 +32,4 @@ public interface Messenger
*/
public int[] promptCharArray();
}
\ No newline at end of file
}
......@@ -106,7 +106,7 @@ public class Registers extends AbstractMemoryCellModel
boolean res ;
if ( res = ( r < 0 || r >= cells.length ) )
{
messenger.println
messenger.error
( java.text.MessageFormat.format
( "attempt to access nonexisting register {0,number,integer}"
, new Object[] { r }
......
......@@ -11,21 +11,22 @@ public class CliRunner implements Messenger {
private static final long STEPS_INFINITE = -1;
private long steps;
private boolean haltonerror;
private MachineState machineState = new MachineState(5000, 2000, this);
private Machine machine = new Machine(machineState, this);
private StepManager stepManager = new StepManager(machine, false);
private CodeTableModel codeTableModel= new CodeTableModel(null, machineState);
public CliRunner(long steps) {
public CliRunner(long steps, boolean haltonerror) {
this.steps = steps;
this.haltonerror = haltonerror;
}
public void run() {
long count = 0;
while (count != steps) {
if(doAStepForward()) {
if(doAStepForward())
return;
}
if(count != STEPS_INFINITE) {
count ++;
......@@ -64,6 +65,7 @@ public class CliRunner implements Messenger {
public void load( Reader r )
{
boolean error = false;
String msg ;
try
{
......@@ -75,14 +77,18 @@ public class CliRunner implements Messenger {
for ( apr = null ; ! ap.isAtEOF() ; )
{
apr = ap.parse1Line( apr ) ;
if ( apr.message != null )
if ( apr.message != null ) {
println( "Line " + apr.lineNr + ": " + apr.message ) ;
error = true;
}
else if ( apr.instrNArgs.size() > 0 )
{
leftOverLabels = new Vector<String>() ;
msg = codeTableModel.enterParsedLine( apr.definedLabels, apr.instrNArgs, leftOverLabels ) ;
if ( msg != null )
if ( msg != null ) {
println( "Line " + apr.lineNr + ": " + msg ) ;
error = true;
}
if ( leftOverLabels.size() == 0 )
apr = null ;
else
......@@ -93,13 +99,18 @@ public class CliRunner implements Messenger {
catch ( Exception ex )
{
ex.printStackTrace() ;
error = true;
}
finally
{
msg = codeTableModel.parseFinalize() ;
if ( msg != null )
if ( msg != null ) {
println( msg ) ;
error = true;
}
}
if (error)
System.exit(1);
resetToInitialState() ;
}
......@@ -113,6 +124,13 @@ public class CliRunner implements Messenger {
System.out.print(s);
}
@Override
public void error(String s) {
System.err.print(s + System.lineSeparator());
if (this.haltonerror)
System.exit(1);
}
@Override
public int promptInt() {
System.out.print("Please enter an integer: ");
......
......@@ -52,13 +52,14 @@ public class Runner extends Thread {
public static void usage() {
System.out.println("Simple Stack Machine Interpreter");
System.out.println("Version " + Config.version() + ", " + Config.versionDate());
System.out.println("usage: [--clisteps <steps>] [--cli] [--file <path> OR --stdin]");
System.out.println("usage: [--clisteps <steps>] [--haltonerror] [--cli] [--file <path> OR --stdin]");
System.out.println(" --help : Print this help");
System.out.println(" --version : Print version");
System.out.println(" --clisteps <steps> : The amount of steps to run. -1 for infinite(default). Only in cli mode");
System.out.println(" --stdin : Read code from stdin");
System.out.println(" --file <path> : Read code from path");
System.out.println(" --cli : No GUI, runs code and exits on halt");
System.out.println(" --haltonerror : Halt on error. Only in cli mode");
System.out.println(" --guidelay : Amount of time to sleep in milliseconds between steps in the GUI. Default: 50");
System.exit(1);
}
......@@ -89,6 +90,7 @@ public class Runner extends Thread {
long steps = -1;
boolean stdin = false;
boolean cli = false;
boolean haltonerror = false;
int guiDelay = 50;
for (int i = 0; i< args.length; i++) {
......@@ -124,6 +126,9 @@ public class Runner extends Thread {
case "--cli":
cli=true;
break;
case "--haltonerror":
haltonerror=true;
break;
case "--guidelay":
i++;
guiDelay = Integer.parseInt(args[i]);
......@@ -139,29 +144,29 @@ public class Runner extends Thread {
}
if (cli) {
CliRunner cliRunner = new CliRunner(steps);
if (stdin) {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
cliRunner.load(reader);
reader.close();
} else {
if (!stdin && initialFile == null) {
System.err.println("Need some input in CLI mode");
}
FileReader fr = new FileReader( initialFile ) ;
cliRunner.load(fr);
fr.close();
CliRunner cliRunner = new CliRunner(steps, haltonerror);
if (stdin) {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
cliRunner.load(reader);
reader.close();
} else {
if (!stdin && initialFile == null) {
System.err.println("Need some input in CLI mode");
}
FileReader fr = new FileReader( initialFile ) ;
cliRunner.load(fr);
fr.close();
}
cliRunner.run();
} else {
Runner r = new Runner(guiDelay);
if(stdin) {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
r.loadReader(reader);
reader.close();
} else if (initialFile != null) {
r.loadFile(initialFile);
}
}
} else {
Runner r = new Runner(guiDelay);
if(stdin) {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
r.loadReader(reader);
reader.close();
} else if (initialFile != null) {
r.loadFile(initialFile);
}
}
}
}
......@@ -639,6 +639,11 @@ public class SSMRunner extends JFrame
outputTextArea.append( s ) ;
}
public void error( String s )
{
outputTextArea.append( s ) ;
}
public int promptInt()
{
while(true)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment