Skip to content

Commit

Permalink
GH-518 Fix REPL console
Browse files Browse the repository at this point in the history
  • Loading branch information
dzikoysk committed May 18, 2020
1 parent efdb5f3 commit 146696b
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 22 deletions.
15 changes: 10 additions & 5 deletions panda/src/main/java/org/panda_lang/panda/shell/repl/Repl.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.panda_lang.panda.shell.repl;

import io.vavr.control.Option;
import org.panda_lang.framework.FrameworkController;
import org.panda_lang.framework.PandaFrameworkException;
import org.panda_lang.framework.design.architecture.dynamic.Frame;
import org.panda_lang.framework.design.architecture.expression.Expression;
Expand Down Expand Up @@ -52,6 +51,7 @@
*/
public final class Repl {

private final ReplConsole console;
private final Context context;
private final ExpressionParser expressionParser;
private final Supplier<Process> processSupplier;
Expand All @@ -63,6 +63,7 @@ public final class Repl {
private StringBuilder history;

Repl(ReplCreator creator) throws Exception {
this.console = creator.console;
this.context = creator.context;
this.expressionParser = context.getComponent(Components.EXPRESSION);
this.processSupplier = creator.processSupplier;
Expand Down Expand Up @@ -105,7 +106,7 @@ public Collection<ReplResult> evaluate(String source) {
}

private Option<ReplResult> evaluateCommand(String command) {
String content = command.substring(1).toLowerCase().trim();
String content = command.toLowerCase().trim();

if (content.isEmpty()) {
content = "help";
Expand All @@ -128,6 +129,10 @@ private Option<ReplResult> evaluateCommand(String command) {
result += history.toString();
break;
}
case "exit": {
console.interrupt();
break;
}
case "?":
case "help": {
result += "Default help page of REPL. All the available commands: \n";
Expand Down Expand Up @@ -199,11 +204,11 @@ private ReplResult evaluateExpression(Snippet expressionSource) {
/**
* Initiate REPL creator
*
* @param frameworkController the framework controller that contains required resources
* @param
* @return a REPL creator instance
*/
public static ReplCreator creator(FrameworkController frameworkController) {
return new ReplCreator(frameworkController);
public static ReplCreator creator(ReplConsole console) {
return new ReplCreator(console);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.panda_lang.panda.shell.repl;

import org.panda_lang.framework.FrameworkController;
import org.panda_lang.panda.Panda;
import org.panda_lang.panda.PandaConstants;

Expand All @@ -25,31 +26,40 @@

public final class ReplConsole {

private final Panda panda;
private final FrameworkController controller;
private final InputStream input;
private final boolean simplified;
private boolean interrupted;

public ReplConsole(Panda panda, InputStream input, boolean simplified) {
this.panda = panda;
public ReplConsole(Panda controller, InputStream input, boolean simplified) {
this.controller = controller;
this.input = input;
this.simplified = simplified;
}

public void launch() throws Exception {
Scanner scanner = new Scanner(input);
panda.getLogger().info("Panda " + PandaConstants.VERSION + " REPL");
panda.getLogger().info("Type 'help' for more information.");
controller.getLogger().info("Panda " + PandaConstants.VERSION + " REPL");
controller.getLogger().info("Type 'help' for more information.");

Repl repl = Repl.creator(panda)
.withCustomExceptionListener(simplified ? (exception, runtime) -> panda.getLogger().error(exception.getMessage()) : null)
Repl repl = Repl.creator(this)
.withCustomExceptionListener(simplified ? (exception, runtime) -> controller.getLogger().error(exception.getMessage()) : null)
.create();

while (scanner.hasNextLine()) {
while (!interrupted && scanner.hasNextLine()) {
Collection<ReplResult> results = repl.evaluate(scanner.nextLine());
ReplUtils.print(panda, results);
ReplUtils.print(controller, results);
}

panda.getLogger().info("REPL has been terminated");
controller.getLogger().info("REPL has been terminated");
}

public void interrupt() {
this.interrupted = true;
}

public FrameworkController getFrameworkController() {
return controller;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.panda_lang.panda.shell.repl;

import org.jetbrains.annotations.Nullable;
import org.panda_lang.framework.FrameworkController;
import org.panda_lang.framework.design.architecture.module.Module;
import org.panda_lang.framework.design.architecture.type.State;
import org.panda_lang.framework.design.architecture.type.Type;
Expand Down Expand Up @@ -45,15 +44,17 @@
*/
public final class ReplCreator {

protected final ReplConsole console;
protected final Context context;
protected final TypeScope typeScope;
protected final ReplScope replScope;
protected ReplExceptionListener exceptionListener;
protected Supplier<Process> processSupplier;
protected ThrowingFunction<ProcessStack, Object, Exception> instanceSupplier;

ReplCreator(FrameworkController frameworkController) {
this.context = PandaContextUtils.createStubContext(frameworkController);
ReplCreator(ReplConsole console) {
this.console = console;
this.context = PandaContextUtils.createStubContext(console.getFrameworkController());

Module module = context.getComponent(Components.SCRIPT).getModule();
Type type = PandaType.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ void test() throws Exception {
PandaFactory pandaFactory = new PandaFactory();
Panda panda = pandaFactory.createPanda(LoggerFactory.getLogger(ReplTest.class));

ReplCreator creator = Repl.creator(panda);
ReplConsole console = new ReplConsole(panda, System.in, true);
ReplCreator creator = Repl.creator(console);
Context context = creator.getContext();

Repl repl = creator
Expand All @@ -55,11 +56,12 @@ void test() throws Exception {
ReplUtils.print(panda, repl.evaluate("i = 4"));

repl.regenerate();

ReplUtils.print(panda, repl.evaluate("i"));
ReplUtils.print(panda, repl.evaluate("String text = 'hello'; 'second expression'"));

ReplUtils.print(panda, repl.evaluate("? vars"));
ReplUtils.print(panda, repl.evaluate("? source"));
ReplUtils.print(panda, repl.evaluate("vars"));
ReplUtils.print(panda, repl.evaluate("history"));

ReplUtils.print(panda, repl.evaluate("Double double = 9.86960440109"));
ReplUtils.print(panda, repl.evaluate("sqrt(double)"));
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@
<version>3.2.0</version>
<executions>
<execution>
<id>attach-javadocs</id>
<!-- <id>attach-javadocs</id> -->
<goals>
<goal>jar</goal>
</goals>
Expand Down

0 comments on commit 146696b

Please sign in to comment.