Skip to content

Commit

Permalink
example/ivy/android: fix demo to handle comments & 'quit' correctly
Browse files Browse the repository at this point in the history
Demo feature is implemented by reading the demo.ivy file included
as an asset, read lines, and feed each line to mobile.Eval if it
is not a comment line. Comments can start from the middle of a line
so previously, the code simply looked for '#' in each line and
treated the substring starting from it as a comment. This assumption
is not correct - see ")format '%#x'". Without proper parsing, it is
hard to do it correctly. This CL passes the entire line to Ivy as
it is - the backend correctly parses the line with comments.
One drawback of this change is, unlike the line comments, we don't
apply the style for comments (gray font). But correctness is more
important. This also simplifies the code.

This CL implements the 'quit' command that terminates a demo
session (as mentioned in the demo.ivy script).

Change-Id: Ib0801338e0dd34c52c6c6209fe47a1846e71f0c8
Reviewed-on: https://go-review.googlesource.com/c/mobile/+/356732
Run-TryBot: Hyang-Ah Hana Kim <[email protected]>
TryBot-Result: Go Bot <[email protected]>
Trust: Hyang-Ah Hana Kim <[email protected]>
Reviewed-by: Hajime Hoshi <[email protected]>
  • Loading branch information
hyangah committed Oct 29, 2021
1 parent d263bad commit 93c5f8a
Showing 1 changed file with 11 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ private void callIvy() {
if (s != null && !s.isEmpty()) {
appendShowText(PROMPT + s, "expr");
}
if (mDemo != null && s.trim().equals("quit")) {
unloadDemo();
s = " "; // this will clear the text box.
}
new IvyCallTask().execute(s); // where call to Ivy backend occurs.
}

Expand Down Expand Up @@ -293,7 +297,7 @@ public void run() {
private class IvyCallTask extends AsyncTask<String, Void, Pair<String, String> > {
private String ivyEval(final String expr) {
try {
// org.golang.ivy.Mobile was generated using
// mobile.Mobile was generated using
// gomobile bind -javapkg=org.golang.ivy robpike.io/ivy/mobile
return Mobile.eval(expr); // Gobind-generated method.
} catch (Exception e) {
Expand All @@ -318,22 +322,15 @@ protected Pair<String, String> checkDemo() {
String showText = null;
while (true) {
String s = readDemo();
if (s == null) { return Pair.create(showText, null); }

int sharp = s.indexOf("#");
if (sharp < 0) {
return Pair.create(showText, s);
}
s += "\n";
if (showText == null) {
showText = s.substring(sharp, s.length());
} else {
showText += s.substring(sharp, s.length());
if (s == null) {
break;
}
if (sharp > 0) {
return Pair.create(s.substring(sharp, s.length()), s.substring(0, sharp));
if (s.startsWith("# ")) {
return Pair.create(s, null);
}
return Pair.create(null, s);
}
return null;
}

@Override
Expand All @@ -355,7 +352,6 @@ public void run() {
}
}
});

}
}
}

0 comments on commit 93c5f8a

Please sign in to comment.