-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsemantic.c
72 lines (58 loc) · 1.67 KB
/
semantic.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
#include <stdio.h>
#include <stdlib.h>
#include "header.h"
void visitor_leaf_first(syntno **root, visitor_action act) {
syntno *r = *root;
for(int i = 0; i < r->childcount; i++) {
visitor_leaf_first(&r->children[i], act);
if (act)
act(root, r->children[i]);
}
}
void collapse_stmts(syntno **root, syntno *no) {
syntno *or = *root;
if (or->type == NO_STMTS && no->type == NO_STMTS) {
//printf("%d: P%d\n", no->id, or->id);
int nsize = sizeof(syntno);
nsize += sizeof(syntno*) * (or->childcount-1);
nsize += sizeof(syntno*) * (no->childcount-1);
// aloca mais espaco para os novos filhos
or = *root = realloc(*root, nsize);
or->childcount--;
for(int i = 0; i < no->childcount; i++) {
// or->children[or->childcount++] =
// no->children[i];
or->children[or->childcount] =
no->children[i];
or->childcount++;
}
free(no);
}
}
void declared_vars(syntno **root, syntno *no) {
// se o pai não é NO_ATTR e o no é do tipo
// NO_TOK e 'V', verifica a existencia na tabela de símbolos
syntno *nr = *root;
// se o nó é NO_ATTR, marca a variável como declarada
if (no->type == NO_ATTR) {
targs *args = no->children[0]->token_args;
int s = search_symbol(args->varname);
if (s != -1)
synames[s].exists = true;
}
else if (nr->type != NO_ATTR) {
targs *args = NULL;
if (no->type == NO_TOK && no->token == 'V')
args = no->token_args;
else if (no->type == NO_PRNT)
args = no->token_args;
if (!args)
return;
int s = search_symbol(args->varname);
if (s == -1 || !synames[s].exists) {
printf("%s:%d:%d: error: identifier %s undeclared.\n",
filename, args->line, args->col, args->varname);
error_count++;
}
}
}