@@ -19,20 +19,21 @@ void free_comp_dict_item_t(comp_dict_item_t* item) {
19
19
/* The global symbols table defined in the program. */
20
20
comp_dict_t symbols_table = NULL ;
21
21
22
- comp_dict_item_t * symbols_table_add (const char * key , int line ,
22
+ comp_dict_item_t * symbols_table_add (const char * key , int line_number ,
23
23
int token_type , const char * token_value , comp_dict_t * table ) {
24
24
25
25
comp_dict_item_t * item = symbols_table_find (key , table );
26
26
27
27
if (item != NULL ) {
28
28
/* an item with the same key is already in the symbols table.
29
29
* we have to update the line where it appeared and the value. */
30
- item -> line_where_it_last_appeared = line ;
30
+ item -> line_where_it_last_appeared = line_number ;
31
31
32
32
if (item -> value != NULL )
33
33
free (item -> value );
34
34
35
- item -> value = interpret_token_value (token_value , token_type );
35
+ item -> value = interpret_token_value (token_value , token_type ,
36
+ line_number );
36
37
37
38
/* note that we don't have to change the token_type or worry about
38
39
* the value's type, since, if the token_type were different,
@@ -45,8 +46,8 @@ comp_dict_item_t* symbols_table_add(const char* key, int line,
45
46
46
47
item -> token = (const char * ) malloc ((strlen (key ) + 1 ) * sizeof (char ));
47
48
strcpy ((char * )item -> token , key );
48
- item -> line_where_it_last_appeared = line ;
49
- item -> value = interpret_token_value (token_value , token_type );
49
+ item -> line_where_it_last_appeared = line_number ;
50
+ item -> value = interpret_token_value (token_value , token_type , line_number );
50
51
item -> token_type = token_type ;
51
52
52
53
HASH_ADD_KEYPTR (hh , * table , item -> token , strlen (item -> token ), item );
@@ -82,42 +83,50 @@ int symbols_table_count(comp_dict_t* table) {
82
83
return HASH_COUNT (* table );
83
84
}
84
85
85
- void * interpret_token_value (const char * text , int token_type ) {
86
+ void * interpret_token_value (const char * text , int token_type , int line_number ) {
87
+ //printf("text %s interpreted as %d -> ", text, token_type);
86
88
void * value = NULL ;
87
89
if (token_type == SIMBOLO_LITERAL_INT ) {
88
90
value = malloc (sizeof (int ));
89
91
* ((int * )value ) = strtol (text , NULL , 10 );
90
92
if (errno == ERANGE ) {
91
- /* int value is off integer limits, defaults to 2^31 - 1.
92
- * should we print an error message here?*/
93
+ /* int value is off integer limits, defaults to 2^31 - 1. */
94
+ printf ("warning: line %d: integer value %s is off limits. "
95
+ "defaulting to %d\n" , line_number , text , * ((int * )value ));
93
96
}
97
+ //printf("%d\n", *((int*)value));
94
98
} else if (token_type == SIMBOLO_LITERAL_CHAR ) {
95
99
value = malloc (sizeof (char ));
96
100
* ((char * )value ) = text [0 ];
101
+ //printf("%c\n", *((char*)value));
97
102
} else if (token_type == SIMBOLO_LITERAL_BOOL ) {
98
103
value = malloc (sizeof (int ));
99
104
* ((int * )value ) = strcmp (text , "false" ); /* strcmp returns 0 if strings
100
105
are equal, and different than 0
101
106
otherwise. so, if it's false, we'll get
102
107
0 anyway :) */
108
+ //printf("%d\n", *((int*)value));
103
109
} else if (token_type == SIMBOLO_LITERAL_FLOAT ) {
104
110
value = malloc (sizeof (float ));
105
111
* ((float * )value ) = strtof (text , NULL );
106
112
if (errno == ERANGE ) {
107
- /* float value overflow. should we print an error message? */
113
+ /* float value overflow. */
114
+ printf ("warning: line %d: float value %s is off limits. "
115
+ "defaulting to %.2f\n" , line_number , text , * ((float * )value ));
108
116
}
117
+ //printf("%f\n", *((float*)value));
109
118
} else if (token_type == SIMBOLO_LITERAL_STRING ) {
110
119
value = malloc ((strlen (text ) + 1 ) * sizeof (char ));
111
120
strcpy (value , text );
121
+ //printf("%s\n", (char*)value);
112
122
} else if (token_type == SIMBOLO_IDENTIFICADOR ) {
113
123
/* if the token is an identifier, it has no value associated to it
114
124
* for now. */
115
125
value = NULL ;
116
126
} else {
117
127
/* error. */
118
- printf ("invalid token type (%d) passed to interpret_token_value().\n" ,
128
+ printf ("invalid token type (%d) passed to interpret_token_value().\n" ,
119
129
token_type );
120
- //assert(0);
121
130
value = NULL ;
122
131
}
123
132
return value ;
0 commit comments