Adding more cleanup; checking whether or not a return has the right type

This commit is contained in:
Wolfgang Bumiller 2012-07-28 16:06:44 +02:00
parent a3919a6074
commit ae12583ada

129
parser.c
View file

@ -17,6 +17,8 @@ typedef struct {
ast_function *function; ast_function *function;
MEM_VECTOR_MAKE(ast_value*, locals); MEM_VECTOR_MAKE(ast_value*, locals);
size_t blocklocal; size_t blocklocal;
size_t errors;
} parser_t; } parser_t;
MEM_VEC_FUNCTIONS(parser_t, ast_value*, globals) MEM_VEC_FUNCTIONS(parser_t, ast_value*, globals)
@ -30,6 +32,8 @@ void parseerror(parser_t *parser, const char *fmt, ...)
{ {
va_list ap; va_list ap;
parser->errors++;
if (parser) if (parser)
printf("error %s:%lu: ", parser->lex->tok->ctx.file, (unsigned long)parser->lex->tok->ctx.line); printf("error %s:%lu: ", parser->lex->tok->ctx.file, (unsigned long)parser->lex->tok->ctx.line);
else else
@ -506,8 +510,6 @@ static bool parser_close_call(parser_t *parser, shunt *sy)
return false; return false;
} }
printf("fid = %i, out_count = %i\n", (int)fid, (int)sy->out_count);
if (fid+1 == sy->out_count) { if (fid+1 == sy->out_count) {
/* no arguments */ /* no arguments */
paramcount = 0; paramcount = 0;
@ -768,25 +770,39 @@ static bool parser_body_do(parser_t *parser, ast_block *block)
{ {
if (!strcmp(parser_tokval(parser), "return")) if (!strcmp(parser_tokval(parser), "return"))
{ {
ast_expression *exp; ast_expression *exp = NULL;
ast_return *ret; ast_return *ret = NULL;
ast_value *expected = parser->function->vtype;
if (!parser_next(parser)) { if (!parser_next(parser)) {
parseerror(parser, "expected return expression"); parseerror(parser, "expected return expression");
return false; return false;
} }
exp = parser_expression(parser); if (parser->tok != ';') {
if (!exp) exp = parser_expression(parser);
return false; if (!exp)
ret = ast_return_new(exp->expression.node.context, exp); return false;
if (!ret) {
ast_delete(exp); if (exp->expression.vtype != expected->expression.next->expression.vtype) {
return false; parseerror(parser, "return with invalid expression");
} }
if (!ast_block_exprs_add(block, (ast_expression*)ret)) {
ast_delete(ret); ret = ast_return_new(exp->expression.node.context, exp);
return false; if (!ret) {
ast_delete(exp);
return false;
}
if (!ast_block_exprs_add(block, (ast_expression*)ret)) {
ast_delete(ret);
return false;
}
} else if (!parser_next(parser)) {
parseerror(parser, "expected semicolon");
if (expected->expression.next->expression.vtype != TYPE_VOID) {
parseerror(parser, "return without value");
}
} }
return true; return true;
} }
@ -1098,51 +1114,68 @@ bool parser_compile(const char *filename, const char *datfile)
lex_close(parser->lex); lex_close(parser->lex);
ir = ir_builder_new("gmqcc_out"); if (!parser->errors)
if (!ir) { {
printf("failed to allocate builder\n"); ir = ir_builder_new("gmqcc_out");
goto cleanup; if (!ir) {
} printf("failed to allocate builder\n");
goto cleanup;
}
for (i = 0; i < parser->imm_float_count; ++i) { for (i = 0; i < parser->imm_float_count; ++i) {
if (!ast_global_codegen(parser->imm_float[i], ir)) { if (!ast_global_codegen(parser->imm_float[i], ir)) {
printf("failed to generate global %s\n", parser->imm_float[i]->name); printf("failed to generate global %s\n", parser->imm_float[i]->name);
}
} }
} for (i = 0; i < parser->imm_string_count; ++i) {
for (i = 0; i < parser->imm_string_count; ++i) { if (!ast_global_codegen(parser->imm_string[i], ir)) {
if (!ast_global_codegen(parser->imm_string[i], ir)) { printf("failed to generate global %s\n", parser->imm_string[i]->name);
printf("failed to generate global %s\n", parser->imm_string[i]->name); }
} }
} for (i = 0; i < parser->imm_vector_count; ++i) {
for (i = 0; i < parser->imm_vector_count; ++i) { if (!ast_global_codegen(parser->imm_vector[i], ir)) {
if (!ast_global_codegen(parser->imm_vector[i], ir)) { printf("failed to generate global %s\n", parser->imm_vector[i]->name);
printf("failed to generate global %s\n", parser->imm_vector[i]->name); }
} }
} for (i = 0; i < parser->globals_count; ++i) {
for (i = 0; i < parser->globals_count; ++i) { if (!ast_global_codegen(parser->globals[i], ir)) {
if (!ast_global_codegen(parser->globals[i], ir)) { printf("failed to generate global %s\n", parser->globals[i]->name);
printf("failed to generate global %s\n", parser->globals[i]->name); }
} }
} for (i = 0; i < parser->functions_count; ++i) {
for (i = 0; i < parser->functions_count; ++i) { if (!ast_function_codegen(parser->functions[i], ir)) {
if (!ast_function_codegen(parser->functions[i], ir)) { printf("failed to generate function %s\n", parser->functions[i]->name);
printf("failed to generate function %s\n", parser->functions[i]->name); }
if (!ir_function_finalize(parser->functions[i]->ir_func)) {
printf("failed to finalize function %s\n", parser->functions[i]->name);
}
} }
if (!ir_function_finalize(parser->functions[i]->ir_func)) {
printf("failed to finalize function %s\n", parser->functions[i]->name);
}
}
ir_builder_dump(ir, printf); ir_builder_dump(ir, printf);
if (!ir_builder_generate(ir, datfile)) if (!ir_builder_generate(ir, datfile))
printf("*** failed to generate output file\n"); printf("*** failed to generate output file\n");
ir_builder_delete(ir); ir_builder_delete(ir);
} else {
printf("*** there were compile errors\n");
}
cleanup: cleanup:
for (i = 0; i < parser->functions_count; ++i) {
ast_delete(parser->functions[i]);
}
for (i = 0; i < parser->imm_vector_count; ++i) {
ast_delete(parser->imm_vector[i]);
}
for (i = 0; i < parser->imm_string_count; ++i) {
ast_delete(parser->imm_string[i]);
}
for (i = 0; i < parser->imm_float_count; ++i) {
ast_delete(parser->imm_float[i]);
}
for (i = 0; i < parser->globals_count; ++i) { for (i = 0; i < parser->globals_count; ++i) {
ast_value_delete(parser->globals[i]); ast_delete(parser->globals[i]);
} }
MEM_VECTOR_CLEAR(parser, globals); MEM_VECTOR_CLEAR(parser, globals);