2
0
mirror of synced 2025-02-24 07:18:15 +00:00

example/ivy/android: fix demo to handle comments & 'quit' correctly

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 <hyangah@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Hyang-Ah Hana Kim <hyangah@gmail.com>
Reviewed-by: Hajime Hoshi <hajimehoshi@gmail.com>
This commit is contained in:
Hana (Hyang-Ah) Kim 2021-10-18 20:45:13 -04:00 committed by Hyang-Ah Hana Kim
parent d263badac8
commit 93c5f8af85

View File

@ -244,6 +244,10 @@ public class MainActivity extends AppCompatActivity {
if (s != null && !s.isEmpty()) { if (s != null && !s.isEmpty()) {
appendShowText(PROMPT + s, "expr"); 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. new IvyCallTask().execute(s); // where call to Ivy backend occurs.
} }
@ -293,7 +297,7 @@ public class MainActivity extends AppCompatActivity {
private class IvyCallTask extends AsyncTask<String, Void, Pair<String, String> > { private class IvyCallTask extends AsyncTask<String, Void, Pair<String, String> > {
private String ivyEval(final String expr) { private String ivyEval(final String expr) {
try { try {
// org.golang.ivy.Mobile was generated using // mobile.Mobile was generated using
// gomobile bind -javapkg=org.golang.ivy robpike.io/ivy/mobile // gomobile bind -javapkg=org.golang.ivy robpike.io/ivy/mobile
return Mobile.eval(expr); // Gobind-generated method. return Mobile.eval(expr); // Gobind-generated method.
} catch (Exception e) { } catch (Exception e) {
@ -318,22 +322,15 @@ public class MainActivity extends AppCompatActivity {
String showText = null; String showText = null;
while (true) { while (true) {
String s = readDemo(); String s = readDemo();
if (s == null) { return Pair.create(showText, null); } if (s == null) {
break;
int sharp = s.indexOf("#");
if (sharp < 0) {
return Pair.create(showText, s);
} }
s += "\n"; if (s.startsWith("# ")) {
if (showText == null) { return Pair.create(s, null);
showText = s.substring(sharp, s.length());
} else {
showText += s.substring(sharp, s.length());
}
if (sharp > 0) {
return Pair.create(s.substring(sharp, s.length()), s.substring(0, sharp));
} }
return Pair.create(null, s);
} }
return null;
} }
@Override @Override
@ -355,7 +352,6 @@ public class MainActivity extends AppCompatActivity {
} }
} }
}); });
} }
} }
} }