Skip to content

Commit

Permalink
REPL improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Aug 25, 2024
1 parent 8a6bbe5 commit 695bb7d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
6 changes: 6 additions & 0 deletions convex-core/src/main/java/convex/core/Result.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ public Result withInfo(Keyword k, ACell v) {
return new Result(values.assoc(INFO_POS, info));
}


public Result withSource(Keyword source) {
return withInfo(Keywords.SOURCE, source);
}

/**
* Returns the log for this Result. May be an empty vector.
*
Expand Down Expand Up @@ -381,4 +386,5 @@ public static Result fromException(Throwable e) {




}
22 changes: 18 additions & 4 deletions convex-gui/src/main/java/convex/gui/panels/REPLPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import convex.core.transactions.ATransaction;
import convex.core.transactions.Invoke;
import convex.core.util.Utils;
import convex.gui.components.ActionButton;
import convex.gui.components.ActionPanel;
import convex.gui.components.BaseTextPane;
import convex.gui.components.CodePane;
Expand All @@ -61,6 +62,7 @@ public class REPLPanel extends JPanel {

protected final CodePane input;
protected final CodePane output;
private final JButton btnRun;
private final JButton btnClear;
private final JButton btnInfo;
private final JCheckBox btnResults;
Expand Down Expand Up @@ -186,10 +188,17 @@ public REPLPanel(Convex convex) {
actionPanel = new ActionPanel();
add(actionPanel, "dock south");

btnRun = new ActionButton("Run",0xe1c4,e -> {
sendMessage(input.getText());
input.requestFocus();
});
actionPanel.add(btnRun);

btnClear = new JButton("Clear");
actionPanel.add(btnClear);
btnClear.addActionListener(e -> {
output.setText("");
input.requestFocus();
});

btnInfo = new JButton("Connection Info");
Expand Down Expand Up @@ -253,14 +262,14 @@ private Address getAddress() {

private void sendMessage(String s) {
if (s.isBlank()) return;
output.append(s);
output.append("\n");

history.add(s);
historyPosition=history.size();

SwingUtilities.invokeLater(() -> {
input.setText("");
output.append(s);
output.append("\n");
try {
AList<ACell> forms = Reader.readAll(s);
ACell code = (forms.count()==1)?forms.get(0):forms.cons(Symbols.DO);
Expand Down Expand Up @@ -302,12 +311,17 @@ private void sendMessage(String s) {
}
log.trace("Sent message");

handleResult(start,future.get(5000, TimeUnit.MILLISECONDS));
handleResult(start,future.get(3000, TimeUnit.MILLISECONDS));
} catch (ParseException e) {
output.append(" PARSE ERROR: "+e.getMessage(),Color.RED);
} catch (TimeoutException t) {
output.append(" TIMEOUT waiting for result",Color.RED);
output.append(" TIMEOUT waiting for result\n",Color.RED);
} catch (IllegalStateException t) {
// General errors we understand
output.append(" ERROR: ",Color.RED);
output.append(t.getMessage() + "\n");
} catch (Exception t) {
// Something bad.....
output.append(" ERROR: ",Color.RED);
output.append(t.getMessage() + "\n");
t.printStackTrace();
Expand Down
11 changes: 6 additions & 5 deletions convex-peer/src/main/java/convex/api/Convex.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import convex.core.ErrorCodes;
import convex.core.Result;
import convex.core.SourceCodes;
import convex.core.State;
import convex.core.crypto.AKeyPair;
import convex.core.data.ABlob;
Expand Down Expand Up @@ -265,7 +266,7 @@ public long getSequence(Address addr) throws TimeoutException, IOException {
public long lookupSequence(Address origin) throws IOException, TimeoutException {
AOp<ACell> code= Special.forSymbol(Symbols.STAR_SEQUENCE);
Result r= querySync(code,origin);
if (r.isError()) throw new RuntimeException("Error trying to get sequence number: "+r);
if (r.isError()) throw new IOException("Error trying to get sequence number: "+r);
ACell rv=r.getValue();
if (!(rv instanceof CVMLong)) throw new RuntimeException("Unexpected sequence result type: "+Utils.getClassName(rv));
long seq=((CVMLong)rv).longValue();
Expand Down Expand Up @@ -371,7 +372,7 @@ private synchronized long getNextSequence(ATransaction t) throws IOException, Ti
* @throws IOException If an IO Exception occurs (most likely the connection is broken)
* @throws TimeoutException In case of timeout
*/
public final synchronized CompletableFuture<Result> transact(ATransaction transaction) throws IOException, TimeoutException {
public final synchronized CompletableFuture<Result> transact(ATransaction transaction) throws TimeoutException, IOException {
SignedData<ATransaction> signed = prepareTransaction(transaction);
CompletableFuture<Result> r= transact(signed);
return r;
Expand All @@ -388,7 +389,7 @@ public final synchronized CompletableFuture<Result> transact(ATransaction transa
* @throws IOException If an IO Exception occurs (most likely the connection is broken)
* @throws TimeoutException In case of timeout
*/
public SignedData<ATransaction> prepareTransaction(ATransaction transaction) throws TimeoutException, IOException {
public SignedData<ATransaction> prepareTransaction(ATransaction transaction) throws IOException, TimeoutException {
Address origin=transaction.getOrigin();
if (origin == null) {
origin=address;
Expand Down Expand Up @@ -496,7 +497,7 @@ public synchronized Result transactSync(String code) throws IOException, Timeout
* @return A Future for the result of the transaction
* @throws IOException If the connection is broken or send buffer is full
*/
public abstract CompletableFuture<Result> transact(SignedData<ATransaction> signed) throws IOException;
public abstract CompletableFuture<Result> transact(SignedData<ATransaction> signed);

/**
* Submits a transfer transaction to the Convex network, returning a future once
Expand Down Expand Up @@ -587,7 +588,7 @@ public synchronized Result transactSync(ATransaction transaction, long timeout)
return Result.fromException(e.getCause());
} catch (InterruptedException e) {
Thread.currentThread().interrupt(); // set interrupt flag since an interruption has occurred
return Result.error(ErrorCodes.INTERRUPTED, "Transaction interrupted while awaiting result");
return Result.error(ErrorCodes.INTERRUPTED, "Transaction interrupted while awaiting result").withSource(SourceCodes.CLIENT);
}
}

Expand Down

0 comments on commit 695bb7d

Please sign in to comment.