summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorkugwa <kugwa2000@gmail.com>2015-12-08 04:42:39 +0800
committerkugwa <kugwa2000@gmail.com>2015-12-08 04:42:39 +0800
commit7c8774b25e6562e301af606f919ff130f26be7fb (patch)
treee2b73929b800cd992f1894db970cf918569da410
parent1eb637f6f8d72a2ed09ec4b3548ed176f2d91504 (diff)
downloadcompiler2015-7c8774b25e6562e301af606f919ff130f26be7fb.tar
compiler2015-7c8774b25e6562e301af606f919ff130f26be7fb.tar.gz
compiler2015-7c8774b25e6562e301af606f919ff130f26be7fb.tar.bz2
compiler2015-7c8774b25e6562e301af606f919ff130f26be7fb.tar.lz
compiler2015-7c8774b25e6562e301af606f919ff130f26be7fb.tar.xz
compiler2015-7c8774b25e6562e301af606f919ff130f26be7fb.tar.zst
compiler2015-7c8774b25e6562e301af606f919ff130f26be7fb.zip
Move code for block node into process_block()
I don't know how to indent beautifully...
-rw-r--r--src/semantic-analysis.c96
1 files changed, 62 insertions, 34 deletions
diff --git a/src/semantic-analysis.c b/src/semantic-analysis.c
index fbab897..84046c0 100644
--- a/src/semantic-analysis.c
+++ b/src/semantic-analysis.c
@@ -465,61 +465,89 @@ static bool process_variable(
return any_error;
}
+static bool process_block(CcmmcAst *block, CcmmcSymbolTable *table)
+{
+ bool any_error = false;
+
+ // Push a new scope for the block
+ ccmmc_symbol_table_open_scope(table);
+ // Insert builtin types
+ ccmmc_symbol_table_insert(table, "int", CCMMC_SYMBOL_KIND_TYPE,
+ (CcmmcSymbolType){ .type_base = CCMMC_AST_VALUE_INT });
+ ccmmc_symbol_table_insert(table, "float", CCMMC_SYMBOL_KIND_TYPE,
+ (CcmmcSymbolType){ .type_base = CCMMC_AST_VALUE_FLOAT });
+ ccmmc_symbol_table_insert(table, "void", CCMMC_SYMBOL_KIND_TYPE,
+ (CcmmcSymbolType){ .type_base = CCMMC_AST_VALUE_VOID });
+
+ if (block->parent->type_node == CCMMC_AST_NODE_DECL &&
+ block->parent->value_decl.kind == CCMMC_KIND_DECL_FUNCTION) {
+ // Insert the parameters
+ CcmmcSymbol *func = ccmmc_symbol_table_retrive(table,
+ block->leftmost_sibling->right_sibling->value_id.name);
+ CcmmcAst *param;
+ size_t i;
+ for (param = block->leftmost_sibling->right_sibling->right_sibling->child, i = 0;
+ i < func->type.param_count; param = param->right_sibling, i++) {
+ if (ccmmc_symbol_scope_exist(table->current,
+ param->child->right_sibling->value_id.name)) {
+ any_error = true;
+ fprintf (stderr, ERROR("ID `%s' redeclared."),
+ param->child->right_sibling->line_number,
+ param->child->right_sibling->value_id.name);
+ continue;
+ }
+ ccmmc_symbol_table_insert(table, param->child->right_sibling->value_id.name,
+ CCMMC_SYMBOL_KIND_VARIABLE, func->type.param_list[i]);
+ }
+ }
+
+ // Process the list of local declarations
+
+ // Process the list of statements
+
+ // Pop this scope
+ ccmmc_symbol_table_close_scope(table);
+ return any_error;
+}
+
static bool process_function(CcmmcAst *func_decl, CcmmcSymbolTable *table)
{
bool any_error = false;
size_t param_count = 0;
CcmmcSymbolType *param_list = NULL;
- size_t i;
- CcmmcAst *param;
// Create an entry for the function in global scope
if (func_decl->child->right_sibling->right_sibling->child != NULL){
- for (param = func_decl->child->right_sibling->right_sibling->child; param != NULL; param = param->right_sibling, param_count++);
+ CcmmcAst *param;
+ size_t i;
+ for (param = func_decl->child->right_sibling->right_sibling->child;
+ param != NULL; param = param->right_sibling, param_count++);
param_list = malloc(sizeof(CcmmcSymbolType) * param_count);
- for (param = func_decl->child->right_sibling->right_sibling->child, i = 0; i < param_count; param = param->right_sibling, i++) {
- param_list[i].type_base = ccmmc_symbol_table_retrive(table, param->child->value_id.name)->type.type_base;
+ for (param = func_decl->child->right_sibling->right_sibling->child, i = 0;
+ i < param_count; param = param->right_sibling, i++) {
+ param_list[i].type_base = ccmmc_symbol_table_retrive(table,
+ param->child->value_id.name)->type.type_base;
if (param->child->right_sibling->value_id.kind == CCMMC_KIND_ID_ARRAY)
- param_list[i].array_size = get_array_size(param->child->right_sibling, &param_list[i].array_dimension);
+ param_list[i].array_size = get_array_size(param->child->right_sibling,
+ &param_list[i].array_dimension);
else
param_list[i].array_dimension = 0;
param_list[i].param_valid = false;
}
}
CcmmcSymbolType func_type = {
- .type_base = ccmmc_symbol_table_retrive(table, func_decl->child->value_id.name)->type.type_base,
+ .type_base = ccmmc_symbol_table_retrive(table,
+ func_decl->child->value_id.name)->type.type_base,
.array_dimension = 0,
.param_valid = true,
.param_count = param_count,
.param_list = param_list };
- ccmmc_symbol_table_insert(table, func_decl->child->right_sibling->value_id.name, CCMMC_SYMBOL_KIND_FUNCTION, func_type);
+ ccmmc_symbol_table_insert(table, func_decl->child->right_sibling->value_id.name,
+ CCMMC_SYMBOL_KIND_FUNCTION, func_type);
- // Push a new scope for the function
- ccmmc_symbol_table_open_scope(table);
- // Insert builtin types
- ccmmc_symbol_table_insert(table, "int", CCMMC_SYMBOL_KIND_TYPE,
- (CcmmcSymbolType){ .type_base = CCMMC_AST_VALUE_INT });
- ccmmc_symbol_table_insert(table, "float", CCMMC_SYMBOL_KIND_TYPE,
- (CcmmcSymbolType){ .type_base = CCMMC_AST_VALUE_FLOAT });
- ccmmc_symbol_table_insert(table, "void", CCMMC_SYMBOL_KIND_TYPE,
- (CcmmcSymbolType){ .type_base = CCMMC_AST_VALUE_VOID });
- // Insert the parameters
- CcmmcSymbol *func = ccmmc_symbol_table_retrive(table, func_decl->child->right_sibling->value_id.name);
- for (param = func_decl->child->right_sibling->right_sibling->child, i = 0; i < param_count; param = param->right_sibling, i++) {
- if (ccmmc_symbol_scope_exist(table->current, param->child->right_sibling->value_id.name)) {
- any_error = true;
- fprintf (stderr, ERROR("ID `%s' redeclared."),
- param->child->right_sibling->line_number, param->child->right_sibling->value_id.name);
- continue;
- }
- ccmmc_symbol_table_insert(table, param->child->right_sibling->value_id.name, CCMMC_SYMBOL_KIND_VARIABLE, func->type.param_list[i]);
- }
-
- // Process the list of local declarations
-
- // Process the list of statements
-
- return any_error;
+ // process the function block
+ return process_block(func_decl->child->right_sibling->right_sibling->right_sibling,
+ table) || any_error;
}
static bool process_program(CcmmcAst *program, CcmmcSymbolTable *table)