harec

[hare] Hare compiler, written in C11 for POSIX OSs
Log | Files | Refs | README | LICENSE

commit 73f8cc5bb31ef25f8832a53767498735bce8b39b
parent 03a6b516d3c3395f6e80a58396b66f740fc2ceba
Author: Armin Weigl <tb46305@gmail.com>
Date:   Sat, 14 Jan 2023 17:30:17 +0100

rt/compile: add support for compiler flags

Signed-off-by: Armin Weigl <tb46305@gmail.com>

Diffstat:
Mrt/compile.ha | 28+++++++++++++++++++++-------
Mrt/cstrings.ha | 7+++++++
2 files changed, 28 insertions(+), 7 deletions(-)

diff --git a/rt/compile.ha b/rt/compile.ha @@ -1,8 +1,12 @@ export type exited = int, signaled = int; export type exit_status = (exited | signaled); +export type typedef = str; +export type define = str; +export type compileflag = (typedef | define); + // Runs the Hare compiler and returns the exit status. -export fn compile(src: const str) exit_status = { +export fn compile(src: const str, flags: compileflag...) exit_status = { let status = 0; let pipefd = [-1, -1]; assert(pipe2(&pipefd, 0) == 0); @@ -15,16 +19,26 @@ export fn compile(src: const str) exit_status = { close(2); const path = "./harec\0"; - const argv: [_]nullable *const char = [ - constchar(path), - constchar("-\0"), - null - ]; + let argv: []nullable *const char = []; + append(argv, constchar(path)); + for (let i = 0z; i < len(flags); i += 1) { + match (flags[i]) { + case let d: define => + append(argv, constchar("-D\0")); + append(argv, alloc_constchar(d)); + case let t: typedef => + append(argv, constchar("-t\0")); + append(argv, alloc_constchar(t)); + }; + }; + append(argv, constchar("-\0")); + append(argv, null); + const envp: [_]nullable *const char = [ constchar("HARECACHE=mod\0"), null ]; - execve(constchar(path), &argv, &envp); + execve(constchar(path), *(&argv: **[*]nullable *const char), &envp); abort(); } else { assert(child != -1, "fork(2) failed"); diff --git a/rt/cstrings.ha b/rt/cstrings.ha @@ -8,3 +8,10 @@ fn constchar(s: str) *const char = { let s = &s: *string; return s.data: *const char; }; + +fn alloc_constchar(s: str) *const char = { + let c: []char = alloc([], len(s) + 1); + append(c, *(&s: *[]char)...); + append(c, 0); + return constchar(*(&c: *str)); +};