Skip to content

Commit

Permalink
Merge pull request #45 from EDEN786/Development : 1.2.2 - User Comman…
Browse files Browse the repository at this point in the history
…d Input

1.2.2 - User Command Input
  • Loading branch information
PandaEden authored Apr 27, 2021
2 parents e415efd + 44fa199 commit c8b7d01
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 6 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,33 @@ If no path is specified it defaults to ***"FileInput.s"*** in the present-workin
- (the default WSL terminal does not support Anscii double-underline. Windows Terminal does)

## Change Log

#### Ver1.2.2 - Configurable Number of Cycles Execution, and Run with new File

- Application will now pause before running execution and ask the user how many cycles to execute.

- The default is 1:

- if they press \<Enter>.
- input a value <1.
- input a value that is not recognized.

- They can also type 'end' and it will run 1000 cycles.


- Application will now pause before exiting and ask the User If they want to run a new file.

- \<Enter> or 'exit' will exit the application.

- 're', or 're-run'|'rerun', Will run the same file again.

- Anything else, it will try to Load, Parse & Run.


#### Ver1.2.1 - RanOutOfMemory Patch

- Output was being stored and only printed at the end of execution.

- This meant if a user ran a log of instructions. Or a very large /infinite loop. Java would run out of memory before printing the execution.
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ compileJava {
options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
}
group 'github.com/EDEN786/MIPS-Teaching-Tool'
version '1.2.1'
version '1.2.2'

repositories {
mavenCentral()
Expand Down
46 changes: 42 additions & 4 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import setup.Parser;

import util.Util;
import util.ansi_codes.Color;
import util.logs.ErrorLog;
import util.logs.ExecutionLog;
Expand All @@ -15,6 +16,8 @@
import java.util.ArrayList;

public class Main {
private static final String ENTER ="'" + Color.fmtCmd( "ENTER" ) + "'";

public static void main(String[] args) {
//Disable Colour for Windows Terminals
if ( System.console( )!=null && System.getenv( ).get( "TERM" )==null )
Expand All @@ -23,9 +26,24 @@ public static void main(String[] args) {
final ErrorLog errorLog=new ErrorLog( new ArrayList<>( ) );
final WarningsLog warningsLog=new WarningsLog( new ArrayList<>( ) );
final MemoryBuilder MEMORY_BUILDER=new MemoryBuilder( errorLog, warningsLog );
final String path=(args.length>0)?args[0]:"";
String path=(args.length>0)?args[0]:"";

run( errorLog, warningsLog, MEMORY_BUILDER, path );
if ( Util.wait ) {
final String FILENAME = Color.fmtCmd("Path\\FileName");
boolean exit=false;
while ( !exit ){
String newpath=Util.input( "\nEnter another "+FILENAME+" to Run again,\n\tOr Press "+ENTER+" to Close the Application..." );
String temp = newpath.toLowerCase();

if ( !(Util.isNullOrBlank( newpath )||temp.equals( "exit" )) ) {
if ( !(temp.equals( "re" ) || temp.equals( "re-run" ) || temp.equals( "rerun" ) ))
path=newpath;
run( errorLog, warningsLog, MEMORY_BUILDER, path );
}else
exit=true;
}
}
}

public static void run(final ErrorLog errorLog, final WarningsLog warningsLog, final MemoryBuilder Memory, String path){
Expand All @@ -38,7 +56,7 @@ public static void run(final ErrorLog errorLog, final WarningsLog warningsLog, f
// Assemble
ArrayList<Instruction> instructions=Memory.assembleInstr( errorLog );
if ( instructions!=null ) {
System.out.println( "Assembly Complete!" );
System.out.println( "Assembly Complete!\n" );
//Execution
// Setup Components
ExecutionLog executionLog=new ExecutionLog( new ArrayList<>( ) );
Expand All @@ -48,10 +66,30 @@ public static void run(final ErrorLog errorLog, final WarningsLog warningsLog, f
Execution ex = new Execution( executionLog,errorLog, dm, rb, instructions );

boolean exit = false;

int n=1;
final String NUMBER = Color.fmtCmd("Number");
while ( !exit ){
if ( Util.wait ) {
String line = Util.input( "Enter a "+NUMBER+" to change the number of cycles to run"
+"\n\tPress "+ENTER+" to Run " +((n==1)?"a Cycle":"the next "+n+" Cycles")+". . .");
// Attempt to find Integer
try {
if ( line.toLowerCase().equals( "end" ) )
n=1000;
else {
n=Integer.parseInt( line );
if ( n<1 )
n=1;
else if ( n>1000 )
n=1000;
}
}catch ( NumberFormatException e ){
// n=1
}
}

StringBuilder out = new StringBuilder();
exit = (ex.runStep( out )==null);
exit = (ex.runSteps( out, n )==null);
System.out.print( out );
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/control/Execution.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import model.instr.Nop;

import util.Convert;
import util.Util;
import util.ansi_codes.Color;
import util.logs.ErrorLog;
import util.logs.ExecutionLog;
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import org.jetbrains.annotations.Nullable;

import java.util.Scanner;

public class Util {
public static boolean wait=true;

/** Min and Max Inclusive */
public static boolean notNullAndInRange (Integer val, int min, int max) {
Expand All @@ -13,4 +16,37 @@ public static boolean notNullAndInRange (Integer val, int min, int max) {
}

public static boolean isNullOrBlank (@Nullable String s){ return (s==null || s.isBlank()); }

public static String input(@Nullable String msg){
if (msg==null){
msg = "Press ENTER to continue . . .";
}
return Input.getInstance().getInput( msg );
}
public static String input(){
return input( null );
}

}
class Input {
private static Scanner scanner;
private static Input input=null;
private Input (Scanner scanner ) {
Input.scanner=scanner;
}

public static Input getInstance ( ) {
if ( input==null )
input=new Input( new Scanner( System.in ) );

return input;
}

public String getInput(String prompt){
System.out.println( prompt );
while ( !scanner.hasNextLine() ){
scanner.nextLine();
}
return scanner.nextLine();
}
}
3 changes: 3 additions & 0 deletions src/main/java/util/ansi_codes/Color.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public static String fmtTitle(int color, String txt){
public static String fmtSubTitle(int color, String txt){
return fmt(underline(csi(color+";"+(bkg(BLACK)))), txt );
}
public static String fmtCmd(String txt){
return fmt( csi("107;40;4"), txt );
}
public static String fmtUnder(String txt){
return Color.fmt(Color.underline( Color.RESET ), txt );
}
Expand Down
7 changes: 6 additions & 1 deletion src/test/java/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import _test.TestSysOut;
import org.junit.jupiter.api.*;

import util.Util;
import util.logs.ErrorLog;
import util.logs.ExecutionLog;

Expand All @@ -15,12 +16,16 @@
class MainTest {
private static final String TEST_RESOURCES_DIR="src" + File.separator + "test" + File.separator + "resources" + File.separator;
private static final String PARSE_COMPLETE = "Parsing Complete!\n";
private static final String ASSEMBLE_COMPLETE = "Assembly Complete!\n";
private static final String ASSEMBLE_COMPLETE = "Assembly Complete!\n\n";
private static final String EX_COMPLETE = "\nExecution Complete!\n";
private static final String END_WITH_ERRORS = "\nExecution Ended With Errors!\n";

private static TestSysOut sysOut;

@BeforeAll
static void beforeAll ( ) {
Util.wait=false;
}
@BeforeEach
void setUp ( ) {
sysOut = new TestSysOut();
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/control/ExecuteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import model.components.RegisterBank;
import model.instr.*;

import util.Util;
import util.logs.ExecutionLog;

import java.util.ArrayList;
Expand All @@ -28,6 +29,7 @@ class ExecuteTest {

@BeforeAll
static void beforeAll ( ) {
Util.wait=false;
testLogs= new TestLogs();
actual = testLogs.actualExecution;
}
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/setup/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import model.components.RegisterBank;

import util.Convert;
import util.Util;
import util.logs.ErrorLog;
import util.logs.ExecutionLog;

Expand All @@ -38,6 +39,7 @@ class ParserTest {

@BeforeAll
static void beforeAll ( ) {
Util.wait=false;
testLogs=new TestLogs( );
expected=testLogs.expectedErrors;

Expand Down

0 comments on commit c8b7d01

Please sign in to comment.