From 07982dd332c739a8135305d22994caaa2fe340ca Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Sun, 12 Jul 2020 15:03:36 -0400 Subject: fixed local #foreign functions --- cgen.c | 19 ++++++++++++------- foreign64.c | 10 ++++++---- main.c | 2 +- parse.c | 2 +- test.toc | 10 ++++++---- 5 files changed, 26 insertions(+), 17 deletions(-) diff --git a/cgen.c b/cgen.c index 16b8e9a..07ed12e 100644 --- a/cgen.c +++ b/cgen.c @@ -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); diff --git a/main.c b/main.c index d63a6fb..d6fa57a 100644 --- a/main.c +++ b/main.c @@ -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 diff --git a/parse.c b/parse.c index 16ceb20..85479d1 100644 --- a/parse.c +++ b/parse.c @@ -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) { diff --git a/test.toc b/test.toc index 88e007c..9df26cf 100644 --- a/test.toc +++ b/test.toc @@ -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(); -- cgit v1.2.3