summaryrefslogtreecommitdiff
path: root/src/rtt/rttnode.c
blob: 6064b7e1dec0a673442ea6600799c7822743a7fe (plain)
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include "rtt.h"

/*
 * node0 - create a syntax tree leaf node.
 */
struct node *node0(id, tok)
int id;
struct token *tok;
   {
   struct node *n;

   n = NewNode(0);
   n->nd_id = id;
   n->tok = tok;
   return n;
   }

/*
 * node1 - create a syntax tree node with one child.
 */
struct node *node1(id, tok, n1)
int id;
struct token *tok;
struct node *n1;
   {
   struct node *n;

   n = NewNode(1);
   n->nd_id = id;
   n->tok = tok;
   n->u[0].child = n1;
   return n;
   }

/*
 * node2 - create a syntax tree node with two children.
 */
struct node *node2(id, tok, n1, n2)
int id;
struct token *tok;
struct node *n1;
struct node *n2;
   {
   struct node *n;

   n = NewNode(2);
   n->nd_id = id;
   n->tok = tok;
   n->u[0].child = n1;
   n->u[1].child = n2;
   return n;
   }

/*
 * node3 - create a syntax tree node with three children.
 */
struct node *node3(id, tok, n1, n2, n3)
int id;
struct token *tok;
struct node *n1;
struct node *n2;
struct node *n3;
   {
   struct node *n;

   n = NewNode(3);
   n->nd_id = id;
   n->tok = tok;
   n->u[0].child = n1;
   n->u[1].child = n2;
   n->u[2].child = n3;
   return n;
   }

/*
 * node4 - create a syntax tree node with four children.
 */
struct node *node4(id, tok, n1, n2, n3, n4)
int id;
struct token *tok;
struct node *n1;
struct node *n2;
struct node *n3;
struct node *n4;
   {
   struct node *n;

   n = NewNode(4);
   n->nd_id = id;
   n->tok = tok;
   n->u[0].child = n1;
   n->u[1].child = n2;
   n->u[2].child = n3;
   n->u[3].child = n4;
   return n;
   }

/*
 * sym_node - create a syntax tree node for a variable. If the identifier
 *  is in the symbol table, create a node that references the entry,
 *  otherwise create a simple leaf node.
 */
struct node *sym_node(tok)
struct token *tok;
   {
   struct sym_entry *sym;
   struct node *n;

   sym = sym_lkup(tok->image);
   if (sym != NULL) {
      n = NewNode(1);
      n->nd_id = SymNd;
      n->tok = tok;
      n->u[0].sym = sym;
      ++sym->ref_cnt;
      /*
       * If this is the result location of an operation, note that it
       *  is explicitly referenced.
       */
      if (sym->id_type == RsltLoc)
         sym->u.referenced = 1;
      return n;
      }
   else
      return node0(PrimryNd, tok);
   }

/*
 * comp_nd - create a node for a compound statement.
 */
struct node *comp_nd(tok, dcls, stmts)
struct token *tok;
struct node *dcls;
struct node *stmts;
   {
   struct node *n;

   n = NewNode(3);
   n->nd_id = CompNd;
   n->tok = tok;
   n->u[0].child = dcls;
   n->u[1].sym = dcl_stk->tended; /* tended declarations are not in dcls */
   n->u[2].child = stmts;
   return n;
   }

/*
 * arith_nd - create a node for an arith_case statement.
 */
struct node *arith_nd(tok, p1, p2, c_int, ci_act, intgr, i_act, dbl, d_act)
struct token *tok;
struct node *p1;
struct node *p2;
struct node *c_int;
struct node *ci_act;
struct node *intgr;
struct node *i_act;
struct node *dbl;
struct node *d_act;
   {
   struct node *n;

   /*
    * Insure the cases are what we expect.
    */
   if (c_int->tok->tok_id != C_Integer)
      errt3(c_int->tok, "expected \"C_integer\", found \"", c_int->tok->image,
         "\"");
   if (intgr->tok->image != icontypes[int_typ].id)
      errt3(intgr->tok, "expected \"integer\", found \"", intgr->tok->image,
         "\"");
   if (dbl->tok->tok_id != C_Double)
      errt3(dbl->tok, "expected \"C_double\", found \"", dbl->tok->image,
         "\"");

   /*
    * Indicate in the symbol table that the arguments are converted to C
    *  values.
    */
   dst_alloc(c_int, p1);
   dst_alloc(c_int, p2);
   dst_alloc(dbl, p1);
   dst_alloc(dbl, p2);

   free_tree(c_int);
   free_tree(intgr);
   free_tree(dbl);

   n = node3(TrnryNd, NULL, ci_act, i_act, d_act);
   return node3(TrnryNd, tok, p1, p2, n);
   }

struct node *dest_node(tok)
struct token *tok;
   {
   struct node *n;
   int typcd;

   n = sym_node(tok);
   typcd = n->u[0].sym->u.typ_indx;
   if (typcd != int_typ && typcd != str_typ && typcd != cset_typ &&
      typcd != real_typ)
      errt2(tok, "cannot convert to ", tok->image);
   return n;
   }


/*
 * free_tree - free storage for a syntax tree.
 */
void free_tree(n)
struct node *n;
   {
   struct sym_entry *sym, *sym1;

   if (n == NULL)
      return;

   /*
    * Free any subtrees and other referenced storage.
    */
   switch (n->nd_id) {
      case SymNd:
         free_sym(n->u[0].sym); /* Indicate one less reference to symbol */
         break;

      case CompNd:
         /*
          * Compound node. Free ordinary declarations, tended declarations,
          *  and executable code.
          */
         free_tree(n->u[0].child);
         sym = n->u[1].sym;
         while (sym != NULL) {
            sym1 = sym;
            sym = sym->u.tnd_var.next;
            free_sym(sym1);
            }
         free_tree(n->u[2].child);
         break;

      case QuadNd:
         free_tree(n->u[3].child);
         /* fall thru to next case */
      case TrnryNd:
         free_tree(n->u[2].child);
         /* fall thru to next case */
      case AbstrNd: case BinryNd: case CommaNd: case ConCatNd: case LstNd:
      case StrDclNd:
         free_tree(n->u[1].child);
         /* fall thru to next case */
      case IcnTypNd: case PstfxNd: case PreSpcNd: case PrefxNd:
         free_tree(n->u[0].child);
         /* fall thru to next case */
      case ExactCnv: case PrimryNd:
         break;

      default:
         fprintf(stdout, "rtt internal error: unknown node type\n");
         exit(EXIT_FAILURE);
         }
   free_t(n->tok);             /* free token */
   free((char *)n);
   }