-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype.go
121 lines (102 loc) · 1.95 KB
/
type.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
package main
import "fmt"
type TypeKind int
const (
TY_BYTE TypeKind = iota
TY_INT
TY_POINTER
TY_ARRAY
)
var typeKindString = map[TypeKind]string{
TY_BYTE: "byte",
TY_INT: "int",
TY_POINTER: "pointer",
TY_ARRAY: "array",
}
var typeNames = map[string]TypeKind{
"int": TY_INT,
"byte": TY_BYTE,
}
func (tk TypeKind) String() string {
return typeKindString[tk]
}
type Type struct {
Kind TypeKind
Ref *Type // The pointer's reference
ArrayLen uint
}
func (t *Type) size() uint {
switch t.Kind {
case TY_BYTE:
return 1
case TY_INT, TY_POINTER:
return 8
case TY_ARRAY:
return t.Ref.size() * t.ArrayLen
default:
panic("unknown type")
}
}
func (t *Type) String() string {
return fmt.Sprintf("%+v", *t)
}
func arrayOf(ty *Type, len uint) *Type {
return &Type{
Kind: TY_ARRAY,
Ref: ty,
ArrayLen: len,
}
}
var intType = &Type{Kind: TY_INT}
var byteType = &Type{Kind: TY_BYTE}
func (t *Type) isInt() bool {
return t.Kind == TY_INT
}
func (t *Type) isPointer() bool {
return t.Kind == TY_POINTER
}
func (t *Type) isArray() bool {
return t.Kind == TY_ARRAY
}
func (n *Node) addType() {
if n == nil || n.Type != nil {
return
}
n.Lhs.addType()
n.Rhs.addType()
n.Cond.addType()
n.Then.addType()
n.Els.addType()
n.Init.addType()
n.Inc.addType()
n.Block.addType()
for _, stmt := range n.Body {
stmt.addType()
}
for _, a := range n.Args {
a.addType()
}
switch n.Kind {
case ND_ADD, ND_SUB, ND_MUL, ND_DIV, ND_EQ, ND_NE, ND_LT, ND_LE, ND_FUNCALL, ND_NUM:
n.Type = intType
case ND_ADDR:
n.Type = &Type{TY_POINTER, n.Lhs.Type, 0}
case ND_DEREF, ND_INDEX:
ref := n.Lhs.Type.Ref
if ref == nil {
errorRef(n)
}
n.Type = ref
}
}
func errorRef(n *Node) {
switch n.Kind {
case ND_DEREF:
panic("invalid pointer dereference")
case ND_INDEX:
errorIndexing(n.Lhs.Type.Kind)
}
}
func errorIndexing(kind TypeKind) {
panic(fmt.Sprintf("invalid operation (type %s does not support indexing)", kind))
}