From 660808af50780dae5292c8064d5bddd77246c52b Mon Sep 17 00:00:00 2001 From: Mel Date: Sun, 6 Jul 2025 03:57:37 +0200 Subject: Correct handling of empty block nodes Signed-off-by: Mel --- boot/parse.c | 6 +++++- boot/tests/parse/empty_block.cskt | 33 +++++++++++++++++++++++++++++++ boot/tests/parse/variadic_parameters.cskt | 3 +-- boot/visit.c | 15 ++++++++------ 4 files changed, 48 insertions(+), 9 deletions(-) create mode 100644 boot/tests/parse/empty_block.cskt diff --git a/boot/parse.c b/boot/parse.c index cd5f019..264cf45 100644 --- a/boot/parse.c +++ b/boot/parse.c @@ -223,7 +223,11 @@ parser_block_node(struct Parser* p, struct Parser_Error* error) while (!parser_probe(p, TOKEN_CURLY_CLOSE)) { struct Statement* statement = CHECK_RETURN(parser_statement(p, error), struct Block_Node); - CHECK_RETURN(parser_end_statement(p, error), struct Block_Node); + + // statement ending token isn't required when the block ends on the same line, + // as in e.g.: `if (true) { print("yes") }` + if (!parser_probe(p, TOKEN_CURLY_CLOSE)) + CHECK_RETURN(parser_end_statement(p, error), struct Block_Node); if (!head) { head = statement; diff --git a/boot/tests/parse/empty_block.cskt b/boot/tests/parse/empty_block.cskt new file mode 100644 index 0000000..c3a203a --- /dev/null +++ b/boot/tests/parse/empty_block.cskt @@ -0,0 +1,33 @@ +check for correct handling of empty blocks + +<<< + +{ + +} + +{ {} } +{{{}}} + +{ +{ +} +} + +{} + +>>> + +(block) +(block + (block) +) +(block + (block + (block) + ) +) +(block + (block) +) +(block) \ No newline at end of file diff --git a/boot/tests/parse/variadic_parameters.cskt b/boot/tests/parse/variadic_parameters.cskt index 3b718a1..e002c93 100644 --- a/boot/tests/parse/variadic_parameters.cskt +++ b/boot/tests/parse/variadic_parameters.cskt @@ -9,5 +9,4 @@ printf = fun (format string, ...args any) { >>> -(expr (binary = (expr (name printf)) (expr (function (param format) (type name string) (param args) (type variadic name any) (block -))))) \ No newline at end of file +(expr (binary = (expr (name printf)) (expr (function (param format) (type name string) (param args) (type variadic name any) (block))))) \ No newline at end of file diff --git a/boot/visit.c b/boot/visit.c index 781c227..68e95bf 100644 --- a/boot/visit.c +++ b/boot/visit.c @@ -984,14 +984,17 @@ printer_visit_block_node(struct Visit* visit, struct Block_Node* node) { TREE_PRINTER_PREAMBLE - PRINT("(block\n"); - printer->indentation_level++; - FOR_EACH (struct Statement*, statement, node->statements) { - VISIT(visit_statement, statement); + PRINT("(block"); + if (node->statements) { PRINT("\n"); + printer->indentation_level++; + FOR_EACH (struct Statement*, statement, node->statements) { + VISIT(visit_statement, statement); + PRINT("\n"); + } + printer->indentation_level--; + tree_printer_indent(printer); } - printer->indentation_level--; - tree_printer_indent(printer); PRINT(")"); } -- cgit 1.4.1