summaryrefslogtreecommitdiffstats
path: root/mfppact.c
blob: 7b13f6d53c1ff7b8ed23e2ae9ae258e34ce27e65 (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
265
266
267
268
269
270
271
272
273
274
275
276
/*
 * mffppact.c
 *
 * Actions for mffpp
 *
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "mfpp.h"

/*
 * Some simple string-memory-management
 */
void *xmalloc(int size)
{
  void *p;
  p = (void *) malloc(size);
  if ( !p )
    {
      fprintf(stderr, "Fatal error: out of memory");
      exit(1);
    }
  return p;
}

char *xstrdup(char *str)
{
  int n;
  char *dst;
  n = strlen(str)+1;
  dst = xmalloc(n);
  memcpy(dst, str, n);
  return dst;
}

char *xstrcat(char *s1, char *s2)
{
  int n1, n2;
  char *dst;
  n1 = strlen(s1);
  n2 = strlen(s2)+1;
  dst = xmalloc(n1+n2);
  memcpy(dst, s1, n1);
  memcpy(dst+n1, s2, n2);
  return dst;
}

/*
 * Handle %if et al
 */

typedef enum
{
  bt_if,			/* In if or elif clause */
  bt_else			/* In else clause */
} blocktype;

#define MAXDEPTH 32

typedef struct
{
  blocktype bt;
  int is_true;
  int was_true;
}
stackelm;

static stackelm stack[MAXDEPTH]; /* Stack */
static stackelm *curr;		/* Current block */
static int depth = 0;

int killcount = 0;		/* Count of false %if's */

/* %if */
void do_if(int cond)
{
  if ( depth >= MAXDEPTH )
    {
      yyerror("Too many nested %ifs");
      return;
    }
  
  curr = &(stack[depth++]);
  curr->bt = bt_if;
  curr->is_true = curr->was_true = cond;

  if ( !cond )
    killcount++;
}

/* %elif */
void do_elif(int cond)
{
  if ( !depth || curr->bt != bt_if )
    {
      yyerror("%%elif without %%if");
      return;
    }
  
  if ( !curr->was_true && cond )
    {
      curr->is_true = curr->was_true = 1;
      killcount--;
    }
  else if ( curr->is_true )
    {
      curr->is_true = 0;
      killcount++;
    }
}

/* %else */
void do_else(void)
{
  if ( !depth || curr->bt != bt_if )
    {
      yyerror("%%else without %%if");
      return;
    }

  if ( curr->is_true )
    {
      curr->is_true = 0;
      killcount++;
    }
  else if ( !curr->was_true )
    {
      curr->is_true = 1;
      killcount--;
    }
}

/* %endif */
void do_endif(void)
{
  if ( !depth || (curr->bt != bt_if && curr->bt != bt_else) )
    {
      yyerror("%%endif without %%if");
      return;
    }
  
  if ( !curr->is_true )
    killcount--;

  depth--;
  curr--;
}

/* balance_blocks - called on end of file: balance loops and conditionals */
void balance_blocks()
{
  if ( depth )
    yyerror("Unterminated %s", (curr->bt == bt_else) ? "%else":"%if");
  killcount = 0;
}

/*
 * Symbol table management
 */

struct symtabent
{
  struct identifier id;
  struct symtabent *next;
};

#define SYMTABSIZE 733		/* Shouldn't be fixed, but this is a
				   special-purpose program */

static struct symtabent *symtab[SYMTABSIZE];

static int symtab_hash_func(char *str)
{
  unsigned int x;
  for ( x = 0 ; *str ; *str++ )
    {
      x = x * 65599 + *str;
    }
  return x % SYMTABSIZE;
}

void symtab_init(void)
{
  int i;
  for ( i = 0 ; i < SYMTABSIZE ; i++ )
    symtab[i] = NULL;
}

/*
 * Look up identifiers
 */

struct identifier *id_lookup(char *name)
{
  struct symtabent *sp;
 
  sp = symtab[symtab_hash_func(name)];

  while ( sp && strcmp(name, sp->id.name) != 0 )
    sp = sp->next;

  return &(sp->id);
}

/*
 * Define or undefine indentifiers.
 */
void id_define(char *name, list *val)
{
  struct symtabent *sp, **stp;
  int n;

  stp = &symtab[n = symtab_hash_func(name)];
  sp = *stp;

#if 0
  if (debug > 1)
    if (txt)
      fprintf(stderr, "%%define %s (%d): %s\n", name, n, txt);
    else
      fprintf(stderr, "%%undef %s (%d)\n", name, n);
#endif

  while ( sp && strcmp(name, sp->id.name) != 0 )
    {
      stp = &(sp->next);
      sp = *stp;
    }
  
  if ( !sp && val )
    {
      sp = xmalloc(sizeof(struct symtabent));
      sp->id.name = name;
      sp->id.val = val;
      sp->next = *stp;
      *stp = sp;
    }
  else if ( /* sp && */ val )
    {
      freelist(sp->id.val);
      sp->id.val = val;
      free(name);
    }
  else if ( /* !val && */ sp )
    {
      *stp = sp->next;
      free(sp->id.name);
      freelist(sp->id.val);
      free(sp);
      free(name);
    }
  else /* !val && !sp */
    free(name);
}

/*
 * Functions to be called from within yacc code.
 * 
 * do_define:      Defines a single-item variable, or undef
 * do_define_list: Defines a list variable
 */

void do_define(char *name, char *txt)
{
  if ( !killcount )
    id_define(name, txt ? makelist(txt) : NULL);
}

void do_define_list(char *name, list *val)
{
  if ( !killcount )
    id_define(name,val);
}