-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymbol.c
71 lines (63 loc) · 1.56 KB
/
symbol.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
/*
* =====================================================================================
*
* Filename: symbol.c
*
* Description: Symbol Table Management
*
* Version: 1.0
* Created: 09/20/2017 03:42:40 PM
* Revision: none
* Compiler: gcc
*
* Author: Brad Theilman (BHT), [email protected]
* Organization:
*
* =====================================================================================
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cmplr.h"
#include "symbol.h"
static int symcount = 0;
int check_symbol(char *nm)
{
/* Check if a symbol is already
* defined in the symbol table */
int i;
for (i = 0; i < symcount; i++) {
if (!strcmp(symtab[i]->name, nm)) {
return 1;
}
}
return 0;
}
Symbol *find_symbol(Node * n)
{
/* Return a reference to a previously defined symbol
* Print an error and exit if the symbol is not defined */
int i;
for (i = 0; i < symcount; i++) {
if (!strcmp(symtab[i]->name, n->name)) {
return symtab[i];
}
}
printf("Error: Variable %s undeclared\n", n->name);
exit(-1);
}
/* Add a symbol to the symbol table.
* Print Error and exit if symbol is already defined */
void add_symbol(Node * var, Node * expr)
{
if (check_symbol(var->name)) {
printf("Symbol %s already declared. Terminating.\n", var->name);
exit(-1);
}
Symbol *sym = malloc(sizeof(Symbol));
strcpy(sym->name, var->name);
sym->expr = expr;
symtab[symcount] = sym;
symcount++;
}