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
|
#include <err.h>
#include <stdio.h>
#include "parser/parser_functions.h"
#include "parser/parser_utils.h"
struct ast *parse_list(struct lexer *lexer, enum parser_state *state)
{
struct ast *node = parse_and_or(lexer, state);
struct ast *ast_list = NULL;
// Who forgot to check that node can be NULL, I sure did
if (node == NULL)
{
QUICK_CLEANUP
}
struct token next = lexer_peek(lexer);
size_t i = 0;
if (next.type == TOKEN_SEMICOLON)
{
ast_list = ast_create(AST_LIST);
if (ast_list == NULL)
{
cleanup(node, state);
return NULL;
}
set_left(ast_list, node);
i++;
}
// If we enter this loop, this means we have a list of more than 1 element
while (next.type == TOKEN_SEMICOLON)
{
lexer_pop(lexer);
next = lexer_peek(lexer);
// Case where we have only one ';', meaning it is the end of the list
if (next.type == TOKEN_EOF || next.type == TOKEN_NEWLINE)
{
// set_right(ast_list, NULL);
return ast_list;
}
struct ast *right = parse_and_or(lexer, state);
set_i(ast_list, right, i);
i++;
// Was checking if right was NULL, but in that case state is ERROR
// and error_check will catch it
next = lexer_peek(lexer);
if (error_check(ast_list, state, next))
{
return NULL;
}
}
return (ast_list == NULL) ? node : ast_list;
}
struct ast *parse_compound_list(struct lexer *lexer, enum parser_state *state)
{
clean_cons_tokens(TOKEN_NEWLINE, lexer);
struct ast *node = parse_and_or(lexer, state);
struct ast *ast_lst = NULL;
struct token next = lexer_peek(lexer);
if (error_check(node, state, next))
{
return NULL;
}
size_t i = 0;
while (next.type == TOKEN_SEMICOLON || next.type == TOKEN_NEWLINE)
{
lexer_pop(lexer);
clean_cons_tokens(TOKEN_NEWLINE, lexer);
next = lexer_peek(lexer);
// This condition might need to be expanded in the future
// But I don't know if there is a better way to do this
if (next.type == TOKEN_THEN || next.type == TOKEN_FI
|| next.type == TOKEN_ELSE || next.type == TOKEN_ELIF
|| next.type == TOKEN_DO || next.type == TOKEN_DONE
|| next.type == TOKEN_CURLY_RIGHT || next.type == TOKEN_PAR_LEFT)
{
return (ast_lst == NULL) ? node : ast_lst;
}
if (ast_lst == NULL)
{
ast_lst = ast_create(AST_LIST);
if (ast_lst == NULL)
{
cleanup(node, state);
return NULL;
}
set_left(ast_lst, node);
i++;
}
next = lexer_peek(lexer);
struct ast *right = parse_and_or(lexer, state);
// Can put the i++ in the set_i function but I'm too scared
set_i(ast_lst, right, i);
i++;
next = lexer_peek(lexer);
if (error_check(ast_lst, state, next))
{
return NULL;
}
}
return (ast_lst == NULL) ? node : ast_lst;
}
|