-
Notifications
You must be signed in to change notification settings - Fork 0
/
processor.go
83 lines (61 loc) · 1.17 KB
/
processor.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
package calculator
import "fmt"
var (
Swap = swap{}
Dup = dup{}
Drop = drop{}
Over = over{}
Rot = rot{}
Dump = dump{}
)
func (c *calculator) Process(instructions ...Instruction) error {
for _, i := range instructions {
if err := i.Invoke(c); err != nil {
return err
}
}
return nil
}
type Instruction interface {
Invoke(c Calculator) error
}
type dump struct{}
func (d dump) Invoke(c Calculator) error {
fmt.Println(c.Dump())
return nil
}
func Push(v interface{}) Instruction { return push{v: v} }
type push struct {
v interface{}
}
func (p push) Invoke(c Calculator) error {
c.Push(p.v)
return nil
}
type swap struct{}
func (p swap) Invoke(c Calculator) error {
return c.Swap()
}
type dup struct{}
func (p dup) Invoke(c Calculator) error {
return c.Dup()
}
type drop struct{}
func (p drop) Invoke(c Calculator) error {
return c.Drop()
}
type over struct{}
func (p over) Invoke(c Calculator) error {
return c.Over()
}
type rot struct{}
func (p rot) Invoke(c Calculator) error {
return c.Rot()
}
func Op2(op string) Instruction { return op2{op: op} }
type op2 struct {
op string
}
func (p op2) Invoke(c Calculator) error {
return c.Op2(p.op)
}