File: commands.c

package info (click to toggle)
chpp 0.3.4-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 3,228 kB
  • ctags: 5,154
  • sloc: ansic: 30,186; cpp: 575; sh: 500; makefile: 314; yacc: 265; asm: 261; lex: 64
file content (534 lines) | stat: -rw-r--r-- 13,087 bytes parent folder | download | duplicates (2)
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/* -*- c -*- */

/*
 * commands.c
 *
 * chpp
 *
 * Copyright (C) 1997-1998 Heinz Deinhart
 *
 * 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 2
 * 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, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "commands.h"
#include "filler.h"

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>

#include "memory.h"
#include "dynstring.h"
/*#include "macros.h"*/
/*#include "symtab.h"*/
#include "arith.h"
#include "error.h"
#include "parser.h"
#include "input.h"

extern int bufPosAtCommandBegin;

extern char filler_cmdChar;
extern int flowsExtern;
extern dynstring externFlow;
extern FlowStack *flowStack;
extern int flowDepth;
extern int zeilenNummer;
extern int incFiles;
extern dynstring currentFileName;
extern InputFileStack *ifStack;

/* static flowLevel = 1; */

static dynstring evalDS( dynstring *ds ) {
  dynstring eds = dsNew();
  inputReader ir = irNewDynstring(ds, 0);
  outputWriter ow = owNewDynstring(&eds);
  
  parParseUntil(&ir, 0, bcwNewOutput(globalEnvironment, &ow), 1, globalEnvironment, 0);
  return eds;
}

static dynstring eatDSUntil( dynstring *body, int *pos, const char *set ) {
  int origPos = *pos;
  while( (*pos) < body->length ) {
    const char *setElement = set;

    while( (*setElement) != 0 ) {
      if( (*setElement) == body->data[(*pos)] ) {
	return dsNewFromBytes( &(body->data[origPos]), (*pos) - origPos );
      }
      setElement++;
    }
    (*pos)++;
  }
  return dsCopy( body );
}

void
emmitLocation( const char *muh ) {
  dynstring fHira;
  InputFileStack *ifs = ifStack;
  static char kurdenBuffer[100];

  fHira = dsNew();
  if( incFiles > 1 ) {
    dsAppendString( &fHira, " (", 2 );
    while( ifs ) {
      dsAppendString( &fHira, ifs->name.data, ifs->name.length );
      dsAppendChar( &fHira, ':' );
      sprintf( kurdenBuffer, "%i", ifs->zeilenNummer );
      dsAppendString( &fHira, kurdenBuffer, strlen( kurdenBuffer ));
      ifs = ifs->next;
      if( ifs ) dsAppendString( &fHira, ", ", 2 );
    }
    dsAppendChar( &fHira, ')' );
  }
  fprintf( stderr, "%s: file %s:%i%s\n",
	   muh, currentFileName.data, zeilenNummer, fHira.data );
}

/*
void
commandFatalError( const char *cmd, const char *format, ... ) {
  va_list ap;

  emmitLocation( "ERROR" );
  
  fprintf( stderr, "       %c%s: ", filler_cmdChar, cmd );
  va_start( ap, format );
  vfprintf( stderr, format, ap );
  va_end( ap );
  fprintf( stderr, "\n" );
  exit( 1 );
}

void
commandWarning( const char *cmd, const char *format, ... ) {
  va_list ap;

  emmitLocation( "WARNING" );
  
  fprintf( stderr, "         %c%s: ", filler_cmdChar, cmd );
  va_start( ap, format );
  vfprintf( stderr, format, ap );
  va_end( ap );
  fprintf( stderr, "\n" );
}
*/

void
cmd_error( const char *what ) {
  issueError( ERRCMD_USER_ERROR, what, 0, 0 );
}

void
cmd_rem( const char *what ) {
  // 1 + 1 = 3
}

void
cmd_elseif( const char *what ) {
  if( !what ) {
    issueWarning( ERRCMD_ONE_ARG, "ifdef", 0, 0 );
    return;
  }
  cmd_endif( 0 );
  cmd_if( what );
}

void
cmd_if( const char *what ) {
  FlowStack *tmpFS = (FlowStack *)memXAlloc( sizeof( FlowStack ));
  dynstring eds, ds;

  if( !what ) {
    issueWarning(  ERRCMD_ONE_ARG, "if", 0, 0);
    return;
  }
  ds = dsNewFrom( what );

  if( flowStack->flowLevel ) {
    dynstring tmpDS;
    dsRemoveOuterWS( &ds );
    eds = evalDS( &ds );
    
    tmpDS = arithEvalDS( &eds, globalEnvironment );
    if( atoi( tmpDS.data )) tmpFS->flowLevel = 1;
    else tmpFS->flowLevel = 0; /* discard all */
  } else {
    tmpFS->flowLevel = 0;
  }
  tmpFS->openCmd = CMD_IF;
  
  tmpFS->next = flowStack;
  flowStack = tmpFS;
  flowDepth++;
}

void
cmd_ifdef( const char *what ) {
  FlowStack *tmpFS = (FlowStack *)memXAlloc( sizeof( FlowStack ));
  dynstring eds, ds;

  if( !what ) {
    issueWarning(  ERRCMD_ONE_ARG, "ifdef", 0, 0);
    return;
  }
  ds = dsNewFrom( what );
  
  if( flowStack->flowLevel ) {
    dsRemoveOuterWS( &ds );
    eds = evalDS( &ds );
    
    if( envGetBinding(globalEnvironment, &eds ) != 0 ) tmpFS->flowLevel = 1;
    else tmpFS->flowLevel = 0; /* discard all */
  } else {
    tmpFS->flowLevel = 0;
  }
  tmpFS->openCmd = CMD_IFDEF;
  
  tmpFS->next = flowStack;
  flowStack = tmpFS;
  flowDepth++;
}

void
cmd_ifndef( const char *what ) {
  FlowStack *tmpFS = (FlowStack *)memXAlloc( sizeof( FlowStack ));
  dynstring eds, ds;

  if( !what ) {
    issueWarning(  ERRCMD_ONE_ARG, "ifdef", 0, 0);
    return;
  }
  ds = dsNewFrom( what );

  if( flowStack->flowLevel ) {
    dsRemoveOuterWS( &ds );
    eds = evalDS( &ds );
    
    if( envGetBinding(globalEnvironment, &eds ) == 0) tmpFS->flowLevel = 1;
    else tmpFS->flowLevel = 0; /* discard all */
  } else {
    tmpFS->flowLevel = 0;
  }
  tmpFS->openCmd = CMD_IFNDEF;
  
  tmpFS->next = flowStack;
  flowStack = tmpFS;
  flowDepth++;
}

void
cmd_else( const char *arg ) {
   if( arg ) issueWarning( WARNCMD_UNUSED_ARGS, "else", 0, 0);

   switch( flowStack->openCmd ) {
   case CMD_IFDEF:
   case CMD_IFNDEF:
   case CMD_IF:
     flowStack->openCmd++; /* IFXX_ELSE is always IFXX + 1 !! */
     flowStack->flowLevel = (flowStack->flowLevel + 1) % 2;

     /* wenn ma schon im discard war, hilfts nix */
     if( flowStack->next && !flowStack->next->flowLevel )
       flowStack->flowLevel = 0;
     break;
   default:
       issueError(ERRCMD_UNMATCHED_COMMAND, "else", 0, 0);
   }
}

void
cmd_endif( const char *arg ) {
  FlowStack *tmpFS = flowStack;

  if( arg ) issueWarning( WARNCMD_UNUSED_ARGS, "endif", 0, 0);

  switch( flowStack->openCmd ) {
  case CMD_IFDEF:
  case CMD_IFDEF_ELSE:
  case CMD_IFNDEF:
  case CMD_IFNDEF_ELSE:
  case CMD_IF:
  case CMD_IF_ELSE:
    flowDepthCeck();
    flowDepth--;
    flowStack = tmpFS->next;
    memFree( tmpFS );
    break;
  default:
    issueError( ERRCMD_UNMATCHED_COMMAND, "endif", 0, 0 );
  }
}

void
cmd_discard( const char *arg ) {
  FlowStack *tmpFS = (FlowStack *)memXAlloc( sizeof( FlowStack ));

  if( arg ) issueWarning(  WARNCMD_UNUSED_ARGS, "discard", 0, 0);

  tmpFS->flowLevel = 0; /* discard all */
  tmpFS->openCmd = CMD_DISCARD;
  
  tmpFS->next = flowStack;
  flowStack = tmpFS;
  flowDepth++;
}

void
cmd_endd( const char *arg ) {
  FlowStack *tmpFS = flowStack;

  if( arg ) issueWarning(  WARNCMD_UNUSED_ARGS, "endd", 0, 0);
  if( flowStack->openCmd != CMD_DISCARD )
    issueError( ERRCMD_UNMATCHED_COMMAND, "endd", 0, 0);

  flowDepthCeck();
  flowDepth--;
  flowStack = tmpFS->next;
  memFree( tmpFS );
}

void
cmd_include( const char *fileName ) {
  dynstring evald, fn;
/*  FILE *ifi;*/

  if( !fileName ) {
    issueWarning(  ERRCMD_ONE_ARG, "include", 0, 0);
    return;
  }
  fn = dsNewFrom( fileName );
  if( !flowStack->flowLevel ) return;
  
  evald = evalDS( &fn );
  dsRemoveOuterWS( &evald );
/* // obsolete:
  
  ifi = fopen( evald.data, "r" );
  if( !ifi )
    issueError(ERRCMD_NO_INCLUDE, evald.data, 0, 0);
  includeFile( ifi, fileName );
  */

  if( !includeFile( evald.data ))
    issueError( ERRCMD_NO_INCLUDE, evald.data, 0, 0 );
}

static dynstring *edef_dynAr;
static int edef_dynNr;
static dynstring edef_evaldName;
static int edef_itIsSimple;

void
cmd_edefine( const char *args ) {
  dynstring name, body = dsNewFrom( args );
  int pos = 0;
  FlowStack *tmpFS = (FlowStack *)memXAlloc( sizeof( FlowStack ));

  if( flowStack->flowLevel ) {
    tmpFS->flowLevel = 1; /* discard all */
  } else {
    tmpFS->flowLevel = 0;
  }
  tmpFS->openCmd = CMD_EDEF;
  
  tmpFS->next = flowStack;
  flowStack = tmpFS;
  flowDepth++;

  if( !flowStack->flowLevel ) return;

  dsRemoveOuterWS( &body ); /* only 1 arg, so we can remove the spaces */

  name = eatDSUntil( &body, &pos, " \t(" );
  if( body.data[pos] == '(' ) { /* macro with arguments */
    dynstring parms = eatDSUntil( &body, &pos, ")" );

    if( pos == body.length )
      issueError(ERRCMD_CLOSE_PAREN_EXPECTED, 0, 0, 0);
    if( pos != body.length - 1 ) {
      issueError(ERRCMD_ONE_ARG, "edefine", 0, 0);
    }
    /* dsShrinkFront( &body, pos+1 ); */
    dsRemoveOuterWS( &body );
    if( parms.length ) { /* parse arguments outya ! */
      dynstring first = dsNew();
      dynstring second = dsNew();
      int pos = 1;
      int dynSize = 8;

      edef_dynAr = (dynstring *)memXAlloc( sizeof( dynstring ) * 8 );
      edef_dynNr = 0;
      second = eatDSUntil( &parms, &pos, "," );
      dsRemoveOuterWS( &second );
      evalDS( &second );
      edef_dynAr[edef_dynNr++] = dsCopy( &second );
      pos++;
      while( parms.length > pos ) {
	first = eatDSUntil( &parms, &pos, "," );
	dsRemoveOuterWS( &first );
	evalDS( &first );
	pos++;
	if( edef_dynNr == dynSize ) {
	  dynSize += 8;
	  edef_dynAr = (dynstring *)memXRealloc( edef_dynAr, sizeof( dynstring ) * 
						 dynSize );
	}
	edef_dynAr[edef_dynNr++] = dsCopy( &first );
      }
      
      edef_itIsSimple = 0;
      edef_evaldName = evalDS( &name );
      flowsExtern = 1;
      externFlow = dsNew();
      return;
      /*     defineGlobalUserDefined( &evaldName, edef_dynNr, dynAr, &body ); */
    }
    /* else no */
  } /* macro with arguments */

  if( pos != body.length )
    issueError(ERRCMD_ONE_ARG, "edefine", 0, 0);

  edef_itIsSimple = 1;
  edef_evaldName = evalDS( &name );
  /*
//  dsShrinkFront( &body, pos+1 );
//  dsRemoveOuterWS( &body );
//  edef_body = dsCopy( &body );
//  defineGlobalVariable( &evaldName, &body );
*/
  flowsExtern = 1;
  externFlow = dsNew();
}

void
cmd_ende( const char * args ) {
  FlowStack *tmpFS = flowStack;
  dynstring ef2;

  if( args ) issueWarning( WARNCMD_UNUSED_ARGS, "endd", 0, 0);
  if( flowStack->openCmd != CMD_EDEF )
    issueError(ERRCMD_UNMATCHED_COMMAND, "ende", 0, 0);

  flowDepthCeck();
  flowDepth--;
  flowStack = tmpFS->next;
  memFree( tmpFS );

  flowsExtern = 0;

  dsShrinkRear( &externFlow, 1 );
  ef2 = evalDS( &externFlow );

  /* now define the macro */
  if( edef_itIsSimple ) {
    envModifyOrAddBinding(globalEnvironment, &edef_evaldName,
			  valueNewScalar(&ef2), globalEnvironment);
  } else {
    inputReader ir = irNewDynstring(&ef2, 0);
    
    envModifyOrAddBinding(globalEnvironment, &edef_evaldName,
			  valueNewLambda(edef_dynNr, 0, 0, edef_dynAr, '%',
					 parParseIntoBCUntil(&ir, 0, 1,
							     globalEnvironment, 0),
					 globalEnvironment),
			  globalEnvironment);

  }
}

void
cmd_end( const char * args ) {
  if( args ) issueWarning( WARNCMD_UNUSED_ARGS, "endd", 0, 0 );
  switch( flowStack->openCmd ) {
  case CMD_EDEF: return cmd_ende( 0 );
  case CMD_IFDEF:
  case CMD_IFDEF_ELSE:
  case CMD_IFNDEF:
  case CMD_IFNDEF_ELSE:
  case CMD_IF:
  case CMD_IF_ELSE: return cmd_endif( 0 );
  case CMD_DISCARD: return cmd_endd( 0 );
  default:
    issueError(ERRCMD_UNMATCHED_COMMAND, "end", 0, 0);
  }
}

void
cmd_define( const char *args ) {
  dynstring evaldName, name, body = dsNewFrom( args );
  int pos = 0;

  if( !flowStack->flowLevel ) return;

  name = eatDSUntil( &body, &pos, " \t(" );
  if( body.data[pos] == '(' ) { /* macro with arguments */
    dynstring parms = eatDSUntil( &body, &pos, ")" );

    if( pos == body.length )
      issueError(ERRCMD_CLOSE_PAREN_EXPECTED, 0, 0, 0);
    dsShrinkFront( &body, pos+1 );
    dsRemoveOuterWS( &body );
    if( parms.length ) { /* parse arguments outya ! */
      dynstring first = dsNew();
      dynstring second = dsNew();
      int pos = 1;
      dynstring *dynAr = (dynstring *)memXAlloc( sizeof( dynstring ) * 8 );
      int dynNr = 0;
      int dynSize = 8;

      second = eatDSUntil( &parms, &pos, "," );
      dsRemoveOuterWS( &second );
      evalDS( &second );
      dynAr[dynNr++] = dsCopy( &second );
      pos++;
      while( parms.length > pos ) {
	first = eatDSUntil( &parms, &pos, "," );
	dsRemoveOuterWS( &first );
	evalDS( &first );
	pos++;
	if( dynNr == dynSize ) {
	  dynSize += 8;
	  dynAr = (dynstring *)memXRealloc( dynAr, sizeof( dynstring ) * dynSize );
	}
	dynAr[dynNr++] = dsCopy( &first );
      }
      
      evaldName = evalDS( &name );
      {
	inputReader ir = irNewDynstring(&evaldName, 0);

      envModifyOrAddBinding(globalEnvironment, &evaldName,
			    valueNewLambda(dynNr, 0, 0, dynAr, '%',
					   parParseIntoBCUntil(&ir, 0, 1,
							       globalEnvironment, 0),
					   globalEnvironment),
			    globalEnvironment);
      }
      return;
    }
    /* else no */
  } /* macro with arguments */

  evaldName = evalDS( &name );
  dsShrinkFront( &body, pos+1 );
  dsRemoveOuterWS( &body );    
  envModifyOrAddBinding(globalEnvironment, &evaldName,
			valueNewScalar(&body), globalEnvironment);
}