From 11a903c817e23bf2d694af0cd026caa94e45671e Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Sun, 28 Sep 2025 15:28:56 +0200 Subject: [PATCH] Implemented utility functions for C strings --- lib/c/length.q | 11 +++++++++++ lib/{strings/c.q => c/string.q} | 2 +- lib/fs/readFile.q | 4 ++-- lib/process/run_unix.q | 4 ++-- lib/process/run_windows.q | 4 ++-- src/core/evaluateDot.go | 2 +- 6 files changed, 19 insertions(+), 8 deletions(-) create mode 100644 lib/c/length.q rename lib/{strings/c.q => c/string.q} (76%) diff --git a/lib/c/length.q b/lib/c/length.q new file mode 100644 index 0000000..c4e7dba --- /dev/null +++ b/lib/c/length.q @@ -0,0 +1,11 @@ +length(ptr *byte) -> uint { + len := 0 + + loop { + if ptr[len] == 0 { + return len + } + + len += 1 + } +} \ No newline at end of file diff --git a/lib/strings/c.q b/lib/c/string.q similarity index 76% rename from lib/strings/c.q rename to lib/c/string.q index 57a9d13..bf95f12 100644 --- a/lib/strings/c.q +++ b/lib/c/string.q @@ -1,6 +1,6 @@ import mem -c(s string) -> !string { +string(s string) -> !string { cstr := mem.alloc(s.len + 1) loop i := 0..s.len { diff --git a/lib/fs/readFile.q b/lib/fs/readFile.q index c5e8834..afb5ab0 100644 --- a/lib/fs/readFile.q +++ b/lib/fs/readFile.q @@ -1,9 +1,9 @@ +import c import io import mem -import strings readFile(path string) -> (!string, error) { - cpath := strings.c(path) + cpath := c.string(path) fd, err := open(cpath.ptr, 0, 0) mem.free(cpath) diff --git a/lib/process/run_unix.q b/lib/process/run_unix.q index 0b15d7e..a8b39ea 100644 --- a/lib/process/run_unix.q +++ b/lib/process/run_unix.q @@ -1,12 +1,12 @@ +import c import mem import run -import strings run(path string) -> error { pid := fork() if pid == 0 { - cpath := strings.c(path) + cpath := c.string(path) argv := new(Argv) argv.path = cpath.ptr err := execve(cpath.ptr, argv, 0) diff --git a/lib/process/run_windows.q b/lib/process/run_windows.q index c09d6a2..f650890 100644 --- a/lib/process/run_windows.q +++ b/lib/process/run_windows.q @@ -1,11 +1,11 @@ +import c import mem -import strings run(path string) -> error { startInfo := new(StartupInfo) startInfo.size = 104 processInfo := new(ProcessInformation) - cpath := strings.c(path) + cpath := c.string(path) success := kernel32.CreateProcessA(0, cpath.ptr, 0, 0, false, 0, 0, 0, startInfo, processInfo) mem.free(cpath) diff --git a/src/core/evaluateDot.go b/src/core/evaluateDot.go index 44875c6..4639825 100644 --- a/src/core/evaluateDot.go +++ b/src/core/evaluateDot.go @@ -32,7 +32,7 @@ func (f *Function) evaluateDot(expr *expression.Expression) (ssa.Value, error) { if isPackage { pkg := f.Env.Packages[pkgValue.Name] - if !pkg.IsExtern && f != f.Env.Init { + if !pkg.IsExtern && pkg.Name != "main" { imp, exists := f.File.Imports[pkgValue.Name] if !exists { -- 2.51.2