authorgravatar for thejoshwolfe@gmail.comJosh Wolfe <thejoshwolfe@gmail.com> 2015-12-03 10:19:00-07:00
committergravatar for thejoshwolfe@gmail.comJosh Wolfe <thejoshwolfe@gmail.com> 2015-12-03 10:19:00-07:00
log708cae3786a5ce103d92bffe7ea8107df039426f
tree57217ddcc48be9759d21e29d6b512d719068b36b
parentcb69cb0f26bb496bb0899da7f98767b2de025296

fix analysis for variable reference


1 files changed, 12 insertions(+), 10 deletions(-)

src/analyze.cpp+12-10
......@@ -345,7 +345,10 @@ static void check_type_compatibility(CodeGen *g, AstNode *node, TypeTableEntry *
345345 return; // TODO: is this true?
346346
347347 // TODO better error message
348 add_node_error(g, node, buf_sprintf("type mismatch. expected %s. got %s", buf_ptr(&expected_type->name), buf_ptr(&actual_type->name)));
348 add_node_error(g, node,
349 buf_sprintf("type mismatch. expected %s. got %s",
350 buf_ptr(&expected_type->name),
351 buf_ptr(&actual_type->name)));
349352}
350353
351354static BlockContext *new_block_context(AstNode *node, BlockContext *parent) {
......@@ -434,8 +437,8 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import,
434437
435438 LocalVariableTableEntry *existing_variable = find_local_variable(context, &variable_declaration->symbol);
436439 if (existing_variable) {
437 add_node_error(g, node, buf_sprintf("redeclaration of variable '%s'.",
438 buf_ptr(&variable_declaration->symbol)));
440 add_node_error(g, node,
441 buf_sprintf("redeclaration of variable '%s'.", buf_ptr(&variable_declaration->symbol)));
439442 } else {
440443 LocalVariableTableEntry *variable_entry = allocate<LocalVariableTableEntry>(1);
441444 buf_init_from_buf(&variable_entry->name, &variable_declaration->symbol);
......@@ -596,12 +599,11 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import,
596599 case NodeTypeSymbol:
597600 {
598601 Buf *symbol_name = &node->data.symbol;
599 FnTableEntry *fn_table_entry = get_context_fn_entry(context);
600 auto table_entry = fn_table_entry->symbol_table.maybe_get(symbol_name);
601 if (table_entry) {
602 SymbolTableEntry *symbol_entry = table_entry->value;
603 return_type = symbol_entry->type_entry;
602 LocalVariableTableEntry *local_variable = find_local_variable(context, symbol_name);
603 if (local_variable) {
604 return_type = local_variable->type;
604605 } else {
606 // TODO: check global variables also
605607 add_node_error(g, node,
606608 buf_sprintf("use of undeclared identifier '%s'", buf_ptr(symbol_name)));
607609 return_type = g->builtin_types.entry_invalid;
......@@ -718,8 +720,8 @@ static void analyze_top_level_declaration(CodeGen *g, ImportTableEntry *import,
718720 // unique definition
719721 context->variable_table.put(&variable_entry->name, variable_entry);
720722 } else {
721 add_node_error(g, node, buf_sprintf("redeclaration of parameter '%s'.",
722 buf_ptr(&existing_entry->name)));
723 add_node_error(g, node,
724 buf_sprintf("redeclaration of parameter '%s'.", buf_ptr(&existing_entry->name)));
723725 if (existing_entry->type == variable_entry->type) {
724726 // types agree, so the type is probably good enough for the rest of analysis
725727 } else {