-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepl.go
71 lines (61 loc) · 1.19 KB
/
repl.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
const DBMSName = "dbiy"
const Version = "0.0.1"
func prompt() {
fmt.Printf("%v> ", DBMSName)
}
func get(r *bufio.Reader) string {
t, _ := r.ReadString('\n')
return strings.TrimSpace(t)
}
func isActive(text string) bool {
if strings.EqualFold("exit", text) {
fmt.Println("Goodbye!")
return false
}
return true
}
func getVersion() {
fmt.Printf("%v version %v \n", DBMSName, Version)
}
func getAbout() {
fmt.Printf("%v is a homemade DBMS \n", DBMSName)
}
func doInsert(object string) {
fmt.Printf("Insert (%v)\n", object)
}
func lookupCommand(text string) {
commands := map[string]interface{}{
"version": getVersion,
"about": getAbout,
"insert": doInsert,
}
command := strings.Fields(text)[0]
if cmd, exists := commands[command]; exists {
if command == "insert" {
object := strings.TrimLeft(text, "insert ")
doInsert(object)
} else {
cmd.(func())()
}
} else if text == "" {
// nothing
} else {
fmt.Printf("Command %v not found \n", text)
}
}
func main() {
reader := bufio.NewReader(os.Stdin)
prompt()
text := get(reader)
for ; isActive(text); text = get(reader) {
lookupCommand(text)
prompt()
}
}