From e927a4b22115e0461db6feeee891256b10650c0e Mon Sep 17 00:00:00 2001 From: Johnny Richard Date: Fri, 14 Apr 2023 23:10:25 +0200 Subject: parser: Create parser for function with return statements This is a very limited parser implementation which parses a single function with return type i32 and body containing a return number statement. The parser doesn't show the 'filepath:row:col' when it fails, a future improvement would be display it to easy find where the compilation problem is located. The ast_nodes are taking the token.value ownership (which is a really bad design since not all token.value ownership has been taken causing memory leaking) but we never free them. For a future fix we could use a string_view instead since we never change the original source code. The string_view will also improve the performance a lot avoiding unnecessary heap memory allocation. Signed-off-by: Johnny Richard --- src/lexer.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'src/lexer.c') diff --git a/src/lexer.c b/src/lexer.c index 14165ae..0327ad5 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -198,3 +198,30 @@ lexer_current_char(lexer_t *lexer) return lexer->src[lexer->cur]; } +char * +token_kind_to_str(token_kind_t kind) +{ + switch (kind) { + case TOKEN_NAME: + return "TOKEN_NAME"; + case TOKEN_OPAREN: + return "("; + case TOKEN_CPAREN: + return ")"; + case TOKEN_COLON: + return ":"; + case TOKEN_SEMICOLON: + return ";"; + case TOKEN_OCURLY: + return "{"; + case TOKEN_CCURLY: + return "}"; + case TOKEN_NUMBER: + return "TOKEN_NUMBER"; + case TOKEN_EOF: + return "TOKEN_EOF"; + default: + return "UNKNOW_TOKEN"; + } +} + -- cgit v1.2.3