-
Notifications
You must be signed in to change notification settings - Fork 9
/
compiler.go
223 lines (204 loc) · 5.94 KB
/
compiler.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
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
218
219
220
221
222
223
package bytecode
import (
"fmt"
"evylang.dev/evy/pkg/parser"
)
var (
// ErrUndefinedVar is returned when a variable name cannot
// be resolved in the symbol table.
ErrUndefinedVar = fmt.Errorf("%w: undefined variable", ErrPanic)
// ErrUnknownOperator is returned when an operator cannot
// be resolved.
ErrUnknownOperator = fmt.Errorf("%w: unknown operator", ErrInternal)
// ErrUnsupportedExpression is returned when an expression is not
// supported by the compiler, this indicates an error in the compiler
// itself, as all parseable evy expressions should be supported.
ErrUnsupportedExpression = fmt.Errorf("%w: unsupported expression", ErrInternal)
)
// Compiler is responsible for turning a parsed evy program into
// bytecode.
type Compiler struct {
constants []value
instructions Instructions
globals *SymbolTable
}
// Bytecode represents raw evy bytecode.
type Bytecode struct {
Constants []value
Instructions Instructions
}
// NewCompiler returns a new compiler.
func NewCompiler() *Compiler {
return &Compiler{globals: NewSymbolTable()}
}
// Compile accepts an AST node and renders it to bytecode internally.
func (c *Compiler) Compile(node parser.Node) error {
switch node := node.(type) {
case *parser.Program:
return c.compileProgram(node)
case *parser.InferredDeclStmt:
return c.compileDecl(node.Decl)
case *parser.AssignmentStmt:
return c.compileAssignment(node)
case *parser.BinaryExpression:
return c.compileBinaryExpression(node)
case *parser.UnaryExpression:
return c.compileUnaryExpression(node)
case *parser.GroupExpression:
return c.Compile(node.Expr)
case *parser.Var:
return c.compileVar(node)
case *parser.NumLiteral:
num := numVal(node.Value)
if err := c.emit(OpConstant, c.addConstant(num)); err != nil {
return err
}
case *parser.BoolLiteral:
opcode := OpFalse
if node.Value {
opcode = OpTrue
}
if err := c.emit(opcode); err != nil {
return err
}
case *parser.StringLiteral:
num := stringVal(node.Value)
if err := c.emit(OpConstant, c.addConstant(num)); err != nil {
return err
}
}
return nil
}
// Bytecode renders the compiler instructions into Bytecode.
func (c *Compiler) Bytecode() *Bytecode {
return &Bytecode{
Instructions: c.instructions,
Constants: c.constants,
}
}
// addConstant appends the provided value to the constants
// and returns the index of that constant.
func (c *Compiler) addConstant(obj value) int {
c.constants = append(c.constants, obj)
return len(c.constants) - 1
}
// addInstruction appends bytes to the instruction set and returns the
// position of the instruction.
func (c *Compiler) addInstruction(ins []byte) {
c.instructions = append(c.instructions, ins...)
}
// emit makes and writes an instruction to the bytecode and returns the
// position of the instruction.
func (c *Compiler) emit(op Opcode, operands ...int) error {
ins, err := Make(op, operands...)
if err != nil {
return err
}
c.addInstruction(ins)
return nil
}
func (c *Compiler) compileBinaryExpression(expr *parser.BinaryExpression) error {
if err := c.Compile(expr.Left); err != nil {
return err
}
if err := c.Compile(expr.Right); err != nil {
return err
}
// equality and inequality are type agnostic in the vm, so no type checking
// is required to decide which opcode to output.
switch expr.Op {
case parser.OP_EQ:
return c.emit(OpEqual)
case parser.OP_NOT_EQ:
return c.emit(OpNotEqual)
}
if expr.Left.Type() == parser.NUM_TYPE && expr.Right.Type() == parser.NUM_TYPE {
return c.compileNumBinaryExpression(expr)
}
if expr.Left.Type() == parser.STRING_TYPE && expr.Right.Type() == parser.STRING_TYPE {
return c.compileStringBinaryExpression(expr)
}
return fmt.Errorf("%w: %s with types %s %s", ErrUnsupportedExpression,
expr, expr.Left.Type(), expr.Right.Type())
}
func (c *Compiler) compileNumBinaryExpression(expr *parser.BinaryExpression) error {
switch expr.Op {
case parser.OP_PLUS:
return c.emit(OpAdd)
case parser.OP_MINUS:
return c.emit(OpSubtract)
case parser.OP_ASTERISK:
return c.emit(OpMultiply)
case parser.OP_SLASH:
return c.emit(OpDivide)
case parser.OP_PERCENT:
return c.emit(OpModulo)
case parser.OP_LT:
return c.emit(OpNumLessThan)
case parser.OP_LTEQ:
return c.emit(OpNumLessThanEqual)
case parser.OP_GT:
return c.emit(OpNumGreaterThan)
case parser.OP_GTEQ:
return c.emit(OpNumGreaterThanEqual)
default:
return fmt.Errorf("%w %s", ErrUnknownOperator, expr.Op)
}
}
func (c *Compiler) compileStringBinaryExpression(expr *parser.BinaryExpression) error {
switch expr.Op {
case parser.OP_PLUS:
return c.emit(OpStringConcatenate)
case parser.OP_LT:
return c.emit(OpStringLessThan)
case parser.OP_LTEQ:
return c.emit(OpStringLessThanEqual)
case parser.OP_GT:
return c.emit(OpStringGreaterThan)
case parser.OP_GTEQ:
return c.emit(OpStringGreaterThanEqual)
default:
return fmt.Errorf("%w %s", ErrUnknownOperator, expr.Op)
}
}
func (c *Compiler) compileUnaryExpression(expr *parser.UnaryExpression) error {
if err := c.Compile(expr.Right); err != nil {
return err
}
switch expr.Op {
case parser.OP_MINUS:
return c.emit(OpMinus)
case parser.OP_BANG:
return c.emit(OpNot)
}
return nil
}
func (c *Compiler) compileProgram(prog *parser.Program) error {
for _, s := range prog.Statements {
if err := c.Compile(s); err != nil {
return err
}
}
return nil
}
func (c *Compiler) compileDecl(decl *parser.Decl) error {
if err := c.Compile(decl.Value); err != nil {
return err
}
symbol := c.globals.Define(decl.Var.Name)
return c.emit(OpSetGlobal, symbol.Index)
}
func (c *Compiler) compileAssignment(stmt *parser.AssignmentStmt) error {
if err := c.Compile(stmt.Target); err != nil {
return err
}
symbol := c.globals.Define(stmt.Value.String())
return c.emit(OpSetGlobal, symbol.Index)
}
func (c *Compiler) compileVar(variable *parser.Var) error {
symbol, ok := c.globals.Resolve(variable.Name)
if !ok {
return fmt.Errorf("%w %s", ErrUndefinedVar, variable.Name)
}
return c.emit(OpGetGlobal, symbol.Index)
}