summaryrefslogtreecommitdiff
path: root/src/texec/worldbuilder.go
diff options
context:
space:
mode:
authorKyle Gunger <kgunger12@gmail.com>2021-11-19 01:39:05 -0500
committerKyle Gunger <kgunger12@gmail.com>2021-11-19 01:39:05 -0500
commit96cf52263053db6bc3069c9fbc664ed0725ac41e (patch)
treef3dca802ad7413a9aa614e73fc593a0e28e6fdfb /src/texec/worldbuilder.go
parent8cdf25536841a698ad6229f73cdf8ef5ccc1e5fa (diff)
Some refactoring, clearing out eval
+ Fixed BuildRoot + Refactored world.go - Deleted most of eval, I'm going to re-do it.
Diffstat (limited to 'src/texec/worldbuilder.go')
-rw-r--r--src/texec/worldbuilder.go66
1 files changed, 47 insertions, 19 deletions
diff --git a/src/texec/worldbuilder.go b/src/texec/worldbuilder.go
index f3cd6e6..d85f1ac 100644
--- a/src/texec/worldbuilder.go
+++ b/src/texec/worldbuilder.go
@@ -18,28 +18,65 @@ package texec
import (
"tparse"
- "path"
)
/**
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 := tparse.StringAsRunes(n.Data.Data)
+ l := len(r)
+ if r[0] == '"' || r[0] == '\'' {
+ return tparse.RunesAsString(r[1:l - 1])
+ }
+ return ""
+}
+
+// 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 {
+ 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.Name = module.Sub[0].Sub[0].Sub[0].Data.Data
- for n := 0 ; n < len(module.Sub) ; n++ {
-
- switch module.Sub[n].Data.Type {
- case 11:
-
- case 10:
-
+ for n := 1 ; n < len(module.Sub) ; n++ {
+ if module.Sub[n].Data.Data == "include" {
+ importFile(evalPreLiteral(module.Sub[n].Sub[0]), &out)
}
}
@@ -47,19 +84,10 @@ func buildModule(module tparse.Node) TModule {
}
// BuildRoot builds the root module, ready for eval
-func BuildRoot(file tparse.Node) TModule {
+func BuildRoot(file string) TModule {
out := TModule{}
- out.Files = append(out.Files, file)
-
- for n := 0 ; n < len(file.Sub) ; n++ {
-
- switch file.Sub[n].Data.Type {
- case 11:
-
- case 10:
- }
- }
+ importFile(file, &out)
return out
}