summaryrefslogtreecommitdiff
path: root/42sh/src/parser/parser_fundec.c
diff options
context:
space:
mode:
Diffstat (limited to '42sh/src/parser/parser_fundec.c')
-rw-r--r--42sh/src/parser/parser_fundec.c63
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;
+}