Skip to content

Commit

Permalink
Allow return statement without arguments (#404)
Browse files Browse the repository at this point in the history
* allow return statement without arguments

* Upd changelog
  • Loading branch information
joente authored Jan 20, 2025
1 parent f0878f2 commit a5b4ab7
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# v1.7.4

* Allow `return;` statement without arguments _(implicitly return nil)_, pr #404.

# v1.7.3

* Improve destroy websocket when unexpected disconnect.
Expand Down
2 changes: 1 addition & 1 deletion grammar/grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions inc/ti/do.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions inc/ti/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -25,7 +25,7 @@
* "-rc0"
* ""
*/
#define TI_VERSION_PRE_RELEASE ""
#define TI_VERSION_PRE_RELEASE "-alpha0"

#define TI_MAINTAINER \
"Jeroen van der Heijden <[email protected]>"
Expand Down
5 changes: 4 additions & 1 deletion itest/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__':
Expand Down
2 changes: 1 addition & 1 deletion src/langdef/langdef.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions src/ti/do.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions src/ti/qbind.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

0 comments on commit a5b4ab7

Please sign in to comment.