summaryrefslogtreecommitdiff
path: root/42sh/src/helper.c
blob: 7f936fc5249f58c8fcb90e619623e6757912a130 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "helper.h"

int _process_input(struct string *input)
{
    struct lexer *lexer = lexer_new(input);
    if (!lexer)
    {
        errx(BAD_GRAMMAR, "main: unable to create lexer.");
    }

    enum parser_state status = OK;

    struct ast *ast = NULL;
    int r = SHELL_TRUE;
    do
    {
        ast_free(ast);
        ast = parse(lexer, &status);

        if (status == ERROR)
        {
            errx(BAD_GRAMMAR, "main: parse failed.");
        }

        if (env_get("PRETTY_PRINT") != NULL)
        {
            pretty_print(ast);
        }
        if (ast)
        {
            r = eval_ast(ast);
        }
    } while (ast != NULL && status != ERROR);
    if (status == ERROR)
    {
        ast_free(ast);
    }

    string_free(lexer->input);
    lexer_free(lexer);

    return r;
}