-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathjff-algol.c
executable file
·206 lines (176 loc) · 4.81 KB
/
jff-algol.c
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
#
//
// jff_a2c
// a simple translator from Algol 60 to plain C
// just for fun, but with educational value
//
// Version 2.1
// Copyright:
// Jan van Katwijk
// TU Delft
// The Netherlands
// July 2002
//
//
//
// jff-algol, a driver program for jff-a2c
//
// Copyright (C) 2002, 2003 J. van Katwijk
//
// 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, 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, you can either send email to this
// program's maintainer or write to: The Free Software Foundation,
// Inc.; 59 Temple Place, Suite 330; Boston, MA 02111-1307, USA.
//
//
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
char *infile = NULL;
char *c_file = NULL;
char *tmp_file = "tmpfile";
char *outfile = NULL;
char *include = NULL;
char *header_file = HEADER;
char *operator = OPERATOR_E;
char *prelude = PRELUDE_E;
char *runtime = RUNTIME;
char *jff_a2c = ACOMPILER;
char *c_compiler = CCOMPILER;
int indent = 1;
int delete_files = 0;
int trace = 0;
int only_frontend = 0;
int languageFlag = 0;
static char v0 [100];
static char v1 [100];
static char v2 [100];
static char v3 [100];
int process_filenames (char *infile) {
char *tail;
if (infile == (char *)0)
return 0;
sprintf (v0, "%s", infile);
tail = strstr (v0, ".alg");
if ((tail == (char *)0) || (tail [4] != (char)0)) {
fprintf (stderr, "File %s does not end with \".alg\"\n", infile);
return 0;
}
tail [0] = (char)0;
sprintf (v1, "%s.c", v0);
c_file = v1;
sprintf (v2, "%s.h", v0);
include = v2;
sprintf (v3, "%s", v0);
outfile = v3;
return 1;
}
void process_file () {
char v [1024];
time_t current_time;
char compile_time [255];
int error_code;
current_time = time (NULL);
memcpy (compile_time, ctime (¤t_time), 24);
compile_time [24] = (char)0;
if (process_filenames (infile) == 0)
return;
sprintf (v, " %s -F %d -c %s -f %s -o %s -p %s -h %s -t \"%s\" %s",
jff_a2c,
languageFlag,
tmp_file,
include,
operator,
prelude,
header_file,
compile_time,
infile);
if (trace)
fprintf (stderr, "Compiling with command %s\n", v);
fflush (stderr);
error_code = system (v);
if (error_code != 0 ) {
fprintf (stderr,
"Error code %d, compiling %s aborted after front end\n",
error_code,
infile);
return;
}
if (only_frontend)
return;
if (indent && !delete_files) {
sprintf (v, "%s %s -o %s", INDENT, tmp_file, c_file);
system (v);
unlink (tmp_file);
}
else {
sprintf (v, "mv %s %s", tmp_file, c_file);
system (v);
}
sprintf (v, "%s -O3 -w -o %s %s %s -lm",
c_compiler, outfile, c_file, runtime);
if (trace)
fprintf (stderr, "Processing the result %s\n", v);
system (v);
if (delete_files) {
unlink (include);
unlink (c_file);
}
}
int main (int argc, char **argv) {
static char v [100];
struct stat x;
int i;
for (i = 1; i < argc; i ++) {
if (argv [i][0] == '-') { // it is an option
switch (argv [i][1]) {
case 's': operator = argv [i + 1]; i ++;
break;
case 'F': operator = OPERATOR_F;
prelude = PRELUDE_F;
languageFlag = 1;
break;
case 'p': prelude = argv [i + 1]; i ++;
break;
case 'h': header_file = argv [i + 1]; i ++;
break;
case 'i': indent = 0;
break;
case 'v': trace = 1;
break;
case 'r': delete_files = 1;
break;
case 'f': runtime = argv [i + 1]; i ++;
break;
case 'o': only_frontend = 1;
break;
default: break;
}
}
else {
// it is the name of the file to be compiled
infile = argv [i];
if (indent && !delete_files) {
sprintf (v, "%s", INDENT);
if (stat (v, &x) == -1)
indent = 0;
}
process_file ();
}
}
exit (0);
}