From d795c15a008a89aa2de86f83f7b01d996757fdbe Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Fri, 27 Sep 2019 15:55:53 -0400 Subject: bug fixes involving function params --- main.c | 4 +++- out.c | 27 --------------------------- out.h | 5 ----- parse.c | 40 ++++++++++++++++++++++++++++------------ test.toc | 4 +++- 5 files changed, 34 insertions(+), 46 deletions(-) delete mode 100644 out.c delete mode 100644 out.h diff --git a/main.c b/main.c index b4bf517..7e5f93f 100644 --- a/main.c +++ b/main.c @@ -1,7 +1,9 @@ /* TODO: -casting named return values +optional params +named params +evaluator (simplify compile time constant expressions) re-do cgen */ #include "toc.c" diff --git a/out.c b/out.c deleted file mode 100644 index de7dad8..0000000 --- a/out.c +++ /dev/null @@ -1,27 +0,0 @@ -#include "out.h" - -/* toc */ -int64_t N = 10; -#include -void foo(int64_t (*out__)[10][10]) { - int64_t av___0[10][10] = {{0}}; - int64_t (*x)[10] = av___0; - memcpy(*out__, x, 100 * sizeof(int64_t )); return; -} -void main__(void) { - int64_t av___1[10][10]; - foo(&av___1); - int64_t (*x)[10] = av___1; - -for (int i = 0; i < 10; i++) - for (int j = 0; j < 10; j++) - printf("%ld", x[i][j]); -puts(""); - ; - return; -} - -int main(void) { - main__(); - return 0; -} diff --git a/out.h b/out.h deleted file mode 100644 index 7b1844e..0000000 --- a/out.h +++ /dev/null @@ -1,5 +0,0 @@ -#include -#include -#include -void foo(int64_t (*out__)[10][10]); -void main__(void); diff --git a/parse.c b/parse.c index ff6c8d2..4e4f5e3 100644 --- a/parse.c +++ b/parse.c @@ -667,12 +667,26 @@ static bool parse_fn_expr(Parser *p, FnExpr *f) { t->token++; arr_create(&f->params, sizeof(Declaration)); bool ret = true; - while (!token_is_kw(t->token, KW_RPAREN)) { - Declaration *decl = arr_add(&f->params); - if (!parse_decl(p, decl, DECL_END_RPAREN_COMMA, PARSE_DECL_ALLOW_CONST_WITH_NO_EXPR)) - ret = false; + if (token_is_kw(t->token, KW_RPAREN)) { + t->token++; + } else { + while (t->token->kind != TOKEN_EOF && !token_is_kw(t->token - 1, KW_RPAREN)) { + Declaration *decl = arr_add(&f->params); + if (!parse_decl(p, decl, DECL_END_RPAREN_COMMA, PARSE_DECL_ALLOW_CONST_WITH_NO_EXPR)) { + ret = false; + /* skip to end of param list */ + while (t->token->kind != TOKEN_EOF && !token_is_kw(t->token, KW_RPAREN)) + t->token++; + break; + } + } } - t->token++; + + if (t->token->kind == TOKEN_EOF) { + tokr_err(t, "End of file encountered while parsing parameter list."); + return false; + } + if (token_is_kw(t->token, KW_LBRACE)) { /* void function */ f->ret_type.kind = TYPE_VOID; @@ -1291,13 +1305,17 @@ static bool parse_decl(Parser *p, Declaration *d, DeclEndType ends_with, uint16_ } d->type = type; } + char end_char = ends_with == DECL_END_SEMICOLON ? ';' : ')'; if (token_is_kw(t->token, KW_EQ)) { t->token++; d->flags |= DECL_FLAG_HAS_EXPR; - Token *end = expr_find_end(p, 0, NULL); - if (!end || !token_is_kw(end, KW_SEMICOLON)) { - tokr_err(t, "Expected ';' at end of declaration."); + uint16_t expr_flags = 0; + if (ends_with == DECL_END_RPAREN_COMMA) + expr_flags |= EXPR_CAN_END_WITH_COMMA; + Token *end = expr_find_end(p, expr_flags, NULL); + if (!end || !ends_decl(end, ends_with)) { + tokr_err(t, "Expected '%c' at end of declaration.", end_char); goto ret_false; } if (!parse_expr(p, &d->expr, end)) { @@ -1307,15 +1325,13 @@ static bool parse_decl(Parser *p, Declaration *d, DeclEndType ends_with, uint16_ if (ends_decl(t->token, ends_with)) { t->token++; } else { - tokr_err(t, "Expected '%c' at end of declaration.", - ends_with == DECL_END_SEMICOLON ? ';' : ')'); + tokr_err(t, "Expected '%c' at end of declaration.", end_char); goto ret_false; } } else if (ends_decl(t->token, ends_with)) { t->token++; } else { - tokr_err(t, "Expected '%c' or '=' at end of delaration.", - ends_with == DECL_END_SEMICOLON ? ';' : ')'); + tokr_err(t, "Expected '%c' or '=' at end of delaration.", end_char); goto ret_false; } diff --git a/test.toc b/test.toc index d041aa3..b876e96 100644 --- a/test.toc +++ b/test.toc @@ -3,6 +3,8 @@ main @= fn() { N, M @= 2; foo := 3; bar : float = foo as float; - + test @= fn(x : i64, y : i32, z,w: i64) i32 { + x + (y as i64) + z + w + x as i32 + }; }; -- cgit v1.2.3