added return statement

This commit is contained in:
Francesco Cozzuto
2021-10-31 16:05:43 +01:00
parent 88efd9515c
commit 7f37c51f98
5 changed files with 108 additions and 5 deletions
+31
View File
@@ -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",