diff options
author | Kimplul <kimi.h.kuparinen@gmail.com> | 2025-03-30 22:36:53 +0300 |
---|---|---|
committer | Kimplul <kimi.h.kuparinen@gmail.com> | 2025-03-30 22:41:21 +0300 |
commit | 957da9056c36a5eea15c6058701f7465b31f64a8 (patch) | |
tree | 7006d7c4ce258e88533e3b0347078a0264fe1bf3 | |
parent | c87f5a8871edf6880b894a00b180c554ffd46d0a (diff) | |
download | fwd-master.tar.gz fwd-master.zip |
+ C allows for a bit more control, and we can manually handle closure
contexts. For example `examples/fib.fwd` now works for effectively any
`n`, pretty cool.
+ Fairly slow Fibonacci, I must admit. Initial profiling indicates it's
mainly due to branch mispredictions, but I'll have to look into this a
bit deeper.
+ The code is a bit hacked together, for now I'm more interested in
getting things working, I'll worry about making things pretty later.
+ For testing, there's also initial support for modules, just so I can
print stuff to the terminal
+ This commit is way too big, lol
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | Makefile | 1 | ||||
m--------- | deps/conts | 0 | ||||
-rw-r--r-- | examples/fib.fwd | 31 | ||||
-rw-r--r-- | examples/sum.fwd | 23 | ||||
-rw-r--r-- | include/fwd/ast.h | 40 | ||||
-rw-r--r-- | include/fwd/compiler.h | 1 | ||||
-rw-r--r-- | include/fwd/mod.h | 111 | ||||
-rw-r--r-- | include/fwd/path.h | 38 | ||||
-rw-r--r-- | include/fwd/scope.h | 5 | ||||
-rw-r--r-- | lib/fwd.h | 66 | ||||
-rw-r--r-- | mod/Makefile | 4 | ||||
-rw-r--r-- | mod/io.c | 31 | ||||
-rw-r--r-- | scripts/makefile | 4 | ||||
-rw-r--r-- | src/analyze.c | 276 | ||||
-rw-r--r-- | src/ast.c | 39 | ||||
-rw-r--r-- | src/compiler.c | 120 | ||||
-rw-r--r-- | src/debug.c | 32 | ||||
-rw-r--r-- | src/lexer.l | 2 | ||||
-rw-r--r-- | src/lower.c | 1072 | ||||
-rw-r--r-- | src/move.c | 137 | ||||
-rw-r--r-- | src/parser.y | 52 | ||||
-rw-r--r-- | src/path.c | 66 | ||||
-rw-r--r-- | src/scope.c | 10 |
24 files changed, 1390 insertions, 772 deletions
@@ -2,6 +2,7 @@ deps.mk docs/output build fwd +*.so gen/*.c gen/*.inc !include/fwd @@ -1,6 +1,7 @@ .PHONY: all all: setup $(MAKE) -f scripts/makefile + $(MAKE) -C mod # this kicks all unrecognised targets to the client script. # note that trying to compile individual files, e.g. diff --git a/deps/conts b/deps/conts -Subproject 56edd66ae3e8661d40d499bd7c72f3ffc7aac4e +Subproject be71a36fd88941a5bc3f25d2e563c8aa0481327 diff --git a/examples/fib.fwd b/examples/fib.fwd index 7084b9d..e6d6ee6 100644 --- a/examples/fib.fwd +++ b/examples/fib.fwd @@ -1,25 +1,30 @@ -/* heh, this technically speaking works, but I guess the compiler can't collapse - * frames so the bifurcating nature of fibonacci just gets mapped to a linear - * sequence of calls, taking up way more stack space than a typical, returning, - * function */ +/* + * Currently the compilation process requires a bit of manual intervention. + * For this particular example, run something like this from the root dir: + * + * ./fwd examples/fib.fwd > /tmp/fib.c + * gcc -Lmod -Iinclude -Ilib -Wl,-rpath=mod -O2 /tmp/fib.c -lfwdio -o /tmp/fib + * /tmp/fib + * + */ -fib(int n, (int) res) +/* modules are just libraries that can be loaded at runtime */ +import "../mod/libfwdio.so" + +fib(i64 n, (i64) res) { if n < 2 { res(1); } else { - fib(n - 1) => int f1; - fib(n - 2) => int f2; + fib(n - 1) => i64 f1; + fib(n - 2) => i64 f2; res(f1 + f2); } } -/* 'extern' println */ -fwd_println(auto n); -fwd_copy(auto n, (auto, auto) n1); - main() { - fib(6) => int n; - fwd_println(n); + fib(42) => i64 n; + print_i64(n); + print_nl(); } diff --git a/examples/sum.fwd b/examples/sum.fwd new file mode 100644 index 0000000..89a2d11 --- /dev/null +++ b/examples/sum.fwd @@ -0,0 +1,23 @@ +print_int(i64 a); +print_nl(); + +sum_inner(i64 s, i64 n, (i64) res) +{ + if n <= 0 { + res(s); + } else { + sum_inner(s + n, n - 1, res); + } +} + +sum(i64 n, (i64) res) +{ + sum_inner(0, n, res); +} + +main() +{ + sum(1000000000) => i64 s; + print_int(s); + print_nl(); +} diff --git a/include/fwd/ast.h b/include/fwd/ast.h index 36aa8c8..69f7bf1 100644 --- a/include/fwd/ast.h +++ b/include/fwd/ast.h @@ -33,7 +33,9 @@ struct ast; enum type_kind { TYPE_ID = 1, TYPE_CONSTRUCT, TYPE_REF, TYPE_PTR, TYPE_CLOSURE, - TYPE_PURE_CLOSURE, TYPE_FUNC_PTR, TYPE_VOID, TYPE_ERR + TYPE_PURE_CLOSURE, TYPE_FUNC_PTR, TYPE_VOID, + TYPE_I8, TYPE_I16, TYPE_I32, TYPE_I64, + TYPE_U8, TYPE_U16, TYPE_U32, TYPE_U64 }; struct type { @@ -72,12 +74,6 @@ enum ast_kind { AST_IF, /** Nil check */ AST_NIL, - /* Owning check */ - AST_OWN, - /** Err branch */ - AST_ERR_BRANCH, - /** Error throwing */ - AST_ERROR, /** Call procedure. */ AST_CALL, /** Procedure definition. */ @@ -152,6 +148,7 @@ enum ast_flag { AST_FLAG_PREANALYZIS = (1 << 1), AST_FLAG_NOMOVES = (1 << 2), AST_FLAG_PUBLIC = (1 << 3), + AST_REQ_FRAME = (1 << 4), }; struct ast { @@ -286,9 +283,16 @@ static inline bool is_lvalue(struct ast *node) static inline bool is_trivially_copyable(struct type *type) { switch (type->k) { + case TYPE_I8: + case TYPE_U8: + case TYPE_I16: + case TYPE_I32: + case TYPE_I64: + case TYPE_U16: + case TYPE_U32: + case TYPE_U64: case TYPE_REF: case TYPE_PTR: - case TYPE_ERR: case TYPE_FUNC_PTR: case TYPE_PURE_CLOSURE: /** @todo primitive types */ @@ -389,9 +393,8 @@ static inline bool is_trivially_copyable(struct type *type) #define call_expr(x) return_a0(x, AST_CALL) #define call_args(x) return_a1(x, AST_CALL) -#define call_err(x) return_a2(x, AST_CALL) -#define gen_call(expr, args, err, loc) \ - gen3(AST_CALL, expr, args, err, loc) +#define gen_call(expr, args, loc) \ + gen2(AST_CALL, expr, args, loc) #define proc_id(x) return_s(x, AST_PROC_DEF) #define proc_params(x) return_a0(x, AST_PROC_DEF) @@ -410,20 +413,9 @@ static inline bool is_trivially_copyable(struct type *type) #define gen_var(id, type, loc) \ gen_ast(AST_VAR_DEF, NULL, NULL, NULL, NULL, type, id, 0, 0., loc) -#define err_branch_id(x) return_s(x, AST_ERR_BRANCH) -#define err_branch_body(x) return_a0(x, AST_ERR_BRANCH) -#define gen_err_branch(id, body, loc) \ - gen_ast(AST_ERR_BRANCH, body, NULL, NULL, NULL, NULL, id, 0, 0., loc) - -#define error_str(x) return_s(x, AST_ERROR) -#define error_id(x) return_a0(x, AST_ERROR) -#define gen_error(str, id, loc) \ - gen_ast(AST_ERROR, id, NULL, NULL, NULL, NULL, str, 0, 0., loc) - #define block_body(x) return_a0(x, AST_BLOCK) -#define block_error(x) return_a1(x, AST_BLOCK) -#define gen_block(body, err, loc) \ - gen2(AST_BLOCK, body, err, loc) +#define gen_block(body, loc) \ + gen1(AST_BLOCK, body, loc) #define trait_id(x) return_s(x, AST_TRAIT_DEF) #define trait_body(x) return_a1(x, AST_TRAIT_DEF) diff --git a/include/fwd/compiler.h b/include/fwd/compiler.h index b7f1569..ac4d7f5 100644 --- a/include/fwd/compiler.h +++ b/include/fwd/compiler.h @@ -21,5 +21,6 @@ * @return \c 0 if compilation was succesful, otherwise some non-zero value. */ int compile(const char *input); +struct scope *compile_file(const char *file); #endif /* FWD_COMPILER_H */ diff --git a/include/fwd/mod.h b/include/fwd/mod.h new file mode 100644 index 0000000..6031c92 --- /dev/null +++ b/include/fwd/mod.h @@ -0,0 +1,111 @@ +#ifndef FWD_MOD_H +#define FWD_MOD_H + +#include <stddef.h> +#include <assert.h> +#include <stdlib.h> + +typedef enum fwd_type { + FWD_VOID, + FWD_I8, + FWD_I16, + FWD_I32, + FWD_I64, + FWD_U8, + FWD_U16, + FWD_U32, + FWD_U64, + FWD_PTR, + /** @todo others */ + FWD_END /* end of types, IDs above this are custom types I guess? */ +} fwd_type_t; + +typedef struct fwd_arg { + fwd_type_t t; + union { + int64_t i64; + void *p; + }; +} fwd_arg_t; + +/** @todo I hate this name */ +typedef struct fwd_extern_args { + size_t argc; + struct fwd_arg *args; +} fwd_extern_args_t; + +typedef struct fwd_state fwd_state_t; +typedef long (*fwd_extern_t)(fwd_extern_args_t args); +typedef long (*fwd_open_t)(fwd_state_t *state); + +extern int fwd_register(struct fwd_state *state, const char *name, fwd_extern_t func, fwd_type_t rtype, ...); + +#define FWD_REGISTER(state, func, rtype, ...) \ + fwd_register(state, #func, func, rtype __VA_OPT__(,) __VA_ARGS__, FWD_END) + +static inline void *fwd_arg(fwd_extern_args_t args, size_t idx, fwd_type_t id) +{ + assert(idx < args.argc); + assert(args.args[idx + 1].t == id); + switch (id) { + case FWD_I64: return &args.args[idx + 1].i64; + default: + } + + return NULL; +} + +#define FWD_ARG(args, idx, type, id) \ + *(type *)fwd_arg(args, idx, id) + +#define FWD_ARG_T(args, idx, type) \ + FWD_ARG(args, idx, type, FWD_T(type)) + +static inline fwd_type_t fwd_t_signed(size_t s) +{ + switch (s) { + case 1: return FWD_I8; + case 2: return FWD_I16; + case 4: return FWD_I32; + case 8: return FWD_I64; + } + + abort(); + return FWD_END; +} + +static inline fwd_type_t fwd_t_unsigned(size_t s) +{ + switch (s) { + case 1: return FWD_U8; + case 2: return FWD_U16; + case 4: return FWD_U32; + case 8: return FWD_U64; + } + + abort(); + return FWD_END; +} + +static inline fwd_type_t fwd_t_ptr(size_t s) +{ + (void)s; + return FWD_PTR; +} + +#define FWD_T(type) \ + _Generic((type)(0),\ + signed char : fwd_t_signed, \ + signed short : fwd_t_signed, \ + signed int : fwd_t_signed, \ + signed long : fwd_t_signed, \ + signed long long : fwd_t_signed, \ + unsigned char : fwd_t_unsigned, \ + unsigned short : fwd_t_unsigned, \ + unsigned int : fwd_t_unsigned, \ + unsigned long : fwd_t_unsigned, \ + unsigned long long : fwd_t_signed, \ + default: fwd_t_ptr)(sizeof(type)) + + +#endif /* FWD_MOD_H */ diff --git a/include/fwd/path.h b/include/fwd/path.h new file mode 100644 index 0000000..277b33e --- /dev/null +++ b/include/fwd/path.h @@ -0,0 +1,38 @@ +/* SPDX-License-Identifier: copyleft-next-0.3.1 */ +/* Copyright 2023 Kim Kuparinen < kimi.h.kuparinen@gmail.com > */ + +#ifndef FWD_PATH_H +#define FWD_PATH_H + +/** + * @file path.h + * + * Path handling helpers. + */ + +/** + * Get basename of file path. + * E.g. src/some/file.c -> file.c + * + * @param file File path to get basename from. + * @return Basename of \p file. + */ +char *fwd_basename(const char *file); + +/** + * Get directory name of path. + * E.g. src/some/file.c -> src/some + * + * @param file File path to get dirname from. + * @return Dirname of \p file. + */ +char *fwd_dirname(const char *file); + +/** + * Get current working directory. + * + * @return Current working directory. + */ +char *fwd_cwdname(); + +#endif /* FWD_PATH */ diff --git a/include/fwd/scope.h b/include/fwd/scope.h index ebe0261..ab268bc 100644 --- a/include/fwd/scope.h +++ b/include/fwd/scope.h @@ -24,6 +24,10 @@ enum scope_flags { #define MAP_NAME visible #include <conts/map.h> +#define VEC_NAME mod_vec +#define VEC_TYPE void * +#include <conts/vec.h> + /** * Scope. * Responsible for keeping track of visibilities and @@ -54,6 +58,7 @@ struct scope { struct visible symbols; struct visible traits; struct visible types; + struct mod_vec mods; enum scope_flags flags; }; diff --git a/lib/fwd.h b/lib/fwd.h new file mode 100644 index 0000000..54d75a2 --- /dev/null +++ b/lib/fwd.h @@ -0,0 +1,66 @@ +#ifndef FWD_H +#define FWD_H + +#include <fwd/mod.h> +#include <stddef.h> +#include <stdint.h> +#include <stdlib.h> +#include <assert.h> + +#ifndef FWD_FRAME_SIZE +#error "no frame size defined, internal compiler error" +#endif + +/* should probably add some compiler checks but good enough for now */ +#define FWD_MUSTTAIL [[clang::musttail]] + +typedef void *fwd_stack_t; +typedef void *fwd_args_t; +typedef fwd_stack_t (*fwd_call_t)(fwd_stack_t, fwd_args_t); + +typedef struct { + fwd_call_t call; + fwd_args_t args; +} fwd_closure_t; + +typedef struct {} fwd_start_t; + +static inline fwd_stack_t create_fwd_stack() +{ + return NULL; +} + +static inline void *fwd_stack_alloc(fwd_stack_t *top) +{ + if (*top) { + fwd_stack_t *prev = *top; + *top = *prev; + return prev + 1; + } + + fwd_stack_t *new = malloc(FWD_FRAME_SIZE + sizeof(fwd_stack_t)); + *new = NULL; + return new + 1; +} + +static inline void fwd_stack_free(fwd_stack_t *stack, void *loc) +{ + fwd_stack_t *freed = (fwd_stack_t *)loc - 1; + *freed = *stack; + *stack = freed; +} + +static inline void destroy_fwd_stack(fwd_stack_t *stack) +{ + fwd_stack_t *prev = *stack; + while (prev) { + fwd_stack_t *cur = prev; + prev = *cur; + free(cur); + } +} + +#define FWD_CONTAINER_OF(ptr, type, member) \ + (type *)((char *)(ptr) - offsetof(type, member)) + +#endif /* FWD_H */ diff --git a/mod/Makefile b/mod/Makefile new file mode 100644 index 0000000..0a0c045 --- /dev/null +++ b/mod/Makefile @@ -0,0 +1,4 @@ +all: libfwdio.so + +libfwdio.so: io.c ../include/fwd/mod.h + $(CC) -I../include -fPIC -O2 -g -Wall -Wextra -shared io.c -o libfwdio.so diff --git a/mod/io.c b/mod/io.c new file mode 100644 index 0000000..380566f --- /dev/null +++ b/mod/io.c @@ -0,0 +1,31 @@ +#include <stdint.h> +#include <assert.h> +#include <stdio.h> + +#include <fwd/mod.h> + +long print_nl(fwd_extern_args_t args) +{ + assert(args.argc == 0); + putchar('\n'); + return 0; +} + +long print_i64(fwd_extern_args_t args) +{ + assert(args.argc == 1); + int64_t n = FWD_ARG_T(args, 0, int64_t); + printf("%lld", (long long)n); + return 0; +} + +int fwdopen(fwd_state_t *state) +{ + FWD_REGISTER(state, print_nl, + FWD_VOID); + + FWD_REGISTER(state, print_i64, + FWD_VOID, FWD_T(int64_t)); + + return 0; +} diff --git a/scripts/makefile b/scripts/makefile index 11229bd..ae42506 100644 --- a/scripts/makefile +++ b/scripts/makefile @@ -42,7 +42,9 @@ COMPILER != [ -n "$(CROSS_COMPILE)" ] \ || echo $(CC) -OBFLAGS := -g +# -rdynamic is apparently platform-dependent, not sure if I want to rely on it +# but good enough for now +OBFLAGS := -g -rdynamic WARNFLAGS := -Wall -Wextra COMPILE_FLAGS := $(CFLAGS) $(WARNFLAGS) $(OPTFLAGS) $(OBFLAGS) $(ASSERTFLAGS) \ diff --git a/src/analyze.c b/src/analyze.c index 0c06096..be52bfb 100644 --- a/src/analyze.c +++ b/src/analyze.c @@ -1,12 +1,21 @@ /* SPDX-License-Identifier: copyleft-next-0.3.1 */ /* Copyright 2024 Kim Kuparinen < kimi.h.kuparinen@gmail.com > */ +#include <fwd/compiler.h> #include <fwd/analyze.h> +#include <fwd/mod.h> #include <stdlib.h> +#include <stdarg.h> #include <string.h> #include <assert.h> +#include <dlfcn.h> + +enum state_flags { + STATE_PROC_REQ_FRAME = (1 << 0) +}; struct state { + enum state_flags flags; }; static int analyze(struct state *state, struct scope *scope, struct ast *node); @@ -62,7 +71,13 @@ static int analyze_proc(struct state *state, struct scope *scope, if (!proc_body(node)) return 0; - return analyze_known_block(&proc_state, proc_scope, proc_body(node)); + if (analyze_known_block(&proc_state, proc_scope, proc_body(node))) + return -1; + + if (proc_state.flags & STATE_PROC_REQ_FRAME) + ast_set_flags(node, AST_REQ_FRAME); + + return 0; } static int analyze_unop(struct state *state, struct scope *scope, @@ -144,28 +159,6 @@ static int analyze_known_block(struct state *state, struct scope *scope, struct if (analyze_list(state, scope, block_body(node))) return -1; - struct ast *err = block_error(node); - if (!err) - return 0; - - if (error_str(err)) - return 0; - - struct ast *id = error_id(err); - assert(id); - - struct ast *def = file_scope_find_symbol(scope, id_str(id)); - if (!def) { - semantic_error(scope, id, "no such symbol"); - return -1; - } - - struct type *t = def->t; - if (t->k != TYPE_ERR) { - semantic_error(scope, id, "invalid error variable type"); - return -1; - } - return 0; } @@ -261,6 +254,9 @@ static int analyze_call(struct state *state, struct scope *scope, struct type *type = types; foreach_node(arg, args) { + if (arg->k == AST_CLOSURE) + state->flags |= STATE_PROC_REQ_FRAME; + if (!types_match(type, arg->t)) { type_mismatch(scope, node, type, arg->t); return -1; @@ -277,7 +273,7 @@ static int analyze_call(struct state *state, struct scope *scope, return -1; } - return analyze(state, scope, call_err(node)); + return 0; } static int analyze_ref(struct state *state, struct scope *scope, @@ -396,17 +392,10 @@ static int analyze_int(struct state *state, struct scope *scope, (void)state; (void)scope; - /** @todo do this properly, very hacky, bad bad bad */ - char *i = strdup("int"); - if (!i) { - internal_error("failed allocating constant int type string"); - return -1; - } - - node->t = tgen_id(i, node->loc); + /* start with largest possible and work down */ + node->t = tgen_type(TYPE_I64, NULL, NULL, node->loc); if (!node->t) { internal_error("failed allocating constant int type"); - free(i); return -1; } @@ -464,40 +453,6 @@ static int analyze_if(struct state *state, struct scope *scope, return 0; } -static int analyze_err_branch(struct state *state, struct scope *scope, struct ast *node) -{ - struct scope *branch_scope = create_scope(); - if (!branch_scope) { - internal_error("failed allocating err branch scope"); - return -1; - } - - node->t = tgen_void(node->loc); - - scope_add_scope(scope, branch_scope); - struct ast *var = gen_var(strdup(err_branch_id(node)), NULL, node->loc); - struct type *err_type = tgen_err(node->loc); - var->t = err_type; - scope_add_symbol(branch_scope, var); - return analyze(state, branch_scope, err_branch_body(node)); -} - -static int analyze_own(struct state *state, struct scope *scope, struct ast *node) -{ - struct ast *found = file_scope_find_symbol(scope, own_id(node)); - if (!found) { - semantic_error(scope, node, "no symbol named \"%s\"", - own_id(node)); - return -1; - } - - if (is_trivially_copyable(found->t)) - semantic_warn(scope, node, "trivially copyable type is never owned"); - - node->t = tgen_void(node->loc); - return analyze(state, scope, own_body(node)); -} - static int analyze(struct state *state, struct scope *scope, struct ast *node) { if (!node) @@ -535,7 +490,6 @@ static int analyze(struct state *state, struct scope *scope, struct ast *node) } switch (node->k) { - case AST_ERR_BRANCH: ret = analyze_err_branch(state, scope, node); break; case AST_CLOSURE: ret = analyze_closure(state, scope, node); break; case AST_PROC_DEF: ret = analyze_proc(state, scope, node); break; case AST_VAR_DEF: ret = analyze_var(state, scope, node); break; @@ -545,7 +499,6 @@ static int analyze(struct state *state, struct scope *scope, struct ast *node) case AST_CALL: ret = analyze_call(state, scope, node); break; case AST_REF: ret = analyze_ref(state, scope, node); break; case AST_LET: ret = analyze_let(state, scope, node); break; - case AST_OWN: ret = analyze_own(state, scope, node); break; case AST_ID: ret = analyze_id(state, scope, node); break; case AST_IF: ret = analyze_if(state, scope, node); break; @@ -556,6 +509,12 @@ static int analyze(struct state *state, struct scope *scope, struct ast *node) node->t = tgen_void(node->loc); ret = 0; break; + + case AST_IMPORT: + node->t = tgen_void(node->loc); + ret = 0; + break; + default: internal_error("missing ast analysis for %s", ast_str(node->k)); return -1; @@ -585,11 +544,43 @@ static int analyze_list(struct state *state, struct scope *scope, static int analyze_type(struct state *state, struct scope *scope, struct type *type) { - /* for now, let's just say all types are fine as they are, specified by - * the user. */ - (void)state; - (void)scope; - (void)type; + if (type->t0 && analyze_type(state, scope, type->t0)) + return -1; + + /* temporary */ + if (type->k != TYPE_ID) + return 0; + + char *id = type->id; + + if (strcmp(id, "i8") == 0) + type->k = TYPE_I8; + + else if (strcmp(id, "u8") == 0) + type->k = TYPE_U8; + + else if (strcmp(id, "i16") == 0) + type->k = TYPE_I16; + + else if (strcmp(id, "u16") == 0) + type->k = TYPE_U16; + + else if (strcmp(id, "i32") == 0) + type->k = TYPE_I32; + + else if (strcmp(id, "u32") == 0) + type->k = TYPE_U32; + + else if (strcmp(id, "i64") == 0) + type->k = TYPE_I64; + + else if (strcmp(id, "u64") == 0) + type->k = TYPE_U64; + else { + internal_error("unhandled type id: %s", id); + abort(); + } + return 0; } @@ -604,6 +595,133 @@ static int analyze_type_list(struct state *state, struct scope *scope, return 0; } +static int copy_scope(bool reexport, struct scope *to, struct scope *from) +{ + foreach(visible, symbol, &from->symbols) { + struct ast *def = symbol->data; + if (!reexport && def->scope != from) + continue; + + if (!ast_flags(def, AST_FLAG_PUBLIC)) + continue; + + if (scope_add_symbol(to, def)) + return -1; + } + + foreach(visible, symbol, &from->types) { + struct ast *def = symbol->data; + if (!reexport && def->scope != from) + continue; + + if (!ast_flags(def, AST_FLAG_PUBLIC)) + continue; + + if (scope_add_symbol(to, def)) + return -1; + } + + foreach(visible, symbol, &from->traits) { + struct ast *def = symbol->data; + if (!reexport && def->scope != from) + continue; + + if (!ast_flags(def, AST_FLAG_PUBLIC)) + continue; + + if (scope_add_symbol(to, def)) + return -1; + } + + return 0; +} + +/* allowed to be noisy */ +static int try_import_file(struct scope *scope, struct ast *node) +{ + const char *file = import_file(node); + struct scope *child = compile_file(file); + if (!child) { + semantic_info(scope, node, "imported here"); + return -1; + } + + if (copy_scope(ast_flags(node, AST_FLAG_PUBLIC), scope, child)) { + semantic_info(scope, node, "imported here"); + return -1; + } + + return 0; +} + +/* should be quiet upon failure */ +static int try_import_mod(struct scope *scope, struct ast *node) +{ + /** @todo cleanup */ + void *mod = dlopen(import_file(node), RTLD_LAZY); + if (!mod) + return -1; + + fwd_open_t fwdopen = dlsym(mod, "fwdopen"); + if (!fwdopen) { + dlclose(mod); + return -1; + } + + mod_vec_append(&scope->mods, mod); + return fwdopen((void *)scope); +} + +static struct type *fwd_type_kind(fwd_type_t type) +{ + switch (type) { + case FWD_VOID: return tgen_void(NULL_LOC()); + case FWD_I64: return tgen_type(TYPE_I64, NULL, NULL, NULL_LOC()); + case FWD_PTR: { + struct type *base = tgen_void(NULL_LOC()); + return tgen_ptr(base, NULL_LOC()); + } + + default: + break; + } + + abort(); + return NULL; +} + +int fwd_register(struct fwd_state *state, const char *name, fwd_extern_t func, fwd_type_t rtype, ...) +{ + struct scope *scope = (void *)state; + struct ast *vars = NULL; + + va_list args; + va_start(args, rtype); + while (1) { + fwd_type_t type = va_arg(args, enum fwd_type); + if (type == FWD_END) + break; + + struct ast *new = gen_var(strdup("arg"), fwd_type_kind(type), NULL_LOC()); + + if (vars) + vars->n = new; + else + vars = new; + } + va_end(args); + + vars = reverse_ast_list(vars); + + struct ast *def = gen_proc(strdup(name), vars, + fwd_type_kind(rtype), NULL, NULL_LOC()); + + if (scope_add_symbol(scope, def)) + return -1; + + return 0; +} + int analyze_root(struct scope *scope, struct ast *root) { foreach_node(node, root) { @@ -628,6 +746,16 @@ int analyze_root(struct scope *scope, struct ast *root) return -1; break; + case AST_IMPORT: { + if (!try_import_mod(scope, node)) + break; + + if (!try_import_file(scope, node)) + break; + + return -1; + } + default: abort(); } @@ -214,9 +214,6 @@ void ast_dump(int depth, struct ast *n) DUMP(AST_CLOSURE); DUMP(AST_IF); DUMP(AST_NIL); - DUMP(AST_OWN); - DUMP(AST_ERR_BRANCH); - DUMP(AST_ERROR); DUMP(AST_LET); DUMP(AST_INIT); DUMP(AST_CALL); @@ -299,6 +296,9 @@ struct ast *clone_ast(struct ast *n) assert(n->k); struct ast *new = create_empty_ast(); + if (!new) + return NULL; + new->scope = n->scope; new->loc = n->loc; new->k = n->k; @@ -574,50 +574,34 @@ void fix_closures(struct ast *root) assert(next); - struct ast *block = gen_block(next, NULL, root->loc); + struct ast *block = gen_block(next, root->loc); closure_body(arg) = block; root->n = NULL; root = next; } } -static bool special_auto_very_bad(struct type *a, struct type *b) -{ - /** @todo massive hack, accept 'auto' as a placeholder and match it - * against anything, will need to be fixed eventually */ - if (a->k == TYPE_ID && strcmp(a->id, "auto") == 0) - return true; - - if (b->k == TYPE_ID && strcmp(b->id, "auto") == 0) - return true; - - return false; -} - bool types_match(struct type *a, struct type *b) { - if (!a && !b) - return true; - if (a && !b) return false; if (!a && b) return false; - if (special_auto_very_bad(a, b)) - return true; - if (a->k != b->k) return false; - if (a->id && b->id && strcmp(a->id, b->id) != 0) + if (!a->t0 && !b->t0) + return true; + + if (a->t0 && !b->t0) return false; - if (!type_lists_match(a->t0, b->t0)) + if (!a->t0 && b->t0) return false; - return true; + return type_lists_match(a->t0, b->t0); } bool type_lists_match(struct type *al, struct type *bl) @@ -649,9 +633,6 @@ const char *ast_str(enum ast_kind k) CASE(AST_CLOSURE); CASE(AST_IF); CASE(AST_NIL); - CASE(AST_OWN); - CASE(AST_ERR_BRANCH); - CASE(AST_ERROR); CASE(AST_LET); CASE(AST_INIT); CASE(AST_CALL); diff --git a/src/compiler.c b/src/compiler.c index 9f564d8..5de992d 100644 --- a/src/compiler.c +++ b/src/compiler.c @@ -19,8 +19,9 @@ #include <fwd/analyze.h> #include <fwd/parser.h> #include <fwd/debug.h> -#include <fwd/scope.h> #include <fwd/lower.h> +#include <fwd/scope.h> +#include <fwd/path.h> #include <fwd/move.h> /** @@ -65,11 +66,11 @@ static char *read_file(const char *file, FILE *f) * @param file File name to process. * @return \c 0 if processing was succesful, non-zero value otherwise. */ -static int process(struct scope **parent, const char *file) +static int process(struct scope *scope, const char *file) { FILE *f = fopen(file, "rb"); if (!f) { - error("failed opening %s: %s\n", file, strerror(errno)); + error("failed opening %s: %s", file, strerror(errno)); return -1; } @@ -86,30 +87,20 @@ static int process(struct scope **parent, const char *file) } parse(p, file, buf); + struct ast *tree = p->tree; bool failed = p->failed; destroy_parser(p); if (failed) { - free((void*)buf); + free((void *)buf); return -1; } ast_dump_list(0, tree); - struct scope *scope = create_scope(); - if (!scope) { - free((void *)buf); - return -1; - } - scope->fctx.fbuf = buf; scope->fctx.fname = strdup(file); - if (*parent) - scope_add_scope(*parent, scope); - else - *parent = scope; - if (analyze_root(scope, tree)) return -1; @@ -119,24 +110,101 @@ static int process(struct scope **parent, const char *file) return 0; } +#define MAP_KEY const char * +#define MAP_TYPE struct scope * +#define MAP_CMP(a, b) strcmp((a), (b)) +#define MAP_HASH(a) CONTS_MAP_STR_HASH(a) +#define MAP_NAME scopes +#include <conts/map.h> + +/* ugly global for now */ +static struct scopes scopes; + +static void destroy_scopes() +{ + foreach(scopes, n, &scopes) { + destroy_scope(n->data); + free((void *)n->key); + } + + scopes_destroy(&scopes); +} + +struct scope *compile_file(const char *file) +{ + struct scope *scope = NULL; + const char *base = NULL, *dir = NULL, *cwd = NULL, *real = NULL; + if (!(base = fwd_basename(file))) { + error("couldn't get basename of %s", file); + goto out; + } + + if (!(dir = fwd_dirname(file))) { + error("couldn't get dirname of %s", file); + goto out; + } + + if (!(cwd = fwd_cwdname())) { + error("couldn't get current working dir"); + goto out; + } + + if (*dir != 0 && chdir(dir)) { + error("couldn't change to directory %s: %s", dir, + strerror(errno)); + goto out; + } + + if (!(real = realpath(base, NULL))) { + error("no such file: %s", file); + goto out; + } + + struct scope **exists = scopes_find(&scopes, real); + if (exists) { + scope = *exists; + goto out; + } + + scope = create_scope(); + scopes_insert(&scopes, strdup(real), scope); + + if (process(scope, base)) { + scope = NULL; + goto out; + } + + if (chdir(cwd)) { + error("couldn't change back to directory %s: %s", cwd, + strerror(errno)); + goto out; + } + +out: + free((void *)base); + free((void *)dir); + free((void *)cwd); + free((void *)real); + return scope; +} + int compile(const char *input) { + scopes = scopes_create(1); + int ret = -1; - struct scope *root = NULL; - if (process(&root, input)) { - destroy_scope(root); - destroy_allocs(); + struct scope *root = compile_file(input); + if (!root) { error("processing of %s stopped due to errors", input); - return ret; + goto out; } if ((ret = lower(root))) { - destroy_scope(root); - destroy_allocs(); - error("lowering of %s stopped due to errors", input); - return ret; + error("lowering of %s failed due to errors", input); + goto out; } - destroy_scope(root); +out: + destroy_scopes(); destroy_allocs(); - return 0; + return ret; } diff --git a/src/debug.c b/src/debug.c index b13a459..f3540f0 100644 --- a/src/debug.c +++ b/src/debug.c @@ -177,8 +177,36 @@ static void _type_str(FILE *f, struct type *type) return; switch (type->k) { - case TYPE_ERR: - fprintf(f, "err"); + case TYPE_I8: + fprintf(f, "i8"); + break; + + case TYPE_U8: + fprintf(f, "u8"); + break; + + case TYPE_I16: + fprintf(f, "i16"); + break; + + case TYPE_U16: + fprintf(f, "u16"); + break; + + case TYPE_I32: + fprintf(f, "i32"); + break; + + case TYPE_U32: + fprintf(f, "u32"); + break; + + case TYPE_I64: + fprintf(f, "i64"); + break; + + case TYPE_U64: + fprintf(f, "u64"); break; case TYPE_VOID: diff --git a/src/lexer.l b/src/lexer.l index e6a3f22..24bda93 100644 --- a/src/lexer.l +++ b/src/lexer.l @@ -122,7 +122,6 @@ STRING \"(\\.|[^"\\])*\" "==" {return EQ;} "=>" {return FATARROW;} -"!>" {return ERRARROW;} "<<" {return LSHIFT;} ">>" {return RSHIFT;} @@ -130,7 +129,6 @@ STRING \"(\\.|[^"\\])*\" "if" {return IF;} "else" {return ELSE;} "nil" {return NIL;} -"own" {return OWN;} "pub" {return PUB;} "import" {return IMPORT;} diff --git a/src/lower.c b/src/lower.c index 89d9aca..a31564f 100644 --- a/src/lower.c +++ b/src/lower.c @@ -5,18 +5,112 @@ #include <stdlib.h> #include <string.h> #include <stdarg.h> +#include <stdint.h> #include <assert.h> #include <fwd/lower.h> #include <fwd/scope.h> +#include <fwd/mod.h> + +/* stuff to be careful of: + * + * + Really easy to forget to pass the stack argument via reference, since it's + * just a void* we don't get a warning for it + * + * + lower_call() and lower_closure_call() share a lot of the same stuff, might + * want to try and merge them somehow, otherwise too easy to make a change in + * one and forget the other + */ + +/* placeholder, should probably scale according to biggest need */ +#define FWD_FRAME_SIZE 1024 + +static inline void mangle(FILE *f, struct ast *id) +{ + assert(id->k == AST_PROC_DEF || id->k == AST_VAR_DEF || id->k == AST_ID); + assert(id->s && id->scope); + fprintf(f, "%s_s%zu", id->s, id->scope->number); +} + +static inline char *mangle2(struct ast *id) +{ + char *buf = NULL; size_t size = 0; + FILE *f = open_memstream(&buf, &size); + assert(f); + + mangle(f, id); + fclose(f); + assert(buf); + + return buf; +} + +static inline __attribute__((format (printf, 1, 2))) +char *buildstr(const char *fmt, ...) +{ + va_list args; + va_start(args, fmt); + + char *buf = NULL; size_t size = 0; + FILE *f = open_memstream(&buf, &size); + assert(f); + + vfprintf(f, fmt, args); + fclose(f); + + va_end(args); + assert(buf); + return buf; +} /** @todo semantics in this file are a bit unclear, should probably do some kind * of "each function starts and ends on an indented empty line" or something */ +#define VEC_NAME string_vec +#define VEC_TYPE char * +#include <conts/vec.h> + +#define MAP_NAME proc_set +#define MAP_KEY struct ast * +#define MAP_TYPE struct ast * +#define MAP_CMP(a, b) ((uintptr_t)(a) - (uintptr_t)(b)) +#define MAP_HASH(a) ((uintptr_t)(a)) +#include <conts/map.h> + struct state { + char *prefix; long indent; + size_t uniq; + FILE *ctx; + FILE *code; + struct ast *current; + struct string_vec *decls; + struct string_vec *defns; + struct string_vec *types; + struct proc_set *procs; }; +static struct state create_state(struct state *parent) +{ + return (struct state){ + .uniq = parent->uniq, + .prefix = parent->prefix, + .indent = parent->indent, + .current = parent->current, + .ctx = parent->ctx, + .code = parent->code, + .decls = parent->decls, + .defns = parent->defns, + .types = parent->types, + .procs = parent->procs, + }; +} + +static size_t uniq(struct state *state) +{ + return state->uniq++; +} + static void increase_indent(struct state *state) { state->indent++; @@ -30,624 +124,748 @@ static void decrease_indent(struct state *state) static void indent(struct state *state) { if (state->indent != 0) - printf("%*c", (int)(2 * state->indent), ' '); + fprintf(state->code, "%*c", (int)(2 * state->indent), ' '); } -static int lower_var(struct ast *expr); +static bool proc_lowered(struct state *state, struct ast *proc) +{ + assert(proc->k == AST_PROC_DEF); + return proc_set_find(state->procs, proc) != NULL; +} -static int lower_expr(struct state *state, struct ast *expr); -static int lower_closure(struct state *state, struct ast *closure); +static void add_proc(struct state *state, struct ast *proc) +{ + assert(proc->k == AST_PROC_DEF); + struct ast **p = proc_set_insert(state->procs, proc, proc); + assert(p && *p == proc); +} -static int lower_block(struct state *state, struct ast *block, bool ret); -static int lower_statement(struct state *state, struct ast *stmt, bool ret); +static void add_decl(struct state *state, char *decl) +{ + string_vec_append(state->decls, decl); +} -static int lower_type(struct type *type); -static int lower_types(struct type *types); +static void add_defn(struct state *state, char *defn) +{ + string_vec_append(state->defns, defn); +} -static int lower_binop(struct state *state, struct ast *binop) +static void add_type(struct state *state, char *type) { - printf("("); - if (lower_expr(state, binop_left(binop))) - return -1; + string_vec_append(state->types, type); +} - switch (binop->k) { - case AST_ADD: printf(" + "); break; - case AST_SUB: printf(" - "); break; - case AST_MUL: printf(" * "); break; - case AST_DIV: printf(" / "); break; - case AST_REM: printf(" %% "); break; - case AST_LSHIFT: printf(" << "); break; - case AST_RSHIFT: printf(" >> "); break; - default: - internal_error("missing binop lowering"); - return -1; - } +static int lower_stmt_list(struct state *state, struct ast *stmt_list, bool last); +static int lower_block(struct state *state, struct ast *block, bool last); +static int lower_params(struct state *state, struct ast *params); +static int lower_expr(struct state *state, struct ast *expr); +static int lower_proc(struct state *state, struct ast *proc); - if (lower_expr(state, binop_right(binop))) - return -1; +static void _type_str(FILE *f, struct type *type) +{ + assert(type); + switch (type->k) { + case TYPE_I8: + fprintf(f, "int8_t"); + break; - printf(")"); - return 0; -} + case TYPE_U8: + fprintf(f, "uint8_t"); + break; -static int lower_comparison(struct state *state, struct ast *comp) -{ - printf("("); - if (lower_expr(state, comparison_left(comp))) - return -1; + case TYPE_I16: + fprintf(f, "int16_t"); + break; - switch (comp->k) { - case AST_LT: printf(" < "); break; - case AST_GT: printf(" > "); break; - case AST_LE: printf(" <= "); break; - case AST_GE: printf(" <= "); break; - case AST_NE: printf(" != "); break; - case AST_EQ: printf(" == "); break; - default: - internal_error("missing comparison lowering"); - return -1; - } + case TYPE_U16: + fprintf(f, "uint16_t"); + break; - if (lower_expr(state, comparison_right(comp))) - return -1; + case TYPE_I32: + fprintf(f, "int32_t"); + break; - printf(")"); - return 0; -} + case TYPE_U32: + fprintf(f, "uint32_t"); + break; -static int lower_exprs(struct state *state, struct ast *exprs) -{ - if (!exprs) - return 0; + case TYPE_I64: + fprintf(f, "int64_t"); + break; - if (lower_expr(state, exprs)) - return -1; + case TYPE_U64: + fprintf(f, "uint64_t"); + break; - foreach_node(expr, exprs->n) { - printf(", "); - if (lower_expr(state, expr)) - return -1; - } + case TYPE_CLOSURE: + fprintf(f, "fwd_closure_t"); + break; - return 0; + default: + internal_error("unhandled type lowering for %s", type_str(type)); + abort(); + break; + } } -static int lower_type_construct(struct type *type) +static char *lower_type_str(struct type *type) { - printf("%s", tconstruct_id(type)); - printf("<"); + assert(type); - if (lower_types(tconstruct_args(type))) - return -1; + char *type_buf = NULL; size_t type_len = 0; + FILE *f = open_memstream(&type_buf, &type_len); + assert(f); - printf(">"); - return 0; + _type_str(f, type); + fclose(f); + assert(type_buf); + return type_buf; } -static int lower_type_callable(struct type *type) +static int lower_closure_call(struct state *state, struct ast *call, struct ast *def, bool last) { - /* std::function has a slight overhead compared to just using auto here, - * but auto doesn't play well with recursive templates like with our - * fib.fwd example, so use std::function for now. Eventually I might - * instead write a C backend or something to have more control over the - * exact semantics, but for now this is good enough. */ - printf("std::function<fwd_err_t("); + char *q = buildstr("%s_call%zu", state->prefix, uniq(state)); + char *args = mangle2(def); - if (lower_types(type->t0)) - return -1; + char *ctx_buf = NULL; size_t ctx_size = 0; + FILE *ctx = open_memstream(&ctx_buf, &ctx_size); + fprintf(ctx, "struct %s {\n fwd_start_t start;\n", q); - printf(")>"); - return 0; -} + indent(state); + fprintf(state->code, "fwd_call_t %s = ctx->%s.call;\n", q, args); -static int lower_type_ref(struct type *type) -{ - if (lower_type(tref_base(type))) - return -1; + indent(state); + fprintf(state->code, "struct %s *%s_ctx = ctx->%s.args;\n", q, q, args); - printf("&"); - return 0; -} + int ret = 0; + size_t idx = 0; + bool returning = false; + foreach_node(a, call_args(call)) { + returning |= a->k == AST_CLOSURE; -static int lower_type_ptr(struct type *type) -{ - /* would I need parentheses in some cases? */ - if (lower_type(tptr_base(type))) - return -1; + char *type = lower_type_str(a->t); + fprintf(ctx, " %s a%zu;\n", type, idx); + free(type); - printf("*"); - return 0; -} + indent(state); + fprintf(state->code, "%s_ctx->a%zu = ", q, idx); + int ret = lower_expr(state, a); + fprintf(state->code, ";\n"); + if (ret) + goto out; -static int lower_type(struct type *type) -{ - switch (type->k) { - case TYPE_ID: printf("%s", tid_str(type)); return 0; - case TYPE_CONSTRUCT: return lower_type_construct(type); - case TYPE_CLOSURE: return lower_type_callable(type); - case TYPE_FUNC_PTR: return lower_type_callable(type); - case TYPE_PURE_CLOSURE: return lower_type_callable(type); - case TYPE_REF: return lower_type_ref(type); - case TYPE_PTR: return lower_type_ptr(type); - default: - internal_error("missing type lowering"); - return -1; + idx++; } - return 0; +out: + if (!returning && last && ast_flags(state->current, AST_REQ_FRAME)) { + indent(state); + fprintf(state->code, "fwd_stack_free(&stack, ctx);\n"); + } + + indent(state); + if (last) { + fprintf(state->code, "FWD_MUSTTAIL return %s(stack, %s_ctx);\n", + q, q); + } + else { + fprintf(state->code, "stack = %s_call(stack, %s_ctx);\n", + q, q); + } + + fprintf(ctx, "};\n\n"); + fclose(ctx); + assert(ctx_buf); + + add_type(state, ctx_buf); + free(args); + free(q); + return ret; } -static int lower_types(struct type *types) +static int lower_closure(struct state *state, struct ast *closure, char **name_out, char **args_out) { - if (!types) - return 0; + char *name = buildstr("%s_closure%zu", state->prefix, uniq(state)); + char *proto = buildstr("static fwd_stack_t %s(fwd_stack_t stack, fwd_args_t args)", name); + char *decl = buildstr("%s;\n", proto); + char *start_of = buildstr("%s_start", name); + add_decl(state, decl); - if (lower_type(types)) - return -1; + char *code_buf = NULL; + size_t code_size = 0; - foreach_type(type, types->n) { - printf(", "); - if (lower_type(type)) - return -1; - } + struct state new_state = create_state(state); + new_state.code = open_memstream(&code_buf, &code_size); + new_state.indent = 0; + assert(new_state.code); - return 0; -} + fprintf(new_state.code, "%s\n{\n", proto); -static int lower_init(struct state *state, struct ast *init) -{ - printf("%s", init_id(init)); + /** @todo unsure if this has the same address as the first element in + * the frame or the last in the previous, I guess we shall soon see */ + fprintf(new_state.ctx, " fwd_start_t %s;\n", start_of); - if (init_args(init)) { - printf("<"); - if (lower_types(init_args(init))) - return -1; + int ret = lower_params(&new_state, closure_bindings(closure)); + assert(ret == 0); - printf(">"); - } + increase_indent(&new_state); + indent(&new_state); + fprintf(new_state.code, "struct %s_ctx *ctx = FWD_CONTAINER_OF(args, struct %s_ctx, %s);\n", + state->prefix, state->prefix, start_of); - printf("{"); + struct ast *block = closure_body(closure); + ret = lower_stmt_list(&new_state, block_body(block), true); - if (lower_exprs(state, init_body(init))) - return -1; + fprintf(new_state.code, "}\n\n"); + fclose(new_state.code); + assert(code_buf); - printf("}"); - return 0; + add_defn(state, code_buf); + free(proto); + + assert(name_out); + assert(args_out); + *name_out = name; + *args_out = start_of; + return ret; } -static int lower_unop(struct state *state, struct ast *expr) +static int lower_comparison(struct state *state, struct ast *expr) { + if (lower_expr(state, comparison_left(expr))) + return -1; + switch (expr->k) { - case AST_LNOT: printf("-"); break; - case AST_NOT: printf("~"); break; - case AST_NEG: printf("-"); break; + case AST_LE: + fprintf(state->code, " <= "); + break; + + case AST_LT: + fprintf(state->code, " < "); + break; + default: - internal_error("missing unop lowering"); - return -1; + internal_error("unhandled lowering for comparison %s", ast_str(expr->k)); + abort(); } - return lower_expr(state, unop_expr(expr)); + return lower_expr(state, comparison_right(expr)); } -static int lower_expr(struct state *state, struct ast *expr) +static int lower_binop(struct state *state, struct ast *expr) { - if (is_unop(expr)) - return lower_unop(state, expr); - - if (is_binop(expr)) - return lower_binop(state, expr); - - if (is_comparison(expr)) - return lower_comparison(state, expr); + if (lower_expr(state, binop_left(expr))) + return -1; switch (expr->k) { - case AST_ID: printf("%s", id_str(expr)); break; - case AST_CONST_INT: printf("%lld", int_val(expr)); break; - case AST_CONST_FLOAT: printf("%f", float_val(expr)); break; - case AST_CONST_BOOL: printf("%s", bool_val(expr) ? "true" : "false"); + case AST_SUB: + fprintf(state->code, " - "); break; - case AST_CONST_STR: printf("\"%s\"", str_val(expr)); break; - case AST_CONST_CHAR: printf("'%c'", (char)char_val(expr)); break; - case AST_INIT: return lower_init(state, expr); - case AST_CLOSURE: return lower_closure(state, expr); - case AST_REF: return lower_expr(state, ref_base(expr)); - case AST_DEREF: return lower_expr(state, deref_base(expr)); - default: - internal_error("missing expr lowering"); - return -1; - } - return 0; -} + case AST_ADD: + fprintf(state->code, " + "); + break; -static int lower_move(struct state *state, struct ast *move) -{ - if (move->k == AST_ID) { - /** @todo once I start messing about with references, moves - * should only be outputted for parameters that take ownership - */ - printf("move(%s)", id_str(move)); - return 0; + default: + internal_error("unhandled comparison %s", ast_str(expr->k)); + abort(); } - return lower_expr(state, move); + return lower_expr(state, binop_right(expr)); } -static int lower_moves(struct state *state, struct ast *moves) +static int lower_expr(struct state *state, struct ast *expr) { - if (!moves) - return 0; + if (is_comparison(expr)) + return lower_comparison(state, expr); - if (lower_move(state, moves)) - return -1; + if (is_binop(expr)) + return lower_binop(state, expr); - foreach_node(move, moves->n) { - printf(", "); - if (lower_move(state, move)) + switch (expr->k) { + case AST_CLOSURE: { + char *name = NULL, *args = NULL; + if (lower_closure(state, expr, &name, &args)) return -1; + + fprintf(state->code, "(fwd_closure_t){%s, &ctx->%s}", name, args); + free(name); + free(args); + break; } - return 0; -} + case AST_ID: { + struct ast *def = file_scope_find_symbol(expr->scope, id_str(expr)); + char *name = mangle2(def); + fprintf(state->code, "ctx->%s", name); + free(name); + break; + } -static int lower_err_branch(struct state *state, struct ast *err) -{ - if (lower_block(state, err_branch_body(err), false)) - return -1; + case AST_CONST_INT: { + fprintf(state->code, "%lld", (long long)int_val(expr)); + break; + } + default: + internal_error("unhandled expr lowering: %s", ast_str(expr->k)); + abort(); + } - printf("\n"); return 0; } -static int lower_mark_moved(struct state *state, struct ast *moves) +static int lower_call(struct state *state, struct ast *call, bool last) { - if (!moves) - return 0; + struct ast *expr = call_expr(call); + assert(expr->k == AST_ID); - foreach_node(move, moves) { - if (move->k != AST_ID) - continue; + struct ast *def = file_scope_find_symbol(call->scope, id_str(expr)); + assert(def); - if (is_trivially_copyable(move->t)) - continue; + if (def->k == AST_VAR_DEF) + return lower_closure_call(state, call, def, last); - if (is_callable(move->t)) - continue; + if (lower_proc(state, def)) + return -1; - printf("%s_owned = false;\n", id_str(move)); - indent(state); - } + char *q = buildstr("%s_call%zu", state->prefix, uniq(state)); - return 0; -} + struct state ctx_state = create_state(state); -/** @todo this is probably more complicated than it really needs to be, maybe - * refactor into lower_checked_call and lower_implicit_call or something for - * explicit and implicit error handling cases? */ -static int lower_call(struct state *state, struct ast *call, bool ret) -{ - struct ast *err = call_err(call); - /** @todo better default error name? */ - const char *err_str = err ? err_branch_id(err) : "_fwd_err"; + char *ctx_buf = NULL; size_t ctx_size = 0; + FILE *ctx = open_memstream(&ctx_buf, &ctx_size); + fprintf(ctx, "struct %s {\n fwd_start_t start;\n", q); - if (lower_mark_moved(state, call_args(call))) - return -1; + indent(state); + fprintf(state->code, "struct %s *%s = ctx->global_args;\n", q, q); - bool direct_ret = ret && !err; - if (direct_ret) - printf("return "); - else - printf("if (auto %s = ", err_str); + int ret = 0; + size_t idx = 0; + bool returning = false; + foreach_node(a, call_args(call)) { + returning |= a->k == AST_CLOSURE; - if (lower_expr(state, call_expr(call))) - return -1; + char *type = lower_type_str(a->t); + fprintf(ctx, " %s a%zu;\n", type, idx); + free(type); - printf("("); + indent(&ctx_state); + fprintf(ctx_state.code, "%s->a%zu = ", q, idx); + int ret = lower_expr(&ctx_state, a); + fprintf(ctx_state.code, ";\n"); - if (lower_moves(state, call_args(call))) - return -1; + if (ret) + goto out; - if (direct_ret) { - printf(");\n"); - return 0; + idx++; } - printf("))"); - if (err) { - if (lower_err_branch(state, err)) - return -1; - - if (ret) { - printf("\n"); - indent(state); - printf("return nullptr;\n"); - } +out: + if (!returning && last && ast_flags(state->current, AST_REQ_FRAME)) { + /** @todo unsure if this applies to all cases but seems to work + * for the simple examples I've tried so far */ + indent(&ctx_state); + fprintf(ctx_state.code, "fwd_stack_free(&stack, ctx);\n"); + } - return 0; + char *target = mangle2(def); + indent(&ctx_state); + if (last) { + fprintf(ctx_state.code, "FWD_MUSTTAIL return %s(stack, %s);\n", + target, q); + } + else { + fprintf(ctx_state.code, "stack = %s(stack, %s);\n", + target, q); } - printf("\n"); + fprintf(ctx, "};\n\n"); + fclose(ctx); + assert(ctx_buf); - increase_indent(state); + add_type(state, ctx_buf); + free(target); + free(q); + return ret; +} + +static int lower_if(struct state *state, struct ast *stmt, bool last) +{ indent(state); - decrease_indent(state); + fprintf(state->code, "if ("); + if (lower_expr(state, if_cond(stmt))) + return -1; - printf("return %s;\n", err_str); + fprintf(state->code, ")\n"); + if (lower_block(state, if_body(stmt), last)) + return -1; - if (ret) { - indent(state); - printf("return nullptr;\n"); - } + if (!if_else(stmt)) + return 0; - return 0; + indent(state); + fprintf(state->code, "else\n"); + return lower_block(state, if_else(stmt), last); } -static int lower_let(struct state *state, struct ast *let, bool ret) +static int lower_stmt(struct state *state, struct ast *stmt, bool last) { - if (lower_var(let_var(let))) - return -1; - - printf(" = "); - - if (lower_expr(state, let_expr(let))) - return -1; + switch (stmt->k) { + case AST_CALL: return lower_call(state, stmt, last); + case AST_IF: return lower_if(state, stmt, last); + default: + internal_error("unhandled statement kind %s", ast_str(stmt->k)); + abort(); + break; + } + return 0; +} - printf(";\n"); - if (ret) { - indent(state); - printf("return nullptr;\n"); +static int lower_stmt_list(struct state *state, struct ast *stmt_list, bool last) +{ + foreach_node(stmt, stmt_list) { + bool req_ret = last && !stmt->n; + if (lower_stmt(state, stmt, req_ret)) + return -1; } return 0; } -static int lower_if(struct state *state, struct ast *stmt, bool ret) +static int lower_block(struct state *state, struct ast *block, bool last) { - printf("if ("); - if (lower_expr(state, if_cond(stmt))) - return -1; + indent(state); + fprintf(state->code, "{\n"); - printf(") "); + increase_indent(state); + int ret = lower_stmt_list(state, block_body(block), last); + decrease_indent(state); - if (lower_block(state, if_body(stmt), ret)) - return -1; + indent(state); + fprintf(state->code, "}\n"); + return ret; +} - if (!if_else(stmt)) { - printf("\n"); - return 0; - } +static int lower_param(struct state *state, struct ast *param) +{ + char *type = lower_type_str(param->t); + char *name = mangle2(param); - printf(" else "); - if (lower_block(state, if_else(stmt), ret)) - return -1; + fprintf(state->ctx, " %s %s;\n", type, name); - printf("\n"); + free(type); + free(name); return 0; } -static int lower_error(struct ast *err) +static int lower_params(struct state *state, struct ast *params) { - assert(error_str(err) || error_id(err)); - if (error_str(err)) { - printf("return %s;\n", error_str(err)); - return 0; + foreach_node(p, params) { + if (lower_param(state, p)) + return -1; } - struct ast *id = error_id(err); - printf("return %s;\n", id_str(id)); return 0; } -static int lower_own(struct state *state, struct ast *stmt, bool ret) +static size_t fwd_typeid(struct type *t) { - /** @todo name mangling */ - printf("if (!%s_owned) ", own_id(stmt)); - if (lower_block(state, own_body(stmt), ret)) - return -1; + /** @todo this should maybe be cached somewhere earlier? */ + switch (t->k) { + case TYPE_I64: return FWD_I64; + case TYPE_PTR: return FWD_PTR; + default: + abort(); + } - printf("\n"); return 0; } -static int lower_statement(struct state *state, struct ast *stmt, bool ret) +static const char *fwd_typeparam(struct type *t) { - switch (stmt->k) { - case AST_OWN: return lower_own(state, stmt, ret); - case AST_LET: return lower_let(state, stmt, ret); - case AST_CALL: return lower_call(state, stmt, ret); - case AST_IF: return lower_if(state, stmt, ret); - case AST_EMPTY: - if (ret) - printf("return nullptr;\n"); - else - printf("\n"); - - return 0; + switch (t->k) { + case TYPE_I64: return "i64"; + case TYPE_PTR: return "p"; default: - internal_error("missing statement lowering"); - return -1; + abort(); } return 0; } -static int lower_block_vars(struct state *state, struct ast *block) +static int lower_extern_proc(struct state *state, struct ast *proc) { - struct scope *scope = block->scope; + /* only void external functions supported atm */ + struct type *rtype = proc_rtype(proc); + assert(rtype->k == TYPE_VOID); - bool populated = false; - foreach(visible, n, &scope->symbols) { - struct ast *def = n->data; - if (def->k != AST_VAR_DEF) - continue; + add_proc(state, proc); + char *name = mangle2(proc); + char *proto = buildstr("static fwd_stack_t %s(fwd_stack_t stack, fwd_args_t args)", + name); - if (is_trivially_copyable(def->t)) - continue; + char *decl = buildstr("%s;\n", proto); + add_decl(state, decl); - if (is_callable(def->t)) - continue; + char *code_buf = NULL, *ctx_buf = NULL; + size_t code_size = 0, ctx_size = 0; - if (!populated) { - indent(state); - printf("[[maybe_unused]] bool %s_owned = true", - var_id(def)); + struct state new_state = create_state(state); + new_state.indent = 0; + new_state.prefix = name; + new_state.current = proc; + new_state.uniq = 0; + new_state.code = open_memstream(&code_buf, &code_size); + new_state.ctx = open_memstream(&ctx_buf, &ctx_size); + assert(new_state.code); - populated = true; - continue; - } + fprintf(new_state.ctx, "struct %s_ctx {\n", name); + fprintf(new_state.code, "%s\n{\n", proto); + increase_indent(&new_state); + + indent(&new_state); + fprintf(new_state.code, "extern long %s(fwd_extern_args_t);\n", proc_id(proc)); - printf(", %s_owned = true", var_id(def)); + /* for now, allocate a new frame for arguments. Might want to + * simplify this by always using the 'external' format for argument + * passing, even internally */ + indent(&new_state); + fprintf(new_state.code, "struct %s_ctx *ctx = args;\n", name); + + indent(&new_state); + fprintf(new_state.code, "struct fwd_arg *extern_args = fwd_stack_alloc(&stack);\n"); + + size_t idx = 0; + foreach_node(p, proc_params(proc)) { + indent(&new_state); + /* leave place for return value */ + fprintf(new_state.code, "extern_args[%zu] = (fwd_arg_t){%zu, " + "{.%s = ctx->a%zu}};\n", + idx + 1, fwd_typeid(p->t), fwd_typeparam(p->t), idx); + + + char *type_str = lower_type_str(p->t); + fprintf(new_state.ctx, " %s a%zu;\n", type_str, idx); + free(type_str); + idx++; } - if (populated) - printf(";\n\n"); + indent(&new_state); + fprintf(new_state.code, "%s((fwd_extern_args_t){.argc = %zu, .args = extern_args});\n", + proc_id(proc), idx); - return 0; -} -static int lower_block(struct state *state, struct ast *block, bool ret) -{ - printf("{\n"); - increase_indent(state); + indent(&new_state); + fprintf(new_state.code, "fwd_stack_free(&stack, extern_args);\n"); - if (lower_block_vars(state, block)) - return -1; + indent(&new_state); + fprintf(new_state.code, "return stack;\n"); - foreach_node(stmt, block_body(block)) { - indent(state); + fprintf(new_state.code, "}\n\n"); + fprintf(new_state.ctx, "};\n\n"); - bool returning = block_error(block) ? false : ret && !stmt->n; - if (lower_statement(state, stmt, returning)) - return -1; - } + fclose(new_state.code); + assert(code_buf); + add_defn(state, code_buf); - if (block_error(block)) { - indent(state); - if (lower_error(block_error(block))) - return -1; - } - else if (!block_body(block)) { - indent(state); - printf("return nullptr;\n"); - } + fclose(new_state.ctx); + assert(ctx_buf); + add_type(state, ctx_buf); - decrease_indent(state); - indent(state); - printf("}"); + free(proto); + free(name); return 0; } -static int lower_var(struct ast *var) +static int lower_param_copy(struct state *state, struct ast *param, FILE *f, size_t idx) { - if (lower_type(var_type(var))) - return -1; - - printf(" %s", var_id(var)); + char *type = lower_type_str(param->t); + fprintf(f, " %s a%zu;\n", type, idx); + free(type); return 0; } -static int lower_vars(struct ast *vars) +static int lower_param_copies(struct state *state, struct ast *params) { - if (!vars) - return 0; - - if (lower_var(vars)) - return -1; + char *param_buf = NULL; size_t param_size = 0; + FILE *f = open_memstream(¶m_buf, ¶m_size); + fprintf(f, "struct %s_params {\n fwd_start_t start;\n", state->prefix); - foreach_node(var, vars->n) { - printf(", "); - if (lower_var(var)) + size_t idx = 0; + foreach_node(p, params) { + if (lower_param_copy(state, p, f, idx)) { + fclose(f); + free(param_buf); return -1; + } + + idx++; } + fprintf(f, "};\n\n"); + fclose(f); + assert(param_buf); + + add_type(state, param_buf); + + indent(state); + fprintf(state->code, "*((struct %s_params *)&ctx->%s_start) =" + " *((struct %s_params *)args);\n", + state->prefix, state->prefix, state->prefix); return 0; } -static int lower_closure(struct state *state, struct ast *closure) +static int lower_proc(struct state *state, struct ast *proc) { - printf("[&]("); - if (lower_vars(closure_bindings(closure))) - return -1; + if (proc_lowered(state, proc)) + return 0; + + if (!proc_body(proc)) + return lower_extern_proc(state, proc); + + add_proc(state, proc); + char *name = mangle2(proc); + char *proto = buildstr("static fwd_stack_t %s(fwd_stack_t stack, fwd_args_t args)", name); + char *decl = buildstr("%s;\n", proto); + add_decl(state, decl); + + char *code_buf = NULL, *ctx_buf = NULL; + size_t code_size = 0, ctx_size = 0; + + struct state new_state = create_state(state); + new_state.indent = 0; + new_state.prefix = name; + new_state.current = proc; + new_state.uniq = 0; + new_state.code = open_memstream(&code_buf, &code_size); + new_state.ctx = open_memstream(&ctx_buf, &ctx_size); + assert(new_state.code); + assert(new_state.ctx); + + char *start_of = buildstr("%s_start", name); + + fprintf(new_state.code, "%s\n{\n", proto); + fprintf(new_state.ctx, "struct %s_ctx {\n", name); + fprintf(new_state.ctx, " fwd_args_t global_args;\n fwd_start_t %s;\n", start_of); + + increase_indent(&new_state); + indent(&new_state); + fprintf(new_state.code, "static_assert(FWD_FRAME_SIZE >= sizeof(struct %s_ctx), \"context exceeds frame size\");\n", + name); + + if (ast_flags(proc, AST_REQ_FRAME)) { + indent(&new_state); + fprintf(new_state.code, "struct %s_ctx *ctx " + "= fwd_stack_alloc(&stack);\n", + name); + } + else { + indent(&new_state); + fprintf(new_state.code, "struct %s_ctx ctx_buf;\n", name); + indent(&new_state); + fprintf(new_state.code, "struct %s_ctx *ctx = &ctx_buf;\n", name); + } - printf(")"); + /* allocates parameter slots */ + int ret = lower_params(&new_state, proc_params(proc)); + assert(ret == 0); - if (lower_block(state, closure_body(closure), true)) - return -1; + /* actually copies values into parameter slots */ + ret = lower_param_copies(&new_state, proc_params(proc)); + assert(ret == 0); - return 0; -} + indent(&new_state); + fprintf(new_state.code, "ctx->global_args = args;\n"); -static int lower_proto(struct ast *proc) -{ - /* 'extern' functions should be provided to us by whatever framework the - * user is using */ - if (!proc_body(proc)) - return 0; + struct ast *block = proc_body(proc); + ret = lower_stmt_list(&new_state, block_body(block), true); - printf("fwd_err_t "); - if (strcmp("main", proc_id(proc)) == 0) - printf("fwd_main("); - else - printf("%s(", proc_id(proc)); + fprintf(new_state.code, "}\n\n"); + fprintf(new_state.ctx, "};\n\n"); + fclose(new_state.code); + fclose(new_state.ctx); + assert(code_buf); + assert(ctx_buf); - if (lower_vars(proc_params(proc))) - return -1; + add_defn(state, code_buf); + add_type(state, ctx_buf); - printf(");\n\n"); - return 0; + free(start_of); + free(proto); + free(name); + return ret; } -static int lower_proc(struct ast *proc) +int lower(struct scope *root) { - if (!proc_body(proc)) - return 0; - - printf("fwd_err_t "); - if (strcmp("main", proc_id(proc)) == 0) - printf("fwd_main("); - else - printf("%s(", proc_id(proc)); + struct ast *main = file_scope_find_symbol(root, "main"); + if (!main) { + error("no main"); + return -1; + } - if (lower_vars(proc_params(proc))) + if (main->k != AST_PROC_DEF) { + error("main is not a procedure"); return -1; + } - printf(")\n"); + struct string_vec defns = string_vec_create(0); + struct string_vec decls = string_vec_create(0); + struct string_vec types = string_vec_create(0); + struct proc_set procs = proc_set_create(0); - struct state state = {0}; - if (lower_block(&state, proc_body(proc), true)) - return -1; + struct state state = { + .indent = 0, + .ctx = NULL, + .code = NULL, + .defns = &defns, + .decls = &decls, + .types = &types, + .procs = &procs + }; - printf("\n\n"); - return 0; -} + if (lower_proc(&state, main)) + return -1; -int lower(struct scope *root) -{ - printf("#include <fwdlib.hpp>\n"); + /* placeholder, should really be calculated to be some maximum frame + * size or something */ + printf("#define FWD_FRAME_SIZE %d\n", 1024); + printf("#include <fwd.h>\n\n"); - foreach(visible, visible, &root->symbols) { - struct ast *proc = visible->data; - assert(proc->k == AST_PROC_DEF); - if (lower_proto(proc)) - return -1; + foreach(string_vec, s, state.types) { + puts(*s); + free(*s); } - foreach(visible, visible, &root->symbols) { - struct ast *proc = visible->data; - if (lower_proc(proc)) - return -1; + foreach(string_vec, s, state.decls) { + puts(*s); + free(*s); } + foreach(string_vec, s, state.defns) { + puts(*s); + free(*s); + } - puts("int main()"); - puts("{"); - puts(" fwd_err_t err = fwd_main();"); - puts(" if (err) {"); - puts(" fprintf(stderr, \"%s\", err);"); - puts(" return -1;"); - puts(" }"); - puts("}"); + char *name = mangle2(main); + printf("int main()\n"); + printf("{\n"); + printf(" fwd_stack_t stack = create_fwd_stack();\n"); + printf(" void *args = fwd_stack_alloc(&stack);\n"); + printf(" stack = %s(stack, args);\n", name); + printf(" fwd_stack_free(&stack, args);\n"); + printf(" destroy_fwd_stack(&stack);\n\n"); + printf("}\n"); + + /* modules require a register function of some kind, implement a stub */ + printf("int fwd_register(struct fwd_state *state, const char *name, fwd_extern_t func, fwd_type_t rtype, ...) {return 0;}\n"); + + string_vec_destroy(&defns); + string_vec_destroy(&decls); + string_vec_destroy(&types); + proc_set_destroy(&procs); + free(name); return 0; } @@ -45,38 +45,6 @@ static struct state create_state(struct state *parent) static void destroy_state(struct state *state) { moved_destroy(&state->moved); - moved_destroy(&state->queued); - moved_destroy(&state->referenced); -} - -struct rm_move { - struct ast_pair data; - struct state *owner; -}; - -static struct rm_move remove_move(struct state *state, struct ast *def) -{ - if (!state) - return (struct rm_move){.data = {}, .owner = NULL}; - - struct ast_pair search = {.def = def}; - struct ast_pair *found = moved_find(&state->moved, search); - if (found) { - moved_remove_found(&state->moved, found); - struct rm_move r = {.data = *found, .owner = state}; - moved_free_found(&state->moved, found); - return r; - } - - return remove_move(state->parent, def); -} - -static void reinsert_move(struct rm_move move) -{ - if (!move.owner) - return; - - moved_insert(&move.owner->moved, move.data); } static struct ast_pair *find_move(struct state *state, struct ast *def) @@ -164,33 +132,6 @@ static void push_up(struct state *state) merge(parent, state); } -static void mark_queued(struct state *state) -{ - if (moved_len(&state->moved) == 0) - return; - - foreach(moved, m, &state->moved) { - moved_insert(&state->queued, *m); - } - - /* empty out moves now that they're all queued */ - moved_destroy(&state->moved); - state->moved = moved_create(); -} - -static void mark_unqueued(struct state *state) -{ - if (moved_len(&state->queued) == 0) - return; - - foreach(moved, q, &state->queued) { - moved_insert(&state->moved, *q); - } - - moved_destroy(&state->queued); - state->queued = moved_create(); -} - static void forget_references(struct state *state) { moved_destroy(&state->referenced); @@ -295,10 +236,10 @@ static int total_check_single(struct state *state, struct scope *scope) static int total_check_proc(struct state *state, struct scope *scope) { int ret = total_check_single(state, scope); - if (scope->flags & SCOPE_PROC) + if (!scope->parent) return ret; - if (!scope->parent) + if (scope->flags & SCOPE_PROC) return ret; return ret | total_check_proc(state, scope->parent); @@ -313,21 +254,10 @@ static int mvcheck_block(struct state *state, struct ast *node) return ret; } - if (block_error(node)) { - if (total_check_proc(&new_state, node->scope)) - semantic_info(node->scope, node, "at end of block"); - - /* mark us queued so moves are not visible for the next scopes, - * but appear to the caller function if we're in a closure */ - mark_queued(&new_state); - push_up(&new_state); - destroy_state(&new_state); - return 0; - } - if (total_check_single(&new_state, node->scope)) semantic_info(node->scope, node, "at end of block"); + /** @todo add exit analysis and run total_check_proc on those blocks */ push_up(&new_state); destroy_state(&new_state); return 0; @@ -361,7 +291,6 @@ static int mvcheck_call(struct state *state, struct ast *node) struct state arg_state = create_state(state); ret = mvcheck(&arg_state, arg); - mark_unqueued(&arg_state); merge(&group_state, &arg_state); destroy_state(&arg_state); @@ -382,41 +311,8 @@ static int mvcheck_call(struct state *state, struct ast *node) destroy_state(&group_state); - /* the next section looks a bit weird, but it kind of makes sense. We - * don't know when the error branch might get taken, so we first check - * it 'before' the moves from any possible closures take place to see if - * there's a possibility that some variable gets leaked. The second time - * around we take the closure moves into account to see if the error - * branch might accidentally move an already moved variable, i.e. the - * user would need to add `own` blocks. */ - - /** @todo this is more or less what needs to happen for correctness, but - * might mean that warnings from leaks in the first check get reprinted - * in the second round, should probably add in some mechanism to check - * against that */ - if (call_err(node)) { - struct state err_state = create_state(state); - ret |= mvcheck(&err_state, call_err(node)); - destroy_state(&err_state); - } - else if (total_check_proc(state, node->scope)) { - semantic_info(node->scope, node, "in implicit err branch"); - } - push_up(&buffer_state); destroy_state(&buffer_state); - - if (call_err(node)) { - struct state err_state = create_state(state); - ret |= mvcheck(&err_state, call_err(node)); - - /* store results of this check */ - push_up(&err_state); - destroy_state(&err_state); - } - /* no need to check implicit error branch since it by definition can't - * move already moved variables */ - return ret; } @@ -584,30 +480,6 @@ static int mvcheck_statements(struct state *state, struct ast *nodes) return 0; } -static int mvcheck_err_branch(struct state *state, struct ast *node) -{ - return mvcheck(state, err_branch_body(node)); -} - -static int mvcheck_own(struct state *state, struct ast *node) -{ - struct ast *def = file_scope_find_symbol(node->scope, own_id(node)); - assert(def); - - if (is_callable(def->t)) { - semantic_error(node->scope, node, - "ownership of callable cannot be checked"); - return -1; - } - - struct rm_move prev = remove_move(state, def); - int ret = mvcheck(state, own_body(node)); - - /* insert back */ - reinsert_move(prev); - return ret; -} - static int mvcheck(struct state *state, struct ast *node) { if (is_unop(node)) @@ -624,8 +496,6 @@ static int mvcheck(struct state *state, struct ast *node) case AST_BLOCK: return mvcheck_block (state, node); case AST_CALL: return mvcheck_call (state, node); case AST_CLOSURE: return mvcheck_closure (state, node); - case AST_ERR_BRANCH: return mvcheck_err_branch(state, node); - case AST_OWN: return mvcheck_own (state, node); case AST_LET: return mvcheck_let (state, node); case AST_ID: return mvcheck_id (state, node); case AST_INIT: return mvcheck_init (state, node); @@ -633,6 +503,7 @@ static int mvcheck(struct state *state, struct ast *node) case AST_REF: return mvcheck_ref (state, node); case AST_DEREF: return mvcheck_deref (state, node); case AST_EMPTY: + case AST_IMPORT: case AST_CONST_INT: case AST_CONST_STR: case AST_CONST_FLOAT: diff --git a/src/parser.y b/src/parser.y index 7b2c651..6ec2eb2 100644 --- a/src/parser.y +++ b/src/parser.y @@ -73,7 +73,6 @@ %token IF "if" %token ELSE "else" %token NIL "nil" -%token OWN "own" %token PUB "pub" %token MUT "mut" %token CONTINUE "continue" @@ -82,7 +81,6 @@ %token DOT "." %token SCOPE "::" %token FATARROW "=>" -%token ERRARROW "!>" %right "[" "]" /* precedence */ @@ -101,10 +99,10 @@ %left "::" %nterm <node> top unit proc proc_decl call closure var expr statement body -%nterm <node> vars exprs statements closures err trailing_closure -%nterm <node> opt_vars opt_exprs opt_statements opt_trailing_closure opt_err opt_error +%nterm <node> vars exprs statements closures trailing_closure +%nterm <node> opt_vars opt_exprs opt_statements opt_trailing_closure %nterm <node> rev_vars rev_exprs rev_closures rev_statements -%nterm <node> let if nil own unop binop construct import +%nterm <node> let if nil unop binop construct import %nterm <node> struct struct_cont trait %nterm <node> type_param type_params opt_type_params @@ -346,29 +344,20 @@ closures } } -err - : "!>" ID body { - $$ = gen_err_branch($2, $3, src_loc(@$)); - } - -opt_err - : err - | { $$ = NULL; } - call - : expr "(" opt_exprs ")" ";" opt_err { - $$ = gen_call($1, $[opt_exprs], $[opt_err], src_loc(@$)); + : expr "(" opt_exprs ")" ";" { + $$ = gen_call($1, $[opt_exprs], src_loc(@$)); } - | expr "(" opt_exprs ")" "=>" opt_vars ";" opt_err { + | expr "(" opt_exprs ")" "=>" opt_vars ";" { /* the rest of the function body is our closure, gets fixed up * later */ struct ast *closure = gen_closure($[opt_vars], NULL, src_loc(@$)); ast_append(&$[opt_exprs], closure); - $$ = gen_call($[expr], $[opt_exprs], $[opt_err], src_loc(@$)); + $$ = gen_call($[expr], $[opt_exprs], src_loc(@$)); } - | expr "(" opt_exprs ")" closures opt_err { + | expr "(" opt_exprs ")" closures { ast_append(&$[opt_exprs], $[closures]); - $$ = gen_call($[expr], $[opt_exprs], $[opt_err], src_loc(@$)); + $$ = gen_call($[expr], $[opt_exprs], src_loc(@$)); } let @@ -384,26 +373,11 @@ nil $$ = gen_nil($2, $3, $5, src_loc(@$)); } -own - : "own" ID body { - $$ = gen_own($2, $3, src_loc(@$)); - } - -opt_error - : "error" STRING { - $$ = gen_error($2, NULL, src_loc(@$)); - } - | "error" ID { - $$ = gen_error(NULL, gen_id($2, src_loc(@$)), src_loc(@$)); - } - | { $$ = NULL; } - statement : call | body | let | nil - | own | if | ";" { $$ = gen_empty(src_loc(@$)); } @@ -419,8 +393,8 @@ opt_statements | { $$ = NULL; } body - : "{" opt_statements opt_error "}" { - $$ = gen_block($2, $3, src_loc(@$)); + : "{" opt_statements "}" { + $$ = gen_block($2, src_loc(@$)); } behaviour @@ -481,18 +455,16 @@ trait import : "import" STRING { - $$ = gen_import($2, src_loc(@$)); + $$ = gen_import(strip($2), src_loc(@$)); } top : proc - | proc_decl | struct | struct_cont | import | trait | "pub" proc { $$ = $2; ast_set_flags($$, AST_FLAG_PUBLIC); } - | "pub" proc_decl { $$ = $2; ast_set_flags($$, AST_FLAG_PUBLIC); } | "pub" struct { $$ = $2; ast_set_flags($$, AST_FLAG_PUBLIC); } | "pub" struct_cont { $$ = $2; ast_set_flags($$, AST_FLAG_PUBLIC); } | "pub" import { $$ = $2; ast_set_flags($$, AST_FLAG_PUBLIC); } diff --git a/src/path.c b/src/path.c new file mode 100644 index 0000000..87cd26b --- /dev/null +++ b/src/path.c @@ -0,0 +1,66 @@ +/* SPDX-License-Identifier: copyleft-next-0.3.1 */ +/* Copyright 2023 Kim Kuparinen < kimi.h.kuparinen@gmail.com > */ + +/** + * @file path.c + * + * Path handling helper implementations. + */ + +#include <stddef.h> +#include <string.h> +#include <unistd.h> +#include <stdlib.h> +#include <errno.h> + +#include <fwd/path.h> +#include <fwd/debug.h> + +char *fwd_basename(const char *file) +{ + size_t l = strlen(file); + size_t n = l - 1; + while (--n) { + if (file[n] == '/') + break; + } + + if (n == 0) + return strdup(file); + + return strndup(file + n + 1, l - n); +} + +char *fwd_dirname(const char *file) +{ + size_t l = strlen(file); + size_t n = l - 1; + while (--n) { + if (file[n] == '/') + break; + } + + return strndup(file, n); +} + +char *fwd_cwdname() +{ + size_t size; + long path_max = pathconf(".", _PC_PATH_MAX); + if (path_max == -1) + size = 1024; + else + size = (size_t)path_max; + + char *buf = malloc(size); + if (!buf) + return NULL; + + if (!getcwd(buf, size)) { + error("%s\n", strerror(errno)); + free(buf); + return NULL; + } + + return buf; +} diff --git a/src/scope.c b/src/scope.c index fe35d58..485b3a6 100644 --- a/src/scope.c +++ b/src/scope.c @@ -8,10 +8,11 @@ #include <stddef.h> #include <string.h> -#include <stdio.h> #include <stdarg.h> #include <stdlib.h> #include <assert.h> +#include <stdio.h> +#include <dlfcn.h> #include <fwd/debug.h> #include <fwd/scope.h> @@ -31,6 +32,7 @@ struct scope *create_scope() scope->symbols = visible_create(16); /* arbitrary number */ scope->traits = visible_create(1); scope->types = visible_create(1); + scope->mods = mod_vec_create(0); return scope; } @@ -44,6 +46,12 @@ void destroy_scope(struct scope *scope) free((void *)scope->fctx.fname); } + foreach(mod_vec, m, &scope->mods) { + dlclose(m); + } + + mod_vec_destroy(&scope->mods); + visible_destroy(&scope->symbols); visible_destroy(&scope->traits); visible_destroy(&scope->types); |