Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 139 lines (115 sloc) 2.911 kb
08b042b @grnt426 Ripped out a lot of common functions to be used outside of translator.c
authored
1 /*
2 * File: symbols.c
3 * Author: Grant Kurtz
4 *
5 * Description: Symbol manipulation...I'll come up with a better description
6 * when it's written.
7 */
8
48afdda @grnt426 Cleaning everything up, creating a much needed Makefile, stopped being a...
authored
9
10 #include <string.h>
11 #include <stdio.h>
12 #include <stdlib.h>
08b042b @grnt426 Ripped out a lot of common functions to be used outside of translator.c
authored
13 #include "symbols.h"
3fd6a82 @grnt426 Modified the symbol error reporting functions to include colored asteris...
authored
14 #include "idents.h"
15 #include "strlib.h"
08b042b @grnt426 Ripped out a lot of common functions to be used outside of translator.c
authored
16
7727944 @grnt426 Added more error reporting functions to handle errors with calling funct...
authored
17 void add_symbol(char *iden, int val, struct symbol_table *tbl, int pos,
18 short type){
08b042b @grnt426 Ripped out a lot of common functions to be used outside of translator.c
authored
19 if(!tbl)
20 return;
21
22 struct symbol *sym = (struct symbol *) malloc(sizeof(struct symbol));
23 sym->iden = iden;
24 sym->val = val;
25 sym->next = 0;
4f3374d @grnt426 Adding functionality for unused symbols.
authored
26 sym->pos = pos;
27 sym->used = 0;
7727944 @grnt426 Added more error reporting functions to handle errors with calling funct...
authored
28 sym->type = type;
4f3374d @grnt426 Adding functionality for unused symbols.
authored
29
08b042b @grnt426 Ripped out a lot of common functions to be used outside of translator.c
authored
30
31 if(tbl->r){
32 tbl->e->next = sym;
33 tbl->e = sym;
34 }
35 else{
36 tbl->r = sym;
37 tbl->e = sym;
38 }
39 }
40
41 struct symbol *find_symbol(char *iden, struct symbol_table *tbl){
42 if(!iden)
43 return 0;
44 if(!tbl)
45 return 0;
46
47 struct symbol *sym = tbl->r;
48 while(sym && strcmp(sym->iden, iden)){
49 sym = sym->next;
50 }
51 return sym;
52 }
53
7727944 @grnt426 Added more error reporting functions to handle errors with calling funct...
authored
54 struct symbol *find_symbol_at(int pos, struct symbol_table *tbl){
55 if(pos < 0)
56 return 0;
57 if(!tbl)
58 return 0;
59
60 struct symbol *sym = tbl->r;
61 while(sym && sym->pos != pos){
62 sym = sym->next;
63 }
64 return sym;
65 }
66
08b042b @grnt426 Ripped out a lot of common functions to be used outside of translator.c
authored
67 void print_symbol(struct symbol *sym, int c){
68
69 if(c > -1)
70 printf( "\t== Symbol %d ==\n", c);
71 else
72 printf( "\t== Symbol ==\n");
73
74 if(!sym){
75 printf("** NULL **\n");
76 }
77 else{
78 printf( "Next:\t%p\n"
79 "Iden:\t%s\n"
4f3374d @grnt426 Adding functionality for unused symbols.
authored
80 "Val:\t%d\n"
7727944 @grnt426 Added more error reporting functions to handle errors with calling funct...
authored
81 "Type:\t%d\n"
82 "Used:\t%d\n", sym->next, sym->iden, sym->val, sym->type,
83 sym->used);
08b042b @grnt426 Ripped out a lot of common functions to be used outside of translator.c
authored
84 }
85 }
86
87 void print_symbols(struct symbol_table *tbl){
88
89 int i = 0;
90
91 printf( "\t\t==== Symbol Table ====\n");
92 if(!tbl){
93 printf("** NULL **\n");
94 }
95 else{
96 struct symbol *sym = tbl->r;
97 while(sym){
98 print_symbol(sym, i++);
99 sym = sym->next;
100 }
101 }
102 }
103
fb5d74e @grnt426 Got a lot of aux code written to handle all the intermediary steps of pr...
authored
104 void print_symbol_not_found(const char *bad_sym, struct program *prog){
3fd6a82 @grnt426 Modified the symbol error reporting functions to include colored asteris...
authored
105 print_asterisk(RED_C, stderr);
106 fprintf(stderr, "%s:\n", prog->input);
107 print_asterisk(RED_C, stderr);
fb5d74e @grnt426 Got a lot of aux code written to handle all the intermediary steps of pr...
authored
108 fprintf(stderr, "\tUnknown Symbol '%s'.\n", bad_sym);
7727944 @grnt426 Added more error reporting functions to handle errors with calling funct...
authored
109 prog->error_code = BAD_SYM; // TODO: create actual error_code
fb5d74e @grnt426 Got a lot of aux code written to handle all the intermediary steps of pr...
authored
110
111 }
112
4f3374d @grnt426 Adding functionality for unused symbols.
authored
113
114 void print_symbol_not_used(const struct symbol *sym, const char *sym_type,
115 const struct program *prog){
3fd6a82 @grnt426 Modified the symbol error reporting functions to include colored asteris...
authored
116 print_asterisk(YLW_C, stderr);
117 fprintf(stderr, "%s, %d:\n", prog->input, sym->pos);
118 print_asterisk(YLW_C, stderr);
4f3374d @grnt426 Adding functionality for unused symbols.
authored
119 fprintf(stderr, "\tWarning: %s '%s' is not used.\n", sym_type, sym->iden);
120 }
121
7727944 @grnt426 Added more error reporting functions to handle errors with calling funct...
authored
122 void print_non_func_call(const struct symbol *sym, struct program *prog,
123 int err_line){
124 print_asterisk(RED_C, stderr);
125 fprintf(stderr, "%s, %d:\n", prog->input, err_line);
126 print_asterisk(RED_C, stderr);
127 fprintf(stderr, "\tUnable to call a non-function, '%s'!\n", sym->iden);
128 prog->error_code = CALL_NON_F;
129 }
130
131 void print_return_from_non_func(int abs_pos, struct program *prog){
132 print_asterisk(RED_C, stderr);
133 fprintf(stderr, "%s, %d:\n", prog->input, abs_pos);
134 print_asterisk(RED_C, stderr);
135 fprintf(stderr, "\tUnable to return without a function declared first!\n");
136 prog->error_code = RET_NON_F;
137 }
138
Something went wrong with that request. Please try again.