mirror of
https://github.com/DarkPlacesEngine/gmqcc.git
synced 2024-11-30 15:41:12 +00:00
handling function definitions better, generating functions
This commit is contained in:
parent
6a346745aa
commit
38edf7c1bd
1 changed files with 54 additions and 14 deletions
68
parser.c
68
parser.c
|
@ -9,9 +9,11 @@ typedef struct {
|
||||||
int tok;
|
int tok;
|
||||||
|
|
||||||
MEM_VECTOR_MAKE(ast_value*, globals);
|
MEM_VECTOR_MAKE(ast_value*, globals);
|
||||||
|
MEM_VECTOR_MAKE(ast_function*, functions);
|
||||||
} parser_t;
|
} parser_t;
|
||||||
|
|
||||||
MEM_VEC_FUNCTIONS(parser_t, ast_value*, globals)
|
MEM_VEC_FUNCTIONS(parser_t, ast_value*, globals)
|
||||||
|
MEM_VEC_FUNCTIONS(parser_t, ast_function*, functions)
|
||||||
|
|
||||||
void parseerror(parser_t *parser, const char *fmt, ...)
|
void parseerror(parser_t *parser, const char *fmt, ...)
|
||||||
{
|
{
|
||||||
|
@ -125,6 +127,7 @@ bool parser_do(parser_t *parser)
|
||||||
{
|
{
|
||||||
if (parser->tok == TOKEN_TYPENAME)
|
if (parser->tok == TOKEN_TYPENAME)
|
||||||
{
|
{
|
||||||
|
ast_function *func = NULL;
|
||||||
lex_ctx ctx = parser_ctx(parser);
|
lex_ctx ctx = parser_ctx(parser);
|
||||||
ast_value *var = parser_parse_type(parser);
|
ast_value *var = parser_parse_type(parser);
|
||||||
if (!var)
|
if (!var)
|
||||||
|
@ -147,6 +150,35 @@ bool parser_do(parser_t *parser)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (var->params_count) {
|
||||||
|
/* a function was defined */
|
||||||
|
ast_value *fval;
|
||||||
|
|
||||||
|
/* turn var into a value of TYPE_FUNCTION, with the old var
|
||||||
|
* as return type
|
||||||
|
*/
|
||||||
|
fval = ast_value_new(ctx, var->name, TYPE_FUNCTION);
|
||||||
|
func = ast_function_new(ctx, var->name, fval);
|
||||||
|
if (!fval || !func) {
|
||||||
|
ast_value_delete(var);
|
||||||
|
if (fval) ast_value_delete(fval);
|
||||||
|
if (func) ast_function_delete(func);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
fval->expression.next = (ast_expression*)var;
|
||||||
|
MEM_VECTOR_MOVE(var, params, func, params);
|
||||||
|
|
||||||
|
if (!parser_t_functions_add(parser, func)) {
|
||||||
|
ast_value_delete(var);
|
||||||
|
if (fval) ast_value_delete(fval);
|
||||||
|
if (func) ast_function_delete(func);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
var = fval;
|
||||||
|
}
|
||||||
|
|
||||||
if (!parser_t_globals_add(parser, var) ||
|
if (!parser_t_globals_add(parser, var) ||
|
||||||
!parser_next(parser))
|
!parser_next(parser))
|
||||||
{
|
{
|
||||||
|
@ -169,27 +201,27 @@ bool parser_do(parser_t *parser)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (parser->tok == '#') {
|
if (parser->tok == '#') {
|
||||||
/* builtin function */
|
if (!func) {
|
||||||
ast_function *func;
|
parseerror(parser, "unexpected builtin number, '%s' is not a function", var->name);
|
||||||
ast_value *fval;
|
return false;
|
||||||
|
}
|
||||||
if (!parser_next(parser)) {
|
if (!parser_next(parser)) {
|
||||||
parseerror(parser, "expected builtin number");
|
parseerror(parser, "expected builtin number");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if (parser->tok != TOKEN_INTCONST) {
|
||||||
fval = ast_value_new(ctx, var->name, TYPE_FUNCTION);
|
parseerror(parser, "builtin number must be an integer constant");
|
||||||
func = ast_function_new(ctx, var->name, fval);
|
return false;
|
||||||
if (!fval || !func) {
|
}
|
||||||
if (fval) ast_value_delete(fval);
|
if (parser_token(parser)->constval.i <= 0) {
|
||||||
if (func) ast_function_delete(func);
|
parseerror(parser, "builtin number must be positive integer greater than zero");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
fval->expression.next = (ast_expression*)var;
|
if (func)
|
||||||
MEM_VECTOR_MOVE(var, params, fval, params);
|
func->builtin = parser_token(parser)->constval.i;
|
||||||
|
} else if (parser->tok == '{') {
|
||||||
/* replace the variable */
|
/* function body */
|
||||||
parser->globals[parser->globals_count-1] = fval;
|
|
||||||
} else {
|
} else {
|
||||||
parseerror(parser, "TODO, const assignment");
|
parseerror(parser, "TODO, const assignment");
|
||||||
}
|
}
|
||||||
|
@ -272,6 +304,14 @@ bool parser_compile(const char *filename)
|
||||||
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) {
|
||||||
|
if (!ast_function_codegen(parser->functions[i], ir)) {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ir_builder_dump(ir, printf);
|
ir_builder_dump(ir, printf);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue