File: ctpp.C

package info (click to toggle)
tela 1.28-2
  • links: PTS
  • area: main
  • in suites: slink
  • size: 6,596 kB
  • ctags: 5,519
  • sloc: ansic: 14,013; cpp: 13,376; lex: 1,651; fortran: 1,048; yacc: 834; sh: 715; makefile: 464
file content (580 lines) | stat: -rw-r--r-- 15,211 bytes parent folder | download
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/*
 * This file is part of tela the Tensor Language.
 * Copyright (c) 1994-1996 Pekka Janhunen
 */

/*
  This is ctpp, C-tela preprocessor.
  It reads standard input and writes to standard output.
  It copies input to output, unless it sees constructs like
  [x,y] = f(a,b)
  which are replaced by corresponding tela-compatible C++ code.
  If it sees a left bracket, and if the last non-whitespace, non-comment
  and non-preprocessor line character before the left bracket was either
  semicolon (;) or right brace (}), it assumes that the left bracket
  begins the above construct.

  Following the pattern [x,y...] = f(a,b,...) must be a single
  ordinary C comment (it may also be absent). It is extracted
  to the help file, or if no help file is generated, file pointers
  to it will be saved so that the .ct source file will serve as
  help file. Following the comment is the function body.
  The end of the function body is signaled by a line beginning
  with '}'. No other line inside the body may contain the right
  brace as the first character. This convention makes it easy
  for ctpp to detect the end of the function body, without doing
  any parsing at all. If you follow the normal C coding practice,
  you will not be bothered by this seemingly ad hoc rule.
*/

#define FNAME_APPENDIX "function"
// The string "function" is appended to the C-tela function names, eg.
// if you declare []=myfunc(...) it's internal link name becomes myfuncfunction.
// If you refer to your own functions, you must follow this convention.
// Also, you should not change the definition above or you may have to find
// all places where you have called C-tela functions.
#define EXTERN_DEFINITION "extern \"C\" "

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#if STDC_HEADERS || HAVE_STRING_H
#  include <string.h>
#  if !STDC_HEADERS && HAVE_MEMORY_H
#    include <memory.h>
#  endif
#else
#  include <strings.h>
#  define strchr index
#  define strrchr rindex
#  define memcpy((d),(s),(n)) bcopy(s,d,n)
#  define memcmp((s1),(s2),(n)) bcmp(s1,s2,n)
#  define memset((s),(towhat),(n)) bzero(s,n); if (towhat) {cerr<<"bzero can only zero things\n"; exit(1);}
#endif

#ifdef CONVEX
extern "C" int remove(const char*);
#define tolower(c) ('A'<=(c)&&(c)<='Z'?(c)-'A'+'a':(c))
#endif

#define MAX_IDENTIFIER 256
#define MAX_NARGS 100
#define FilenameSeparator '/'
#define OUTSUFFIX "-.c"
#define HELPSUFFIX ".hlp"

#define GETC(ch) ch=getc(in); if (ch=='\n') line++
#define UNGETC(ch) if (ch=='\n') line--; ungetc(ch,in)

static int line = 1;	// keeps track of input line numbers
static int HelpFileFlag = 0;	// don't create help file by default

static void usage(FILE *f)
{
	fprintf(f,"Usage: ctpp [-h] [file.ct].\n");
	fprintf(f,"C-tela preprocessor. Preprocesses each file.ct, creating file-.c.\n");
	fprintf(f,"Options: -h will create help file(s) file.hlp. Use -h if you don't want\n");
	fprintf(f,"  to give users access to the source file.\n");
}

inline int iselectric(int ch) {return ch == '}' || ch == ';';}
inline int isIdentifierChar(int ch) {return isalpha(ch) || ch == '_';}

static void EmitLineNumber(FILE *out)
{
	fprintf(out,"#line %d\n",line);
}

static void ReadString(FILE *in, FILE *out, int endch)
{
	int ch;
	int allowbreak=1;
	for (;;) {
		GETC(ch);
		if (feof(in)) break;
		if (ch == '\\')
			allowbreak = 0;
		else if (ch == endch && allowbreak) {
			putc(ch,out);
			break;
		} else
			allowbreak = 1;
		putc(ch,out);
	}
}

static void ReadEndlineComment(FILE *in, FILE *out) {
	int ch;
	for (;;) {
		GETC(ch);
		if (feof(in)) break;
		if (ch == '\n') {
			putc(ch,out);
			break;
		}
		putc(ch,out);
	}
}

static void ReadOrdinaryComment(FILE *in, FILE *out) {
	int ch;
	int lastWasStar=0;
	for (;;) {
		GETC(ch);
		if (feof(in)) break;
		if (ch == '/' && lastWasStar) {
			putc(ch,out);
			break;
		}
		lastWasStar = ch == '*';
		putc(ch,out);
	}
}

static void ReadPPLine(FILE *in, FILE *out)
{
	int ch;
	int lastWasEscape=0;
	for (;;) {
		GETC(ch);
		if (feof(in)) break;
		if (ch == '\n' && !lastWasEscape) {
			putc(ch,out);
			break;
		}
		lastWasEscape = ch == '\\';
		putc(ch,out);
	}
}


static void syntaxerror(const char msg[])
{
	fprintf(stderr,"*** ctpp: Syntax error at line %d: %s\n",line,msg);
	exit(1);
}

static int nextNonWhiteSpace(FILE *in)
{
	int ch;
	for (;;) {
		GETC(ch);
		if (feof(in)) break;
		if (!isspace(ch)) break;
	}
	return ch;
}

#define LEFTPAREN '('
#define RIGHTPAREN ')'
#define LEFTBRACKET '['
#define RIGHTBRACKET ']'
#define COMMA ','
#define SEMICOLON ';'
#define EQUAL '='
#define ELLIPSIS 257
#define IDENTIFIER 258

typedef char Tidentifier[MAX_IDENTIFIER+1];

static Tidentifier theIdentifier;

static int lex(FILE *in)
{
	int ch;
	ch = nextNonWhiteSpace(in);
	switch (ch) {
	case LEFTPAREN:
	case RIGHTPAREN:
	case LEFTBRACKET:
	case RIGHTBRACKET:
	case COMMA:
	case SEMICOLON:
	case EQUAL:
		return ch;
	case '.':
		GETC(ch); if (ch!='.') syntaxerror("Read one '.' and expected second in arglist");
		GETC(ch); if (ch!='.') syntaxerror("Read two '.'s and expected third in arglist");
		return ELLIPSIS;
	default:
		{
			if (!isIdentifierChar(ch)) syntaxerror("Illegal character in arglist");
                        int i;
			for (i=0; ; i++) {
				theIdentifier[i] = ch;
				GETC(ch);
				if (!isdigit(ch) && !isIdentifierChar(ch)) {UNGETC(ch); break;}
				if (i >= MAX_IDENTIFIER) syntaxerror("Identifier too long");
			}
			theIdentifier[i+1] = '\0';
			return IDENTIFIER;
		}
	}
}

static void ExpectToken(FILE *in, int token)
{
	if (lex(in) != token) {
		char s[80];
		if (token == ELLIPSIS)
			sprintf(s,"'...' expected");
		else if (token == IDENTIFIER)
			sprintf(s,"Identifier expected");
		else
			sprintf(s,"'%c' expected",token);
		syntaxerror(s);
	}
}

typedef Tidentifier Targlist[MAX_NARGS];

Targlist out_arglist, in_arglist;	/* global vars */

static void ReadArglist(FILE *in, Targlist arglist, int& FirstOptional, int& HasEllipsisAtTheEnd, int& HasSemicolon)
{
	int token,i;
	FirstOptional = -1; HasEllipsisAtTheEnd = HasSemicolon = 0;
 start:
	token = lex(in);
	if (token == IDENTIFIER) {
		strcpy(arglist[i=0],theIdentifier);
		for (i=1; ; i++) {
			if (i >= MAX_NARGS) {
				syntaxerror("Too many arguments");
			}
			token = lex(in);
			if (token == COMMA) {
				ExpectToken(in,IDENTIFIER);
				strcpy(arglist[i],theIdentifier);
			} else if (token == SEMICOLON) {
				if (FirstOptional >= 0) syntaxerror("Only one semicolon per arglist");
				HasSemicolon = 1;
				FirstOptional = i;
				ExpectToken(in,IDENTIFIER);
				strcpy(arglist[i],theIdentifier);
			} else if (token == ELLIPSIS) {
				arglist[i][0] = '\0';
				HasEllipsisAtTheEnd = 1;
				FirstOptional = i;
				return;
			} else {
				UNGETC(token);
				arglist[i][0] = '\0';
				return;
			}
		}
	} else if (token == SEMICOLON) {
		FirstOptional = 0;
		HasSemicolon = 1;
		goto start;
	} else if (token == ELLIPSIS) {
		HasEllipsisAtTheEnd = 1;
		arglist[0][0] = '\0';
		return;
	} else {
		UNGETC(token);
		arglist[0][0] = '\0';
	}
} // ReadArglist

struct Tinfo;

struct Tinfo {
	Tinfo* next;
	Tidentifier name;
	int minin,minout, maxin,maxout;
	long helpstart,helpend;
};

static Tinfo *infos = 0;

static long HELP1=0, HELP2=0;	// global vars that record comment start and end positions from RecordComment

static void RecordComment(FILE *in, FILE *help)
{
	int ch;
	int lastWasStar=0;
	HELP2 = HELP1;	// so that if no help comment is generated, none will be displayed
	ch = nextNonWhiteSpace(in);
	if (ch != '/') {UNGETC(ch); return;}
	GETC(ch);
	if (ch != '*') return;
	int FirstTime = 1;
	for (;;) {
		GETC(ch);
		if (feof(in)) break;
		if (ch == '/')
			if (lastWasStar)
				break;
			else {
				if (HelpFileFlag)
					putc(ch,help);
				else {
					if (FirstTime) {
						HELP1=ftell(in);
						FirstTime = 0;
					}
					HELP2=ftell(in);
				}
			}
		if (lastWasStar) {
			if (HelpFileFlag)
				putc('*',help);
			else {
				if (FirstTime) {
					HELP1=ftell(in);
					FirstTime = 0;
				}
				HELP2=ftell(in);
			}
		}
		lastWasStar = ch == '*';
		if (!lastWasStar)
			if (HelpFileFlag)
				putc(ch,help);
			else {
				if (FirstTime) {
					HELP1=ftell(in);
					FirstTime = 0;
				}
				HELP2=ftell(in);
			}
	}
}

static void ReadFunctionBody(FILE *in, FILE *out)
/* Read input file until a line begins with '}' in the first position.
   This is thought to correspond to the end of function body.
   Echo characters to file out. The line following '}' is also read,
   up to newline. */
{
	int ch, LastWasNewline=0;
	// Loop until the pair "\n}" appears in input
	for (;;) {
		GETC(ch);
		if (feof(in)) return;
		putc(ch,out);
		if (ch=='\n')
			LastWasNewline=1;
		else if (ch=='}' && LastWasNewline)
			break;
		else
			LastWasNewline=0;
	}
	// Scan to next newline
	for (;;) {
		GETC(ch);
		if (feof(in)) return;
		putc(ch,out);
		if (ch=='\n') break;
	}
}

static void ReadPattern(FILE *in, FILE *out, FILE *help)
/*
  Recognize patterns of the form
  
  <arglist> ']' '=' IDENTIFIER '(' <arglist> ')'
  
  Also read in the following single ordinary C comment.
  Then read the following function body. The end of the
  function body is assumed to be a line beginning with '}'.
  The comment and the function body is echoed to file out.
*/
{
	int FirstOptionalIn,HasEllipsisIn;
	int FirstOptionalOut,HasEllipsisOut;
	int HasSemicolonOut,HasSemicolonIn;
	int minin,maxin,minout,maxout;
	Tidentifier fname;
	ReadArglist(in,out_arglist,FirstOptionalOut,HasEllipsisOut,HasSemicolonOut);
	ExpectToken(in,RIGHTBRACKET);
	ExpectToken(in,EQUAL);
	ExpectToken(in,IDENTIFIER);
	strcpy(fname,theIdentifier);
	ExpectToken(in,LEFTPAREN);
	ReadArglist(in,in_arglist,FirstOptionalIn,HasEllipsisIn,HasSemicolonIn);
	ExpectToken(in,RIGHTPAREN);
	fprintf(out,"\n");
	maxout = 0;
        int i;
	for (i=0; out_arglist[i][0]; i++) {
		fprintf(out,"#define %s (*(argout[%d]))\n",out_arglist[i],i);
		maxout++;
	}
	if (HasSemicolonOut) {
		minout = (FirstOptionalOut>=0) ? FirstOptionalOut : maxout;
		if (HasEllipsisOut && (FirstOptionalOut < 0)) minout = 0;
	} else {
		minout = maxout;	// if output list has no semicolon, all args are obligatory
	}
	if (HasEllipsisOut) maxout = -1;
	maxin = 0;
	for (i=0; in_arglist[i][0]; i++) {
		fprintf(out,"#define %s (*(argin[%d]))\n",in_arglist[i],i);
		maxin++;
	}
	minin = (FirstOptionalIn>=0) ? FirstOptionalIn : maxin;
	if (HasEllipsisIn && (FirstOptionalIn < 0)) minin = 0;
	if (HasEllipsisIn) maxin = -1;
	fprintf(out,"%sint %s%s\n\t(const TConstObjectPtr argin[], const int Nargin, const TObjectPtr argout[], const int Nargout)\n",
			EXTERN_DEFINITION,fname,FNAME_APPENDIX);
	static Tinfo *lastinfo=0, *newinfo=0;
	if (infos) {
		newinfo = new Tinfo;
		lastinfo->next = newinfo;
		lastinfo = newinfo;
	} else
		infos = lastinfo = newinfo = new Tinfo;
	newinfo->next = 0;
	strcpy(newinfo->name,fname);
	newinfo->minin = minin;
	newinfo->maxin = maxin;
	newinfo->minout = minout;
	newinfo->maxout = maxout;
	if (HelpFileFlag) {
		newinfo->helpstart = ftell(help);
		RecordComment(in,help);
		newinfo->helpend = ftell(help);
	} else {
		RecordComment(in,help);
		newinfo->helpstart = HELP1;
		newinfo->helpend = HELP2;
	}
	EmitLineNumber(out);
	ReadFunctionBody(in,out);
	// Undefine the parameters after the function body has been read
	for (i=0; out_arglist[i][0]; i++)
		fprintf(out,"#undef %s\n",out_arglist[i]);
	for (i=0; in_arglist[i][0]; i++)
		fprintf(out,"#undef %s\n",in_arglist[i]);
	// The undef's are extra lines, correct line number
	EmitLineNumber(out);
}

static void WriteInfo(FILE *out, char name[])
{
	fprintf(out,"extern \"C\" {\nTCFunctionInfo fninfo_%s[] = {\n",name);
	for (Tinfo*p=infos; p; p=p->next) {
		fprintf(out,"{(Tchar*)\"%s\",(Tchar*)helpname,%s%s, %d,%d,%d,%d, %ld,%ld},\n",
				p->name,p->name,FNAME_APPENDIX,p->minin,p->minout,p->maxin,p->maxout,p->helpstart,p->helpend);
	}
	fprintf(out,"{0,0, 0,0,0,0, 0,0}\n};\n}\n\n");
}

static void parse(FILE *in, FILE *out, FILE *help)
{
	int ch;
	int lastWasElectric=1;
	int lastWasNewline=1;
	int lastWasSlash=0;
	
	for (;;) {
		
		GETC(ch);
		if (feof(in)) break;
		
		if (lastWasElectric && ch == '[') {
			ReadPattern(in,out,help);
			lastWasElectric = 1;
			    // ReadPattern reads the function body, which ends with 'electric' right brace
		} else if (ch == '#' && lastWasNewline) {
			putc(ch,out);
			ReadPPLine(in,out);
		} else if (ch == '*' && lastWasSlash) {
			putc(ch,out);
			ReadOrdinaryComment(in,out);
		} else if (ch == '/' && lastWasSlash) {
			putc(ch,out);
			ReadEndlineComment(in,out);
		} else if (ch == '"') {
			putc(ch,out);
			ReadString(in,out,'"');
			lastWasElectric = 0;
		} else if (ch == '\'') {
			putc(ch,out);
			ReadString(in,out,'\'');
			lastWasElectric = 0;
		} else {
			putc(ch,out);
			lastWasNewline = ch == '\n';
			lastWasSlash = ch == '/';
			if (iselectric(ch))
				lastWasElectric = 1;
			else if (!isspace(ch) && !lastWasSlash)
				lastWasElectric = 0;
		}
	}
}

static char *MakeOutname(char *inname, char *suffix)
{
	int L = strlen(inname);
	if (tolower(inname[L-1])!='t' || tolower(inname[L-2])!='c' || tolower(inname[L-3])!='.') {
		fprintf(stderr,"*** ctpp: input files should end with .ct: '%s'\n",inname);
		exit(1);
	}
	char *outname = new char [L+1-3+strlen(suffix)];
	strcpy(outname,inname);
	strcpy(outname+L-3,suffix);
	return outname;
}

static char *ExtractFilenameBody(char *s)
{
	// s MUST end with ".ct"
	int L = strlen(s);
	int L1 = 0;
        int i;
	for (i=L-4; i>=0 && s[i]!=FilenameSeparator; i--) L1++;
	char *result = new char [L1+1];
	int j=0;
	for (i=L-3-L1; i<=L-4; i++,j++) result[j] = s[i];
	result[j] = '\0';
	return result;
}

int main(int argc, char *argv[])
{
	if (argc <= 1) {usage(stderr); return 1;}
	for (int i=1; i<argc; i++) {
		static char *outsuffix = OUTSUFFIX;
		static char *helpsuffix = HELPSUFFIX;
		if (argv[i][0] == '-') {
			if (!strcmp(argv[i],"-h")) HelpFileFlag = 1;
			else {fprintf(stderr,"ctpp: unrecognized option '%s' ignored\n",argv[i]);}
		} else {
			char *outname = MakeOutname(argv[i],outsuffix);
			char *helpname = MakeOutname(argv[i],helpsuffix);
			FILE *in = fopen(argv[i],"r");
			if (!in) {fprintf(stderr,"*** ctpp: could not open '%s' for reading\n",argv[i]); return 1;}
			FILE *out = fopen(outname,"w");
			if (!out) {fprintf(stderr,"*** ctpp: could not open '%s' for writing\n",outname); return 1;}
			FILE *help=0;
			long help0;
			if (HelpFileFlag) {
				help = fopen(helpname,"w");
				if (!help) {fprintf(stderr,"*** ctpp: could not open '%s' for writing\n",helpname); return 1;}
				help0 = ftell(help);
			} else
				help0 = ftell(in);
			fprintf(out,"#include \"symbol.H\"\n#include \"objarithm.H\"\n#include \"common.H\"\n\n");
			fprintf(out,"#line 1 \"%s\"\n",argv[i]);
			parse(in,out,help);
			fprintf(out,"\n#ifdef helpname\n#  undef helpname\n#endif\n");	// ensure that 'helpname' is not a macro
			fprintf(out,"\nstatic char helpname[] = \"%s\";\n\n",HelpFileFlag ? helpname : argv[i]);
			WriteInfo(out,ExtractFilenameBody(argv[i]));
			fclose(in);
			fclose(out);
			if (HelpFileFlag) {
				long help1 = ftell(help);
				if (help1 == help0) {
					fclose(help);
					remove(helpname);	// if helpfile is empty, remove it
				} else
					fclose(help);
			}
		}
	}
	return 0;
}