-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.go
51 lines (44 loc) · 993 Bytes
/
input.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//go:build !js || !wasm
package main
import (
"bufio"
"fmt"
"os"
"reflect"
"regexp"
"strconv"
"strings"
)
// BASIC Version 3 Reference Manual (1-8, 7-9)
// https://archive.org/details/bitsavers_cdccyberlaBASICOct80_41102953/page/n23/mode/2up
// https://archive.org/details/bitsavers_cdccyberlaBASICOct80_41102953/page/n81/mode/2up
func input(i any) {
fmt.Print("? ")
r, err := bufio.NewReader(os.Stdin).ReadString('\n')
if err != nil {
r = ""
} else {
r = strings.Trim(regexp.MustCompile(`\r?\n`).ReplaceAllString(r, ""), " ")
}
e := reflect.ValueOf(i).Elem()
switch i.(type) {
case *string:
e.SetString(strings.ToUpper(strings.Trim(r, "\"")))
case *int:
o, err := strconv.ParseInt(r, 10, 64)
if err != nil {
fmt.Println(" ILLEGAL DATA, RETYPE INPUT")
input(i)
return
}
e.SetInt(o)
case *float64:
f, err := strconv.ParseFloat(r, 64)
if err != nil {
fmt.Println(" ILLEGAL DATA, RETYPE INPUT")
input(i)
return
}
e.SetFloat(f)
}
}