diff options
| author | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:07:58 +0200 |
|---|---|---|
| committer | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:07:58 +0200 |
| commit | 967be9e750221ab2ab783f95df79bb26d290a45e (patch) | |
| tree | 6802900a5e975f9f68b169f0f503f040056d6952 /42sh/src/parser/parser_fundec.c | |
Diffstat (limited to '42sh/src/parser/parser_fundec.c')
| -rw-r--r-- | 42sh/src/parser/parser_fundec.c | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/42sh/src/parser/parser_fundec.c b/42sh/src/parser/parser_fundec.c new file mode 100644 index 0000000..08d4816 --- /dev/null +++ b/42sh/src/parser/parser_fundec.c @@ -0,0 +1,63 @@ +#include "parser/parser_functions.h" +#include "parser/parser_utils.h" + +struct ast *parse_funcdec(struct lexer *lexer, enum parser_state *state, + struct string *txt) +{ + struct ast *function = ast_create(AST_FUNCTION); + if (function == NULL) + { + QUICK_CLEANUP + } + + struct token next = lexer_peek(lexer); + + if (txt == NULL && next.type != TOKEN_WORD) + { + cleanup(function, state); + return NULL; + } + else if (txt == NULL) + { + ((struct ast_function *)function)->name = next.value; + lexer_pop(lexer); + next = lexer_peek(lexer); + } + else + { + // If we used the word in the txt variable, no need to peek + ((struct ast_function *)function)->name = txt; + } + + if (next.type != TOKEN_PAR_LEFT) + { + cleanup(function, state); + return NULL; + } + + lexer_pop(lexer); + next = lexer_peek(lexer); + + if (next.type != TOKEN_PAR_RIGHT) + { + cleanup(function, state); + return NULL; + } + + lexer_pop(lexer); + next = lexer_peek(lexer); + + clean_cons_tokens(TOKEN_NEWLINE, lexer); + + struct ast *body = parse_shell_command(lexer, state); + next = lexer_peek(lexer); + + if (error_check(function, state, next)) + { + return NULL; + } + + set_left(function, body); + + return function; +} |
