This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compound_statement_parser.c
134 lines (118 loc) · 3.04 KB
/
compound_statement_parser.c
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
#include <assert.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "assignment_parser.h"
#include "ast.h"
#include "branch_parser.h"
#include "buffer.h"
#include "compound_statement_parser.h"
#include "declaration_parser.h"
#include "function_call_parser.h"
#include "lexer.h"
#include "loop_parser.h"
#include "return_parser.h"
#include "symbol.h"
#include "unary_statement_parser.h"
#include "utils.h"
ast_t* statement_parser(buffer_t* buffer, symbol_t** global_table, symbol_t** local_table)
{
char c = buf_getchar_rollback(buffer);
if (isalpha(c))
{
char* lexem = lexer_getalphanum_rollback(buffer);
if (sym_search(*local_table, lexem) != NULL)
{
buf_lock(buffer);
int pos = buffer->it;
buf_forward(buffer, strlen(lexem));
char c = buf_getchar_rollback(buffer);
buf_rollback_and_unlock(buffer, buffer->it - pos);
if (c == '=')
{
return assignment_parser(buffer, global_table, local_table);
}
else if (c == '+' || c == '-')
{
ast_t* unary = unary_parser(buffer, global_table, local_table);
if (buf_getchar_after_blank(buffer) != ';')
{
parse_error("Invalid token, expected ';'.", buffer, 1);
}
return unary;
}
else
{
parse_error("Invalid token.", buffer, 1);
}
}
else if (strcmp(lexem, "si") == 0)
{
return branch_parser(buffer, global_table, local_table);
}
else if (strcmp(lexem, "entier") == 0 || strcmp(lexem, "booleen") == 0)
{
return declaration_parser(buffer, global_table, local_table);
}
else if (sym_search(*global_table, lexem) != NULL)
{
ast_t* call = function_call_parser(buffer, global_table, local_table);
if (buf_getchar_after_blank(buffer) != ';')
{
parse_error("Invalid token, expected ';'.", buffer, 1);
}
return call;
}
else if (strcmp(lexem, "tantque") == 0)
{
return loop_parser(buffer, global_table, local_table);
}
else if (strcmp(lexem, "retourner") == 0)
{
return return_parser(buffer, global_table, local_table);
}
else
{
parse_error("Invalid token.", buffer, 1);
}
free(lexem);
}
else if (c == '+' || c == '-')
{
ast_t* unary = unary_parser(buffer, global_table, local_table);
if (buf_getchar_after_blank(buffer) != ';')
{
parse_error("Invalid token, expected ';'.", buffer, 1);
}
return unary;
}
else if (c == '{')
{
return compound_statement_parser(buffer, global_table, local_table);
}
parse_error("Invalid token.", buffer, 1);
}
ast_t* compound_statement_parser(buffer_t* buffer, symbol_t** global_table, symbol_t** local_table)
{
if (buf_getchar_after_blank(buffer) != '{')
{
parse_error("Invalid token, expected '{'.", buffer, 1);
}
ast_list_t* statements = NULL;
while (true)
{
char c = buf_getchar_rollback(buffer);
if (c == '}')
{
break;
}
ast_t* ast = statement_parser(buffer, global_table, local_table);
assert(ast != NULL);
statements = ast_list_add(statements, ast);
}
if (buf_getchar_after_blank(buffer) != '}')
{
parse_error("Invalid token, expected '}'.", buffer, 1);
}
return ast_new_comp_stmt(statements);
}