added return statement
This commit is contained in:
@@ -8,6 +8,7 @@ typedef enum {
|
||||
NODE_EXPR,
|
||||
NODE_IFELSE,
|
||||
NODE_COMP,
|
||||
NODE_RETURN,
|
||||
} NodeKind;
|
||||
|
||||
typedef enum {
|
||||
@@ -86,4 +87,9 @@ typedef struct {
|
||||
Node base;
|
||||
Node *head;
|
||||
} CompoundNode;
|
||||
|
||||
typedef struct {
|
||||
Node base;
|
||||
Node *val;
|
||||
} ReturnNode;
|
||||
#endif
|
||||
@@ -205,6 +205,19 @@ static _Bool emit_instr_for_node(ExeBuilder *exeb, Node *node, Error *error)
|
||||
return 1;
|
||||
}
|
||||
|
||||
case NODE_RETURN:
|
||||
{
|
||||
ReturnNode *ret = (ReturnNode*) node;
|
||||
|
||||
if(!emit_instr_for_node(exeb, ret->val, error))
|
||||
return 0;
|
||||
|
||||
if(!ExeBuilder_Append(exeb, error, OPCODE_RETURN, NULL, 0, ret->base.offset, ret->base.length))
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
default:
|
||||
UNREACHABLE;
|
||||
return 0;
|
||||
|
||||
@@ -29,6 +29,7 @@ typedef enum {
|
||||
TKWNONE,
|
||||
TKWTRUE,
|
||||
TKWFALSE,
|
||||
TKWRETURN,
|
||||
} TokenKind;
|
||||
|
||||
typedef struct Token Token;
|
||||
@@ -140,6 +141,10 @@ AST *parse(Source *src, BPAlloc *alloc, Error *error)
|
||||
{
|
||||
tok->kind = TKWFALSE;
|
||||
}
|
||||
else if(matchstr(str + tok->offset, tok->length, "return"))
|
||||
{
|
||||
tok->kind = TKWRETURN;
|
||||
}
|
||||
|
||||
#undef matchstr
|
||||
}
|
||||
@@ -366,6 +371,32 @@ static Node *parse_statement(Context *ctx)
|
||||
return node;
|
||||
}
|
||||
|
||||
case TKWRETURN:
|
||||
{
|
||||
int offset = current_token(ctx)->offset;
|
||||
|
||||
next(ctx); // Consume the "return" keyword.
|
||||
|
||||
Node *val = parse_expression_statement(ctx);
|
||||
|
||||
if(val == NULL)
|
||||
return NULL;
|
||||
|
||||
ReturnNode *node = BPAlloc_Malloc(ctx->alloc, sizeof(ReturnNode));
|
||||
|
||||
if(node == NULL)
|
||||
{
|
||||
Error_Report(ctx->error, 1, "No memory");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
node->base.kind = NODE_RETURN;
|
||||
node->base.next = NULL;
|
||||
node->base.offset = offset;
|
||||
node->base.length = val->offset + val->length - offset;
|
||||
node->val = val;
|
||||
return (Node*) node;
|
||||
}
|
||||
}
|
||||
|
||||
Error_Report(ctx->error, 0, "Got token \"%.*s\" where the start of a statement was expected",
|
||||
|
||||
Reference in New Issue
Block a user