diff options
author | Ting-Wei Lan <lantw44@gmail.com> | 2015-12-10 17:37:25 +0800 |
---|---|---|
committer | Ting-Wei Lan <lantw44@gmail.com> | 2015-12-10 17:58:28 +0800 |
commit | 911d19623ae4aca89b5fade6ae6be15d45ac05af (patch) | |
tree | e1ab0bb5cfdf9d35ecc07bdc6bc0eadd08e50a84 | |
parent | 10e3ec33973c6ff2ddc26a71f34c4fcd5f00e67f (diff) | |
download | compiler2015-911d19623ae4aca89b5fade6ae6be15d45ac05af.tar compiler2015-911d19623ae4aca89b5fade6ae6be15d45ac05af.tar.gz compiler2015-911d19623ae4aca89b5fade6ae6be15d45ac05af.tar.bz2 compiler2015-911d19623ae4aca89b5fade6ae6be15d45ac05af.tar.lz compiler2015-911d19623ae4aca89b5fade6ae6be15d45ac05af.tar.xz compiler2015-911d19623ae4aca89b5fade6ae6be15d45ac05af.tar.zst compiler2015-911d19623ae4aca89b5fade6ae6be15d45ac05af.zip |
Handle builtin functions: read, fread, write
-rw-r--r-- | src/semantic-analysis.c | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/semantic-analysis.c b/src/semantic-analysis.c index 758c01a..ecfca31 100644 --- a/src/semantic-analysis.c +++ b/src/semantic-analysis.c @@ -398,6 +398,19 @@ static bool check_call(CcmmcAst *call, CcmmcSymbolTable *table) return true; } + // Check special function: write + if (strcmp(func->name, "write") == 0) { + CcmmcAst *arg = call->child->right_sibling->child; + call->type_value = func->type.type_base; + if (arg->type_node == CCMMC_AST_NODE_CONST_VALUE && + arg->value_const.kind == CCMMC_KIND_CONST_STRING) { + arg->type_value = CCMMC_AST_VALUE_CONST_STRING; + return any_error; + } + any_error = check_relop_expr(arg, table) || any_error; + return any_error; + } + // Check each parameter param = call->child->right_sibling->child; size_t i = 0; @@ -966,6 +979,18 @@ bool ccmmc_semantic_check(CcmmcAst *root, CcmmcSymbolTable *table) (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 builtin functions + ccmmc_symbol_table_insert(table, "read", CCMMC_SYMBOL_KIND_FUNCTION, + (CcmmcSymbolType){ .type_base = CCMMC_AST_VALUE_INT, + .param_valid = true, .param_count = 0 }); + ccmmc_symbol_table_insert(table, "fread", CCMMC_SYMBOL_KIND_FUNCTION, + (CcmmcSymbolType){ .type_base = CCMMC_AST_VALUE_FLOAT, + .param_valid = true, .param_count = 0 }); + ccmmc_symbol_table_insert(table, "write", CCMMC_SYMBOL_KIND_FUNCTION, + (CcmmcSymbolType){ .type_base = CCMMC_AST_VALUE_VOID, + .param_valid = true, .param_count = 1, + .param_list = (CcmmcSymbolType[]){ + { .type_base = CCMMC_AST_VALUE_INT }}}); // Start processing from the program node any_error = process_program(root, table) || any_error; return !any_error; |