-
Notifications
You must be signed in to change notification settings - Fork 0
/
symbols.c
104 lines (85 loc) · 1.97 KB
/
symbols.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
/*
* File: symbols.c
* Author: Grant Kurtz
*
* Description: Symbol manipulation...I'll come up with a better description
* when it's written.
*/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "symbols.h"
#include "idents.h"
#include "strlib.h"
void add_symbol(char *iden, int val, struct symbol_table *tbl, int pos){
if(!tbl)
return;
struct symbol *sym = (struct symbol *) malloc(sizeof(struct symbol));
sym->iden = iden;
sym->val = val;
sym->next = 0;
sym->pos = pos;
sym->used = 0;
if(tbl->r){
tbl->e->next = sym;
tbl->e = sym;
}
else{
tbl->r = sym;
tbl->e = sym;
}
}
struct symbol *find_symbol(char *iden, struct symbol_table *tbl){
if(!iden)
return 0;
if(!tbl)
return 0;
struct symbol *sym = tbl->r;
while(sym && strcmp(sym->iden, iden)){
sym = sym->next;
}
return sym;
}
void print_symbol(struct symbol *sym, int c){
if(c > -1)
printf( "\t== Symbol %d ==\n", c);
else
printf( "\t== Symbol ==\n");
if(!sym){
printf("** NULL **\n");
}
else{
printf( "Next:\t%p\n"
"Iden:\t%s\n"
"Val:\t%d\n"
"Used:\t%d\n", sym->next, sym->iden, sym->val, sym->used);
}
}
void print_symbols(struct symbol_table *tbl){
int i = 0;
printf( "\t\t==== Symbol Table ====\n");
if(!tbl){
printf("** NULL **\n");
}
else{
struct symbol *sym = tbl->r;
while(sym){
print_symbol(sym, i++);
sym = sym->next;
}
}
}
void print_symbol_not_found(const char *bad_sym, struct program *prog){
print_asterisk(RED_C, stderr);
fprintf(stderr, "%s:\n", prog->input);
print_asterisk(RED_C, stderr);
fprintf(stderr, "\tUnknown Symbol '%s'.\n", bad_sym);
prog->error_code = -1; // TODO: create actual error_code
}
void print_symbol_not_used(const struct symbol *sym, const char *sym_type,
const struct program *prog){
print_asterisk(YLW_C, stderr);
fprintf(stderr, "%s, %d:\n", prog->input, sym->pos);
print_asterisk(YLW_C, stderr);
fprintf(stderr, "\tWarning: %s '%s' is not used.\n", sym_type, sym->iden);
}