From ba60f93afadcc37d9bde7158c96d4d56d2e5304f Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Thu, 28 Mar 2019 22:09:21 -0400 Subject: [PATCH] added array initialization --- example.src | 22 ++++++++++++---------- source/lex.c | 2 +- source/parser.c | 21 +++++++++++++-------- 3 files changed, 26 insertions(+), 19 deletions(-) diff --git a/example.src b/example.src index 6200d0d..0c6e513 100644 --- a/example.src +++ b/example.src @@ -1,16 +1,6 @@ require ("fmt") provide (main) -let const_true bool = true -let const_false bool = false -let const_uint int = 123 -let const_string string = "" - -var var_true bool = true -var var_false bool = false -var var_uint int = 123 -var var_string string = "" - type type_int = int type type_intary = int[] type type_intaryary = int[][] @@ -26,6 +16,18 @@ type type_union = union { bar = float } +let const_true bool = true +let const_false bool = false +let const_uint int = 123 +let const_string string = "" +let const_intary int[] = [123,321] + +var var_true bool = true +var var_false bool = false +var var_uint int = 123 +var var_string string = "" +var var_intary int[] = [123,321] + fun main(args string[]) int { let foo int = 123 var bar int = 123 diff --git a/source/lex.c b/source/lex.c index de5bba9..1351a98 100644 --- a/source/lex.c +++ b/source/lex.c @@ -215,7 +215,7 @@ void lex(Parser* ctx) { } } -static LexFile* get_file(Parser* p, char* path) { +static LexFile* get_file(Parser* p, char const* path) { LexFile* lf = p->file; while (lf && strcmp(lf->path, path)) lf = lf->next; diff --git a/source/parser.c b/source/parser.c index dd01fb2..55a0ecc 100644 --- a/source/parser.c +++ b/source/parser.c @@ -14,7 +14,7 @@ static AST* struct_fields(Parser* p); static AST* expression_block(Parser* p); static AST* if_expression(Parser* p); static AST* identifier(Parser* p); -static AST* func_expr_list(Parser* p); +static AST* expr_list(Parser* p, int firstc, int endc); /* Parsing Routines *****************************************************************************/ @@ -158,6 +158,8 @@ static AST* expression(Parser* p) { expect(p, '('); exp = expression(p); expect(p, ')'); + } else if (matches(p, '[')) { + exp = expr_list(p, '[', ']'); } else if (matches(p, '{')) { exp = expression_block(p); } else if (matches(p, T_IF)) { @@ -170,10 +172,10 @@ static AST* expression(Parser* p) { /* determine if this is a function call */ if (matches(p, '(')) { - exp = Apply(exp, func_expr_list(p)); + exp = Apply(exp, expr_list(p, '(', ')')); } else if (accept(p, '.')) { AST* func = identifier(p); - AST* args = func_expr_list(p); + AST* args = expr_list(p, '(', ')'); explist_prepend(args, exp); exp = Apply(func, args); } @@ -274,15 +276,18 @@ static AST* identifier(Parser* p) { } } -static AST* func_expr_list(Parser* p) { - expect(p, '('); +static AST* expr_list(Parser* p, int firstc, int endc) { + expect(p, firstc); AST* list = ExpList(); - while (!matches(p, ')')) { + while (!matches(p, endc)) { explist_append(list, expression(p)); - if (!matches(p, ')')) + if (!matches(p, endc)) { expect(p, ','); + if (matches(p, endc)) + error(p, "Expected an expression"); + } } - expect(p, ')'); + expect(p, endc); return list; } -- 2.54.0