summaryrefslogtreecommitdiff
path: root/42sh/src/parser/parser_conditionnals.c
blob: ef1f67a2258092a3169c9220781e765aecf330f3 (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "parser/parser_functions.h"
#include "parser/parser_utils.h"

struct ast *parse_rule_if(struct lexer *lexer, enum parser_state *state)
{
    struct token next = lexer_peek(lexer);
    // Not mandatory because we checked before going in here
    if (next.type != TOKEN_IF)
    {
        QUICK_CLEANUP
    }

    struct ast *root = ast_create(AST_IF);

    if (root == NULL)
    {
        *state = ERROR;
        return NULL;
    }

    lexer_pop(lexer);

    struct ast *cond = parse_compound_list(lexer, state);

    next = lexer_peek(lexer);
    if (error_check(root, state, next))
    {
        return NULL;
    }

    set_left(root, cond);

    if (next.type != TOKEN_THEN)
    {
        cleanup(root, state);
        return NULL;
    }

    lexer_pop(lexer);

    cond = parse_compound_list(lexer, state);
    next = lexer_peek(lexer);
    if (error_check(root, state, next))
    {
        return NULL;
    }

    if (cond == NULL)
    {
        cleanup(root, state);
        return NULL;
    }

    set_right(root, cond);

    if (next.type != TOKEN_FI)
    {
        cond = parse_else_clause(lexer, state);
        if (cond == NULL)
        {
            cleanup(root, state);
            return NULL;
        }
        set_i(root, cond, 2);
    }

    lexer_pop(lexer);

    return root;
}

struct ast *parse_else_clause(struct lexer *lexer, enum parser_state *state)
{
    struct token next = lexer_peek(lexer);
    struct ast *root = NULL;

    if (next.type == TOKEN_ELSE)
    {
        lexer_pop(lexer);

        struct ast *cond = parse_compound_list(lexer, state);

        if (cond == NULL)
        {
            cleanup(root, state);
            return NULL;
        }

        if (error_check(cond, state, next))
        {
            return NULL;
        }

        return cond;
    }
    else if (next.type != TOKEN_ELIF)
    {
        cleanup(NULL, state);
        return NULL;
    }

    lexer_pop(lexer);

    // Doesn' it seems stupid to just manually re-creating the IF again?
    // Can't I just call parse_if there ? But we won't be following the grammar
    // This is a late-game optimization that might not be worth it
    root = ast_create(AST_IF);
    if (root == NULL)
    {
        cleanup(root, state);
        return NULL;
    }

    struct ast *cond = parse_compound_list(lexer, state);
    set_left(root, cond);

    if (error_check(root, state, next))
    {
        return NULL;
    }

    next = lexer_peek(lexer);

    if (next.type != TOKEN_THEN)
    {
        cleanup(root, state);
        return NULL;
    }

    lexer_pop(lexer);

    set_right(root, parse_compound_list(lexer, state));

    if (error_check(root, state, next))
    {
        return NULL;
    }

    next = lexer_peek(lexer);

    if (next.type == TOKEN_ELSE || next.type == TOKEN_ELIF)
    {
        cond = parse_else_clause(lexer, state);
        if (error_check(root, state, next))
        {
            return NULL;
        }
        set_i(root, cond, 2);
    }

    return root;
}