This repository has been archived by the owner on Jul 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymbol.h
74 lines (62 loc) · 1.54 KB
/
symbol.h
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
#ifndef __SYMBOL_H__
#define __SYMBOL_H__
#include <stdio.h>
/**
* There are three possible types of symbols that can be an entry of a symbol table
* */
typedef enum
{
CONST,
VAR,
PROC
} SymbolType;
/**
* Struct that holds information of a single symbol table entry.
* The validity of fields are as follows:
* type : CONST, VAR, PROC
* name : CONST, VAR, PROC
* value : CONST
* level : CONST, VAR, PROC
* address: VAR, PROC
* scope : CONST, VAR, PROC
* */
typedef struct Symbol Symbol;
struct Symbol {
SymbolType type;
char name[12];
int value;
unsigned int level;
unsigned int address;
Symbol* scope;
};
/**
* Symbol table.
* */
typedef struct {
Symbol* symbols;
int numberOfSymbols;
} SymbolTable;
/**
* Initializes the given symbol table to a empty symbol table.
* */
void initSymbolTable(SymbolTable*);
/**
* Destructs the symbol table by making necessary deallocations on the members
* of the symbol table struct.
* */
void deleteSymbolTable(SymbolTable*);
/**
* Appends a copy of the given symbol to the given symbol table.
* */
Symbol* addSymbol(SymbolTable*, Symbol);
/**
* Given symbol table, prints the entries of symbol table to the given file.
* */
void printSymbolTable(SymbolTable*, FILE*);
/**
* In the given symbolTable, searches the symbol with symbolName.
* Iteratively, the scopes are searched starting from the given scope to its
* ancestors until the global scope (NULL) is reached.
* */
Symbol* findSymbol(SymbolTable* symbolTable, Symbol* scope, const char* symbolName);
#endif