issue #98 dump list of locals for each function, and start work on type checker

pull/168/head
Dibyendu Majumdar 5 years ago
parent 65ad3ea3e3
commit 3a98ffe3c2

@ -31,6 +31,8 @@ if (ASM_VM)
# TODO A fix is needed to ensure that in shared library the asm functions are resolved
set(STATIC_BUILD ON)
set(LTESTS OFF)
set(LLVM_JIT OFF)
set(OMR_JIT OFF)
endif()
if (STATIC_BUILD)

@ -41,7 +41,6 @@
/* Select native target if no target defined. */
#ifndef RAVI__TARGET
#if defined(__i386) || defined(__i386__) || defined(_M_IX86)
#error "No support for this architecture (yet)"
#define RAVI__TARGET RAVI__ARCH_X86
#elif defined(__x86_64__) || defined(__x86_64) || defined(_M_X64) || defined(_M_AMD64)
#define RAVI__TARGET RAVI__ARCH_X64

@ -1311,6 +1311,7 @@ function()
x --global symbol ?
=
function()
--locals i
for
i --local symbol ?
=
@ -1345,6 +1346,7 @@ function()
x --global symbol ?
=
function()
--locals a
local
--[symbols]
a --local symbol ?

@ -1,6 +1,7 @@
function()
return
function()
--locals a, y, a, z
local
--[symbols]
a --local symbol ?
@ -65,6 +66,7 @@ function()
,
e --local symbol ?
)
--locals a, b, c, d, e
if
--[binary expr start]
--[primary start]
@ -130,6 +132,7 @@ function()
function(
a --local symbol ?
)
--locals a
while
--[binary expr start]
--[primary start]
@ -164,6 +167,7 @@ function()
function(
a --local symbol ?
)
--locals a
::L2::
if
--[unary expr start]
@ -237,3 +241,73 @@ function()
end
end
function()
return
function()
--locals sum, j, k
local
--[symbols]
sum --local symbol ?
for
j --local symbol ?
=
1
,
500
do
--[expression statement start]
--[var list start]
--[primary start]
sum --local symbol ?
--[primary end]
= --[var list end]
--[expression list start]
0.0000000000000000
--[expression list end]
--[expression statement end]
for
k --local symbol ?
=
1
,
10000
do
--[expression statement start]
--[var list start]
--[primary start]
sum --local symbol ?
--[primary end]
= --[var list end]
--[expression list start]
--[binary expr start]
--[primary start]
sum --local symbol ?
--[primary end]
+
--[binary expr start]
1.0000000000000000
/
--[primary start]
--[binary expr start]
--[primary start]
k --local symbol ?
--[primary end]
*
--[primary start]
k --local symbol ?
--[primary end]
--[binary expr end]
--[primary end]
--[binary expr end]
--[binary expr end]
--[expression list end]
--[expression statement end]
end
end
return
--[primary start]
sum --local symbol ?
--[primary end]
end
end

@ -60,3 +60,17 @@ str=
end
]]
doast(str)
str=
[[return function ()
local sum
for j = 1,500 do
sum = 0.0
for k = 1,10000 do
sum = sum + 1.0/(k*k)
end
end
return sum
end
]]
doast(str)

@ -228,6 +228,7 @@ struct ast_node {
struct lua_symbol_list *args; /* arguments, also must be part of the function block's symbol list */
struct ast_node_list *child_functions; /* child functions declared in this function */
struct lua_symbol_list *upvalues; /* List of upvalues */
struct lua_symbol_list *locals; /* List of locals */
} function_expr; /* a literal expression whose result is a value of type function */
struct {
struct var_type type;
@ -376,6 +377,7 @@ static struct lua_symbol *new_local_symbol(struct parser_state *parser, TString
symbol->var.block = scope;
symbol->var.var_name = name;
add_symbol(parser->container, &scope->symbol_list, symbol); // Add to the end of the symbol list
add_symbol(parser->container, &scope->function->function_expr.locals, symbol);
// Note that Lua allows multiple local declarations of the same name
// so a new instance just gets added to the end
return symbol;
@ -1260,7 +1262,7 @@ static struct ast_node *parse_goto_statment(struct parser_state *parser) {
/* skip no-op statements */
static void skip_noop_statements(struct parser_state *parser) {
LexState *ls = parser->ls;
while (ls->t.token == ';') // || ls->t.token == TK_DBCOLON)
while (ls->t.token == ';') // || ls->t.token == TK_DBCOLON)
parse_statement(parser);
}
@ -1687,6 +1689,7 @@ static struct ast_node *new_function(struct parser_state *parser) {
node->function_expr.args = NULL;
node->function_expr.child_functions = NULL;
node->function_expr.upvalues = NULL;
node->function_expr.locals = NULL;
node->function_expr.main_block = NULL;
node->function_expr.function_statement_list = NULL;
node->function_expr.parent_function = parser->current_function;
@ -1965,6 +1968,11 @@ static void print_ast_node(membuff_t *buf, struct ast_node *node, int level) {
else {
printf_buf(buf, "%pfunction()\n", level);
}
if (node->function_expr.locals) {
printf_buf(buf, "%p%c ", level, "locals ");
print_symbol_names(buf, node->function_expr.locals);
printf_buf(buf, "\n");
}
if (node->function_expr.upvalues) {
printf_buf(buf, "%p%c ", level, "upvalues ");
print_symbol_names(buf, node->function_expr.upvalues);
@ -2228,6 +2236,127 @@ static int parse_to_ast(lua_State *L, ZIO *z, Mbuffer *buff, const char *name, i
return 0; /* OK */
}
/* Type checker - WIP */
static void typecheck_ast_node(struct ast_node *function, struct ast_node *node);
/* Type checker - WIP */
static void typecheck_unaryop(struct ast_node *function, struct ast_node *node) {
UnOpr op = node->unary_expr.unary_op;
typecheck_ast_node(function, node->unary_expr.expr);
}
/* Type checker - WIP */
static void typecheck_ast_node(struct ast_node *function, struct ast_node *node) {
switch (node->type) {
case AST_FUNCTION_EXPR: {
break;
}
case AST_NONE:
break;
case AST_RETURN_STMT: {
break;
}
case AST_LOCAL_STMT: {
break;
}
case AST_FUNCTION_STMT: {
break;
}
case AST_LABEL_STMT: {
break;
}
case AST_GOTO_STMT: {
break;
}
case AST_DO_STMT: {
break;
}
case AST_EXPR_STMT: {
break;
}
case AST_IF_STMT: {
struct ast_node *test_then_block;
FOR_EACH_PTR(node->if_stmt.if_condition_list, test_then_block) {}
END_FOR_EACH_PTR(node);
if (node->if_stmt.else_block) {
}
break;
}
case AST_WHILE_STMT: {
break;
}
case AST_REPEAT_STMT: {
break;
}
case AST_FORIN_STMT: {
break;
}
case AST_FORNUM_STMT: {
break;
}
case AST_SUFFIXED_EXPR: {
if (node->suffixed_expr.suffix_list) {
}
break;
}
case AST_FUNCTION_CALL_EXPR: {
if (node->function_call_expr.methodname) {
}
else {
}
break;
}
case AST_SYMBOL_EXPR: {
break;
}
case AST_BINARY_EXPR: {
break;
}
case AST_UNARY_EXPR: {
typecheck_unaryop(function, node);
break;
}
case AST_LITERAL_EXPR: {
break;
}
case AST_FIELD_SELECTOR_EXPR: {
break;
}
case AST_Y_INDEX_EXPR: {
break;
}
case AST_INDEXED_ASSIGN_EXPR: {
if (node->indexed_assign_expr.index_expr) {
}
break;
}
case AST_TABLE_EXPR: {
break;
}
default:
assert(0);
}
}
/* Type checker - WIP */
static void typecheck_ast_list(struct ast_node *function, struct ast_node_list *list) {
struct ast_node *node;
FOR_EACH_PTR(list, node) { typecheck_statement(function, node); }
END_FOR_EACH_PTR(node);
}
/* Type checker - WIP */
static void typecheck_function(struct ast_node *func) {
typecheck_ast_list(func, func->function_expr.function_statement_list);
}
/* Type checker - WIP */
static void do_typechecks(struct ast_container *container) {
struct ast_node *main_function = container->main_function;
typecheck_function(main_function);
}
/*
** ravi parser context
*/

Loading…
Cancel
Save