From bc1b6b8d783574a35bde7279437378a821ea5c0e Mon Sep 17 00:00:00 2001 From: dibyendumajumdar Date: Sun, 31 Jul 2022 11:32:57 +0100 Subject: [PATCH] #245 when lowering generic for statement add locals from the for body --- ravicomp/src/ast_lower.c | 6 ++++++ tests/comptests/inputs/74_luajit_andor.lua | 8 ++++++++ tests/comptests/inputs/75_fornum_locals.lua | 11 +++++++++++ 3 files changed, 25 insertions(+) create mode 100644 tests/comptests/inputs/74_luajit_andor.lua create mode 100644 tests/comptests/inputs/75_fornum_locals.lua diff --git a/ravicomp/src/ast_lower.c b/ravicomp/src/ast_lower.c index 674f31be..fb4026b5 100644 --- a/ravicomp/src/ast_lower.c +++ b/ravicomp/src/ast_lower.c @@ -222,6 +222,7 @@ static void lower_for_in_statement(CompilerState *compiler_state, AstNode *node) { ForStatement *for_stmt = &node->for_stmt; AstNode *function = for_stmt->for_scope->function; + Scope *for_body_scope = for_stmt->for_body; // FIXME - the for variables must be removed from parent scope @@ -317,6 +318,11 @@ static void lower_for_in_statement(CompilerState *compiler_state, AstNode *node) } END_FOR_EACH_PTR(n2) + LuaSymbol *sym; + FOR_EACH_PTR(for_body_scope->symbol_list, LuaSymbol, sym) { + raviX_add_symbol(compiler_state, &while_scope->symbol_list, sym); + } END_FOR_EACH_PTR(sym) + // Replace the original generic for ast with the new do block *node = *do_stmt; } diff --git a/tests/comptests/inputs/74_luajit_andor.lua b/tests/comptests/inputs/74_luajit_andor.lua new file mode 100644 index 00000000..7c9f594b --- /dev/null +++ b/tests/comptests/inputs/74_luajit_andor.lua @@ -0,0 +1,8 @@ +-- source https://github.com/LuaJIT/LuaJIT-test-cleanup/blob/master/test/lang/andor.lua + +do --- smoke + local x = ((1 or false) and true) or false + assert(x == true) +end + +print '74 Ok' \ No newline at end of file diff --git a/tests/comptests/inputs/75_fornum_locals.lua b/tests/comptests/inputs/75_fornum_locals.lua new file mode 100644 index 00000000..6c37355e --- /dev/null +++ b/tests/comptests/inputs/75_fornum_locals.lua @@ -0,0 +1,11 @@ +local values = {} +for k,v in pairs({ name='Dibyendu', surname='Majumdar' }) do + local key = k + local value = v + values[key] = value +end + +assert(values.name == 'Dibyendu') +assert(values.surname == 'Majumdar') + +print '75 Ok' \ No newline at end of file