From de42b8b0ebf248db4033c7940a7f8aea29dfb240 Mon Sep 17 00:00:00 2001 From: Dibyendu Majumdar Date: Sat, 30 Nov 2019 16:21:51 +0000 Subject: [PATCH] issue #98 some work work on type checking - table expressions, do while and repeat statements --- ravi-tests/ravi_test_ast.expected | 77 +++++++++++++++++++++++++++++++ ravi-tests/ravi_test_ast.lua | 7 ++- src/ravi_ast_parse.c | 33 ------------- src/ravi_ast_typecheck.c | 17 +++++-- 4 files changed, 97 insertions(+), 37 deletions(-) diff --git a/ravi-tests/ravi_test_ast.expected b/ravi-tests/ravi_test_ast.expected index 275a064..a5e65c9 100644 --- a/ravi-tests/ravi_test_ast.expected +++ b/ravi-tests/ravi_test_ast.expected @@ -1665,3 +1665,80 @@ function() end -- +--[[return {1,2,3} +]] +function() + return + { --[table constructor start] table + --[indexed assign start] integer + --[value start] + 1 + --[value end] + --[indexed assign end] + , + --[indexed assign start] integer + --[value start] + 2 + --[value end] + --[indexed assign end] + , + --[indexed assign start] integer + --[value start] + 3 + --[value end] + --[indexed assign end] + } --[table constructor end] +end + +-- +--[[local a: integer return {1+a} +]] +function() +--locals a + local + --[symbols] + a --local symbol integer + return + { --[table constructor start] table + --[indexed assign start] integer + --[value start] + --[binary expr start] integer + 1 + + + --[suffixed expr start] integer + --[primary start] integer + a --local symbol integer + --[primary end] + --[suffixed expr end] + --[binary expr end] + --[value end] + --[indexed assign end] + } --[table constructor end] +end + +-- +--[[return {[1] = a} +]] +function() + return + { --[table constructor start] table + --[indexed assign start] any + --[index start] + --[Y index start] any + [ + 1 + ] + --[Y index end] + --[index end] + --[value start] + --[suffixed expr start] any + --[primary start] any + a --global symbol ? + --[primary end] + --[suffixed expr end] + --[value end] + --[indexed assign end] + } --[table constructor end] +end + +-- diff --git a/ravi-tests/ravi_test_ast.lua b/ravi-tests/ravi_test_ast.lua index 1594bc7..933c2a9 100644 --- a/ravi-tests/ravi_test_ast.lua +++ b/ravi-tests/ravi_test_ast.lua @@ -81,4 +81,9 @@ dotest 'return @number 54.4' dotest 'return @User.Type a' dotest 'for i=1,10 do print(i+1) end return' -dotest 'for i=1.0, 10.0 do print(i+4.2) end return' \ No newline at end of file +dotest 'for i=1.0, 10.0 do print(i+4.2) end return' + +dotest 'return {1,2,3}' + +dotest 'local a: integer return {1+a}' +dotest 'return {[1] = a}' \ No newline at end of file diff --git a/src/ravi_ast_parse.c b/src/ravi_ast_parse.c index 85c274d..4a86b89 100644 --- a/src/ravi_ast_parse.c +++ b/src/ravi_ast_parse.c @@ -172,27 +172,6 @@ static struct lua_symbol *search_for_variable_in_block(struct block_scope *scope return NULL; } -// static struct lua_symbol *search_for_label_in_block(struct block_scope *scope, const TString *name) { -// struct lua_symbol *symbol; -// // Lookup in reverse order so that we discover the -// // most recently added local symbol - as Lua allows same -// // symbol to be declared local more than once in a scope -// // Should also work with nesting as the function when parsed -// // will only know about vars declared in parent function until -// // now. -// FOR_EACH_PTR_REVERSE(scope->symbol_list, symbol) { -// switch (symbol->symbol_type) { -// case SYM_LABEL: { -// if (name == symbol->var.var_name) { return symbol; } -// break; -// } -// default: break; -// } -// } -// END_FOR_EACH_PTR_REVERSE(symbol); -// return NULL; -//} - /* Each function has a list of upvalues, searches this list for given name */ static struct lua_symbol *search_upvalue_in_function(struct ast_node *function, const TString *name) { @@ -272,18 +251,6 @@ static struct lua_symbol *search_for_variable(struct parser_state *parser, const return NULL; } -/* Searches for a label in current function - */ -// static struct lua_symbol *search_for_label(struct parser_state *parser, const TString *name) { -// struct block_scope *current_scope = parser->current_scope; -// while (current_scope && current_scope->function == parser->current_function) { -// struct lua_symbol *symbol = search_for_label_in_block(current_scope, name); -// if (symbol) return symbol; -// current_scope = current_scope->parent; -// } -// return NULL; -//} - /* Adds an upvalue to current_function and its parents until var_function; var_function being where the symbol * exists as a local or an upvalue. If the symbol is found in a function's upvalue list then there is no need to * check parent functions. diff --git a/src/ravi_ast_typecheck.c b/src/ravi_ast_typecheck.c index 5e84cfa..cff79af 100644 --- a/src/ravi_ast_typecheck.c +++ b/src/ravi_ast_typecheck.c @@ -347,6 +347,14 @@ static void typecheck_if_statement(struct ast_container *container, struct ast_n } } +static void typecheck_while_or_repeat_statement(struct ast_container *container, struct ast_node *function, + struct ast_node *node) { + typecheck_ast_node(container, function, node->while_or_repeat_stmt.condition); + if (node->while_or_repeat_stmt.loop_statement_list) { + typecheck_ast_list(container, function, node->while_or_repeat_stmt.loop_statement_list); + } +} + /* Type checker - WIP */ static void typecheck_ast_node(struct ast_container *container, struct ast_node *function, struct ast_node *node) { switch (node->type) { @@ -386,10 +394,9 @@ static void typecheck_ast_node(struct ast_container *container, struct ast_node typecheck_if_statement(container, function, node); break; } - case AST_WHILE_STMT: { - break; - } + case AST_WHILE_STMT: case AST_REPEAT_STMT: { + typecheck_while_or_repeat_statement(container, function, node); break; } case AST_FORIN_STMT: { @@ -439,10 +446,14 @@ static void typecheck_ast_node(struct ast_container *container, struct ast_node } case AST_INDEXED_ASSIGN_EXPR: { if (node->indexed_assign_expr.index_expr) { + typecheck_ast_node(container, function, node->indexed_assign_expr.index_expr); } + typecheck_ast_node(container, function, node->indexed_assign_expr.value_expr); + copy_type(node->indexed_assign_expr.type, node->indexed_assign_expr.value_expr->common_expr.type); break; } case AST_TABLE_EXPR: { + typecheck_ast_list(container, function, node->table_expr.expr_list); break; } default: