fixed a few mod system bugs

This commit is contained in:
2024-08-22 15:41:27 +02:00
parent 3e776ec65f
commit 2ca95bcc03
11 changed files with 2066 additions and 352 deletions
+38 -8
View File
@@ -7,7 +7,7 @@ import (
type wmap [2]string
var wyst_map []wmap = []wmap{{"int", "int"}, {"void", "void"}, {"main", "main"}}
var wyst_map []wmap = []wmap{{"int", "int"}, {"void", "void"}, {"main", "main"}, {"string", "string"}, {"true", "true"}, {"false", "false"}}
func TranspileToken(node ASTNode) string {
res := ""
@@ -42,6 +42,8 @@ func TranspileToken(node ASTNode) string {
res += ", "
case "IDENT":
res += fmt.Sprintf("_%#x", node.Text)
case "GOIDENTIFIER":
res += strings.TrimPrefix(node.Text, "^")
default:
fmt.Printf("\x1b[33mDEV NOTICE:\x1b[0m Missing token case for '%s'\n", node.Rule)
@@ -83,9 +85,6 @@ func (m *Module) TranspileNode(node ASTNode) string {
res += m.TranspileNode(node.Inner[0])
res += m.TranspileNode(node.Inner[1])
case "round_call":
res += fmt.Sprintf("(%s)", m.TranspileNodes(node.Inner))
case "struct_def":
res += fmt.Sprintf("type %s struct %s\n", m.TranspileNode(node.Inner[0]), m.TranspileNode(node.Inner[1]))
@@ -126,14 +125,45 @@ func (m *Module) TranspileNode(node ASTNode) string {
res += fmt.Sprintf("import . \"%s\"\n", m.ImportModule(node.Inner[0].Text))
case "use_statement":
ident := strings.Split(node.Inner[0].Text, ".")
ident := strings.Split(node.Inner[0].Text, "::")
ident2 := strings.Split(m.TranspileNode(node.Inner[0]), ".")
modname := m.ImportModule(ident[0])
m := strings.Split(modname, "/")
res += fmt.Sprintf("import %s \"%s\"\n", m[len(m)-1], modname)
md := strings.Split(modname, "/")
res += fmt.Sprintf("import %s \"%s\"\n", md[len(md)-1], modname)
if len(ident) > 1 {
res += fmt.Sprintf("var %s = %s\n", ident[len(ident)-1], strings.Join(ident, "."))
res += fmt.Sprintf("var %s = %s\n", ident2[len(ident2)-1], m.TranspileNode(node.Inner[0]))
}
case "go_import":
res += "import " + m.TranspileNode(node.Inner[0]) + "\n"
case "map":
wyst_map = append(wyst_map, wmap{node.Inner[0].Text, node.Inner[2].Text})
case "go_call":
res += m.TranspileNode(node.Inner[0])
res += m.TranspileNode(node.Inner[1])
case "round_call":
res += "(" + m.TranspileNodes(node.Inner) + ")"
case "if_tree":
res += m.TranspileNodes(node.Inner)
case "if_statement":
res += "if " + m.TranspileNodes(node.Inner)
case "if_expr":
res += m.TranspileNodes(node.Inner)
case "else_statement":
res += "else "
res += m.TranspileNodes(node.Inner)
case "elseif_statement":
res += "else "
res += m.TranspileNodes(node.Inner)
default:
fmt.Printf("\x1b[33mDEV NOTICE:\x1b[0m Missing case for '%s'\n", node.Rule)
+13 -11
View File
@@ -4,8 +4,11 @@ import (
"fmt"
"os"
"path"
"strings"
)
const OUT_FOLDER = "build/"
func FileExists(filename string) bool {
_, err := os.Stat(filename)
return err == nil || !os.IsNotExist(err)
@@ -43,7 +46,7 @@ func NewRoot(file_path string) Module {
root := Module{}
root.Path = file_path
root.Out = "out/main.go"
root.Out = OUT_FOLDER+"main.go"
root.Name = "root"
root.Code = root.Transpile(readFile(file_path))
@@ -53,16 +56,15 @@ func NewRoot(file_path string) Module {
func (root *Module) ImportModule(modname string) string {
module := Module{Name: modname}
module.Path = path.Join(path.Dir(root.Path), module.Name+".wyst")
if FileExists(module.Path) {
module.Code = "package " + fmt.Sprintf("_%#x", modname) + "\n" + module.Transpile(readFile(module.Path))
} else {
if !FileExists(module.Path) {
module.Path = path.Join(path.Dir(root.Path), module.Name, "mod.wyst")
if FileExists(module.Path) {
module.Code = "package " + fmt.Sprintf("_%#x", modname) + "\n" + module.Transpile(readFile(module.Path))
}
}
module.Out = fmt.Sprintf("%s/I%#x/%s.go", path.Dir(root.Out), module.Name, module.Name)
root.Inner = append(root.Inner, module)
if FileExists(module.Path) {
module.Out = fmt.Sprintf("%s/I%#x/%s.go", path.Dir(root.Out), module.Name, module.Name)
module.Code = "package " + fmt.Sprintf("_%#x", modname) + "\n" + module.Transpile(readFile(module.Path))
root.Inner = append(root.Inner, module)
}
return fmt.Sprintf("%s/I%#x", path.Dir(root.Out), module.Name)
}
@@ -74,7 +76,7 @@ func WriteModules(root Module) {
}
func WriteRoot(root Module) {
writeFile("out/main.go", "package main\n"+root.Code)
writeFile("out/go.mod", "module out\n")
writeFile(OUT_FOLDER+"main.go", "package main\n"+root.Code)
writeFile(OUT_FOLDER+"go.mod", "module "+strings.ReplaceAll(OUT_FOLDER, "/", "")+"\n")
WriteModules(root)
}