diff options
-rw-r--r-- | cgen.c | 19 | ||||
-rw-r--r-- | foreign64.c | 10 | ||||
-rw-r--r-- | main.c | 2 | ||||
-rw-r--r-- | parse.c | 2 | ||||
-rw-r--r-- | test.toc | 10 |
5 files changed, 26 insertions, 17 deletions
@@ -1415,15 +1415,20 @@ static void cgen_fn(CGenerator *g, FnExpr *f) { static void cgen_decl(CGenerator *g, Declaration *d) { if (!g->block || (g->block->kind == BLOCK_NMS)) return; // already dealt with - int has_expr = d->flags & DECL_HAS_EXPR; - if (cgen_fn_is_direct(g, d)) - return; // dealt with in the loop that defines all the functions in cgen_file - if (d->flags & DECL_FOUND_VAL) { + DeclFlags flags = d->flags; + DeclFlags has_expr = flags & DECL_HAS_EXPR, + is_const = flags & DECL_IS_CONST; + + if (flags & DECL_FOUND_VAL) { // declarations where we use a value for (int idx = 0, nidents = (int)arr_len(d->idents); idx < nidents; ++idx) { Identifier i = d->idents[idx]; if (ident_eq_str(i, "_")) continue; Type *type = decl_type_at_index(d, idx); + if (type->kind == TYPE_FN) { + // we don't need to generate this, because we can just use the function's name + continue; + } if (type_is_compileonly(&d->type)) { continue; } @@ -1431,10 +1436,10 @@ static void cgen_decl(CGenerator *g, Declaration *d) { if (has_expr) { cgen_val_pre(g, val, type); } - if (d->flags & DECL_IS_CONST) + if (is_const) cgen_write(g, "static "); cgen_type_pre(g, type); - cgen_write(g, " "); + cgen_write(g, is_const ? " const " : " "); cgen_ident(g, i); cgen_type_post(g, type); if (has_expr) { @@ -1467,7 +1472,7 @@ static void cgen_decl(CGenerator *g, Declaration *d) { } if (has_expr) { Expression *expr = &d->expr; - assert((g->block || g->fn) && !(d->flags & DECL_IS_CONST)); + assert((g->block || g->fn) && !is_const); if (expr->type.kind == TYPE_TUPLE) { cgen_set_tuple(g, NULL, d->idents, NULL, expr); } else { diff --git a/foreign64.c b/foreign64.c index d7a8a37..41d7a19 100644 --- a/foreign64.c +++ b/foreign64.c @@ -97,9 +97,8 @@ static Status foreign_call(ForeignFnManager *ffmgr, FnExpr *fn, Type *ret_type, possibly_static_assert(sizeof(double) == 8); // if either of these assertions fails, you'll need to use libffcall possibly_static_assert(sizeof(float) == 4); -#if 0 -#define FOREIGN_DEBUGGING 1 -#endif +//#define FOREIGN_DEBUGGING 1 + #if FOREIGN_DEBUGGING printf("Foreign call: %s(", fn->foreign.name); #endif @@ -122,6 +121,10 @@ static Status foreign_call(ForeignFnManager *ffmgr, FnExpr *fn, Type *ret_type, type += arg_types_stride; ++word; } +#if FOREIGN_DEBUGGING + printf(") => "); + fflush(stdout); +#endif int kind = 0; // 0=>integer, 1=>f32, 2=>f64 switch (ret_type->kind) { case TYPE_BUILTIN: @@ -194,7 +197,6 @@ static Status foreign_call(ForeignFnManager *ffmgr, FnExpr *fn, Type *ret_type, break; } #if FOREIGN_DEBUGGING - printf(") => "); fprint_val(stdout, *ret, ret_type); #if 1 printf(" (errno: %d)", errno); @@ -8,11 +8,11 @@ see development.md for development information @TODO: -fix #foreign not at global scope - right now the cgen'd definition doesn't use the proper type figure out how printf is gonna work improve type_to_str: Foo ::= struct(t::Type) {} type_to_str(Foo(int)) + - maybe make a bunch of functions for dynamic arrays of char? switch - #fallthrough enums @@ -2667,7 +2667,7 @@ static void print_block_location(Block *b) { static void fprint_fn_expr(FILE *out, FnExpr *f) { if (f->flags & FN_EXPR_FOREIGN) { - fprintf(out, "#foreign fn;"); + fprintf(out, "#foreign fn"); } else { fprintf(out, "fn ("); arr_foreach(f->params, Declaration, decl) { @@ -1,7 +1,9 @@ -#include "main.c", t; - main ::= fn() { - t.puts("hi"); + foo ::= #foreign("sleep", "libc.so.6") fn(#C unsigned) #C unsigned; + foo(1); + bar ::= foo; + bar(2); + x ::= 8; } -main(); +//main(); |