-
Notifications
You must be signed in to change notification settings - Fork 2
/
gen_utils.c
116 lines (103 loc) · 2.83 KB
/
gen_utils.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
/*
Group 36
2017B4A70495P Manan Soni
2017B4A70549P Siddharth Singh
2017B4A70636P Nayan Khanna
2017B4A70740P Aditya Tulsyan
*/
#include "linked_list.h"
#include <stdio.h>
#include "gen_utils.h"
#include "grammar_structs.h"
#include "parse_tree.h"
#include <stdlib.h>
linked_list * all_errors = NULL;
void assert(bool condition, char *error_string) {
if (!condition) {
printf("ERROR: Assertion failed - %s\n", error_string);
exit(1);
}
}
void printErrorsHeader()
{
printf("\n%45s","|Error Statements Table|");
printf("\n|%5s|%10s|%10s|%10s|%10s|%20s|%10s|%5s|%40s|\n",
"num", "stmt type", "operator", "lexeme 1", "type 1",
"lexeme 2", "type 2", "depth", "error message");
}
void printErrorEntries(ErrorNode* err){
if (err != NULL)
{
printf("|"KCYN"%5d"RESET"|%10s|%10s|%10s|"KRED"%10s"RESET"|%20s|"KRED"%10s"RESET"|%5d|"KRED"%40s"RESET"|\n",
err->line_num, err->stmt_type, err->op, err->lex1, err->type1,
err->lex2, err->type2, err->depth, err->message);
}
}
char *getStmtType(Parse_tree_node *p) {
while (p || p->depth >1) {
switch (p->tok->lexeme.s) {
case decl_stmts: {
return "decl stmt";
break;
}
case assign_stmts: {
return "assgn stmt";
break;
}
case main_program : {
return "UNKNOWN";
break;
}
}
p = p->parent;
}
return "UNKNOWN";
}
void init_errors(){
if(all_errors){
ll_node* head = all_errors->head;
while(head){
all_errors->head = all_errors->head->next;
free(head);
head = all_errors->head;
}
free(all_errors);
}
all_errors = create_linked_list();
}
bool assert_debug(bool condition, char *error_string, Parse_tree_node *p,
char *t1, char *t2, char *operator, char *lex1,
char *lex2) {
if(!condition){
ErrorNode *er = (ErrorNode *)malloc(sizeof(ErrorNode));
er->depth = p->depth;
er->message = error_string;
er->stmt_type = getStmtType(p);
er->type1 = t1;
er->type2 = t2;
er->lex1 = lex1;
er->lex2 = lex2;
er->op = operator;
Parse_tree_node *temp1 = (Parse_tree_node *)calloc(1, sizeof(Parse_tree_node));
temp1 = p;
while (!temp1->tok->lexeme.is_terminal)
{
temp1 = temp1->child;
}
er->line_num = temp1->tok->line_no;
//printErrorEntries(er);
if(!all_errors)
init_errors();
ll_append(all_errors, er);
return false;
}
return true;
}
void print_all_errors(){
if(!all_errors)
return;
printErrorsHeader();
for(ll_node *head = all_errors->head; head; head=head->next){
printErrorEntries((ErrorNode *)head->data);
}
}