-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmenu.c
326 lines (306 loc) · 10.6 KB
/
menu.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
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
/***************************************************************************/
/* This module implements the menus to select the desired garbage strategy.*/
/* It may be: */
/* - garbage collection performed whenever possible. */
/* - garbage collection attempted only if the number of operators in the */
/* graph reaches a certain limit. In this case the user is asked to */
/* choose such limit. */
/* The only external functions are: */
/* - menu(): It activates the different menus depending on user's choice. */
/* - info(): It allows the user to get informations about the computation. */
/* The external functions are: */
/* - do_menu1(): It prints the first menu, asking the user which strategy */
/* he wants to use. */
/* - do_menu2(): If the second strategyhas been chosen, this function */
/* shows some possible limits for memory occupation and asks */
/* the user to select one of them. */
/* - do_menu3(): It is used to insert a limit different from the ones */
/* listed by the previous function. */
/* */
/***************************************************************************/
/***************************************************************************/
/* 1. Inclusion of header files. */
/***************************************************************************/
#include "bohm.h"
#include <stdio.h>
#include <stdlib.h>
static bool do_menu1();
static bool do_menu2();
static void do_menu3();
static int take_char();
/***************************************************************************/
/* 2. Definitions of variables to be exported. */
/***************************************************************************/
int option; /* Initial option selected */
unsigned limit; /* Limit for allocable operators (only for option 2) */
bool seetime;
bool seenumber;
bool seenode;
bool seegarb;
/****************************************************************************/
/* 3. Definitions of functions to be exported. */
/****************************************************************************/
void menu(choice)
int choice;
{
if (choice == 1) option = 1;
else if (choice > 1) /* we interpret this value as max */
/* allocation before invoking g.c. */
{option = 2; limit = choice;}
else
if(do_menu1())
if(do_menu2())
do_menu3();
printf("***********************************************************\n");
printf("* O.K. Your choice has been recorded . . . *\n");
printf("***********************************************************\n\n");
}
void info(choices)
char *choices;
{
bool nomoreargs;
int select;
nomoreargs = choices[0] == '\0';
if (nomoreargs) select = 'x'; else select = choices[0];
while((select!='y')&&(select!='n')){
printf("Display time?\n");
printf("Please answer 'y' or 'n' (type 'h' for help).\n\n>");
select = take_char();
printf("\n");
switch(select){
case 'y': break;
case 'n': break;
case 'h':
printf("HELP . . .\n\n");
printf("Answering 'y', the interpreter will display, after\n");
printf("each reduction, the elapsed time, divided into:\n");
printf("- User time (given in seconds).\n");
printf("- System time (given in seconds).\n");
break;
default:
printf("Illegal answer (type 'h' for help) . . .\n");
}
printf("\n");
}
switch(select){
case 'y':
seetime=true; break;
case 'n':
seetime=false;
}
nomoreargs = nomoreargs || choices[1] == '\0';
if (nomoreargs) select = 'x'; else select = choices[1];
while((select!='y')&&(select!='n')){
printf("Display number of interactions?\n");
printf("Please answer 'y' or 'n' (type 'h' for help).\n\n>");
select = take_char();
printf("\n");
switch(select){
case 'y': break;
case 'n': break;
case 'h':
printf("HELP . . .\n\n");
printf("Answering 'y', the interpreter will display,\n");
printf("after each reduction, the following informations:\n");
printf("- Total number of interactions.\n");
printf(" (but the garbage ones)\n");
printf("- Total number of family redex reductions.\n");
printf("- Total number of proper interactions.\n");
printf(" (those involving not-structural nodes\n");
break;
default:
printf("Illegal answer (type 'h' for help) . . .\n");
}
printf("\n");
}
switch(select){
case 'y':
seenumber=true; break;
case 'n':
seenumber=false;
}
nomoreargs = nomoreargs || choices[2] == '\0';
if (nomoreargs) select = 'x'; else select = choices[2];
while((select!='y')&&(select!='n')){
printf("Display memory occupation informations ?\n");
printf("Please answer 'y' or 'n' (type 'h' for help).\n\n>");
select = take_char();
printf("\n");
switch(select){
case 'y': break;
case 'n': break;
case 'h':
printf("HELP . . .\n\n");
printf("Answering 'y', the program will display\n");
printf("the following informations:\n");
printf("- Initial number of nodes\n");
printf(" (at the beginning of every reduction).\n");
printf("- Maximum number of nodes\n");
printf(" (existing at the same time during the\n");
printf(" whole program execution).\n");
printf("- Final number of nodes\n");
printf(" (at the end of every reduction).\n");
break;
default:
printf("Illegal answer (type 'h' for help) . . .\n");
}
printf("\n");
};
switch(select){
case 'y':
seenode=true; break;
case 'n':
seenode=false;
};
nomoreargs = nomoreargs || choices[3] == '\0';
if (nomoreargs) select = 'x'; else select = choices[3];
while((select!='y')&&(select!='n')){
printf("Display garbage collection informations ?\n");
printf("Please answer 'y' or 'n' (type 'h' for help).\n\n>");
select = take_char();
printf("\n");
switch(select){
case 'y': break;
case 'n': break;
case 'h':
printf("HELP . . .\n\n");
printf("Answering 'y' the program will display\n");
printf("the following informations:\n");
printf("- Number of erasing operations required\n");
printf(" for eliminating the previous term and\n");
printf(" the corresponding time.\n");
printf("- Number of erasing operations performed\n");
printf(" during the actual reduction, the\n");
printf(" corresponding time, and the number\n");
printf(" of garbage collection activations\n");
printf("- Number of erasing operations done\n");
printf(" in collecting disconnected parts of the\n");
printf(" term.\n");
break;
default:
printf("Illegal answer (type 'h' for help) . . .\n");
}
printf("\n");
};
switch(select){
case 'y':
seegarb=true; break;
case 'n':
seegarb=false;
};
system("clear");
printf("***********************************************************\n");
printf("* O.K. Your choice has been recorded . . . *\n");
printf("***********************************************************\n\n");
}
/****************************************************************************/
/* 4. Definitions of functions strictly local to the module. */
/****************************************************************************/
static
int take_char()
{
int taken;
bool warning;
do{
warning=false;
do
taken = getchar();
while (taken == '\n');
while(getchar()!='\n')
warning=true;
if(warning){
printf("\nOne-character answer is required.\n");
printf("Answer again please . . .\n\n>");
}
}while(warning);
return(taken);
}
static
bool do_menu1()
{
bool cont = false;
option=0;
while((option!=1)&&(option!=2)&&(option!=3)){
printf("Please choose one of the following strategies:\n");
printf("1. Garbage collection performed whenever possible.\n");
printf("2. Garbage collection depending on memory occupation.\n");
printf("3. No garbage collection.\n");
printf("4. Help . . .\n\n>");
scanf("%d",&option);
while(getchar()!='\n');
system("clear");
if(option==4){
printf("HELP . . .\n\n");
printf("Choosing option 1. garbage collection will be\n");
printf("invoked whenever possible. \n");
printf("With option 2. g.c. will be attempted whenever\n");
printf("the number of allocated nodes reaches a certain\n");
printf("limit that you may specify using a menu. \n");
printf("Both options support the possibility of erasing\n");
printf("the previous term when a new reduction is started\n");
printf("(obviously, this is done only if the previous\n");
printf("term wasn't a global declaration. \n");
printf("Option 3. prevents g.c. if not explicitly called\n");
printf("by the user via the '#garbage' directive.\n\n");
}else if((option!=1)&&(option!=2)&&(option!=3))
printf("Illegal option . . .\n\n");
}
if(option==2)
cont=true;
return(cont);
}
static
bool do_menu2()
{
bool cont=false;
int select=0;
while((select!=1)&&(select!=2)&&(select!=3)&&(select!=4)){
printf("Please choose a limit for memory occupation:\n");
printf("1. 5000 operators.\n");
printf("2. 10000 operators.\n");
printf("3. 20000 operators.\n");
printf("4. others.\n");
printf("5. Help . . .\n\n>");
scanf("%d",&select);
while(getchar()!='\n');
system("clear");
if(select==5){
printf("HELP . . .\n\n");
printf("Options 1. 2. 3. fix the upper bound to the\n");
printf("specified value.\n");
printf("Option 4. allows you to input a different value.\n");
}else if((select!=1)&&(select!=2)&&(select!=3)&&(select!=4))
printf("Illegal option . . .\n\n");
}
switch(select){
case 1:
limit=5000;
break;
case 2:
limit=10000;
break;
case 3:
limit=20000;
break;
case 4:
cont=true;
break;
}
return(cont);
}
static void
do_menu3()
{
while((limit<1000)||(limit>50000)){
printf("Please insert an upper bound for memory utililization:\n>");
scanf("%u",&limit);
while(getchar()!='\n');
system("clear");
if((limit<1000)||(limit>50000)){
limit = 0;
printf("You have to fix an upper bound for the number of\n");
printf("nodes, before garbage collection is attempted.\n");
printf("The possible range is from 1000 to 50000.\n\n");
}
}
}