-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.c
65 lines (58 loc) · 1.63 KB
/
list.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
#include "list.h"
//Setup symbol table
void setupSymbolTable(struct symbolList** head, struct symbolList** tail)
{
struct symbolList* curr = NULL;
curr = createSymbol(curr, "SP", 0);
*head = curr;
curr = createSymbol(curr, "LCL", 1);
curr = createSymbol(curr, "ARG", 2);
curr = createSymbol(curr, "THIS", 3);
curr = createSymbol(curr, "THAT", 4);
curr = createSymbol(curr, "R0", 0);
curr = createSymbol(curr, "R1", 1);
curr = createSymbol(curr, "R2", 2);
curr = createSymbol(curr, "R3", 3);
curr = createSymbol(curr, "R4", 4);
curr = createSymbol(curr, "R5", 5);
curr = createSymbol(curr, "R6", 6);
curr = createSymbol(curr, "R7", 7);
curr = createSymbol(curr, "R8", 8);
curr = createSymbol(curr, "R9", 9);
curr = createSymbol(curr, "R10", 10);
curr = createSymbol(curr, "R11", 11);
curr = createSymbol(curr, "R12", 12);
curr = createSymbol(curr, "R13", 13);
curr = createSymbol(curr, "R14", 14);
curr = createSymbol(curr, "R15", 15);
curr = createSymbol(curr, "SCREEN", 16384);
curr = createSymbol(curr, "KBD", 24576);
*tail = curr;
}
//Create a symbol helper function
struct symbolList* createSymbol(struct symbolList* prev, char* symbol, int val)
{
struct symbolList* curr;
curr = malloc(sizeof(struct symbolList));
if(prev)
prev->next = curr;
int len = strlen(symbol);
curr->symbol = malloc(len + 1);
strncpy(curr->symbol, symbol, len+1);
curr ->val = val;
return curr;
}
//Clean up the symbol table
void cleanupSymbolTable(struct symbolList** head)
{
struct symbolList* curr;
struct symbolList* prev;
curr = *head;
while(curr != NULL)
{
prev = curr;
curr = curr->next;
free(prev->symbol);
free(prev);
}
}