From ff73e4ff0229fa018721224007668d1ccc59f175 Mon Sep 17 00:00:00 2001 From: Jeroen van der Heijden Date: Mon, 20 Jan 2025 14:59:13 +0100 Subject: [PATCH 1/2] allow return statement without arguments --- CHANGELOG.md | 4 ++++ grammar/grammar.py | 2 +- inc/ti/do.h | 1 + inc/ti/version.h | 4 ++-- itest/test_syntax.py | 5 ++++- src/langdef/langdef.c | 2 +- src/ti/do.c | 7 +++++++ src/ti/qbind.c | 7 +++++++ 8 files changed, 27 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b8973b70..8efd0572 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # v1.7.3 +* Allow `return;` statement without arguments _(implicit return nil)_. + +# v1.7.3 + * Improve destroy websocket when unexpected disconnect. * Fix in recursive `for-each` loop. diff --git a/grammar/grammar.py b/grammar/grammar.py index 0c1a678b..72dccb80 100644 --- a/grammar/grammar.py +++ b/grammar/grammar.py @@ -161,7 +161,7 @@ class LangDef(Grammar): return_statement = Sequence( k_return, - List(THIS, mi=1, ma=3, opt=False)) + List(THIS, mi=0, ma=3, opt=False)) for_statement = Sequence( k_for, diff --git a/inc/ti/do.h b/inc/ti/do.h index 60486ce4..0d9c80cb 100644 --- a/inc/ti/do.h +++ b/inc/ti/do.h @@ -28,6 +28,7 @@ int ti_do_compare_and(ti_query_t * query, cleri_node_t * nd, ex_t * e); int ti_do_compare_or(ti_query_t * query, cleri_node_t * nd, ex_t * e); int ti_do_ternary(ti_query_t * query, cleri_node_t * nd, ex_t * e); int ti_do_if_statement(ti_query_t * query, cleri_node_t * nd, ex_t * e); +int ti_do_return_nil(ti_query_t * query, cleri_node_t * nd, ex_t * e); int ti_do_return_val(ti_query_t * query, cleri_node_t * nd, ex_t * e); int ti_do_return_alt_deep(ti_query_t * query, cleri_node_t * nd, ex_t * e); int ti_do_return_alt_flags(ti_query_t * query, cleri_node_t * nd, ex_t * e); diff --git a/inc/ti/version.h b/inc/ti/version.h index 1902d674..5e993e58 100644 --- a/inc/ti/version.h +++ b/inc/ti/version.h @@ -6,7 +6,7 @@ #define TI_VERSION_MAJOR 1 #define TI_VERSION_MINOR 7 -#define TI_VERSION_PATCH 3 +#define TI_VERSION_PATCH 4 /* The syntax version is used to test compatibility with functions * using the `ti_nodes_check_syntax()` function */ @@ -25,7 +25,7 @@ * "-rc0" * "" */ -#define TI_VERSION_PRE_RELEASE "" +#define TI_VERSION_PRE_RELEASE "-alpha0" #define TI_MAINTAINER \ "Jeroen van der Heijden " diff --git a/itest/test_syntax.py b/itest/test_syntax.py index d47aa532..58ae2674 100755 --- a/itest/test_syntax.py +++ b/itest/test_syntax.py @@ -177,8 +177,11 @@ async def test_fixed_keywords(self, client): ]) async def test_empty(self, client): - await client.query("") + res = await client.query("") + self.assertIsNone(res) await client.query("nil;;;") + self.assertIsNone(res) + await client.query("return;") if __name__ == '__main__': diff --git a/src/langdef/langdef.c b/src/langdef/langdef.c index e0db6750..a91cb621 100644 --- a/src/langdef/langdef.c +++ b/src/langdef/langdef.c @@ -232,7 +232,7 @@ cleri_grammar_t * compile_langdef(void) CLERI_GID_RETURN_STATEMENT, 2, k_return, - cleri_list(CLERI_NONE, CLERI_THIS, cleri_token(CLERI_NONE, ","), 1, 3, 0) + cleri_list(CLERI_NONE, CLERI_THIS, cleri_token(CLERI_NONE, ","), 0, 3, 0) ); cleri_t * for_statement = cleri_sequence( CLERI_GID_FOR_STATEMENT, diff --git a/src/ti/do.c b/src/ti/do.c index 4788029c..b2203aff 100644 --- a/src/ti/do.c +++ b/src/ti/do.c @@ -924,6 +924,13 @@ int ti_do_if_statement(ti_query_t * query, cleri_node_t * nd, ex_t * e) return ti_do_statement(query, nd, e); } +int ti_do_return_nil(ti_query_t * query, cleri_node_t * UNUSED(nd), ex_t * e) +{ + query->rval = (ti_val_t *) ti_nil_get(); + ex_set_return(e); + return e->nr; +} + int ti_do_return_val(ti_query_t * query, cleri_node_t * nd, ex_t * e) { if (ti_do_statement(query, nd->children->next->children, e) == 0) diff --git a/src/ti/qbind.c b/src/ti/qbind.c index ee6c1c9e..4804f48d 100644 --- a/src/ti/qbind.c +++ b/src/ti/qbind.c @@ -1347,6 +1347,13 @@ static inline void qbind__return_statement( cleri_node_t * nd) { cleri_node_t * node = nd->children->next->children; + if (!node) + { + nd->data = ti_do_return_nil; + nd->children->data = NULL; + return; + } + qbind__statement(qbind, node); if (node->next) { From 545798075c17ee6983c601d7566cc138d54cac58 Mon Sep 17 00:00:00 2001 From: Jeroen van der Heijden Date: Mon, 20 Jan 2025 15:01:23 +0100 Subject: [PATCH 2/2] Upd changelog --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8efd0572..005c332f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ -# v1.7.3 +# v1.7.4 -* Allow `return;` statement without arguments _(implicit return nil)_. +* Allow `return;` statement without arguments _(implicitly return nil)_, pr #404. # v1.7.3