From eab63ecc6c40a96781a2a56404062bef17aaa4f5 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Tue, 27 Jun 2023 01:29:25 +0200 Subject: [PATCH] allow curly brace on next line also after subroutine and when --- docs/source/todo.rst | 3 +++ examples/test.p8 | 36 ++++++++++++++++-------------------- parser/antlr/Prog8ANTLR.g4 | 4 ++-- 3 files changed, 21 insertions(+), 22 deletions(-) diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 35a1eb36b..c7ecc743d 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -1,6 +1,9 @@ TODO ==== +- also allow this? + if variable { txt.print("yes") } + else { txt.print("no") } - is it possible to allow the curly brace to be on the next line instead of requiring it to follow on the same line? ... diff --git a/examples/test.p8 b/examples/test.p8 index e2e77c39f..cbeffe534 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -1,28 +1,24 @@ %import textio -%option no_sysinit -%zeropage dontuse +%zeropage basicsafe main { - uword variable=55555 + sub start() + { + ubyte variable=55 + when variable + { + 33 -> txt.print("33") + } - sub start() { - txt.print("active rambank=") - txt.print_ub(cx16.getrambank()) - txt.print("\nvar addr=") - txt.print_uwhex(&variable, true) - txt.print("\nvalue=") - txt.print_uw(variable) - txt.print("\n(rambank 10) variable=") - cx16.rambank(10) - txt.print_uw(variable) - txt.print("\n(rambank 2) variable=") - cx16.rambank(2) - txt.print_uw(variable) - txt.print("\n(rambank 1) variable=") - cx16.rambank(1) - txt.print_uw(variable) - txt.nl() + if variable + { + txt.print("yes") + } + else + { + txt.print("no") + } } } diff --git a/parser/antlr/Prog8ANTLR.g4 b/parser/antlr/Prog8ANTLR.g4 index f8e4b9973..07163f3a3 100644 --- a/parser/antlr/Prog8ANTLR.g4 +++ b/parser/antlr/Prog8ANTLR.g4 @@ -239,7 +239,7 @@ inlineir: '%ir' INLINEASMBLOCK; inline: 'inline'; subroutine : - 'sub' identifier '(' sub_params? ')' sub_return_part? (statement_block EOL) + 'sub' identifier '(' sub_params? ')' sub_return_part? EOL? (statement_block EOL) ; sub_return_part : '->' datatype ; @@ -296,6 +296,6 @@ repeatloop: 'repeat' expression? EOL? (statement | statement_block) ; unrollloop: 'unroll' integerliteral? EOL? (statement | statement_block) ; -whenstmt: 'when' expression '{' EOL (when_choice | EOL) * '}' EOL? ; +whenstmt: 'when' expression EOL? '{' EOL (when_choice | EOL) * '}' EOL? ; when_choice: (expression_list | 'else' ) '->' (statement | statement_block ) ;