Skip to content

Commit

Permalink
demo: make it cleaner on mobile platforms
Browse files Browse the repository at this point in the history
Add a flag to the config so the mobile wrapper can report it's on
a mobile platform, and use this to give instructions for )demo on
mobile. Tell the user in that case to use the Demo button, which
works fine on Android but needs to be implemented on iOS,
but it should be easy.

Also refactor the demo runner a bit so that it prints instructions
from demo.ivy before accepting user input.
  • Loading branch information
robpike committed Oct 12, 2021
1 parent adacf8f commit 670bccd
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 11 deletions.
13 changes: 13 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type Config struct {
// Bases: 0 means C-like, base 10 with 07 for octal and 0xa for hex.
inputBase int
outputBase int
mobile bool // Running on a mobile platform.
}

func (c *Config) init() {
Expand All @@ -63,6 +64,7 @@ func (c *Config) init() {
c.maxDigits = 1e4
c.maxStack = 1e5
c.floatPrec = 256
c.mobile = false
}
}

Expand Down Expand Up @@ -312,3 +314,14 @@ func (c *Config) SetBase(inputBase, outputBase int) {
c.inputBase = inputBase
c.outputBase = outputBase
}

// Mobile reports whether we are running on a mobile platform.
func (c *Config) Mobile() bool {
return c.mobile
}

// SetMobile sets the Mobile bit as specified.
func (c *Config) SetMobile(mobile bool) {
c.init()
c.mobile = mobile
}
20 changes: 14 additions & 6 deletions demo/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,18 @@ func Text() string {
// line from the file is delivered to ivy. If the user's input line has text, that
// is delivered instead and the file does not advance.
func Run(userInput io.Reader, toIvy io.Writer, output io.Writer) error {
fmt.Println(`# Type a newline.`)
text := demoText // Don't overwrite the global!
scan := bufio.NewScanner(userInput)
nextLine := func() (line []byte) {
nl := bytes.IndexByte(text, '\n')
if nl < 0 { // EOF or incomplete line.
return nil
}
line, text = text[:nl+1], text[nl+1:]
return line
}
// Show first line, with instructions, before accepting user input.
output.Write(nextLine())
for scan.Scan() {
// User typed something; step back across the newline.
if len(scan.Bytes()) > 0 {
Expand All @@ -48,15 +57,14 @@ func Run(userInput io.Reader, toIvy io.Writer, output io.Writer) error {
}
} else {
// User typed newline; send next line of file's text.
nl := bytes.IndexByte(text, '\n')
if nl < 0 { // EOF or incomplete line.
line := nextLine()
if line == nil {
break
}
output.Write(text[:nl+1]) // Show the line from the file.
if _, err := toIvy.Write(text[:nl+1]); err != nil {
output.Write(line)
if _, err := toIvy.Write(line); err != nil {
return err
}
text = text[nl+1:]
}
}
return scan.Err()
Expand Down
4 changes: 2 additions & 2 deletions demo/demo.ivy
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This is a demo of ivy. Type a newline to advance to each new step.
# Type the word "quit" or EOF to end the demo and return to ivy.
# This is a demo of ivy. Type a newline to advance to each new step. Type one now.
# At any time, type the word "quit" or EOF to end the demo and return to ivy.
# Each step in the demo is one line of input followed by some output from ivy. Type a newline now to see.
2+2
# The first line you see above (2+2) is input; the next (4) is output from a running ivy.
Expand Down
3 changes: 2 additions & 1 deletion doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,8 @@ base 10 and must be non-negative on input.
Toggle or set the named debugging flag. With no argument, lists
the settings.
) demo
Run a line-by-line interactive demo. Requires a Go installation.
Run a line-by-line interactive demo. On mobile platforms,
use the Demo menu option instead.
) format ""
Set the format for printing values. If empty, the output is printed
using the output base. If non-empty, the format determines the
Expand Down
3 changes: 2 additions & 1 deletion mobile/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,8 @@ base 10 and must be non-negative on input.
Toggle or set the named debugging flag. With no argument, lists
the settings.
) demo
Run a line-by-line interactive demo. Requires a Go installation.
Run a line-by-line interactive demo. On mobile platforms,
use the Demo menu option instead.
) format &#34;&#34;
Set the format for printing values. If empty, the output is printed
using the output base. If non-empty, the format determines the
Expand Down
1 change: 1 addition & 0 deletions mobile/mobile.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func Eval(expr string) (result string, errors error) {

conf.SetOutput(stdout)
conf.SetErrOutput(stderr)
conf.SetMobile(true)

scanner := scan.New(context, " ", reader)
parser := parse.NewParser(" ", scanner, context)
Expand Down
3 changes: 2 additions & 1 deletion parse/help.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions parse/special.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ Switch:
}
case "demo":
p.need(scan.EOF)
if conf.Mobile() {
p.Printf("For a demo on mobile platforms, use the Demo button in the UI.\n")
break
}
// Use a default configuration.
var conf config.Config
err := demo.Run(os.Stdin, p.demoRunner(), conf.Output())
Expand Down

0 comments on commit 670bccd

Please sign in to comment.