-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler.go
55 lines (48 loc) · 1.08 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
package compiler
import (
"brainfucker/ast"
"brainfucker/lexer"
"github.com/llir/llvm/ir"
"github.com/llir/llvm/ir/constant"
"github.com/llir/llvm/ir/types"
)
type Compiler struct {
module *ir.Module
main *ir.Block
buf *ir.Global
currIdx *ir.Global
}
func NewCompiler() *Compiler {
m := ir.NewModule()
return &Compiler{
module: ir.NewModule(),
buf: m.NewGlobalDef("buf", constant.NewArray(types.NewArray(30000, types.I8))),
currIdx: m.NewGlobal("currIdx", types.NewInt(16)),
}
}
func (c Compiler) Compile(program ast.Program) (*ir.Module, error) {
for _, node := range program.Nodes {
switch node.(type) {
case ast.Command:
cmd := node.(ast.Command)
if err := c.handleCommand(cmd); err != nil {
return nil, err
}
case ast.Loop:
loop := node.(ast.Loop)
if err := c.handleLoop(loop); err != nil {
return nil, err
}
}
}
return c.module, nil
}
func (c Compiler) handleCommand(command ast.Command) error {
switch command.Token() {
case lexer.Increment:
}
return nil
}
func (c Compiler) handleLoop(loop ast.Loop) error {
return nil
}