diff options
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | TODO | 27 | ||||
| -rw-r--r-- | examples/fib.fwd | 4 | ||||
| -rw-r--r-- | examples/ptrs.fwd | 7 | ||||
| -rw-r--r-- | examples/vec.fwd | 143 | ||||
| -rw-r--r-- | include/fwd/ast.h | 189 | ||||
| -rw-r--r-- | include/fwd/debug.h | 5 | ||||
| -rw-r--r-- | include/fwd/mod.h | 4 | ||||
| -rw-r--r-- | include/fwd/rewrite.h | 9 | ||||
| -rw-r--r-- | include/fwd/scope.h | 7 | ||||
| -rw-r--r-- | mod/Makefile | 8 | ||||
| -rw-r--r-- | mod/io.c | 8 | ||||
| -rw-r--r-- | mod/mem.c | 50 | ||||
| -rw-r--r-- | mod/util.c | 20 | ||||
| -rwxr-xr-x | scripts/gen-deps | 2 | ||||
| -rw-r--r-- | src/analyze.c | 582 | ||||
| -rw-r--r-- | src/ast.c | 75 | ||||
| -rw-r--r-- | src/debug.c | 37 | ||||
| -rw-r--r-- | src/lexer.l | 19 | ||||
| -rw-r--r-- | src/lower.c | 221 | ||||
| -rw-r--r-- | src/move.c | 284 | ||||
| -rw-r--r-- | src/parser.y | 210 | ||||
| -rw-r--r-- | src/rewrite.c | 96 | ||||
| -rw-r--r-- | src/scope.c | 87 |
24 files changed, 1775 insertions, 321 deletions
@@ -18,7 +18,7 @@ all: setup .PHONY: analyze analyze: setup - CC='gcc -fanalyzer' SKIP_ANALYZER='-fno-analyzer' $(MAKE) CROSS_COMPILE= + CFLAGS='$$CFLAGS -fanalyzer' SKIP_ANALYZER='-fno-analyzer' $(MAKE) .PHONY: setup setup: @@ -0,0 +1,27 @@ ++ Some kind of "unreachable" might be nice to help the move checker? for +instance, + + if abc { + release(var); + fwdpanic("!!!"); + } + + use(var) + +might be nicer to write than + + if abc { + release(var); + fwdpanic("!!!"); + } else { + use(var); + } + +or the current alternative + + guard(abc) => { + release(var); + fwdpanic("!!!"); + } => ; + + use (var); diff --git a/examples/fib.fwd b/examples/fib.fwd index e6d6ee6..4583a26 100644 --- a/examples/fib.fwd +++ b/examples/fib.fwd @@ -25,6 +25,6 @@ fib(i64 n, (i64) res) main() { fib(42) => i64 n; - print_i64(n); - print_nl(); + fwdprint_i64(n); + fwdprint_nl(); } diff --git a/examples/ptrs.fwd b/examples/ptrs.fwd index a0f8aac..a1efc84 100644 --- a/examples/ptrs.fwd +++ b/examples/ptrs.fwd @@ -1,7 +1,6 @@ -fwd_null(auto x, () null, (&auto) ok); -fwd_copy(auto x, (auto, auto) ok); -fwd_println(auto x); -fwd_intalloc((*int) ok); +extern fwd_copy(auto x, (auto, auto) ok); +extern fwd_println(auto x); +extern fwd_intalloc((*int) ok); main() { diff --git a/examples/vec.fwd b/examples/vec.fwd new file mode 100644 index 0000000..22049e0 --- /dev/null +++ b/examples/vec.fwd @@ -0,0 +1,143 @@ +import "../mod/libfwdio.so" +import "../mod/libfwdmem.so" +import "../mod/libfwdutil.so" + +vec { + u64 n; + u64 s; + *i64 buf; +} + +init_vec(u64 n, (vec) ok) +{ + ok([n => n, 0 as u64 => s, * => buf] vec); +} + +next_s_vec(u64 s, (u64) ok) +{ + if s == 0 as u64 { + ok(1 as u64); + } else { + ok(2 as u64 * s); + } +} + +reserve_vec(vec v, u64 c, (vec) ok) +{ + v => [n => n, s => s, buf => buf]; + n + c => nn; + + /* expand if we run out of space */ + if nn > s { + next_s_vec(s) => s; + + /* note that fwdrealloc creates a new closure, meaning that it + * takes ownership of v.buf, so we can't back out and do a + * single ok(v) at the end of the function */ + fwdrealloc(buf, s * sizeof(i64)) => nbuf; + ok([nn => n, s => s, nbuf => buf] vec); + } else { + ok([nn => n, s => s, buf => buf] vec); + } +} + +set_vec(vec v, i64 i, i64 e, (vec) ok) +{ + v => [n => n, s => s, buf => buf]; + buf + (n - 1 as u64) => nbuf; + nil nbuf { + nil nbuf; + fwdfree(buf); + fwdpanic("should never happen"); + } => dst; + + /* perform actual store */ + e => dst*; + + nil nbuf; + ok([n => n, s => s, buf => buf] vec); +} + +n_vec(vec v, (vec, u64) ok) +{ + v => [n => n, s => s, buf => buf]; + ok([n => n, s => s, buf => buf] vec, n); +} + +append_vec(vec v, i64 e, (vec) ok) +{ + n_vec(v) => v, n; + reserve_vec(v, n + 1 as u64) => v; + set_vec(v, n as i64 - 1, e) => v; + ok(v); +} + +at_vec(vec v, u64 i, (vec, &i64) ok) +{ + v => [n => n, s => s, buf => buf]; + guard(i < n) => { + fwdfree(buf); + fwdpanic("bounds error"); + } => ; + + buf + i => *i64 nbuf; + nil nbuf { + nil nbuf; + fwdfree(buf); + fwdpanic("should never happen"); + } => &i64 bufr; + + nil nbuf; + ok([n => n, s => s, buf => buf] vec, bufr); +} + +destroy_vec(vec v) +{ + v => [n => n, s => s, buf => buf]; + fwdfree(buf); + nil n; + nil s; +} + +populate_vec(i64 i, i64 n, vec v, (vec) ok) +{ + if i < n { + append_vec(v, i) => vec v; + populate_vec(i + 1, n, v, ok); + } else { + ok(v); + } +} + +guard(bool c, () err | () ok) +{ + if c { + err(); + } else { + ok(); + } +} + +check_vec(i64 i, i64 n, vec v, (vec) ok) +{ + if i < n { + at_vec(v, i as u64) => v, elem; + + guard(elem* != i) => { + destroy_vec(v); + fwdpanic("vec built wrong"); + } => ; + + check_vec(i + 1, n, v, ok); + } else { + ok(v); + } +} + +main() +{ + init_vec(0 as u64) => vec v; + populate_vec(0, 1000000, v) => vec v; + check_vec(0, 1000000, v) => vec v; + destroy_vec(v); +} diff --git a/include/fwd/ast.h b/include/fwd/ast.h index 69f7bf1..0fd1118 100644 --- a/include/fwd/ast.h +++ b/include/fwd/ast.h @@ -32,12 +32,47 @@ struct src_loc { 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_ID = 1, TYPE_REF, TYPE_PTR, TYPE_CLOSURE, + TYPE_PURE_CLOSURE, TYPE_FUNC_PTR, TYPE_VOID, TYPE_NIL, TYPE_I8, TYPE_I16, TYPE_I32, TYPE_I64, - TYPE_U8, TYPE_U16, TYPE_U32, TYPE_U64 + TYPE_U8, TYPE_U16, TYPE_U32, TYPE_U64, + TYPE_BOOL }; +static inline bool is_int_type(enum type_kind k) +{ + switch (k) { + case TYPE_I8: + case TYPE_I16: + case TYPE_I32: + case TYPE_I64: + case TYPE_U8: + case TYPE_U16: + case TYPE_U32: + case TYPE_U64: + return true; + + default: + return false; + } + + return false; +} + +static inline bool is_ptr_type(enum type_kind k) +{ + switch (k) { + case TYPE_PTR: + case TYPE_FUNC_PTR: + return true; + + default: + return false; + } + + return false; +} + struct type { enum type_kind k; @@ -70,9 +105,13 @@ enum ast_kind { AST_IMPORT, /** Creating new variable */ AST_LET, + AST_PUT, + AST_WRITE, /** If statement */ AST_IF, /** Nil check */ + AST_NIL_CHECK, + /** Nil */ AST_NIL, /** Call procedure. */ AST_CALL, @@ -80,8 +119,6 @@ enum ast_kind { AST_PROC_DEF, /** Variable declaration/definition. */ AST_VAR_DEF, - /** Dot. \c a.b; */ - AST_DOT, /* Closure */ AST_CLOSURE, /** Construct new type, something!{} */ @@ -124,12 +161,30 @@ enum ast_kind { AST_EQ, /** Negation, \c - */ AST_NEG, + /** Bitwise negation, \c ~ */ + AST_BNEG, /** Logical negation, \c ! */ AST_LNOT, /** Bitwise negation, \c ~ */ AST_NOT, /** Reference (note, not addrof!) */ AST_REF, + /** Sizeof */ + AST_SIZEOF, + AST_AS, + AST_CONSTRUCTION, + AST_CONSTRUCT, + AST_DECONSTRUCTION, + AST_DECONSTRUCT, + AST_EXPLODE, + AST_SELF, + AST_FORGET, + AST_PASTE, + AST_IMPLEMENTS, + AST_TEMPLATE, + AST_INSTANCE, + AST_SUPERTEMPLATE, + AST_INSTANCE_CONT, /** Dereferencing. * @todo should references and pointers both use this? */ @@ -149,6 +204,7 @@ enum ast_flag { AST_FLAG_NOMOVES = (1 << 2), AST_FLAG_PUBLIC = (1 << 3), AST_REQ_FRAME = (1 << 4), + AST_FLAG_LOWERED = (1 << 5) }; struct ast { @@ -270,11 +326,12 @@ static inline bool is_const(struct ast *x) static inline bool is_lvalue(struct ast *node) { switch (node->k) { + case AST_DEREF: case AST_ID: - /** @todo others */ return true; - default: return false; + default: + return false; } return false; @@ -292,16 +349,11 @@ static inline bool is_trivially_copyable(struct type *type) case TYPE_U32: case TYPE_U64: case TYPE_REF: - case TYPE_PTR: + case TYPE_BOOL: case TYPE_FUNC_PTR: case TYPE_PURE_CLOSURE: - /** @todo primitive types */ return true; - case TYPE_ID: - /* very special, bad bad bad */ - return strcmp(type->id, "int") == 0; - default: return false; } @@ -318,11 +370,11 @@ static inline bool is_trivially_copyable(struct type *type) #define gen_str(k, s, loc) gen_ast(k, NULL, NULL, NULL, NULL, NULL, s, -1, 0., \ loc) - #define gen4(k, a, b, c, d, loc) gen_ast(k, a, b, c, d, NULL, NULL, -1, 0., loc) #define gen3(k, a, b, c, loc) gen4(k, a, b, c, NULL, loc) #define gen2(k, a, b, loc) gen3(k, a, b, NULL, loc) #define gen1(k, a, loc) gen2(k, a, NULL, loc) +#define gen0(k, loc) gen1(k, NULL, loc) /* kind of hacky but I guess it works, and allows us to check that the type is @@ -340,17 +392,12 @@ static inline bool is_trivially_copyable(struct type *type) #define tgen_void(loc) \ tgen_type(TYPE_VOID, NULL, NULL, loc) -#define tgen_err(loc) \ - tgen_type(TYPE_ERR, NULL, NULL, loc) - #define tid_str(x) return_id(x, TYPE_ID) #define tgen_id(id, loc) \ tgen_type(TYPE_ID, NULL, id, loc) -#define tconstruct_id(x) return_id(x, TYPE_CONSTRUCT) -#define tconstruct_args(x) return_t0(x, TYPE_CONSTRUCT) -#define tgen_construct(id, args, loc) \ - tgen_type(TYPE_CONSTRUCT, args, id, loc) +#define tgen_nil(loc) \ + tgen_type(TYPE_NIL, NULL, NULL, loc) #define tptr_base(x) return_t0(x, TYPE_PTR) #define tgen_ptr(base, loc) \ @@ -364,7 +411,7 @@ static inline bool is_trivially_copyable(struct type *type) #define tgen_closure(args, loc) \ tgen_type(TYPE_CLOSURE, args, NULL, loc) -#define tpure_closure_args(x) return_t0(x, TYPE_CLOSURE) +#define tpure_closure_args(x) return_t0(x, TYPE_PURE_CLOSURE) #define tgen_pure_closure(args, loc) \ tgen_type(TYPE_PURE_CLOSURE, args, NULL, loc) @@ -387,6 +434,59 @@ static inline bool is_trivially_copyable(struct type *type) #define gen_comparison(op, left, right, loc) \ gen2(op, left, right, loc) +#define construction_id(x) return_s(x, AST_CONSTRUCTION) +#define construction_expr(x) return_a0(x, AST_CONSTRUCTION) +#define gen_construction(id, expr, loc) \ + gen_str1(AST_CONSTRUCTION, id, expr, loc) + +#define construct_id(x) return_s(x, AST_CONSTRUCT) +#define construct_members(x) return_a0(x, AST_CONSTRUCT) +#define gen_construct(id, members, loc) \ + gen_str1(AST_CONSTRUCT, id, members, loc) + +#define deconstruction_id(x) return_s(x, AST_DECONSTRUCTION) +#define deconstruction_var(x) return_a0(x, AST_DECONSTRUCTION) +#define gen_deconstruction(id, expr, loc) \ + gen_str1(AST_DECONSTRUCTION, id, expr, loc) + +#define deconstruct_members(x) return_a0(x, AST_DECONSTRUCT) +#define gen_deconstruct(members, loc) \ + gen1(AST_DECONSTRUCT, members, loc) + +#define gen_paste(l, r, loc) \ + gen2(AST_PASTE, l, r, loc) + +#define forget_id(x) return_s(x, AST_FORGET) +#define gen_forget(id, loc) \ + gen_str(AST_FORGET, id, loc) + +#define gen_implements(id, loc) \ + gen_str(AST_IMPLEMENTS, id, loc) + +#define template_type_params(x) return_a0(x, AST_TEMPLATE) +#define template_body(x) return_a2(x, AST_TEMPLATE) +#define gen_template(id, types, params, body, loc) \ + gen_str3(AST_TEMPLATE, id, types, params, body, loc) + +#define gen_supertemplate(id, types, params, inst, body, loc) \ + gen_ast(AST_SUPERTEMPLATE, types, params, inst, body, NULL, id, 0, 0., loc) + +#define gen_self(loc) \ + gen0(AST_SELF, loc) + +#define instance_id(x) return_s(x, AST_INSTANCE) +#define instance_templ(x) return_a1(x, AST_INSTANCE) +#define instance_type_args(x) return_t2(x, AST_INSTANCE) +#define gen_instance(name, templ, types, exprs, loc) \ + gen_ast(AST_INSTANCE, exprs, templ, NULL, NULL, types, name, 0, 0., loc) + +#define gen_instance_cont(name, body, loc) \ + gen_str1(AST_INSTANCE_CONT, name, body, loc) + +#define sizeof_type(x) return_t2(x, AST_SIZEOF) +#define gen_sizeof(type, loc) \ + gen_ast(AST_SIZEOF, NULL, NULL, NULL, NULL, type, NULL, 0, 0., loc) + #define unop_expr(x) ({assert(is_unop(x)); x->a0;}) #define gen_unop(op, expr, loc) \ gen1(op, expr, loc) @@ -403,11 +503,6 @@ static inline bool is_trivially_copyable(struct type *type) #define gen_proc(id, params, rtype, body, loc) \ gen_ast(AST_PROC_DEF, params, body, NULL, NULL, rtype, id, 0, 0., loc) -#define dot_id(x) return_s(x, AST_DOT) -#define dot_expr(x) return_a0(x, AST_DOT) -#define gen_dot(id, expr, loc) \ - gen_str1(AST_DOT, id, expr, loc) - #define var_id(x) return_s(x, AST_VAR_DEF) #define var_type(x) return_t2(x, AST_VAR_DEF) #define gen_var(id, type, loc) \ @@ -427,10 +522,9 @@ static inline bool is_trivially_copyable(struct type *type) gen_str(AST_TRAIT_DEF, id, loc) #define struct_id(x) return_s(x, AST_STRUCT_DEF) -#define struct_params(x) return_a0(x, AST_STRUCT_DEF) -#define struct_body(x) return_a1(x, AST_STRUCT_DEF) -#define gen_struct(id, params, body, loc) \ - gen_str2(AST_STRUCT_DEF, id, params, body, loc) +#define struct_body(x) return_a0(x, AST_STRUCT_DEF) +#define gen_struct(id, body, loc) \ + gen_str1(AST_STRUCT_DEF, id, body, loc) #define import_file(x) return_s(x, AST_IMPORT) #define gen_import(file, loc) \ @@ -467,6 +561,20 @@ static inline bool is_trivially_copyable(struct type *type) #define gen_let(var, from, loc) \ gen2(AST_LET, var, from, loc) +#define put_dst(x) return_a0(x, AST_PUT) +#define gen_put(dst, loc) \ + gen1(AST_PUT, dst, loc) + +#define write_dst(x) return_a0(x, AST_WRITE) +#define write_src(x) return_a1(x, AST_WRITE) +#define gen_write(dst, src, loc) \ + gen2(AST_WRITE, dst, src, loc) + +#define explode_deconstruct(x) return_a0(x, AST_EXPLODE) +#define explode_expr(x) return_a1(x, AST_EXPLODE) +#define gen_explode(deconstruct, expr, loc) \ + gen2(AST_EXPLODE, deconstruct, expr, loc) + #define init_args(x) return_t2(x, AST_INIT) #define init_body(x) return_a0(x, AST_INIT) #define init_id(x) return_s(x, AST_INIT) @@ -479,11 +587,20 @@ static inline bool is_trivially_copyable(struct type *type) #define gen_if(cond, body, els, loc) \ gen3(AST_IF, cond, body, els, loc) -#define nil_var(x) return_s(x, AST_NIL) -#define nil_body(x) return_a1(x, AST_NIL) -#define nil_ref(x) return_a2(x, AST_NIL) -#define gen_nil(var, body, ref, loc) \ - gen_ast(AST_NIL, NULL, body, ref, NULL, NULL, var, 0, 0., loc) +#define nil_check_expr(x) return_a0(x, AST_NIL_CHECK) +#define nil_check_body(x) return_a1(x, AST_NIL_CHECK) +#define nil_check_ref(x) return_a2(x, AST_NIL_CHECK) +#define nil_check_rest(x) return_a3(x, AST_NIL_CHECK) +#define gen_nil_check(expr, body, ref, loc) \ + gen3(AST_NIL_CHECK, expr, body, ref, loc) + +#define gen_nil(loc) \ + gen0(AST_NIL, loc) + +#define as_expr(x) return_a0(x, AST_AS) +#define as_type(x) return_t2(x, AST_AS) +#define gen_as(expr, type, loc) \ + gen_ast(AST_AS, expr, NULL, NULL, NULL, type, NULL, 0, 0., loc) #define own_id(x) return_s(x, AST_OWN) #define own_body(x) return_a1(x, AST_OWN) diff --git a/include/fwd/debug.h b/include/fwd/debug.h index c3dce63..ac4dfdf 100644 --- a/include/fwd/debug.h +++ b/include/fwd/debug.h @@ -96,6 +96,9 @@ void semantic_warn(struct scope *scope, struct ast *node, const char *fmt, ...); void semantic_error(struct scope *scope, struct ast *node, const char *fmt, ...); +void type_error(struct scope *scope, struct type *type, const char *fmt, + ...); + void loc_error(struct scope *scope, struct src_loc loc, const char *fmt, ...); /** @@ -141,6 +144,6 @@ void type_mismatch(struct scope *scope, struct ast *node, void move_error(struct ast *new_use, struct ast *prev_use); void reference_error(struct ast *new_use, struct ast *prev_use); -const char *type_str(struct type *t); +char *type_str(struct type *t); #endif /* FWD_DEBUG_H */ diff --git a/include/fwd/mod.h b/include/fwd/mod.h index 6031c92..51aa9c4 100644 --- a/include/fwd/mod.h +++ b/include/fwd/mod.h @@ -61,6 +61,10 @@ static inline void *fwd_arg(fwd_extern_args_t args, size_t idx, fwd_type_t id) #define FWD_ARG_T(args, idx, type) \ FWD_ARG(args, idx, type, FWD_T(type)) +/* unimplemented as of yet */ +#define FWD_RET(id, x) \ + abort() + static inline fwd_type_t fwd_t_signed(size_t s) { switch (s) { diff --git a/include/fwd/rewrite.h b/include/fwd/rewrite.h new file mode 100644 index 0000000..d9c4129 --- /dev/null +++ b/include/fwd/rewrite.h @@ -0,0 +1,9 @@ +#ifndef FWD_REWRITE_H +#define FWD_REWRITE_H + +#include <fwd/ast.h> + +int rewrite_types(struct ast *node, char *orig, char *new); +int rewrite_holes(struct ast *node, char *new); + +#endif /* FWD_REWRITE_H */ diff --git a/include/fwd/scope.h b/include/fwd/scope.h index ab268bc..c3bafa0 100644 --- a/include/fwd/scope.h +++ b/include/fwd/scope.h @@ -55,6 +55,7 @@ struct scope { /** List of child scopes. */ struct scope *children; + struct visible templates; struct visible symbols; struct visible traits; struct visible types; @@ -113,13 +114,11 @@ int scope_add_scratch(struct scope *scope, struct ast *scratch); */ void scope_add_scope(struct scope *parent, struct scope *child); - int scope_add_symbol(struct scope *scope, struct ast *symbol); struct ast *scope_find_symbol(struct scope *scope, char *id); struct ast *file_scope_find_symbol(struct scope *scope, char *id); int scope_add_type(struct scope *scope, struct ast *type); -int scope_add_chain(struct scope *scope, struct ast *type); struct ast *scope_find_type(struct scope *scope, char *id); struct ast *file_scope_find_type(struct scope *scope, char *id); @@ -127,4 +126,8 @@ int scope_add_trait(struct scope *scope, struct ast *trait); struct ast *scope_find_trait(struct scope *scope, char *id); struct ast *file_scope_find_trait(struct scope *scope, char *id); +int scope_add_template(struct scope *scope, struct ast *trait); +struct ast *scope_find_template(struct scope *scope, char *id); +struct ast *file_scope_find_template(struct scope *scope, char *id); + #endif /* SCOPE_H */ diff --git a/mod/Makefile b/mod/Makefile index 0a0c045..9121afd 100644 --- a/mod/Makefile +++ b/mod/Makefile @@ -1,4 +1,10 @@ -all: libfwdio.so +all: libfwdio.so libfwdmem.so libfwdutil.so libfwdio.so: io.c ../include/fwd/mod.h $(CC) -I../include -fPIC -O2 -g -Wall -Wextra -shared io.c -o libfwdio.so + +libfwdmem.so: mem.c ../include/fwd/mod.h + $(CC) -I../include -fPIC -O2 -g -Wall -Wextra -shared mem.c -o libfwdmem.so + +libfwdutil.so: util.c ../include/fwd/mod.h + $(CC) -I../include -fPIC -O2 -g -Wall -Wextra -shared util.c -o libfwdutil.so @@ -4,14 +4,14 @@ #include <fwd/mod.h> -long print_nl(fwd_extern_args_t args) +long fwdprint_nl(fwd_extern_args_t args) { assert(args.argc == 0); putchar('\n'); return 0; } -long print_i64(fwd_extern_args_t args) +long fwdprint_i64(fwd_extern_args_t args) { assert(args.argc == 1); int64_t n = FWD_ARG_T(args, 0, int64_t); @@ -21,10 +21,10 @@ long print_i64(fwd_extern_args_t args) int fwdopen(fwd_state_t *state) { - FWD_REGISTER(state, print_nl, + FWD_REGISTER(state, fwdprint_nl, FWD_VOID); - FWD_REGISTER(state, print_i64, + FWD_REGISTER(state, fwdprint_i64, FWD_VOID, FWD_T(int64_t)); return 0; diff --git a/mod/mem.c b/mod/mem.c new file mode 100644 index 0000000..96d6d0e --- /dev/null +++ b/mod/mem.c @@ -0,0 +1,50 @@ +#include <fwd/mod.h> + +long fwdmalloc(fwd_extern_args_t args) +{ + assert(args.argc == 1); + size_t n = FWD_ARG_T(args, 0, size_t); + FWD_RET(FWD_PTR, malloc(n)); + return 0; +} + +long fwdrealloc(fwd_extern_args_t args) +{ + assert(args.argc == 2); + void *ptr = FWD_ARG(args, 0, void *, FWD_PTR); + size_t n = FWD_ARG_T(args, 1, size_t); + FWD_RET(FWD_PTR, realloc(ptr, n)); + return 0; +} + +long fwdfree(fwd_extern_args_t args) +{ + assert(args.argc == 1); + void *ptr = FWD_ARG(args, 0, void *, FWD_PTR); + return 0; +} + +long fwdptradd(fwd_extern_args_t args) +{ + assert(args.argc == 2); + void *ptr = FWD_ARG(args, 0, void *, FWD_PTR); + int64_t add = FWD_ARG_T(args, 1, int64_t); + FWD_RET(FWD_PTR, (char *)ptr + add); + return 0; +} + +int fwdopen(fwd_state_t *state) +{ + FWD_REGISTER(state, fwdmalloc, + FWD_PTR, FWD_T(size_t)); + + FWD_REGISTER(state, fwdrealloc, + FWD_PTR, FWD_PTR, FWD_T(size_t)); + + FWD_REGISTER(state, fwdfree, + FWD_VOID, FWD_PTR); + + FWD_REGISTER(state, fwdptradd, + FWD_PTR, FWD_PTR, FWD_T(int64_t)); + return 0; +} diff --git a/mod/util.c b/mod/util.c new file mode 100644 index 0000000..c967349 --- /dev/null +++ b/mod/util.c @@ -0,0 +1,20 @@ +#include <fwd/mod.h> +#include <stdio.h> + +long fwdpanic(fwd_extern_args_t args) +{ + assert(args.argc == 1); + char *str = FWD_ARG_T(args, 0, char *); + fprintf(stderr, "%s", str); + exit(1); + return 0; +} + +int fwdopen(fwd_state_t *state) +{ + /** @todo passing around strings might be common enough to warrant its + * own type? */ + FWD_REGISTER(state, fwdpanic, + FWD_VOID, FWD_PTR); + return 0; +} diff --git a/scripts/gen-deps b/scripts/gen-deps index f45707c..0073980 100755 --- a/scripts/gen-deps +++ b/scripts/gen-deps @@ -18,7 +18,7 @@ done shift $((OPTIND - 1)) # create all subdirectories -mkdir -p $(echo "${@}" | tr ' ' '\n' | sed "s|[^/]*$||;s|^|${BUILD}/|" | uniq) +mkdir -p $(echo "${@}" | tr ' ' '\n' | sed "s|[^/]*$||;s|^|${BUILD}/|" | sort -u) for s in ${@} do diff --git a/src/analyze.c b/src/analyze.c index be52bfb..04b7856 100644 --- a/src/analyze.c +++ b/src/analyze.c @@ -3,7 +3,9 @@ #include <fwd/compiler.h> #include <fwd/analyze.h> +#include <fwd/rewrite.h> #include <fwd/mod.h> + #include <stdlib.h> #include <stdarg.h> #include <string.h> @@ -104,6 +106,19 @@ static int analyze_binop(struct state *state, struct scope *scope, if (analyze(state, scope, rhs)) return -1; + /* allow pointer arithmetic, follows C conventions */ + if (node->k == AST_ADD || node->k == AST_SUB) { + if (is_ptr_type(lhs->t->k) && is_int_type(rhs->t->k)) { + node->t = lhs->t; + return 0; + } + + if (is_ptr_type(rhs->t->k) && is_int_type(lhs->t->k)) { + node->t = rhs->t; + return 0; + } + } + if (!types_match(lhs->t, rhs->t)) { type_mismatch(scope, node, lhs->t, rhs->t); @@ -133,17 +148,9 @@ static int analyze_comparison(struct state *state, struct scope *scope, return -1; } - /** @todo check type is some primitive */ - char *tf = strdup("bool"); - if (!tf) { - internal_error("failed allocating comparison bool str"); - return -1; - } - - node->t = tgen_id(tf, node->loc); + node->t = tgen_type(TYPE_BOOL, NULL, NULL, node->loc); if (!node->t) { internal_error("failed allocating comparison bool type"); - free(tf); return -1; } @@ -188,10 +195,14 @@ static int analyze_var(struct state *state, struct scope *scope, static int analyze_let(struct state *state, struct scope *scope, struct ast *node) { - if (analyze(state, scope, let_var(node))) + if (analyze(state, scope, let_expr(node))) return -1; - if (analyze(state, scope, let_expr(node))) + struct ast *var = let_var(node); + if (!(var_type(var))) + var_type(var) = (let_expr(node))->t; + + if (analyze(state, scope, var)) return -1; struct type *l = (let_var(node))->t; @@ -214,16 +225,40 @@ static int analyze_init(struct state *state, struct scope *scope, if (analyze(state, scope, init_body(node))) return -1; - /** @todo check that all parameters match, though that requires that the - * type is defined in fwd and not in C++, so this might have to wait a - * bit */ - node->t = tgen_construct(strdup(init_id(node)), init_args(node), - node->loc); - if (!node->t) { - internal_error("failed allocating type for construct"); + internal_error("init unimplemented"); + return -1; +} + +static int deduce_closure_types(struct scope *scope, struct ast *node, struct type *type) +{ + /* a bit of checking duplication here, hmm */ + if (node->k != AST_CLOSURE) + return 0; + + if (type->k != TYPE_CLOSURE && type->k != TYPE_PURE_CLOSURE) { + semantic_error(scope, node, "surprise closure"); return -1; } + struct ast *param = closure_bindings(node); + struct type *t = (type->k == TYPE_CLOSURE) + ? tclosure_args(type) + : tpure_closure_args(type) + ; + + if (ast_list_len(param) != type_list_len(t)) { + semantic_error(scope, node, "expected %zu closure parameters, got %zu", + type_list_len(t), ast_list_len(param)); + return -1; + } + + for (; param && t; param = param->n, t = t->n) { + if (var_type(param)) + continue; + + var_type(param) = t; + } + return 0; } @@ -241,8 +276,6 @@ static int analyze_call(struct state *state, struct scope *scope, struct type *types = callable_types(expr->t); struct ast *args = call_args(node); - if (analyze_list(state, scope, args)) - return -1; size_t expected = type_list_len(types); size_t got = ast_list_len(args); @@ -253,18 +286,24 @@ static int analyze_call(struct state *state, struct scope *scope, } struct type *type = types; - foreach_node(arg, args) { + struct ast *arg = args; + for (; arg && type; type = type->n, arg = arg->n) { + if (deduce_closure_types(scope, arg, type)) + return -1; + + if (analyze(state, scope, arg)) + return -1; + if (arg->k == AST_CLOSURE) state->flags |= STATE_PROC_REQ_FRAME; if (!types_match(type, arg->t)) { - type_mismatch(scope, node, type, arg->t); + type_mismatch(scope, arg, type, arg->t); return -1; } /* clone group info */ arg->t->group = type->group; - type = type->n; } node->t = tgen_void(node->loc); @@ -309,7 +348,7 @@ static int analyze_deref(struct state *state, struct scope *scope, semantic_error(node->scope, node, "deref of raw ptr not allowed"); semantic_info(node->scope, node, - "use fwd_null() to convert to ref"); + "use a nil check to convert to ref"); return -1; } @@ -319,7 +358,7 @@ static int analyze_deref(struct state *state, struct scope *scope, return -1; } - node->t = tptr_base(expr->t); + node->t = tref_base(expr->t); return 0; } @@ -358,7 +397,6 @@ static int analyze_closure(struct state *state, struct scope *scope, } node->scope = closure_scope; - closure_scope->flags |= SCOPE_PROC; scope_add_scope(scope, closure_scope); if (analyze_list(state, closure_scope, closure_bindings(node))) @@ -432,6 +470,189 @@ static int analyze_str(struct state *state, struct scope *scope, return 0; } +static int analyze_template(struct state *state, struct scope *scope, struct ast *node) +{ + struct scope *template_scope = create_scope(); + scope_add_scope(scope, template_scope); + + /* enough for vec example since traits are as of yet unimplemented */ + + node->t = tgen_void(node->loc); + return 0; +} + +static int analyze_instance(struct state *state, struct scope *scope, struct ast *node) +{ + (void)state; + + struct ast *id = instance_templ(node); + struct ast *template = file_scope_find_template(scope, id_str(id)); + if (!template) { + semantic_error(scope, node, "no such template: %s\n", id_str(id)); + return -1; + } + + /** @todo check that type implements trait */ + struct ast *params = template_type_params(template); + struct type *args = instance_type_args(node); + if (ast_list_len(params) != type_list_len(args)) { + semantic_error(scope, node, "expected %zu types, got %zu\n", + ast_list_len(params), type_list_len(args)); + return -1; + } + + /* make a working copy that we modify */ + struct ast *copy = clone_ast(template); + if (!copy) { + internal_error("failed copying template body"); + return -1; + } + + if (rewrite_holes(copy, instance_id(node))) + return -1; + + /* replace type args */ + struct ast *p = params; + struct type *a = args; + for (; p && a; p = p->n, a = a->n) { + if (rewrite_types(copy, var_id(p), tid_str(a))) + return -1; + } + + printf("// --- instance %s ---\n", instance_id(node)); + ast_dump(0, copy); + + /* analyze newly initialized things (might not work for supertemplates?) */ + if (analyze_root(scope, template_body(copy))) + return -1; + + node->t = tgen_void(node->loc); + return 0; +} + +static int analyze_struct(struct state *state, struct scope *scope, struct ast *node) +{ + struct scope *struct_scope = create_scope(); + scope_add_scope(scope, struct_scope); + node->scope = struct_scope; + + if (analyze_list(state, struct_scope, struct_body(node))) + return -1; + + node->t = tgen_void(node->loc); + return 0; +} + +static int analyze_construct(struct state *state, struct scope *scope, struct ast *node) +{ + struct ast *def = file_scope_find_type(scope, construct_id(node)); + if (!def) { + semantic_error(scope, node, "no such type: %s", construct_id(node)); + return -1; + } + + if (def->k != AST_STRUCT_DEF) { + semantic_error(scope, node, "type is not a struct"); + return -1; + } + + struct ast *params = struct_body(def); + struct ast *args = construct_members(node); + + if (ast_list_len(params) != ast_list_len(args)) { + semantic_error(scope, node, "expected %zu args, got %zu\n", + ast_list_len(params), ast_list_len(args)); + return -1; + } + + foreach_node(arg, args) { + struct ast *exists = scope_find_symbol(def->scope, construction_id(arg)); + if (!exists) { + semantic_error(scope, arg, "no member %s in %s\n", + construction_id(arg), construct_id(node)); + return -1; + } + + struct ast *expr = construction_expr(arg); + if (analyze(state, scope, expr)) + return -1; + + if (!types_match(exists->t, expr->t)) { + type_mismatch(scope, arg, exists->t, expr->t); + return -1; + } + + /** @todo check for duplicates, preferably not in O(n^2) or with + * a memory allocation? */ + } + + node->t = tgen_id(strdup(construct_id(node)), node->loc); + return 0; +} + +static int analyze_explode(struct state *state, struct scope *scope, struct ast *node) +{ + if (analyze(state, scope, explode_expr(node))) + return -1; + + struct type *type = (explode_expr(node))->t; + if (type->k != TYPE_ID) { + char *str = type_str(type); + semantic_error(scope, explode_expr(node), + "expected struct type, got %s\n", + str); + free(str); + return -1; + } + + struct ast *def = file_scope_find_type(scope, type->id); + if (!def || def->k != AST_STRUCT_DEF) { + semantic_error(scope, explode_expr(node), + "no such struct type: %s\n", + type->id); + return -1; + } + + struct ast *params = struct_body(def); + struct ast *args = explode_deconstruct(node); + + if (ast_list_len(params) != ast_list_len(args)) { + semantic_error(scope, node, "expected %zu args, got %zu\n", + ast_list_len(params), ast_list_len(args)); + return -1; + } + + foreach_node(arg, args) { + struct ast *var = deconstruction_var(arg); + struct ast *exists = scope_find_symbol(def->scope, deconstruction_id(arg)); + if (!exists) { + semantic_error(scope, arg, "no member %s in %s\n", + deconstruction_id(arg), type->id); + return -1; + } + + if (!var_type(var)) + (var_type(var)) = exists->t; + + if (analyze(state, scope, var)) + return -1; + + if (!types_match(exists->t, var->t)) { + type_mismatch(scope, arg, exists->t, var->t); + return -1; + } + + if (scope_add_symbol(scope, var)) + return -1; + + /** @todo check for duplicates, preferably not in O(n^2) or with + * a memory allocation? */ + } + + node->t = tgen_void(node->loc); + return 0; +} + static int analyze_if(struct state *state, struct scope *scope, struct ast *node) { @@ -453,6 +674,164 @@ static int analyze_if(struct state *state, struct scope *scope, return 0; } +static int analyze_nil(struct state *state, struct scope *scope, struct ast *node) +{ + (void)state; + (void)scope; + + node->t = tgen_nil(node->loc); + return 0; +} + +static bool castable_type(struct type *type) +{ + switch (type->k) { + case TYPE_PTR: + case TYPE_FUNC_PTR: + case TYPE_I8: + case TYPE_I16: + case TYPE_I32: + case TYPE_I64: + case TYPE_U8: + case TYPE_U16: + case TYPE_U32: + case TYPE_U64: + return true; + + default: + return false; + } + + return false; +} + +static int analyze_as(struct state *state, struct scope *scope, struct ast *node) +{ + if (analyze(state, scope, as_expr(node))) + return -1; + + if (analyze_type(state, scope, as_type(node))) + return -1; + + /* for now, any 'register' size type can be cast to any other. Might + * restrict this, like signed to unsigned might be weird if its negative + * etc and should be done via a helper function */ + struct type *expr_type = (as_expr(node))->t; + struct type *type = as_type(node); + if (!castable_type(expr_type)) { + char *s = type_str(expr_type); + semantic_error(scope, as_expr(node), "can't cast from %s\n", s); + free(s); + return -1; + } + + if (!castable_type(type)) { + char *s = type_str(type); + type_error(scope, type, "can't cast to %s\n", s); + free(s); + return -1; + } + + node->t = type; + return 0; +} + +static int analyze_sizeof(struct state *state, struct scope *scope, struct ast *node) +{ + if (analyze_type(state, scope, sizeof_type(node))) + return -1; + + /* hmm, no built-in size_t */ + node->t = tgen_type(TYPE_U64, NULL, NULL, node->loc); + return 0; +} + +static int analyze_nil_check(struct state *state, struct scope *scope, struct ast *node) +{ + struct ast *expr = nil_check_expr(node); + if (analyze(state, scope, expr)) + return -1; + + if (!is_ptr_type(expr->t->k)) { + char *s = type_str(expr->t); + semantic_error(scope, expr, "expected ptr type, got %s", s); + free(s); + return -1; + } + + if (analyze(state, scope, nil_check_body(node))) + return -1; + + struct ast *var = nil_check_ref(node); + struct type *base = tptr_base(expr->t); + struct type *ref = tgen_ref(clone_type(base), node->loc); + if (!var_type(var)) + var_type(var) = ref; + + if (analyze(state, scope, var)) + return -1; + + if (!types_match(ref, var->t)) { + type_mismatch(scope, var, ref, var->t); + return -1; + } + + if (analyze(state, scope, nil_check_rest(node))) + return -1; + + node->t = tgen_void(node->loc); + return 0; +} + +static int analyze_forget(struct state *state, struct scope *scope, struct ast *node) +{ + struct ast *def = file_scope_find_symbol(scope, forget_id(node)); + if (!def) { + semantic_error(scope, node, "no such symbol"); + return -1; + } + + node->t = tgen_void(node->loc); + return 0; +} + +static int analyze_put(struct state *state, struct scope *scope, struct ast *put) +{ + struct ast *dst = put_dst(put); + if (analyze(state, scope, dst)) + return -1; + + struct type *type = dst->t; + if (type->k != TYPE_REF) { + char *str = type_str(type); + semantic_error(scope, put, "trying to write to non-ref type %s\n", str); + free(str); + return -1; + } + + put->t = tref_base(type); + return 0; +} + +static int analyze_write(struct state *state, struct scope *scope, struct ast *write) +{ + struct ast *src = write_src(write); + if (analyze(state, scope, src)) + return -1; + + struct ast *dst = write_dst(write); + if (analyze(state, scope, dst)) + return -1; + + if (!types_match(src->t, dst->t)) { + type_mismatch(scope, write, src->t, dst->t); + return -1; + } + + write->t = tgen_void(write->loc); + return 0; +} + static int analyze(struct state *state, struct scope *scope, struct ast *node) { if (!node) @@ -502,9 +881,32 @@ static int analyze(struct state *state, struct scope *scope, struct ast *node) case AST_ID: ret = analyze_id(state, scope, node); break; case AST_IF: ret = analyze_if(state, scope, node); break; + case AST_NIL_CHECK: ret = analyze_nil_check(state, scope, node); break; + case AST_CONST_INT: ret = analyze_int(state, scope, node); break; case AST_CONST_STR: ret = analyze_str(state, scope, node); break; + case AST_INSTANCE: ret = analyze_instance(state, scope, node); break; + case AST_TEMPLATE: ret = analyze_template(state, scope, node); break; + + case AST_STRUCT_DEF: ret = analyze_struct(state, scope, node); break; + case AST_CONSTRUCT: ret = analyze_construct(state, scope, node); break; + case AST_EXPLODE: ret = analyze_explode(state, scope, node); break; + + case AST_SIZEOF: ret = analyze_sizeof(state, scope, node); break; + case AST_NIL: ret = analyze_nil(state, scope, node); break; + case AST_AS: ret = analyze_as(state, scope, node); break; + + case AST_WRITE: ret = analyze_write(state, scope, node); break; + case AST_PUT: ret = analyze_put(state, scope, node); break; + + case AST_FORGET: ret = analyze_forget(state, scope, node); break; + + case AST_TRAIT_DEF: + node->t = tgen_void(node->loc); + ret = 0; + break; + case AST_EMPTY: node->t = tgen_void(node->loc); ret = 0; @@ -544,43 +946,70 @@ static int analyze_list(struct state *state, struct scope *scope, static int analyze_type(struct state *state, struct scope *scope, struct type *type) { - if (type->t0 && analyze_type(state, scope, type->t0)) + if (type->t0 && analyze_type_list(state, scope, type->t0)) return -1; /* temporary */ - if (type->k != TYPE_ID) + if (!type->id) return 0; char *id = type->id; - if (strcmp(id, "i8") == 0) + /* handle primitives */ + if (strcmp(id, "i8") == 0) { type->k = TYPE_I8; + return 0; + } - else if (strcmp(id, "u8") == 0) + if (strcmp(id, "u8") == 0) { type->k = TYPE_U8; + return 0; + } - else if (strcmp(id, "i16") == 0) + if (strcmp(id, "i16") == 0) { type->k = TYPE_I16; + return 0; + } - else if (strcmp(id, "u16") == 0) + if (strcmp(id, "u16") == 0) { type->k = TYPE_U16; + return 0; + } - else if (strcmp(id, "i32") == 0) + if (strcmp(id, "i32") == 0) { type->k = TYPE_I32; + return 0; + } - else if (strcmp(id, "u32") == 0) + if (strcmp(id, "u32") == 0) { type->k = TYPE_U32; + return 0; + } - else if (strcmp(id, "i64") == 0) + if (strcmp(id, "i64") == 0) { type->k = TYPE_I64; + return 0; + } - else if (strcmp(id, "u64") == 0) + if (strcmp(id, "u64") == 0) { type->k = TYPE_U64; - else { - internal_error("unhandled type id: %s", id); - abort(); + return 0; + } + + if (strcmp(id, "bool") == 0) { + type->k = TYPE_BOOL; + return 0; } + /* check for user-defined types */ + struct ast *def = file_scope_find_type(scope, id); + if (!def) { + type_error(scope, type, "no such type: %s", id); + return -1; + } + + /* don't change type->k since we might want to check up on this again + * later */ return 0; } @@ -609,27 +1038,39 @@ static int copy_scope(bool reexport, struct scope *to, struct scope *from) return -1; } - foreach(visible, symbol, &from->types) { - struct ast *def = symbol->data; + foreach(visible, type, &from->types) { + struct ast *def = type->data; if (!reexport && def->scope != from) continue; if (!ast_flags(def, AST_FLAG_PUBLIC)) continue; - if (scope_add_symbol(to, def)) + if (scope_add_type(to, def)) return -1; } - foreach(visible, symbol, &from->traits) { - struct ast *def = symbol->data; + foreach(visible, trait, &from->traits) { + struct ast *def = trait->data; if (!reexport && def->scope != from) continue; if (!ast_flags(def, AST_FLAG_PUBLIC)) continue; - if (scope_add_symbol(to, def)) + if (scope_add_trait(to, def)) + return -1; + } + + foreach(visible, template, &from->templates) { + struct ast *def = template->data; + if (!reexport && def->scope != from) + continue; + + if (!ast_flags(def, AST_FLAG_PUBLIC)) + continue; + + if (scope_add_template(to, def)) return -1; } @@ -677,9 +1118,10 @@ 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_U64: return tgen_type(TYPE_U64, NULL, NULL, NULL_LOC()); case FWD_PTR: { - struct type *base = tgen_void(NULL_LOC()); - return tgen_ptr(base, NULL_LOC()); + /* hack? */ + return tgen_nil(NULL_LOC()); } default: @@ -697,20 +1139,42 @@ int fwd_register(struct fwd_state *state, const char *name, fwd_extern_t func, f va_list args; va_start(args, rtype); + + size_t idx = 0; 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()); + /* eight bytes is likely more than enough, since first three are + * var, then we have four bytes for indexes and one trailing + * NULL */ + char *name = calloc(8, sizeof(char)); + assert(name); + + sprintf(name, "%s%zu", "var", idx); + struct ast *new = gen_var(name, fwd_type_kind(type), NULL_LOC()); if (vars) - vars->n = new; - else - vars = new; + new->n = vars; + + vars = new; + idx++; } va_end(args); + /* append 'return' as a continuation */ + if (rtype != FWD_VOID) { + char *name = strdup("ret"); + struct ast *new = gen_var(name, + tgen_closure(fwd_type_kind(rtype), NULL_LOC()), + NULL_LOC()); + if (vars) + new->n = vars; + + vars = new; + } + vars = reverse_ast_list(vars); struct ast *def = gen_proc(strdup(name), vars, @@ -737,8 +1201,7 @@ int analyze_root(struct scope *scope, struct ast *root) break; case AST_STRUCT_CONT: - if (scope_add_chain(scope, node)) - return -1; + abort(); break; case AST_TRAIT_DEF: @@ -756,6 +1219,19 @@ int analyze_root(struct scope *scope, struct ast *root) return -1; } + case AST_TEMPLATE: + if (scope_add_template(scope, node)) + return -1; + break; + + case AST_SUPERTEMPLATE: + if (scope_add_template(scope, node)) + return -1; + break; + + case AST_INSTANCE: + break; + default: abort(); } @@ -215,11 +215,12 @@ void ast_dump(int depth, struct ast *n) DUMP(AST_IF); DUMP(AST_NIL); DUMP(AST_LET); + DUMP(AST_PUT); + DUMP(AST_WRITE); DUMP(AST_INIT); DUMP(AST_CALL); DUMP(AST_PROC_DEF); DUMP(AST_VAR_DEF); - DUMP(AST_DOT); DUMP(AST_BLOCK); DUMP(AST_ID); DUMP(AST_EMPTY); @@ -243,6 +244,23 @@ void ast_dump(int depth, struct ast *n) DUMP(AST_NOT); DUMP(AST_REF); DUMP(AST_DEREF); + DUMP(AST_NIL_CHECK); + DUMP(AST_BNEG); + DUMP(AST_SIZEOF); + DUMP(AST_AS); + DUMP(AST_CONSTRUCTION); + DUMP(AST_CONSTRUCT); + DUMP(AST_DECONSTRUCTION); + DUMP(AST_DECONSTRUCT); + DUMP(AST_EXPLODE); + DUMP(AST_SELF); + DUMP(AST_FORGET); + DUMP(AST_PASTE); + DUMP(AST_IMPLEMENTS); + DUMP(AST_TEMPLATE); + DUMP(AST_INSTANCE); + DUMP(AST_SUPERTEMPLATE); + DUMP(AST_INSTANCE_CONT); DUMP(AST_CONST_INT); DUMP(AST_CONST_CHAR); DUMP(AST_CONST_BOOL); @@ -320,6 +338,9 @@ struct ast *clone_ast(struct ast *n) if (n->a3) new->a3 = clone_ast_list(n->a3); + if (n->t2) + new->t2 = clone_type_list(n->t2); + /** @todo rebuild opt group? */ return new; @@ -351,13 +372,14 @@ struct type *clone_type(struct type *type) return NULL; new->k = type->k; - if (type->id && !(new->id = strdup(type->id))) - return NULL; + if (type->id) + new->id = strdup(type->id); - if (type->t0 && !(new->t0 = clone_type_list(type->t0))) - return NULL; + if (type->t0) + new->t0 = clone_type_list(type->t0); new->group = type->group; + new->loc = type->loc; return new; } @@ -548,10 +570,24 @@ struct type *reverse_type_list(struct type *root) void fix_closures(struct ast *root) { while (root) { - if (root->k != AST_CALL) { + if (root->k != AST_CALL && root->k != AST_NIL_CHECK) { root = root->n; continue; } + if (root->k == AST_NIL_CHECK) { + struct ast *next = root->n; + if (!next) + next = gen_empty(root->loc); + + struct ast *block = gen_block(next, root->loc); + nil_check_rest(root) = block; + root->n = NULL; + + root = next; + continue; + } + + /* call */ struct ast *arg = ast_last(call_args(root)); if (!arg) { root = root->n; @@ -589,6 +625,13 @@ bool types_match(struct type *a, struct type *b) if (!a && b) return false; + /* nil matches any kind of pointer */ + if (is_ptr_type(a->k) && b->k == TYPE_NIL) + return true; + + if (a->k == TYPE_NIL && is_ptr_type(b->k)) + return true; + if (a->k != b->k) return false; @@ -634,11 +677,12 @@ const char *ast_str(enum ast_kind k) CASE(AST_IF); CASE(AST_NIL); CASE(AST_LET); + CASE(AST_PUT); + CASE(AST_WRITE); CASE(AST_INIT); CASE(AST_CALL); CASE(AST_PROC_DEF); CASE(AST_VAR_DEF); - CASE(AST_DOT); CASE(AST_BLOCK); CASE(AST_ID); CASE(AST_EMPTY); @@ -662,11 +706,28 @@ const char *ast_str(enum ast_kind k) CASE(AST_NOT); CASE(AST_REF); CASE(AST_DEREF); + CASE(AST_NIL_CHECK); + CASE(AST_BNEG); + CASE(AST_SIZEOF); + CASE(AST_AS); + CASE(AST_CONSTRUCTION); + CASE(AST_CONSTRUCT); + CASE(AST_SELF); + CASE(AST_FORGET); + CASE(AST_PASTE); + CASE(AST_IMPLEMENTS); + CASE(AST_TEMPLATE); + CASE(AST_INSTANCE); + CASE(AST_SUPERTEMPLATE); + CASE(AST_INSTANCE_CONT); CASE(AST_CONST_INT); CASE(AST_CONST_CHAR); CASE(AST_CONST_BOOL); CASE(AST_CONST_FLOAT); CASE(AST_CONST_STR); + CASE(AST_DECONSTRUCT); + CASE(AST_DECONSTRUCTION); + CASE(AST_EXPLODE); } #undef CASE diff --git a/src/debug.c b/src/debug.c index f3540f0..538fdfe 100644 --- a/src/debug.c +++ b/src/debug.c @@ -134,14 +134,27 @@ void semantic_error(struct scope *scope, struct ast *node, va_end(args); } +void type_error(struct scope *scope, struct type *type, + const char *fmt, ...) +{ + va_list args; + va_start(args, fmt); + struct src_issue issue; + issue.level = SRC_ERROR; + issue.loc = type->loc; + issue.fctx = scope->fctx; + _issue(issue, fmt, args); + va_end(args); +} + void type_mismatch(struct scope *scope, struct ast *node, struct type *l, struct type *r) { - const char *ls = type_str(l); - const char *rs = type_str(r); + char *ls = type_str(l); + char *rs = type_str(r); semantic_error(scope, node, "type mismatch: %s vs %s\n", ls, rs); - free((void *)ls); - free((void *)rs); + free(ls); + free(rs); } void move_error(struct ast *new_use, struct ast *prev_use) @@ -209,10 +222,18 @@ static void _type_str(FILE *f, struct type *type) fprintf(f, "u64"); break; + case TYPE_BOOL: + fprintf(f, "bool"); + break; + case TYPE_VOID: fprintf(f, "void"); break; + case TYPE_NIL: + fprintf(f, "nil"); + break; + case TYPE_PTR: fprintf(f, "*"); _type_list_str(f, tptr_base(type)); @@ -244,16 +265,10 @@ static void _type_str(FILE *f, struct type *type) _type_list_str(f, type->t0); fprintf(f, ")"); break; - - case TYPE_CONSTRUCT: - fprintf(f, "%s![", type->id); - _type_list_str(f, type->t0); - fprintf(f, "]"); - break; } } -const char *type_str(struct type *t) +char *type_str(struct type *t) { if (!t) return strdup("NULL"); diff --git a/src/lexer.l b/src/lexer.l index 24bda93..4f6598a 100644 --- a/src/lexer.l +++ b/src/lexer.l @@ -38,8 +38,7 @@ INT {HEX}|{DEC}|{OCT}|{BIN} HEXF [+-]?0[xX][0-9a-fA-F]+([pP][+-]?[0-9]+) DECF [+-]?[0-9]+[.]([eE]?[+-]?[0-9]+)?[fF]? -ID [_a-zA-Z][_a-zA-Z0-9]* -APPLY {ID}! +ID ((<>)|[_a-zA-Z])[_a-zA-Z0-9]* STRING \"(\\.|[^"\\])*\" @@ -64,7 +63,6 @@ STRING \"(\\.|[^"\\])*\" \n {} } -"::" {return SCOPE;} "(" {return LPAREN;} ")" {return RPAREN;} "{" {return LBRACE;} @@ -126,6 +124,7 @@ STRING \"(\\.|[^"\\])*\" "<<" {return LSHIFT;} ">>" {return RSHIFT;} +"as" {return AS;} "if" {return IF;} "else" {return ELSE;} "nil" {return NIL;} @@ -138,9 +137,9 @@ STRING \"(\\.|[^"\\])*\" "mut" {return MUT;} +"sizeof" {return SIZEOF;} + {STRING} { - /* seems risky, I know, but letting the parser choose when to allocate a - * new string seems to help with syntax error cleanup */ yylval->str = strdup(yytext); return STRING; } @@ -155,16 +154,6 @@ STRING \"(\\.|[^"\\])*\" return ID; } -{APPLY} { - /* strip trailing '!' */ - char *s = yytext + strlen(yytext); - s[-1] = '\0'; - - yylval->str = strdup(yytext); - return APPLY; -} - - [[:space:]]+ {/* skip whitespace */} . { diff --git a/src/lower.c b/src/lower.c index e29365d..9689928 100644 --- a/src/lower.c +++ b/src/lower.c @@ -27,7 +27,11 @@ 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->k == AST_STRUCT_DEF + || 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); } @@ -150,6 +154,12 @@ static void add_defn(struct state *state, char *defn) string_vec_append(state->defns, defn); } +static bool type_lowered(struct ast *type) +{ + assert(type->k == AST_STRUCT_DEF); + return ast_flags(type, AST_FLAG_LOWERED); +} + static void add_type(struct state *state, char *type) { string_vec_append(state->types, type); @@ -160,8 +170,10 @@ 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); +static int lower_type_def(struct state *state, struct ast *type); +static char *lower_type_str(struct state *state, struct scope *scope, struct type *type); -static void _type_str(FILE *f, struct type *type) +static int _type_str(FILE *f, struct state *state, struct scope *scope, struct type *type) { assert(type); switch (type->k) { @@ -201,14 +213,40 @@ static void _type_str(FILE *f, struct type *type) fprintf(f, "fwd_closure_t"); break; + case TYPE_PURE_CLOSURE: + fprintf(f, "fwd_closure_t"); + break; + + case TYPE_ID: { + struct ast *def = file_scope_find_type(scope, type_str(type)); + + if (!ast_flags(def, AST_FLAG_LOWERED)) + if (lower_type_def(state, def)) + return -1; + + char *name = mangle2(def); + fprintf(f, "%s", name); + free(name); + break; + } + + case TYPE_PTR: { + char *rest = lower_type_str(state, scope, tptr_base(type)); + fprintf(f, "%s*", rest); + free(rest); + break; + } + default: internal_error("unhandled type lowering for %s", type_str(type)); abort(); break; } + + return 0; } -static char *lower_type_str(struct type *type) +static char *lower_type_str(struct state *state, struct scope *scope, struct type *type) { assert(type); @@ -216,9 +254,15 @@ static char *lower_type_str(struct type *type) FILE *f = open_memstream(&type_buf, &type_len); assert(f); - _type_str(f, type); + int r = _type_str(f, state, scope, type); fclose(f); assert(type_buf); + + if (r) { + free(type_buf); + return NULL; + } + return type_buf; } @@ -243,7 +287,7 @@ static int lower_closure_call(struct state *state, struct ast *call, struct ast foreach_node(a, call_args(call)) { returning |= a->k == AST_CLOSURE; - char *type = lower_type_str(a->t); + char *type = lower_type_str(state, a->scope, a->t); fprintf(ctx, " %s a%zu;\n", type, idx); free(type); @@ -269,7 +313,7 @@ out: q, q); } else { - fprintf(state->code, "stack = %s_call(stack, %s_ctx);\n", + fprintf(state->code, "stack = %s(stack, %s_ctx);\n", q, q); } @@ -340,10 +384,26 @@ static int lower_comparison(struct state *state, struct ast *expr) fprintf(state->code, " <= "); break; + case AST_GE: + fprintf(state->code, " >= "); + break; + case AST_LT: fprintf(state->code, " < "); break; + case AST_GT: + fprintf(state->code, " > "); + break; + + case AST_EQ: + fprintf(state->code, " == "); + break; + + case AST_NE: + fprintf(state->code, " != "); + break; + default: internal_error("unhandled lowering for comparison %s", ast_str(expr->k)); abort(); @@ -366,8 +426,12 @@ static int lower_binop(struct state *state, struct ast *expr) fprintf(state->code, " + "); break; + case AST_MUL: + fprintf(state->code, " * "); + break; + default: - internal_error("unhandled comparison %s", ast_str(expr->k)); + internal_error("unhandled binop %s", ast_str(expr->k)); abort(); } @@ -406,6 +470,41 @@ static int lower_expr(struct state *state, struct ast *expr) fprintf(state->code, "%lld", (long long)int_val(expr)); break; } + + case AST_NIL: { + fprintf(state->code, "NULL"); + break; + } + + case AST_CONSTRUCT: { + char *name = lower_type_str(state, expr->scope, expr->t); + fprintf(state->code, "(%s){", name); + free(name); + + foreach_node(n, construct_members(expr)) { + fprintf(state->code, ".%s = ", construction_id(n)); + + if (lower_expr(state, construction_expr(n))) + return -1; + + fprintf(state->code, ", "); + } + fprintf(state->code, "}"); + break; + } + + case AST_AS: { + char *name = lower_type_str(state, expr->scope, expr->t); + fprintf(state->code, "(%s)(", name); + free(name); + + if (lower_expr(state, as_expr(expr))) + return -1; + + fprintf(state->code, ")"); + break; + } + default: internal_error("unhandled expr lowering: %s", ast_str(expr->k)); abort(); @@ -445,7 +544,7 @@ static int lower_call(struct state *state, struct ast *call, bool last) foreach_node(a, call_args(call)) { returning |= a->k == AST_CLOSURE; - char *type = lower_type_str(a->t); + char *type = lower_type_str(state, a->scope, a->t); fprintf(ctx, " %s a%zu;\n", type, idx); free(type); @@ -508,11 +607,72 @@ static int lower_if(struct state *state, struct ast *stmt, bool last) return lower_block(state, if_else(stmt), last); } +static int lower_explode(struct state *state, struct ast *stmt, bool last) +{ + /* not significant to us */ + (void)last; + + int u = uniq(state); + struct ast *expr = explode_expr(stmt); + + char *type = lower_type_str(state, expr->scope, expr->t); + + indent(state); + fprintf(state->code, "%s explode_%d = ", type, u); + free(type); + + if (lower_expr(state, expr)) + return -1; + + fprintf(state->code, ";\n"); + + foreach_node(node, explode_deconstruct(stmt)) { + struct ast *var = deconstruction_var(node); + + char *type = lower_type_str(state, var->scope, var->t); + char *name = mangle2(var); + + indent(state); + fprintf(state->code, "%s %s = explode_%d.%s;\n", + type, name, u, deconstruction_id(node)); + + free(type); + free(name); + } + + return 0; +} + +static int lower_let(struct state *state, struct ast *stmt, bool last) +{ + /* not relevant */ + (void)last; + + struct ast *var = let_var(stmt); + char *type = lower_type_str(state, var->scope, var->t); + char *name = mangle2(var); + + indent(state); + fprintf(state->code, "%s %s = ", type, name); + + free(type); + free(name); + + if (lower_expr(state, let_expr(stmt))) + return -1; + + fprintf(state->code, ";\n"); + return 0; +} + static int lower_stmt(struct state *state, struct ast *stmt, bool last) { switch (stmt->k) { case AST_CALL: return lower_call(state, stmt, last); case AST_IF: return lower_if(state, stmt, last); + case AST_EXPLODE: return lower_explode(state, stmt, last); + case AST_LET: return lower_let(state, stmt, last); + case AST_EMPTY: break; default: internal_error("unhandled statement kind %s", ast_str(stmt->k)); abort(); @@ -548,7 +708,7 @@ static int lower_block(struct state *state, struct ast *block, bool last) static int lower_param(struct state *state, struct ast *param) { - char *type = lower_type_str(param->t); + char *type = lower_type_str(state, param->scope, param->t); char *name = mangle2(param); fprintf(state->ctx, " %s %s;\n", type, name); @@ -644,7 +804,7 @@ static int lower_extern_proc(struct state *state, struct ast *proc) idx + 1, fwd_typeid(p->t), fwd_typeparam(p->t), idx); - char *type_str = lower_type_str(p->t); + char *type_str = lower_type_str(state, p->scope, p->t); fprintf(new_state.ctx, " %s a%zu;\n", type_str, idx); free(type_str); idx++; @@ -679,7 +839,7 @@ static int lower_extern_proc(struct state *state, struct ast *proc) static int lower_param_copy(struct state *state, struct ast *param, FILE *f, size_t idx) { - char *type = lower_type_str(param->t); + char *type = lower_type_str(state, param->scope, param->t); fprintf(f, " %s a%zu;\n", type, idx); free(type); @@ -716,6 +876,45 @@ static int lower_param_copies(struct state *state, struct ast *params) return 0; } +static int lower_type_def(struct state *state, struct ast *type) +{ + assert(type->k == AST_STRUCT_DEF); + if (type_lowered(type)) + return 0; + + ast_flags(type, AST_FLAG_LOWERED); + + char *decl_buf = NULL; size_t decl_len = 0; + FILE *decl = open_memstream(&decl_buf, &decl_len); + assert(decl); + + char *name = mangle2(type); + fprintf(decl, "struct %s;\n", name); + fclose(decl); + + char *defn_buf = NULL; size_t defn_len = 0; + FILE *defn = open_memstream(&defn_buf, &defn_len); + assert(defn); + + fprintf(defn, "typedef struct %s {\n", name); + + foreach_node(n, struct_body(type)) { + assert(n->k == AST_VAR_DEF); + char *t = lower_type_str(state, n->scope, var_type(n)); + fprintf(defn, "\t%s %s;\n", t, var_id(n)); + free(t); + } + + fprintf(defn, "} %s;\n", name); + + fclose(defn); + free(name); + + add_type(state, decl_buf); + add_type(state, defn_buf); + return 0; +} + static int lower_proc(struct state *state, struct ast *proc) { if (proc_lowered(state, proc)) @@ -138,9 +138,9 @@ static void forget_references(struct state *state) state->referenced = moved_create(); } -static int mvcheck(struct state *state, struct ast *node); -static int mvcheck_list(struct state *state, struct ast *nodes); -static int mvcheck_statements(struct state *state, struct ast *nodes); +static int mvcheck_expr(struct state *state, struct ast *node); +static int mvcheck_expr_list(struct state *state, struct ast *nodes); +static int mvcheck_statements(struct state *state, struct ast *nodes, bool last); static int refcheck_id(struct state *state, struct ast *node) { @@ -192,20 +192,7 @@ static int mvcheck_deref(struct state *state, struct ast *node) { /** @todo good enough for now but probably won't hold when we start * doing element lookups in structs etc. */ - return mvcheck(state, deref_base(node)); -} - -static int mvcheck_proc(struct state *state, struct ast *node) -{ - /* extern, can't really do anything so just say it's fine */ - if (!proc_body(node)) - return 0; - - struct state new_state = create_state(state); - /* we don't need to merge things into the parent state */ - int ret = mvcheck(&new_state, proc_body(node)); - destroy_state(&new_state); - return ret; + return mvcheck_expr(state, deref_base(node)); } static int total_check_single(struct state *state, struct scope *scope) @@ -245,16 +232,20 @@ static int total_check_proc(struct state *state, struct scope *scope) return ret | total_check_proc(state, scope->parent); } -static int mvcheck_block(struct state *state, struct ast *node) +static int mvcheck_block(struct state *state, struct ast *node, bool last) { + assert(node->k == AST_BLOCK); struct state new_state = create_state(state); - int ret = mvcheck_statements(&new_state, block_body(node)); + int ret = mvcheck_statements(&new_state, block_body(node), last); if (ret) { destroy_state(&new_state); return ret; } - if (total_check_single(&new_state, node->scope)) + if (last && total_check_proc(&new_state, node->scope)) + semantic_info(node->scope, node, "at end of block"); + + else if (!last && 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 */ @@ -263,9 +254,20 @@ static int mvcheck_block(struct state *state, struct ast *node) return 0; } -static int mvcheck_call(struct state *state, struct ast *node) +static int mvcheck_closure(struct state *state, struct ast *node, bool last) +{ + struct state new_state = create_state(state); + new_state.pure = ast_flags(node, AST_FLAG_NOMOVES); + + int ret = mvcheck_block(&new_state, closure_body(node), last); + push_up(&new_state); + destroy_state(&new_state); + return ret; +} + +static int mvcheck_call(struct state *state, struct ast *node, bool last) { - if (mvcheck(state, call_expr(node))) + if (mvcheck_expr(state, call_expr(node))) return -1; struct ast *args = call_args(node); @@ -277,10 +279,35 @@ static int mvcheck_call(struct state *state, struct ast *node) if (arg->k == AST_CLOSURE) continue; - if (mvcheck(state, arg)) + if (mvcheck_expr(state, arg)) return -1; } + /* count how many closure groups call has. If the call is an exit point + * (last == true), then a single closure group must also be an exit + * point */ + size_t groups = 0; + foreach_node(arg, call_args(node)) { + if (arg->k != AST_CLOSURE) + continue; + + groups++; + struct ast *next = arg->n; + while (next && next->t->group == arg->t->group) + next = next->n; + + if (!next) + break; + + arg = next; + } + + if (!last && (groups > 0)) { + semantic_error(node->scope, node, + "calls with closures must currently be exit points, sorry!"); + return -1; + } + /* check into closures */ int ret = 0; struct state buffer_state = create_state(state); @@ -290,7 +317,7 @@ static int mvcheck_call(struct state *state, struct ast *node) continue; struct state arg_state = create_state(state); - ret = mvcheck(&arg_state, arg); + ret = mvcheck_closure(&arg_state, arg, last && (groups == 1)); merge(&group_state, &arg_state); destroy_state(&arg_state); @@ -316,21 +343,6 @@ static int mvcheck_call(struct state *state, struct ast *node) return ret; } -static int mvcheck_closure(struct state *state, struct ast *node) -{ - struct state new_state = create_state(state); - new_state.pure = ast_flags(node, AST_FLAG_NOMOVES); - - int ret = mvcheck(&new_state, closure_body(node)); - push_up(&new_state); - destroy_state(&new_state); - - if (total_check_single(state, node->scope)) - semantic_info(node->scope, node, "in closure"); - - return ret; -} - static void opt_group_left(struct state *state, struct ast *def, struct ast *node) { @@ -396,29 +408,36 @@ static int mvcheck_id(struct state *state, struct ast *node) static int mvcheck_let(struct state *state, struct ast *node) { - return mvcheck(state, let_expr(node)); + return mvcheck_expr(state, let_expr(node)); } static int mvcheck_init(struct state *state, struct ast *node) { - return mvcheck_list(state, init_body(node)); + return mvcheck_expr_list(state, init_body(node)); } -static int mvcheck_if(struct state *state, struct ast *node) +static int mvcheck_if(struct state *state, struct ast *node, bool last) { - if (mvcheck(state, if_cond(node))) + if (!last) { + semantic_error(node->scope, node, + "`if` statements must currently be exit points, sorry!"); return -1; + } + + assert(if_else(node)); + + /* don't check cond since it can't take ownership of anything */ struct state body_state = create_state(state); struct state else_state = create_state(state); - if (mvcheck(&body_state, if_body(node))) { + if (mvcheck_block(&body_state, if_body(node), last)) { destroy_state(&body_state); destroy_state(&else_state); return -1; } - if (mvcheck(&else_state, if_else(node))) { + if (mvcheck_block(&else_state, if_else(node), last)) { destroy_state(&body_state); destroy_state(&else_state); return -1; @@ -432,82 +451,124 @@ static int mvcheck_if(struct state *state, struct ast *node) return 0; } -static int mvcheck_unop(struct state *state, struct ast *node) +static int mvcheck_expr_list(struct state *state, struct ast *nodes) { - return mvcheck(state, unop_expr(node)); + foreach_node(node, nodes) { + if (mvcheck_expr(state, node)) + return -1; + } + + return 0; } -static int mvcheck_binop(struct state *state, struct ast *node) +static int mvcheck_construct(struct state *state, struct ast *node) { - if (mvcheck(state, binop_left(node))) - return -1; + foreach_node(expr, construct_members(node)) { + if (mvcheck_expr(state, expr)) + return -1; + } - return mvcheck(state, binop_right(node)); + return 0; } -static int mvcheck_comparison(struct state *state, struct ast *node) +static int mvcheck_construction(struct state *state, struct ast *node) { - if (mvcheck(state, comparison_left(node))) - return -1; + return mvcheck_expr(state, construction_expr(node)); +} - return mvcheck(state, comparison_right(node)); +static int mvcheck_as(struct state *state, struct ast *as) +{ + return mvcheck_expr(state, as_expr(as)); } -static int mvcheck_list(struct state *state, struct ast *nodes) +static int mvcheck_forget(struct state *state, struct ast *node) { - foreach_node(node, nodes) { - if (mvcheck(state, node)) - return -1; - } + struct ast *def = file_scope_find_symbol(node->scope, forget_id(node)); + assert(def); + /* act as if a move has happened */ + insert_move(state, def, node); return 0; } -static int mvcheck_statements(struct state *state, struct ast *nodes) +static int mvcheck_explode(struct state *state, struct ast *node) { - foreach_node(node, nodes) { - struct state new_state = create_state(state); - if (mvcheck(&new_state, node)) { - destroy_state(&new_state); - return -1; - } + return mvcheck_expr(state, explode_expr(node)); +} - forget_references(&new_state); - push_up(&new_state); - destroy_state(&new_state); +static int mvcheck_write(struct state *state, struct ast *node) +{ + return mvcheck_expr(state, write_src(node)); +} + +static int mvcheck_nil_check(struct state *state, struct ast *node, bool last) +{ + if (!last) { + /** @todo would this be an internal error? */ + semantic_error(node->scope, node, + "`nil check` must be exit point, sorry"); + return -1; + } + + assert(nil_check_rest(node)); + + struct state body_state = create_state(state); + struct state rest_state = create_state(state); + + if (mvcheck_block(&body_state, nil_check_body(node), last)) { + destroy_state(&body_state); + destroy_state(&rest_state); + return -1; + } + + if (mvcheck_block(&rest_state, nil_check_rest(node), last)) { + destroy_state(&body_state); + destroy_state(&rest_state); + return -1; } + push_up(&body_state); + push_up(&rest_state); + + destroy_state(&body_state); + destroy_state(&rest_state); return 0; } -static int mvcheck(struct state *state, struct ast *node) +static int mvcheck_expr(struct state *state, struct ast *node) { + /* unary, binary and comparison operators (a kind of binary operator, + * fair enough) must operate on primitive types, and as such don't need + * to be move checked as none of them can change ownership or a value */ if (is_unop(node)) - return mvcheck_unop(state, node); + return 0; if (is_binop(node)) - return mvcheck_binop(state, node); + return 0; if (is_comparison(node)) - return mvcheck_comparison(state, node); + return 0; switch (node->k) { - case AST_PROC_DEF: return mvcheck_proc (state, 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_LET: return mvcheck_let (state, node); case AST_ID: return mvcheck_id (state, node); case AST_INIT: return mvcheck_init (state, node); - case AST_IF: return mvcheck_if (state, node); case AST_REF: return mvcheck_ref (state, node); case AST_DEREF: return mvcheck_deref (state, node); + case AST_CONSTRUCT: return mvcheck_construct (state, node); + case AST_AS: return mvcheck_as (state, node); + case AST_FORGET: return mvcheck_forget (state, node); + case AST_EXPLODE: return mvcheck_explode (state, node); + case AST_WRITE: return mvcheck_write (state, node); + case AST_CONSTRUCTION: return mvcheck_construction(state, node); + case AST_SIZEOF: + case AST_STRUCT_DEF: case AST_EMPTY: case AST_IMPORT: case AST_CONST_INT: case AST_CONST_STR: case AST_CONST_FLOAT: case AST_CONST_CHAR: + case AST_NIL: return 0; default: break; } @@ -516,11 +577,72 @@ static int mvcheck(struct state *state, struct ast *node) return -1; } +static int mvcheck_statement(struct state *state, struct ast *node, bool last) +{ + switch (node->k) { + case AST_CALL: return mvcheck_call (state, node, last); + case AST_IF: return mvcheck_if (state, node, last); + case AST_NIL_CHECK: return mvcheck_nil_check(state, node, last); + case AST_EXPLODE: return mvcheck_explode (state, node); + case AST_LET: return mvcheck_let (state, node); + case AST_WRITE: return mvcheck_write (state, node); + case AST_FORGET: return mvcheck_forget (state, node); + case AST_EMPTY: return 0; + default: break; + } + + internal_error("unhandled move statement: %s", ast_str(node->k)); + return -1; +} + +static int mvcheck_statements(struct state *state, struct ast *nodes, bool last) +{ + foreach_node(node, nodes) { + struct state new_state = create_state(state); + if (mvcheck_statement(&new_state, node, last && !node->n)) { + destroy_state(&new_state); + return -1; + } + + forget_references(&new_state); + push_up(&new_state); + destroy_state(&new_state); + } + + return 0; +} + +static int mvcheck_proc(struct state *state, struct ast *node) +{ + /* extern, can't really do anything so just say it's fine */ + if (!proc_body(node)) + return 0; + + struct state new_state = create_state(state); + /* we don't need to merge things into the parent state */ + int ret = mvcheck_block(&new_state, proc_body(node), true); + destroy_state(&new_state); + return ret; +} + +static int mvcheck_top(struct state *state, struct ast *node) +{ + switch (node->k) { + case AST_PROC_DEF: return mvcheck_proc(state, node); + case AST_IMPORT: return 0; + case AST_STRUCT_DEF: return 0; + default: break; + } + + internal_error("missing top move check for %s", ast_str(node->k)); + return -1; +} + int mvcheck_root(struct ast *root) { foreach_node(node, root) { struct state state = create_state(NULL); - if (mvcheck(&state, node)) + if (mvcheck_top(&state, node)) return -1; destroy_state(&state); diff --git a/src/parser.y b/src/parser.y index 6ec2eb2..dad3210 100644 --- a/src/parser.y +++ b/src/parser.y @@ -36,7 +36,6 @@ %token <dbl> FLOAT %token <str> STRING %token <str> ID -%token <str> APPLY %token QUESTION "?" %token SQUOTE "'" @@ -70,6 +69,7 @@ %token RBRACE "}" %token LBRACKET "[" %token RBRACKET "]" +%token AS "as" %token IF "if" %token ELSE "else" %token NIL "nil" @@ -79,7 +79,6 @@ %token IMPORT "import" %token ERROR "error" %token DOT "." -%token SCOPE "::" %token FATARROW "=>" %right "[" "]" @@ -95,24 +94,31 @@ %left "*" "/" "%" %left "as" "sizeof" %right "'" "!" "~" +%right ".." %left "." "=>" "(" ")" -%left "::" -%nterm <node> top unit proc proc_decl call closure var expr statement body -%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 unop binop construct import +%nterm <node> top unit proc proc_decl call closure expr statement body +%nterm <node> exprs statements closures trailing_closure +%nterm <node> opt_exprs opt_statements opt_trailing_closure +%nterm <node> rev_exprs rev_closures rev_statements +%nterm <node> let put if nil unop binop construct import + +%nterm <node> opt_vars vars rev_vars var +%nterm <node> opt_params params rev_params param %nterm <node> struct struct_cont trait %nterm <node> type_param type_params opt_type_params %nterm <node> behaviour behaviours opt_behaviours %nterm <node> member members opt_members +%nterm <node> opt_elements elements rev_elements element +%nterm <node> instance template supertemplate +%nterm <node> opt_constructions constructions construction +%nterm <node> opt_deconstructions deconstructions deconstruction +%nterm <node> id impl forget +%nterm <node> explode %nterm <type> type rev_types types opt_types - - %{ /** Modifies the signature of yylex to fit our parser better. */ @@ -189,12 +195,29 @@ static char *strip(const char *s); %start input; %% -var +param : type ID { $$ = gen_var($2, $1, src_loc(@$)); } +rev_params + : rev_params "," param { $$ = $3; $$->n = $1; } + | rev_params "|" param { $$ = $3; $$->n = $1; opt_group($1, $3); } + | param + +params + : rev_params { $$ = reverse_ast_list($1); } + | rev_params "," { $$ = reverse_ast_list($1); } + +opt_params + : params + | {$$ = NULL;} + + +var + : param + | ID { $$ = gen_var($1, NULL, src_loc(@$)); } + rev_vars : rev_vars "," var { $$ = $3; $$->n = $1; } - | rev_vars "|" var { $$ = $3; $$->n = $1; opt_group($1, $3); } | var vars @@ -206,13 +229,13 @@ opt_vars | {$$ = NULL;} proc - : ID "(" opt_vars ")" body { - $$ = gen_proc($[ID], $[opt_vars], NULL, $[body], src_loc(@$)); + : ID "(" opt_params ")" body { + $$ = gen_proc($[ID], $[opt_params], NULL, $[body], src_loc(@$)); } proc_decl - : ID "(" opt_vars ")" ";" { - $$ = gen_proc($[ID], $[opt_vars], NULL, NULL, src_loc(@$)); + : ID "(" opt_params ")" ";" { + $$ = gen_proc($[ID], $[opt_params], NULL, NULL, src_loc(@$)); } binop @@ -229,16 +252,15 @@ binop | expr ">=" expr { $$ = gen_comparison(AST_GE, $1, $3, src_loc(@$)); } | expr "!=" expr { $$ = gen_comparison(AST_NE, $1, $3, src_loc(@$)); } | expr "==" expr { $$ = gen_comparison(AST_EQ, $1, $3, src_loc(@$)); } + | id "~" id { $$ = gen_paste($1, $3, src_loc(@$)); } unop : "-" expr { $$ = gen_unop(AST_NEG, $2, src_loc(@$)); } - | "!" expr { $$ = gen_unop(AST_LNOT, $2, src_loc(@$)); } + | "!" expr { $$ = gen_unop(AST_LNOT, $2, src_loc(@$)); } + | "~" expr { $$ = gen_unop(AST_BNEG, $2, src_loc(@$)); } type - : ID { $$ = tgen_id($[ID], src_loc(@$)); } - | APPLY "[" opt_types "]" { - $$ = tgen_construct($[APPLY], $[opt_types], src_loc(@$)); - } + : ID { $$ = tgen_id($1, src_loc(@$)); } | "(" opt_types ")" { $$ = tgen_closure($2, src_loc(@$)); } | "&&" type { if ($2->k == TYPE_CLOSURE) { @@ -276,26 +298,54 @@ opt_types : types | { $$ = NULL; } -construct - : APPLY "{" opt_exprs "}" { - $$ = gen_init($[APPLY], NULL, $[opt_exprs], src_loc(@$)); +construction + : expr "=>" ID { + $$ = gen_construction($3, $1, src_loc(@$)); } - | APPLY "[" opt_types "]" "{" opt_exprs "}" { - $$ = gen_init($[APPLY], $[opt_types], $[opt_exprs], src_loc(@$)); + +constructions + : constructions "," construction { $$ = $3; $$->n = $1; } + | construction + +opt_constructions + : constructions + | { $$ = NULL; } + +deconstruction + : ID "=>" var { + $$ = gen_deconstruction($1, $3, src_loc(@$)); } +deconstructions + : deconstructions "," deconstruction { $$ = $3; $$->n = $1; } + | deconstruction + +opt_deconstructions + : deconstructions + | { $$ = NULL; } + +construct + : "[" opt_constructions "]" ID { + $$ = gen_construct($4, $2, src_loc(@$)); + } + +id + : ID { $$ = gen_id($1, src_loc(@$)); } + expr - : expr "." ID { $$ = gen_dot($3, $1, src_loc(@$)); } + : "sizeof" "(" type ")" { $$ = gen_sizeof($3, src_loc(@$)); } | STRING { $$ = gen_const_str(strip($1), src_loc(@$)); } | FLOAT { $$ = gen_const_float($1, src_loc(@$)); } | BOOL { $$ = gen_const_bool($1, src_loc(@$)); } | CHAR { $$ = gen_const_char($1, src_loc(@$)); } | INT { $$ = gen_const_int($1, src_loc(@$)); } - | ID { $$ = gen_id($1, src_loc(@$)); } + | "*" { $$ = gen_nil(src_loc(@$)); } | "(" expr ")" { $$ = $2; } | expr "&" { $$ = gen_ref($1, src_loc(@$)); } | expr "*" { $$ = gen_deref($1, src_loc(@$)); } + | expr "as" type { $$ = gen_as($1, $3, src_loc(@$)); } | construct + | id | binop | unop @@ -345,7 +395,7 @@ closures } call - : expr "(" opt_exprs ")" ";" { + : expr "(" opt_exprs ")" { $$ = gen_call($1, $[opt_exprs], src_loc(@$)); } | expr "(" opt_exprs ")" "=>" opt_vars ";" { @@ -360,22 +410,40 @@ call $$ = gen_call($[expr], $[opt_exprs], src_loc(@$)); } +put + : put "*" { $$ = gen_put($1, src_loc(@$)); } + | id "*" { $$ = gen_put($1, src_loc(@$)); } + let : expr "=>" var ";" { $$ = gen_let($3, $1, src_loc(@$)); } + | expr "=>" put ";" { $$ = gen_write($3, $1, src_loc(@$)); } + +explode + : expr "=>" "[" opt_deconstructions "]" { + $$ = gen_explode($4, $1, src_loc(@$)); + } if - : "if" expr body { $$ = gen_if($2, $3, NULL, src_loc(@$)); } - | "if" expr body "else" body { $$ = gen_if($2, $3, $5, src_loc(@$)); } + : "if" expr body "else" body { $$ = gen_if($2, $3, $5, src_loc(@$)); } | "if" expr body "else" if { $$ = gen_if($2, $3, $5, src_loc(@$)); } nil - : "nil" ID body "=>" var { - $$ = gen_nil($2, $3, $5, src_loc(@$)); + : "nil" expr body "=>" var ";" { + /** @todo should nil check define new scope for created + * variable? */ + $$ = gen_nil_check($2, $3, $5, src_loc(@$)); + } + +forget + : "nil" ID ";" { + $$ = gen_forget($2, src_loc(@$)); } statement : call + | forget | body + | explode | let | nil | if @@ -398,9 +466,9 @@ body } behaviour - : APPLY ";" { $$ = gen_trait_apply($1, src_loc(@$)); } - | proc_decl ";" + : proc_decl | proc + | impl behaviours : behaviours behaviour { $$ = $1; $1->n = $2; } @@ -410,16 +478,18 @@ opt_behaviours : behaviours | { $$ = NULL; } +impl + : ID "!" { $$ = gen_implements($1, src_loc(@$)); } + member - : var ";" - | behaviour + : param members - : members member { $$ = $1; $1->n = $2; } + : members ";" member { $$ = $3; $$->n = $1; } | member opt_members - : members + : members ";" | { $$ = NULL; } type_param @@ -438,19 +508,53 @@ opt_type_params : type_params | { $$ = NULL; } +element + : proc + | struct + | impl + | var ";" + +rev_elements + : rev_elements element { $$ = $2; $$->n = $1; } + | element + +elements + : rev_elements { $$ = reverse_ast_list($1); } + +opt_elements + : elements + | { $$ = NULL; } + +template + : ID "[" opt_type_params "]" "(" opt_vars ")" "{" opt_elements "}" { + $$ = gen_template($1, $3, $6, $9, src_loc(@$)); + } + +supertemplate + : ID "[" opt_type_params "]" "(" opt_vars ")" "=" + id "[" opt_types "]" "(" opt_exprs ")" "{" opt_elements "}" { + struct ast *pseudoinst = gen_instance(NULL, $9, $11, $14, src_loc(@$)); + $$ = gen_supertemplate($1, $3, $6, pseudoinst, $17, src_loc(@$)); + } + struct - : ID "[" opt_type_params "]" "{" opt_members "}" { - $$ = gen_struct($1, $3, $6, src_loc(@$)); + : ID "{" opt_members "}" { + $$ = gen_struct($1, $3, src_loc(@$)); } struct_cont - : "continue" ID "[" opt_type_params "]" "{" opt_behaviours "}" { - $$ = gen_struct_cont($2, $4, $7, src_loc(@$)); + : "continue" ID "{" opt_behaviours "}" { + $$ = gen_instance_cont($2, $4, src_loc(@$)); } trait - : ID "{" opt_behaviours "}" { - $$ = gen_trait($1, $3, src_loc(@$)); + : ID "=" "{" opt_behaviours "}" { + $$ = gen_trait($1, $4, src_loc(@$)); + } + +instance + : ID "=" id "[" opt_types "]" "(" opt_exprs ")" { + $$ = gen_instance($1, $3, $5, $8, src_loc(@$)); } import @@ -464,11 +568,17 @@ top | struct_cont | import | trait - | "pub" proc { $$ = $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); } - | "pub" trait { $$ = $2; ast_set_flags($$, AST_FLAG_PUBLIC); } + | instance + | template + | supertemplate + | "pub" proc { $$ = $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); } + | "pub" trait { $$ = $2; ast_set_flags($$, AST_FLAG_PUBLIC); } + | "pub" instance { $$ = $2; ast_set_flags($$, AST_FLAG_PUBLIC); } + | "pub" template { $$ = $2; ast_set_flags($$, AST_FLAG_PUBLIC); } + | "pub" supertemplate { $$ = $2; ast_set_flags($$, AST_FLAG_PUBLIC); } | error { $$ = gen_empty(src_loc(@$)); parser->failed = true; diff --git a/src/rewrite.c b/src/rewrite.c new file mode 100644 index 0000000..e3cea01 --- /dev/null +++ b/src/rewrite.c @@ -0,0 +1,96 @@ +#include <fwd/rewrite.h> +#include <stddef.h> +#include <stdlib.h> + +struct type_helper { + char *orig; + char *new; +}; + +/* types are simple enough (for now) that this works fine */ +static int rewrite_type_ids(struct type *type, char *orig, char *new) +{ + if (type->k == TYPE_ID && strcmp(type->id, orig) == 0) { + char *r = strdup(new); + free(type->id); + type->id = r; + } + + if (type->t0 && rewrite_type_ids(type->t0, orig, new)) + return -1; + + if (type->n && rewrite_type_ids(type->n, orig, new)) + return -1; + + return 0; +} + +static int rewrite_type_visit(struct ast *node, struct type_helper *helper) +{ + if (node->t2) + return rewrite_type_ids(node->t2, helper->orig, helper->new); + + return 0; +} + +int rewrite_types(struct ast *node, char *orig, char *new) +{ + struct type_helper helper = {.orig = orig, .new = new}; + return ast_visit((ast_callback_t)rewrite_type_visit, NULL, node, &helper); +} + +/* not the fastest thing in the world but should work well enough for now */ +static char *rewrite_hole(char *old, char *new) +{ + /* skip "<>" */ + size_t on = strlen(old) - 2; + size_t nn = strlen(new); + + /* +1 for null terminator */ + char *r = malloc(on + nn + 1); + + memcpy(r, new, nn); + + /* +2 to skip "<>", +1 for null terminator */ + memcpy(r + nn, old + 2, on + 1); + return r; +} + +static int rewrite_type_holes(struct type *type, char *new) +{ + if (type->id && strncmp(type->id, "<>", 2) == 0) { + char *r = rewrite_hole(type->id, new); + free(type->id); + type->id = r; + } + + if (type->t0 && rewrite_type_holes(type->t0, new)) + return -1; + + if (type->n && rewrite_type_holes(type->n, new)) + return -1; + + return 0; +} + +static int rewrite_holes_visit(struct ast *node, char *new) +{ + if (node->k == AST_CONST_STR) + return 0; + + if (node->s && strncmp(node->s, "<>", 2) == 0) { + char *r = rewrite_hole(node->s, new); + free(node->s); + node->s = r; + } + + if (node->t2 && rewrite_type_holes(node->t2, new)) + return -1; + + return 0; +} + +int rewrite_holes(struct ast *node, char *new) +{ + return ast_visit((ast_callback_t)rewrite_holes_visit, NULL, node, new); +} diff --git a/src/scope.c b/src/scope.c index 485b3a6..e49b828 100644 --- a/src/scope.c +++ b/src/scope.c @@ -30,8 +30,9 @@ struct scope *create_scope() scope->number = counter++; scope->symbols = visible_create(16); /* arbitrary number */ - scope->traits = visible_create(1); - scope->types = visible_create(1); + scope->templates = visible_create(0); + scope->traits = visible_create(0); + scope->types = visible_create(0); scope->mods = mod_vec_create(0); return scope; } @@ -47,12 +48,13 @@ void destroy_scope(struct scope *scope) } foreach(mod_vec, m, &scope->mods) { - dlclose(m); + dlclose(*m); } mod_vec_destroy(&scope->mods); visible_destroy(&scope->symbols); + visible_destroy(&scope->templates); visible_destroy(&scope->traits); visible_destroy(&scope->types); @@ -108,7 +110,7 @@ struct ast *file_scope_find_symbol(struct scope *scope, char *id) int scope_add_type(struct scope *scope, struct ast *type) { assert(type->k == AST_STRUCT_DEF); - struct ast **exists = visible_insert(&scope->symbols, type->s, type); + struct ast **exists = visible_insert(&scope->types, type->s, type); if (!exists) { internal_error("failed inserting type into scope"); return -1; @@ -123,42 +125,6 @@ int scope_add_type(struct scope *scope, struct ast *type) return 0; } -int scope_add_chain(struct scope *scope, struct ast *type) -{ - assert(type->k == AST_STRUCT_CONT); - struct ast *exists = file_scope_find_type(scope, type->s); - if (!exists) { - semantic_error(scope, type, "no previous definition"); - return -1; - } - - assert(exists->k == AST_STRUCT_DEF || exists->k == AST_STRUCT_CONT); - if (ast_flags(exists, AST_FLAG_PUBLIC) || !ast_flags(type, AST_FLAG_PUBLIC)) { - type->chain = exists; - struct ast **v = visible_insert(&scope->types, type->s, type); - if (!v) { - internal_error("failed inserting type into chain"); - return -1; - } - - /* overwrite root */ - if (*v != type) - *v = type; - - return 0; - } - - struct ast *next = exists->chain; - while (next->k == AST_STRUCT_CONT && !ast_flags(next, AST_FLAG_PUBLIC)) { - exists = next; - next = next->chain; - } - - exists->chain = type; - type->chain = exists; - return 0; -} - struct ast *scope_find_type(struct scope *scope, char *id) { struct ast **v = visible_find(&scope->types, id); @@ -183,7 +149,7 @@ struct ast *file_scope_find_type(struct scope *scope, char *id) int scope_add_trait(struct scope *scope, struct ast *trait) { assert(trait->k == AST_TRAIT_DEF); - struct ast **exists = visible_insert(&scope->symbols, trait->s, trait); + struct ast **exists = visible_insert(&scope->traits, trait->s, trait); if (!exists) { internal_error("failed inserting trait into scope"); return -1; @@ -197,6 +163,7 @@ int scope_add_trait(struct scope *scope, struct ast *trait) return 0; } + struct ast *scope_find_trait(struct scope *scope, char *id) { struct ast **v = visible_find(&scope->traits, id); @@ -218,6 +185,44 @@ struct ast *file_scope_find_trait(struct scope *scope, char *id) return file_scope_find_trait(scope->parent, id); } +int scope_add_template(struct scope *scope, struct ast *template) +{ + struct ast **exists = visible_insert(&scope->templates, template->s, template); + if (!exists) { + internal_error("failed inserting template into scope"); + return -1; + } + + if (*exists != template) { + semantic_error(scope, template, "template redefined"); + semantic_info(scope, *exists, "previously here"); + return -1; + } + + return 0; +} + +struct ast *scope_find_template(struct scope *scope, char *id) +{ + struct ast **v = visible_find(&scope->templates, id); + if (!v) + return NULL; + + return *v; +} + +struct ast *file_scope_find_template(struct scope *scope, char *id) +{ + if (!scope) + return NULL; + + struct ast *found = scope_find_template(scope, id); + if (found) + return found; + + return file_scope_find_template(scope->parent, id); +} + void scope_add_scope(struct scope *parent, struct scope *child) { assert(parent); |
