-
Notifications
You must be signed in to change notification settings - Fork 0
/
ir.go
122 lines (113 loc) · 2.76 KB
/
ir.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
package semantics
import (
"fmt"
"github.com/nihei9/ino/ir"
"github.com/nihei9/ino/parser"
)
type irBuilder struct {
tyEnv *tyEnv
valEnv *valEnv
}
func (b *irBuilder) run(root *parser.Node) (*ir.File, error) {
return b.buildRoot(root)
}
func (b *irBuilder) buildRoot(root *parser.Node) (*ir.File, error) {
decls, err := b.buildDecls(root.Children[0])
if err != nil {
return nil, err
}
return &ir.File{
Decls: decls,
}, nil
}
func (b *irBuilder) buildDecls(node *parser.Node) ([]ir.Decl, error) {
decls := make([]ir.Decl, 0, len(node.Children))
for _, c := range node.Children {
d, err := b.buildDecl(c)
if err != nil {
return nil, err
}
decls = append(decls, d)
}
return decls, nil
}
func (b *irBuilder) buildDecl(node *parser.Node) (ir.Decl, error) {
d := node.Children[0]
switch d.KindName {
case "data":
return b.buildData(d)
}
return nil, fmt.Errorf("invalid declaration node kind: %v", d.KindName)
}
func (b *irBuilder) buildData(node *parser.Node) (ir.Decl, error) {
dataName := node.Children[0].Text
tyVars := node.Children[1]
conss := node.Children[2]
tyVar2Num := map[symbol]int{}
for i, v := range tyVars.Children {
tyVar2Num[symbol(v.Children[0].Text)] = i + 1
}
valConss := make([]*ir.ValCons, len(conss.Children))
for i, cons := range conss.Children {
consName := cons.Children[0].Text
ee, ok := b.valEnv.lookup(symbol(consName))
if !ok {
return nil, fmt.Errorf("symbol not found: %v", consName)
}
consTy, ok := ee.ty.(*funcType)
if !ok {
return nil, fmt.Errorf("value constructor must be a function type: %T", ee.ty)
}
params := make([]ir.Type, len(consTy.params))
for i, t := range consTy.params {
param, err := genType(t, tyVar2Num)
if err != nil {
return nil, fmt.Errorf("failed to generate a type of a value constructor %v of %v: %w", consName, dataName, err)
}
params[i] = param
}
valConss[i] = &ir.ValCons{
Name: consName,
Params: params,
}
}
return &ir.DataDecl{
Name: dataName,
TypeVarCount: len(tyVars.Children),
Conss: valConss,
}, nil
}
func genType(ty declType, tyVar2Num map[symbol]int) (ir.Type, error) {
switch t := ty.(type) {
case basicType:
return &ir.BasicType{
Name: t.String(),
}, nil
case *dataType:
return &ir.NamedType{
Name: string(t.name),
}, nil
case *concreteType:
abTy, err := genType(t.abstractTy, tyVar2Num)
if err != nil {
return nil, err
}
args := make([]ir.Type, len(t.args))
for i, p := range t.args {
pTy, err := genType(p, tyVar2Num)
if err != nil {
return nil, err
}
args[i] = pTy
}
return &ir.ConcreteType{
AbstractTy: abTy,
Args: args,
}, nil
case *typeVar:
return &ir.TypeVar{
Num: tyVar2Num[t.name],
}, nil
}
return nil, fmt.Errorf("invalid type: %T", ty)
}