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
|
/*
Copyright 2020 Kyle Gunger
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package texec
import (
"tparse"
)
/**
worldbuilder.go - take in a file name and construct a root TModule based on it.
*/
// Note: This is good enough, I guess. Gonna mark this as the final version, only update on major errors.
// Supported features:
// Importing other files
// Sub-modules across files
// Semi-borked sub-folders:
// Because the builder doesn't preserve the paths you are taking, it will not figure out which folder each file is in properly.
// Technically, you could work around this by making all imports in EVERY FILE EVERYWHERE look as if they are pathed from the folder
// where the root file is, but this would be a headache. I am just planning on fixing this in the full compiler.
// Returns generated value and general "type" of value (string, number)
func evalPreLiteral(n tparse.Node) string {
r := []rune(n.Data.Data)
l := len(r)
if r[0] == '"' || r[0] == '\'' {
return string(r[1:l - 1])
}
return ""
}
func modDef(n tparse.Node, m *TModule) {
t := getType(n.Sub[0])
s, vs := modDefVars(n.Sub[1], t)
for i := 0; i < len(s); i++ {
m.Defs[s[i]] = vs[i]
}
}
// Generate a variable list for a module
// For sub = 0, give the vlist
// May be horribly broken. Definitely doesn't support composite types.
func modDefVars(n tparse.Node, t TType) ([]string, []TVariable) {
s := []string{}
v := []TVariable{}
for i := 0; i < len(n.Sub); i++ {
if n.Sub[i].Data.Type == tparse.DEFWORD {
s = append(s, n.Sub[i].Data.Data)
v = append(v, TVariable{t, nil})
} else if n.Sub[i].Data.Data == "=" && n.Sub[i].Sub[0].Data.Type == tparse.DEFWORD {
s = append(s, n.Sub[i].Sub[0].Data.Data)
v = append(v, TVariable{t, getLiteral(n.Sub[i].Sub[1], t)})
} else {
errOut("Unexpected thing in definition. Expected '=' or DEFWORD.", n.Sub[i].Data)
}
}
return s, v
}
func modDefStruct(n tparse.Node, m *TModule) {
var name string
tmap := make(TypeMap)
for i := 0; i < len(n.Sub); i++ {
if n.Sub[i].Data.Type == tparse.DEFWORD {
name = n.Sub[i].Data.Data
} else if n.Sub[i].Data.Data == "plist" && n.Sub[i].Data.Type == 10 {
var t TType
for j := 0; j < len(n.Sub[i].Sub); j++ {
if n.Sub[i].Sub[j].Data.Type == 10 && n.Sub[i].Sub[j].Data.Data == "type" {
t = getType(n.Sub[i].Sub[j])
} else if n.Sub[i].Sub[j].Data.Type == tparse.DEFWORD {
tmap[n.Sub[i].Sub[j].Data.Data] = t
}
}
}
}
m.Defs[name] = TVariable{tStruct, tmap}
}
func modDefEnum(n tparse.Node, m *TModule) {
name := n.Sub[0].Data.Data
t := getType(n.Sub[1].Sub[0])
s, vs := modDefVars(n.Sub[2], t)
out := TVariable{tEnum, make(VarMap)}
for i := 0; i < len(s); i++ {
out.Data.(VarMap)[s[i]] = vs[i]
}
m.Defs[name] = out
}
// Parse a file and make an AST from it.
func parseFile(p string) tparse.Node {
tokens := tparse.TokenizeFile(p)
return tparse.MakeTree(&(tokens), p)
}
// Import a file and auto-import sub-modules and files
func importFile(f string, m *TModule) {
froot := parseFile(f)
for n := 0 ; n < len(froot.Sub) ; n++ {
if froot.Sub[n].Data.Data == "block" {
if froot.Sub[n].Sub[0].Sub[0].Data.Data == "module" {
m.Sub = append(m.Sub, buildModule(froot.Sub[n]))
} else {
m.Artifacts = append(m.Artifacts, froot.Sub[n])
}
} else if froot.Sub[n].Data.Data == "include" {
importFile(evalPreLiteral(froot.Sub[n].Sub[0]), m)
} else if froot.Sub[n].Data.Data == "define" {
modDef(froot.Sub[n], m)
} else if froot.Sub[n].Data.Data == "enum"{
modDefEnum(froot.Sub[n], m)
} else if froot.Sub[n].Data.Data == "struct" || froot.Sub[n].Data.Data == "raw"{
modDefStruct(froot.Sub[n], m)
} else {
m.Artifacts = append(m.Artifacts, froot.Sub[n])
}
}
}
// Build a module from a module block node
func buildModule(module tparse.Node) TModule {
out := TModule{}
out.Defs = make(VarMap)
out.Name = module.Sub[0].Sub[0].Sub[0].Data.Data
for n := 1 ; n < len(module.Sub) ; n++ {
if module.Sub[n].Data.Data == "include" {
importFile(evalPreLiteral(module.Sub[n].Sub[0]), &out)
}
}
return out
}
// BuildRoot builds the root module, ready for eval
func BuildRoot(file string) TModule {
out := TModule{}
out.Defs = make(VarMap)
importFile(file, &out)
return out
}
|