summaryrefslogtreecommitdiff
path: root/go/forth/forth.go
blob: 86759417559eab960dcb6f529b4dbb02d18354e4 (plain)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package forth

import (
	"errors"
	"log"
	"strconv"
	"strings"
)

type stacker interface {
	push(int) error
	pop() (int, error)
	values() []int
}

type stack []int

func (s *stack) push(n int) error {
	*s = append(*s, n)
	return nil
}

func (s *stack) pop() (int, error) {
	depth := len(*s)
	if depth < 1 {
		return 0, errors.New("stack underflow")
	}
	tos := (*s)[depth-1]
	*s = (*s)[:depth-1]
	return tos, nil
}

func (s *stack) values() []int {
	return []int(*s)
}

func pop2(s stacker) (int, int, error) {
	tos, err := s.pop()
	if err != nil {
		return 0, 0, err
	}
	nos, err := s.pop()
	if err != nil {
		return 0, 0, err
	}
	return tos, nos, nil
}

func pushN(s stacker, n ...int) error {
	for _, v := range n {
		if err := s.push(v); err != nil {
			return err
		}
	}
	return nil
}

type dictionary map[string][]func(stacker) error

func (d dictionary) find(name string) ([]func(stacker) error, bool) {
	fs, ok := d[strings.ToLower(name)]
	return fs, ok
}

func (d dictionary) add(name string, fs ...func(stacker) error) {
	d[strings.ToLower(name)] = fs
}

func add(s stacker) error {
	tos, nos, err := pop2(s)
	if err != nil {
		return err
	}
	return s.push(nos + tos)
}

func sub(s stacker) error {
	tos, nos, err := pop2(s)
	if err != nil {
		return err
	}
	return s.push(nos - tos)
}

func mul(s stacker) error {
	tos, nos, err := pop2(s)
	if err != nil {
		return err
	}
	return s.push(nos * tos)
}

func div(s stacker) error {
	tos, nos, err := pop2(s)
	if err != nil {
		return err
	}
	if tos == 0 {
		return errors.New("division by zero")
	}
	return s.push(nos / tos)
}

func dup(s stacker) error {
	tos, err := s.pop()
	if err != nil {
		return err
	}
	return pushN(s, tos, tos)
}

func drop(s stacker) error {
	_, err := s.pop()
	return err
}

func swap(s stacker) error {
	tos, nos, err := pop2(s)
	if err != nil {
		return err
	}
	return pushN(s, tos, nos)
}

func over(s stacker) error {
	tos, nos, err := pop2(s)
	if err != nil {
		return err
	}
	return pushN(s, nos, tos, nos)
}

func literal(n int) func(stacker) error {
	return func(s stacker) error {
		return s.push(n)
	}
}

func number(word string) (int, bool) {
	n, err := strconv.Atoi(word)
	return n, err == nil
}

func index(s []string, r string) int {
	for i, v := range s {
		if v == r {
			return i
		}
	}
	return -1
}

func compile(dict dictionary, s []string) ([]func(stacker) error, error) {
	log.Println("compile", s)
	var words []func(stacker) error
	for i := 0; i < len(s); i++ {
		v := s[i]
		// lookup dictionary first
		if w, ok := dict.find(v); ok {
			words = append(words, w...)
			continue
		}
		if v == ":" {
			next := index(s[i+1:], ";")
			if next == -1 {
				return nil, errors.New("unterminated colon operator")
			}
			name := s[i+1]
			if _, ok := number(name); ok {
				return nil, errors.New("name cannot be a number")
			}
			w, err := compile(dict, s[i+2:i+next+1])
			if err != nil {
				return nil, err
			}
			log.Println("add", name)
			dict.add(name, w...)
			i += next + 1
			continue
		}
		// try to parse literal
		if n, ok := number(v); ok {
			words = append(words, literal(n))
			continue
		}
		return nil, errors.New("unknown word")
	}
	return words, nil
}

func Forth(v []string) ([]int, error) {
	s := new(stack)
	dict := make(dictionary)
	dict.add("+", add)
	dict.add("-", sub)
	dict.add("*", mul)
	dict.add("/", div)
	dict.add("dup", dup)
	dict.add("drop", drop)
	dict.add("swap", swap)
	dict.add("over", over)

	var words []func(stacker) error
	for _, line := range v {
		w, err := compile(dict, strings.Split(line, " "))
		if err != nil {
			return nil, err
		}
		words = append(words, w...)
	}
	for _, w := range words {
		if err := w(s); err != nil {
			return nil, err
		}
	}
	return s.values(), nil
}