diff options
author | Leo Tenenbaum <pommicket@gmail.com> | 2019-09-27 15:55:53 -0400 |
---|---|---|
committer | Leo Tenenbaum <pommicket@gmail.com> | 2019-09-27 15:55:53 -0400 |
commit | d795c15a008a89aa2de86f83f7b01d996757fdbe (patch) | |
tree | 4619118772c89ee76081e1533b0bf6ec9d678e99 | |
parent | 5a1ed7f5eaa0e9b110c45d02a1dbc35084ba15ac (diff) |
bug fixes involving function params
-rw-r--r-- | main.c | 4 | ||||
-rw-r--r-- | out.c | 27 | ||||
-rw-r--r-- | out.h | 5 | ||||
-rw-r--r-- | parse.c | 40 | ||||
-rw-r--r-- | test.toc | 4 |
5 files changed, 34 insertions, 46 deletions
@@ -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" @@ -1,27 +0,0 @@ -#include "out.h" - -/* toc */ -int64_t N = 10; -#include <stdio.h> -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; -} @@ -1,5 +0,0 @@ -#include <stddef.h> -#include <stdint.h> -#include <string.h> -void foo(int64_t (*out__)[10][10]); -void main__(void); @@ -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; } @@ -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 + }; }; |