From ec8aa7ab9cb9986be40e86a0eca5eea4864d7634 Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Fri, 30 Aug 2019 23:50:34 -0400 Subject: added parsing of tuple types --- out.c | 7 ------- parse.c | 44 +++++++++++++++++++++++++++++++++++--------- test.toc | 5 +++-- 3 files changed, 38 insertions(+), 18 deletions(-) diff --git a/out.c b/out.c index 36d37d0..2e4e26a 100644 --- a/out.c +++ b/out.c @@ -2,10 +2,3 @@ /* toc */ void main__(void) { - int64_t salkdfj = 0; float something = 0; int64_t baz = 0; int64_t bar = 0; int64_t foo = 0; -} - -int main(void) { - main__(); - return 0; -} diff --git a/parse.c b/parse.c index 4bb3127..f12c683 100644 --- a/parse.c +++ b/parse.c @@ -414,7 +414,7 @@ static bool parse_type(Parser *p, Type *type) { type->builtin = kw_to_builtin_type(t->token->kw); if (type->builtin != BUILTIN_TYPE_COUNT) { t->token++; - return true; + break; } /* Not a builtin */ switch (t->token->kw) { @@ -444,17 +444,18 @@ static bool parse_type(Parser *p, Type *type) { } t->token++; /* move past ) */ Type *ret_type = type->fn.types.data; - /* if there's a symbol that isn't [, that can't be the start of a type */ + /* if there's a symbol that isn't [ or (, that can't be the start of a type */ if (t->token->kind == TOKEN_KW && t->token->kw <= KW_LAST_SYMBOL - && t->token->kw != KW_LSQUARE) { + && t->token->kw != KW_LSQUARE + && t->token->kw != KW_LPAREN) { ret_type->kind = TYPE_VOID; ret_type->flags = 0; } else { if (!parse_type(p, ret_type)) return false; } - return true; + break; } case KW_LSQUARE: { /* array type */ @@ -469,15 +470,40 @@ static bool parse_type(Parser *p, Type *type) { if (!parse_type(p, type->arr.of)) return false; type->flags = 0; type->where = start->where; - return true; + break; } - default: break; + case KW_LPAREN: + /* tuple! */ + type->kind = TYPE_TUPLE; + arr_create(&type->tuple, sizeof(Type)); + t->token++; /* move past ( */ + while (1) { + Type *child = arr_add(&type->tuple); + parse_type(p, child); + if (token_is_kw(t->token, KW_RPAREN)) { /* we're done with the tuple */ + t->token++; /* move past ) */ + break; + } + if (token_is_kw(t->token, KW_COMMA)) { + t->token++; /* move past , */ + continue; + } else { + tokr_err(t, "Expected , to list next tuple type or ) to end tuple type."); + return false; + } + } + break; + default: + tokr_err(t, "Unrecognized type."); + return false; } break; - default: break; + default: + tokr_err(t, "Unrecognized type."); + return false; } - tokr_err(t, "Unrecognized type."); - return false; + return true; + } static bool parse_param(Parser *parser, Param *p) { diff --git a/test.toc b/test.toc index 72dced0..8568967 100644 --- a/test.toc +++ b/test.toc @@ -1,3 +1,4 @@ -main @= fn() { - foo, bar, baz : int, something : float, salkdfj : int; +main @ fn() = fn() { + asdkofhj : fn() (int, int); + foo, bar, baz : int, something : float, salkdfj : int= 2, 2, 2, 2, 2; }; -- cgit v1.2.3