-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlinkedlist.c
130 lines (112 loc) · 4.08 KB
/
linkedlist.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
#include "linkedlist.h" // custom linkedlist
void initSymbolTable()
{
symbolTable = (struct symbol*) malloc ( sizeof(struct symbol) );
symbolTable->next = NULL;
}
// value is the address of this variable
void addToSymbolTable( char newVariable[], char value[] )
{
struct symbol* conductor = symbolTable;
//printf("Adding to symbol table\n");
if( conductor != NULL )
{
while ( conductor->next != NULL )
{
/*check existance here and give error if already defined*/
conductor = conductor -> next;
}
}
conductor->next = malloc (sizeof(struct symbol));
conductor->next->definedWithoutKnowing = 0;
strncpy ( conductor->next->varName, newVariable, sizeof(conductor->next->varName) );
strncpy ( conductor->next->varValue, value, sizeof(conductor->next->varValue) );
// conductor->next->varValue = value;
conductor->next->next = NULL;
}
void printSymbolTable()
{
struct symbol* conductor = symbolTable;
if(conductor != NULL) // need to move away from the root because the root doesn't have a symbol
{
conductor = conductor -> next;
}
while ( conductor != NULL )
{
//printf("Symbol: %s\t,\tValue: %d\n", conductor -> varName, conductor -> varValue );
printf("%s %s -> %s\n", conductor->varName, conductor->pseudoName, conductor->varValue);
conductor = conductor -> next;
}
}
struct symbol* lookForSymbol ( char wantedVariable[] ) // return found symbol or null
{
struct symbol* conductor = symbolTable;
if(conductor != NULL) // need to move away from the root because the root doesn't have a symbol
{
conductor = conductor -> next;
}
while ( conductor != NULL )
{
if( !strcmp(wantedVariable, conductor->varName) ) // if they are equal
{
return conductor;
}
conductor = conductor -> next;
}
return NULL; // couldn't find variable
}
void freeLinkedList ()
{
struct symbol* conductor = symbolTable;
struct symbol* prev = NULL;
if(conductor != NULL) // need to move away from the root because the root doesn't have a symbol
{
conductor = conductor -> next;
}
while ( conductor != NULL )
{
prev = conductor;
conductor = conductor -> next;
free(prev);
}
}
// BELOW THIS LINE IS MOSTLY COPIED FROM ABOVE CODE
// BELOW THIS LINE IS MOSTLY COPIED FROM ABOVE CODE
void addToSymbolTableOnlyPseudo( char newPseudo[], char value[] )
{
struct symbol* conductor = symbolTable;
//printf("Adding to symbol table\n");
if( conductor != NULL )
{
while ( conductor->next != NULL )
{
/*check existance here and give error if already defined*/
conductor = conductor -> next;
}
}
conductor->next = malloc (sizeof(struct symbol));
strncpy ( conductor->next->pseudoName, newPseudo, sizeof(conductor->next->pseudoName) );
strncpy ( conductor->next->varValue, value, sizeof(conductor->next->varValue) );
conductor->next->definedWithoutKnowing = 0;
// conductor->next->varValue = value;
conductor->next->next = NULL;
}
// looks for a pseudo variable name and returns the pointer for that
struct symbol* lookForPseudoName ( char wantedPseudoName[] )
{
struct symbol* conductor = symbolTable;
if(conductor != NULL) // need to move away from the root because the root doesn't have a symbol
{
conductor = conductor -> next;
}
while ( conductor != NULL )
{
//printf("Looked at Symbol: %s\n", conductor -> varName );
if( !strcmp(wantedPseudoName, conductor->pseudoName) ) // if they are equal
{
return conductor;
}
conductor = conductor -> next;
}
return NULL; // couldn't find variable
}