-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor logger and enable/disable log trace
- Loading branch information
1 parent
90d55db
commit b9432e0
Showing
12 changed files
with
92 additions
and
36 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,66 @@ | ||
package fr.imt.parser; | ||
|
||
import fr.imt.logger.Color; | ||
import fr.imt.logger.Logger; | ||
|
||
import java.util.Scanner; | ||
|
||
public class Repl<T> { | ||
|
||
private final Parsable<T> parser; | ||
private final ReplProcessable<T> processor; | ||
private boolean showLog; | ||
|
||
public Repl(Parsable<T> parser, ReplProcessable<T> processor) { | ||
this.parser = parser; | ||
this.processor = processor; | ||
this.showLog = true; | ||
} | ||
|
||
public void run() { | ||
Scanner scanner = new Scanner(System.in); | ||
|
||
System.out.println("Hindley Milner Type System"); | ||
showHelp(); | ||
|
||
while (true) { | ||
System.out.print("> "); | ||
String line = scanner.nextLine().trim(); | ||
|
||
// TODO: implement a set of commands | ||
// TODO: refactor by creating a command handler service | ||
if (line.equals(":q")) { | ||
break; | ||
} else if (line.equals(":log")) { | ||
this.showLog = !this.showLog; | ||
System.out.println(Color.grey("Log " + (this.showLog ? "enabled" : "disabled"))); | ||
} else if (line.equals(":h")) { | ||
showHelp(); | ||
} else { | ||
process(line); | ||
} | ||
} | ||
|
||
String res = parser.parse(line) | ||
.fold( | ||
parseError -> Color.red("Parsing error : " + parseError), | ||
processor::process | ||
); | ||
System.out.println(Color.grey("Bye!")); | ||
} | ||
|
||
System.out.println(res); | ||
} | ||
private void process(String line) { | ||
String result = parser.parse(line) | ||
.fold( | ||
parseError -> Color.red("Parsing error : " + parseError), | ||
processor::process | ||
); | ||
|
||
if (this.showLog) System.out.print(Logger.instance); | ||
|
||
System.out.println(Color.purple(result)); | ||
} | ||
|
||
System.out.println("Bye!"); | ||
private void showHelp() { | ||
System.out.println("\nCommands:"); | ||
System.out.println("\t\t- " + Color.green(":log") + "\t: enable/disable logs"); | ||
System.out.println("\t\t- " + Color.green(":h") + "\t: help"); | ||
System.out.println("\t\t- " + Color.green(":q") + "\t: quit"); | ||
System.out.println(); | ||
} | ||
|
||
} |