parser+codegen: Add support for 'return' statement
Signed-off-by: Ian Moffett <ian@mirocom.org>
This commit is contained in:
@@ -25,6 +25,48 @@ resolve_func(struct rifle_state *state, struct ast_node *root)
|
||||
return mu_gen_label(state, symbol->name, symbol->pub);
|
||||
}
|
||||
|
||||
static int
|
||||
resolve_return(struct rifle_state *state, struct ast_node *root)
|
||||
{
|
||||
struct symbol *func_sym;
|
||||
struct ast_node *rhs;
|
||||
data_type_t type;
|
||||
|
||||
if (state == NULL || root == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((func_sym = state->cur_func) == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (root->type != AST_RETURN) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((rhs = root->right) == NULL) {
|
||||
trace_error(state, "internal: return has no rhs\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
type = data_raw_type(&func_sym->dtype) ;
|
||||
switch (rhs->type) {
|
||||
case AST_NUMBER:
|
||||
mu_gen_retimm(
|
||||
state,
|
||||
rhs->v,
|
||||
type_to_msize(type)
|
||||
);
|
||||
|
||||
break;
|
||||
default:
|
||||
trace_error(state, "internal: bad ast node in return\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
cg_resolve_node(struct rifle_state *state, struct ast_node *root)
|
||||
{
|
||||
@@ -38,6 +80,12 @@ cg_resolve_node(struct rifle_state *state, struct ast_node *root)
|
||||
return -1;
|
||||
}
|
||||
|
||||
break;
|
||||
case AST_RETURN:
|
||||
if (resolve_return(state, root) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
break;
|
||||
default:
|
||||
trace_error(state, "unknown ast node %d\n", root->type);
|
||||
|
||||
Reference in New Issue
Block a user