issue #98 some work work on type checking - table expressions, do while and repeat statements

defer
Dibyendu Majumdar 4 years ago
parent 2c66f62c09
commit de42b8b0eb

@ -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
--

@ -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'
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}'

@ -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.

@ -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:

Loading…
Cancel
Save