-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconstsym.c
More file actions
184 lines (165 loc) · 4.11 KB
/
constsym.c
File metadata and controls
184 lines (165 loc) · 4.11 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* p2c -- Pascal to C converter.
Copyright (C) 2015 abhijit13@gmail.com Abhijit A.M..
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include "data_struct.h"
#include "prototype.h"
extern Funptr symtab;
extern Funptr currfun;
extern Typeptr currtype;
extern Varptr currvar;
extern Constptr currconst;
extern Argptr currarg;
extern Idptr currid, curridlist; //curridlist is current list of identifiers
// currid points to the last node in the current list
extern Type basic_types[4];
extern Funptr tempfun;
extern Typeptr temptype;
extern Varptr tempvar;
extern Argptr temparg;
extern Idptr tempid;
extern int func_level;
extern int prev_func_level;
/* Creates a Constant record.
* This record is added to the current list of constants for the current
* function .
*/
int
SymtabInsertConst (char *lexeme, Constptr ptr)
{
Constptr temp;
++(currfun->no_of_consts);
temp = (Constptr) malloc (sizeof (Constant));
temp->name = (char *) malloc (strlen (lexeme) + 1);
strcpy (temp->name, lexeme);
temp->value = ptr->value;
temp->nxt = NULL;
temp->const_type = ptr->const_type;
temp->func=currfun;
if (currfun->constlist == NULL)
{
currfun->constlist = temp;
currconst = temp;
}
else
{
currconst->nxt = temp;
currconst = temp;
}
}
/*--------------------------------------------------------------------*/
/* print the list of constants defined for the current function
*/
int
SymtabPrintConstlist ()
{
Constptr temp;
int i = 0;
printf ("\n No. of constants = %d", currfun->no_of_consts);
temp = currfun->constlist;
while (temp != NULL)
{
i++;
printf ("\n %d) id= %s value= %f ", i, temp->name, temp->value);
temp = temp->nxt;
}
}
Constptr
SymtabIsConstPresent (char *name)
{
Constptr tempconst;
Funptr tempfun;
tempfun = currfun;
while (tempfun != NULL)
{
tempconst = tempfun->constlist;
while (tempconst != NULL)
{
if (mystrcmp (tempconst->name, name) == 0)
{
return tempconst;
}
else
tempconst = tempconst->nxt;
}
tempfun = tempfun->parent;
}
printf ("\n IsConstPresent : %s not found", name);
return NULL;
}
Constptr
SymtabCreateNewConst (int type, ...)
{
va_list ap;
Constptr temp;
temp = (Constptr) malloc (sizeof (Constant));
temp->name = NULL;
temp->nxt = NULL;
temp->const_type = type;
va_start (ap, type);
switch (type)
{
case CONST_CHAR:
{
char *tmp;
tmp = va_arg (ap, char *);
temp->string = (char *) malloc (strlen (tmp) + 1);
strcpy (temp->string, tmp + 1); // +1 for eliminating the '
temp->string[strlen (temp->string) - 1] = '\0';
printf ("\n CreateNewConst : string= %s ", tmp);
}
break;
case CONST_INT:
{
char *tmp;
tmp = va_arg (ap, char *);
temp->value = (float) atoi (tmp);
printf ("\n CreateNewConst: string= %s value= %d ", tmp,
(int) temp->value);
}
break;
case CONST_REAL:
{
char *tmp;
tmp = va_arg (ap, char *);
temp->value = (double)atof (tmp);
printf ("\n CreateNewConst: string= %s value= %g ", tmp, temp->value);
}
break;
case CONST_ID:
{
Constptr tmp;
char *tmpch;
tmpch = va_arg (ap, char *);
tmp = SymtabIsConstPresent (tmpch);
if (tmp != NULL)
{
temp->value = tmp->value;
temp->const_type = tmp->const_type;
temp->nxt = NULL;
}
else
{
free (temp);
return NULL;
}
}
break;
default:
break;
}
va_end (ap);
return temp;
}