Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow return statement without arguments #404

Merged
merged 2 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 <jeroen@cesbit.com>"
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
Loading