-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsymbol.c
136 lines (122 loc) · 2.36 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
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
135
136
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "error.h"
#include "parse.h"
#include "symbol.h"
struct symbol_hash symbol_hash[HASH_SIZE];
int ram_address = 16;
int rom_address = 0;
void inc_rom_address()
{
++rom_address;
}
int get_rom_address()
{
return rom_address;
}
int get_ram_address()
{
return ram_address;
}
void print_hash()
{
int i = 0;
printf("---------- HASH TABLE ----------\n");
printf("Position\tAdd\tName\n");
while(i < HASH_SIZE)
{
if(symbol_hash[i].name[0] != '\0')
{
printf("HASH: %d\t%d\t%s\n", i, symbol_hash[i].address, symbol_hash[i].name);
}
++i;
}
}
int hash(char symbol[])
{
int hash = 0;
int i = 0;
while(symbol[i] != '\0')
{
hash += symbol[i];
++i;
}
hash = hash % HASH_SIZE;
return hash;
}
int add_entry(char symbol[], int address)
{
int hash_val;
hash_val = hash(symbol);
if(symbol_hash[hash_val].name[0] == '\0') /* no entry at this location yet */
{
strcpy(symbol_hash[hash_val].name, symbol);
if(address < 0)
{
symbol_hash[hash_val].address = ram_address++;
} else {
symbol_hash[hash_val].address = address;
}
return 1;
} else { /* hash alreay used */
/* compare value of symbol here with input of function */
/* this could be a repeat hash */
if(strcmp(symbol_hash[hash_val].name, symbol) == 0) { return 1; }
if(next_hash_space() != -1)
{
strcpy(symbol_hash[hash_val].name, symbol);
if(address < 0)
{
symbol_hash[hash_val].address = ram_address++;
} else {
symbol_hash[hash_val].address = address;
}
return 1;
} else {
print_hash();
int i = find_line_num();
line_notification(i);
exit_error(13, "Symbol table full");
}
}
return 0;
}
int next_hash_space()
{
int i = -1;
do
{
++i;
} while (symbol_hash[i].name[0] != '\0' && i < HASH_SIZE);
if(symbol_hash[i].name[0] == '\0')
{
return i;
} else {
return -1;
}
}
int get_address(char symbol[])
{
int hash_val, i = 0, j = 0;
hash_val = hash(symbol);
if(symbol_hash[hash_val].name != NULL)
{
return symbol_hash[hash_val].address;
} else {
/*
could not find hash -- loop through strings
for length of symbol_hash looking for a match
*/
while(strcmp(symbol_hash[i].name, symbol) != 0 && j < HASH_SIZE)
{
if(i == HASH_SIZE){ i = 0; } else { ++i; }
++j;
}
if(strcmp(symbol_hash[i].name, symbol) == 0)
{
return symbol_hash[i].address;
}
}
return -1;
}