-
Notifications
You must be signed in to change notification settings - Fork 14
/
translate.go
139 lines (119 loc) · 2.54 KB
/
translate.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
package acc
import (
"fmt"
"github.com/mmcloughlin/addchain/acc/ast"
"github.com/mmcloughlin/addchain/acc/ir"
"github.com/mmcloughlin/addchain/internal/errutil"
)
// Translate converts an abstract syntax tree to an intermediate representation.
func Translate(c *ast.Chain) (*ir.Program, error) {
s := newstate()
for _, stmt := range c.Statements {
if err := s.statement(stmt); err != nil {
return nil, err
}
}
return s.prog, nil
}
type state struct {
prog *ir.Program
n int
variable map[ast.Identifier]*ir.Operand
}
func newstate() *state {
return &state{
prog: &ir.Program{},
n: 1,
variable: map[ast.Identifier]*ir.Operand{},
}
}
func (s *state) statement(stmt ast.Statement) error {
out, err := s.expr(stmt.Expr)
if err != nil {
return err
}
if err := s.define(stmt.Name, out); err != nil {
return err
}
return nil
}
func (s *state) expr(expr ast.Expr) (*ir.Operand, error) {
switch e := expr.(type) {
case ast.Operand:
return &ir.Operand{Index: int(e)}, nil
case ast.Identifier:
return s.lookup(e)
case ast.Add:
return s.add(e)
case ast.Double:
return s.double(e)
case ast.Shift:
return s.shift(e)
default:
return nil, errutil.UnexpectedType(e)
}
}
func (s *state) add(a ast.Add) (*ir.Operand, error) {
x, err := s.expr(a.X)
if err != nil {
return nil, err
}
y, err := s.expr(a.Y)
if err != nil {
return nil, err
}
if x.Index > y.Index {
x, y = y, x
}
out := ir.Index(s.n)
inst := &ir.Instruction{
Output: out,
Op: ir.Add{X: x, Y: y},
}
s.prog.AddInstruction(inst)
s.n++
return out, nil
}
func (s *state) double(d ast.Double) (*ir.Operand, error) {
x, err := s.expr(d.X)
if err != nil {
return nil, err
}
out := ir.Index(s.n)
inst := &ir.Instruction{
Output: out,
Op: ir.Double{X: x},
}
s.prog.AddInstruction(inst)
s.n++
return out, nil
}
func (s *state) shift(sh ast.Shift) (*ir.Operand, error) {
x, err := s.expr(sh.X)
if err != nil {
return nil, err
}
s.n += int(sh.S)
out := ir.Index(s.n - 1)
inst := &ir.Instruction{
Output: out,
Op: ir.Shift{X: x, S: sh.S},
}
s.prog.AddInstruction(inst)
return out, nil
}
func (s *state) define(name ast.Identifier, op *ir.Operand) error {
if _, found := s.variable[name]; found {
return fmt.Errorf("cannot redefine %q", name)
}
op.Identifier = string(name)
s.variable[name] = op
return nil
}
func (s state) lookup(name ast.Identifier) (*ir.Operand, error) {
operand, ok := s.variable[name]
if !ok {
return nil, fmt.Errorf("variable %q undefined", name)
}
return operand, nil
}