-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
152 lines (133 loc) · 2.75 KB
/
main.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
type stackElement interface {
String() string
Equals(stackElement) bool
}
type stack struct {
elements []stackElement
}
type intElement int32
// type stringElement string
func (i intElement) String() string {
return fmt.Sprintf("%d", i)
}
func (i intElement) Equals(e stackElement) bool {
switch t := e.(type) {
case intElement:
return i == t
}
return false
}
// func (s stringElement) String() string {
// return string(s)
// }
// func (s stringElement) Equals(e stackElement) bool {
// switch t := e.(type) {
// case stringElement:
// return s == t
// }
// return false
// }
type dictionary map[string]func(*stack) (*stack, string, error)
type interpreter struct {
s *stack
dict dictionary
}
func newInterpreter() *interpreter {
return &interpreter{
s: &stack{},
dict: initialDict, // builtin.go
}
}
const True = intElement(-1)
const False = intElement(0)
func (s *stack) String() string {
ret := ""
// reverse order so top is at top:
for i := len(s.elements) - 1; i >= 0; i-- {
ret += fmt.Sprintf("\t%v\n", s.elements[i])
}
return ret
}
func (s *stack) push(e stackElement) {
s.elements = append(s.elements, e)
}
func (s *stack) pop() (stackElement, error) {
if len(s.elements) == 0 {
return nil, fmt.Errorf("empty stack")
}
l := len(s.elements)
e := s.elements[l-1]
s.elements = s.elements[:l-1]
return e, nil
}
func parse(input string) []string {
halves := strings.Split(input, "\\")
// discard comments:
nonComment := halves[0]
trimmed := strings.Trim(nonComment, " \t\n")
return strings.Split(trimmed, " ")
}
func (i *interpreter) handleInputLine(input string) (string, error) {
ret := ""
words := parse(input)
var err error
var out string
for _, word := range words {
if word == "" {
continue
}
// Is it in the dictionary?
if f, ok := i.dict[word]; ok {
i.s, out, err = f(i.s)
ret += out
if err != nil {
return ret, err
}
continue
}
// Not in the dictionary? Try to parse it
// as an int (FIXME: add floats, strings, ...)
if intVal, err := strconv.Atoi(word); err == nil {
i.s.push(intElement(intVal))
continue
}
// Out of options:
return ret, fmt.Errorf("unknown word: %v", word)
}
return ret, nil
}
func main() {
// print a preamble
fmt.Print("Welcome to Toobeci.\n\n")
rdr := bufio.NewReader(os.Stdin)
i := newInterpreter()
for {
// print a prompt
fmt.Print("> ")
// read a line of input from stdin
input, err := rdr.ReadString('\n')
if err != nil {
if err.Error() == "EOF" {
fmt.Println("\nGoodbye.")
return
}
fmt.Println(err)
continue
}
out, err := i.handleInputLine(input)
if err != nil {
fmt.Println(err)
}
if out != "" {
fmt.Println(out)
}
}
}