This repository has been archived by the owner on Jun 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
/
module.go
148 lines (123 loc) · 2.76 KB
/
module.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
package ast
import (
"bytes"
"fmt"
"path/filepath"
"strings"
"github.com/ark-lang/ark/src/lexer"
"github.com/ark-lang/ark/src/parser"
"github.com/ark-lang/ark/src/util/log"
)
type Module struct {
Name *ModuleName
ModScope *Scope
Dirpath string
Trees []*parser.ParseTree
Parts map[string]*Submodule
LinkedLibraries []string
resolved bool
}
type Submodule struct {
Parent *Module
UseScope *Scope
File *lexer.Sourcefile
Nodes []Node
inferred bool
}
type ModuleLookup struct {
Name string
Module *Module
Children map[string]*ModuleLookup
}
func NewModuleLookup(name string) *ModuleLookup {
res := &ModuleLookup{
Name: name,
Children: make(map[string]*ModuleLookup),
}
return res
}
func (v *ModuleLookup) Get(name *ModuleName) (*ModuleLookup, error) {
ml := v
for idx, part := range name.Parts {
nml, ok := ml.Children[part]
if ok {
ml = nml
} else {
return nil, fmt.Errorf("Module not found in lookup: %s",
(&ModuleName{Parts: name.Parts[0 : idx+1]}).String())
}
}
return ml, nil
}
func (v *ModuleLookup) Create(name *ModuleName) *ModuleLookup {
ml := v
for _, part := range name.Parts {
nml, ok := ml.Children[part]
if !ok {
nml = NewModuleLookup(part)
ml.Children[part] = nml
}
ml = nml
}
return ml
}
func (v *ModuleLookup) Dump(i int) {
if v.Name != "" {
log.Debug("main", "%s", strings.Repeat(" ", i))
log.Debugln("main", "%s", v.Name)
}
for _, child := range v.Children {
child.Dump(i + 1)
}
}
type ModuleName struct {
Parts []string
}
func NewModuleName(node *parser.NameNode) *ModuleName {
res := &ModuleName{
Parts: make([]string, len(node.Modules)+1),
}
for idx, mod := range node.Modules {
res.Parts[idx] = mod.Value
}
res.Parts[len(res.Parts)-1] = node.Name.Value
return res
}
func JoinModuleName(modName *ModuleName, part string) *ModuleName {
res := &ModuleName{
Parts: make([]string, len(modName.Parts)+1),
}
copy(res.Parts, modName.Parts)
res.Parts[len(res.Parts)-1] = part
return res
}
func ModuleNameFromUnresolvedName(unresName UnresolvedName) *ModuleName {
res := &ModuleName{}
res.Parts = append(res.Parts, unresName.ModuleNames...)
res.Parts = append(res.Parts, unresName.Name)
return res
}
func (v *ModuleName) Last() string {
idx := len(v.Parts) - 1
return v.Parts[idx]
}
func (v *ModuleName) String() string {
buf := new(bytes.Buffer)
for idx, parent := range v.Parts {
buf.WriteString(parent)
if idx < len(v.Parts)-1 {
buf.WriteString("::")
}
}
return buf.String()
}
func (v *ModuleName) ToPath() string {
buf := new(bytes.Buffer)
for idx, parent := range v.Parts {
buf.WriteString(parent)
if idx < len(v.Parts)-1 {
buf.WriteRune(filepath.Separator)
}
}
return buf.String()
}