-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
407 lines (368 loc) · 10.5 KB
/
functions.php
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
<?php
function process_command($line, $cmd, $data, $debug=FALSE){
$output = '';
$cmds = array('LOAD', 'STORE', 'IN', 'ADD', 'SUBTRACT', 'MULTIPLY', 'DIVIDE', 'JUMP', 'JIZERO', 'JINEG', 'PRINT', 'OUT', 'LINE', 'HALT');
$cmdsp = array('LOAD', 'STORE', 'ADD', 'SUBTRACT', 'MULTIPLY', 'DIVIDE', 'JUMP', 'JIZERO', 'JINEG', 'PRINT');
switch ($cmd){
case 'exit':
die;
break;
case 'load':
if (strpos($line,' ')===false){
return array($data, 'You must enter a filename'.PHP_EOL, $debug);
}else{
$filename = trim(substr($line, strpos($line,' ')+1));
return array(load($filename),'', $debug);
}
break;
case 'save':
if (strpos($line,' ')===false){
return array($data, 'You must enter a filename'.PHP_EOL, $debug);
}else{
$filename = trim(substr($line, strpos($line,' ')+1));
save($filename, serialize($data));
}
break;
case 'new':
$i = 0;
unset($data);
unset($value);
while(true){
echo '>';
$line = fgets(STDIN);
// have we done?
if (trim($line)=='exit') break;
// parse the entered line
if (strpos($line,'"')!==FALSE){
$text = trim(substr($line,strpos($line,'"')+1,(strlen($line)-strpos($line,'"'))-3));
$line = trim(substr($line,0,strpos($line,'"')));
}else{
$text = '';
$line = trim($line);
}
$split = split_line($line);
if ($debug) echo 'Line *'.$line.'*'.PHP_EOL;
if ($debug) echo 'Text *'.$text.'*'.PHP_EOL;
// store the line depending on how many tokens have been entered
switch (count($split)){
case 0:
echo 'You don\'t seem to have entered a valid CESIL command.'.PHP_EOL;
break;
case 1:
// check that the command given is valid
if (in_array($split[0], $cmds)){
// does the command need a parameter?
if (in_array($split[0], $cmdsp) && (empty($text))){
echo $split[0].' requires a parameter.'.PHP_EOL;
break;
}else{
$data[$i]['label'] = '';
$data[$i]['cmd'] = $split[0];
if (empty($text)){
$data[$i]['goto'] = '';
}else{
$data[$i]['goto'] = $text;
}
$i++;
}
}else{
echo $split[0].' is not a valid CESIL command.'.PHP_EOL;
}
break;
case 2:
// check that the command given is valid
if (in_array($split[0], $cmds)){
$data[$i]['label'] = '';
$data[$i]['cmd'] = $split[0];
if (empty($text)){
$data[$i]['goto'] = $split[1];
}else{
$data[$i]['goto'] = $text;
}
$i++;
}elseif (in_array($split[1], $cmds)){
// does the command need a parameter?
if (in_array($split[1], $cmdsp) && (empty($text))){
echo $split[1].' requires a parameter.'.PHP_EOL;
break;
}else{
$data[$i]['label'] = $split[0];
$data[$i]['cmd'] = $split[1];
if (empty($text)){
$data[$i]['goto'] = $split[2];
}else{
$data[$i]['goto'] = $text;
}
$i++;
}
}else{
echo $split[1].' is not a valid CESIL command.'.PHP_EOL;
}
break;
case 3:
// check that the command given is valid
if (in_array($split[1], $cmds)){
// does the command need a parameter?
if (in_array($split[1], $cmdsp) && (empty($text) && empty($split[2]))){
echo $split[1].' requires a parameter.'.PHP_EOL;
break;
}else{
$data[$i]['label'] = $split[0];
$data[$i]['cmd'] = $split[1];
if (empty($text)){
$data[$i]['goto'] = $split[2];
}else{
$data[$i]['goto'] = $text;
}
$i++;
}
}else{
echo trim($split[1]).' is not a valid CESIL command.'.PHP_EOL;
}
break;
}
}
break;
case 'list':
// reject if no script entered
if (!isset($data)){
$output .= 'No CESIL script loaded.'.PHP_EOL;
break;
}
// list the current script
for ($i = 0; $i<count($data); $i++){
$output .= substr('00'.$i,(strlen('00'.$i)-3),3)."\t".$data[$i]['label']."\t".$data[$i]['cmd']."\t".$data[$i]['goto'].PHP_EOL;
}
break;
case 'run':
// reject if no script entered
if (!isset($data)){
$output .= 'No CESIL script loaded.'.PHP_EOL;
break;
}
// run the script
run($data, $debug);
break;
case 'dir':
$dir = opendir('files');
while(($file = readdir($dir))){
if ($file != '.' && $file != '..'){
$output .= $file.PHP_EOL;
}
}
closedir($dir);
break;
case 'debug':
$debug = !$debug;
break;
case 'help':
$output .= 'debug - toggle debugging on or off'.PHP_EOL;
$output .= 'exit - leave the CESIL interpreter'.PHP_EOL;
$output .= 'help - displays this list'.PHP_EOL;
$output .= 'load - load a previously saved file'.PHP_EOL;
$output .= 'new - create a new program'.PHP_EOL;
$output .= 'run - execute a loaded CESIL script'.PHP_EOL;
$output .= 'dir - list all the script available'.PHP_EOL;
$output .= 'save - save a CESIL file'.PHP_EOL;
$output .= 'Valid CESIL commands are: ';
for ($i = 0; $i<count($cmds); $i++){
if ($i == count($cmds)-1){
$output .= $cmds[$i].PHP_EOL;
}else{
$output .= $cmds[$i].', ';
}
}
break;
default:
echo 'Command not recognised'.PHP_EOL;
}
return array($data, $output, $debug);
}
function run($data, $debug=FALSE){
// set the accumulator
$acc = 0;
$value = [];
$i = 0;
// run the script
while($i < count($data)){
if ($debug) echo 'Executing line '.$i.' '.$data[$i]['label']."\t".$data[$i]['cmd']."\t".$data[$i]['goto'].PHP_EOL;
switch (strtoupper($data[$i]['cmd'])){
case 'LOAD':
if (is_numeric($data[$i]['goto'])){
$acc = $data[$i]['goto'];
}else{
$acc = $value[$data[$i]['goto']];
}
break;
case 'STORE':
$value[$data[$i]['goto']] = $acc;
break;
case 'IN':
$in = '';
while (empty($in) || !is_numeric($in)){
echo '>';
$in = trim(fgets(STDIN));
}
$acc = $in;
break;
case 'ADD':
if (is_numeric($data[$i]['goto'])){
$acc = $acc + $data[$i]['goto'];
}else{
if (!isset($value[$data[$i]['goto']])){
echo $data[$i]['goto'].' hasn\'t been set.'.PHP_EOL;
break 2;
}else{
$acc = $acc + $value[$data[$i]['goto']];
}
}
break;
case 'SUBTRACT':
if (is_numeric($data[$i]['goto'])){
$acc = $acc - $data[$i]['goto'];
}else{
if (!isset($value[$data[$i]['goto']])){
echo $data[$i]['goto'].' hasn\'t been set.'.PHP_EOL;
break 2;
}else{
$acc = $acc - $value[$data[$i]['goto']];
}
}
break;
case 'MULTIPLY':
if (is_numeric($data[$i]['goto'])){
$acc = $acc * $data[$i]['goto'];
}{
if (!isset($value[$data[$i]['goto']])){
echo $data[$i]['goto'].' hasn\'t been set.'.PHP_EOL;
break 2;
}else{
$acc = $acc * $value[$data[$i]['goto']];
}
}
break;
case 'DIVIDE':
if (is_numeric($data[$i]['goto'])){
if ($data[$i]['goto']==0){
echo 'Division by zero.'.PHP_EOL;
break 2;
}
$acc = $acc / $data[$i]['goto'];
}else{
if (!isset($value[$data[$i]['goto']])){
echo $data[$i]['goto'].' hasn\'t been set.'.PHP_EOL;
break 2;
}else{
if ($value[$data[$i]['goto']]==0){
echo 'Division by zero.'.PHP_EOL;
break 2;
}
$acc = $acc / $value[$data[$i]['goto']];
}
}
break;
case 'JUMP':
// check the jump point exists
$newline = check_jump($data[$i]['goto'],$data);
if ($newline == 0){
echo 'Label '.$data[$i]['goto'].' hasn\'t been set.'.PHP_EOL;
break 2;
}else{
if ($debug) echo 'Jumping to line '.$newline.PHP_EOL;
$i = $newline-1;
}
break;
case 'JIZERO':
if ($acc != 0) break;
// check the jump point exists
$newline = check_jump($data[$i]['goto'],$data);
if ($newline == 0){
echo 'Label '.$data[$i]['goto'].' hasn\'t been set.'.PHP_EOL;
break 2;
}else{
if ($debug) echo 'Jumping to line '.$newline.PHP_EOL;
$i = $newline-1;
}
break;
case 'JINEG':
if ($acc >= 0) break;
// check the jump point exists
$newline = check_jump($data[$i]['goto'],$data);
if ($newline == 0){
echo 'Label '.$data[$i]['goto'].' hasn\'t been set.'.PHP_EOL;
break 2;
}else{
if ($debug) echo 'Jumping to line '.$newline.PHP_EOL;
$i = $newline-1;
}
break;
case 'PRINT':
// is it a variable?
if (isset($value[$data[$i]['goto']])){
echo $value[$data[$i]['goto']].PHP_EOL;
}else{
if (substr($data[$i]['goto'],0,1) == '"'){
echo substr(trim($data[$i]['goto']),1,strlen(trim($data[$i]['goto']))-2);
}else{
echo trim($data[$i]['goto']);
}
}
break;
case 'OUT':
echo $acc;
break;
case 'LINE':
echo PHP_EOL;
break;
case 'HALT':
break 2;
}
$i++;
}
}
function load($filename, $interactive=TRUE){
$data = '';
if (file_exists('/Users/neilthompson/Development/PHPCESIL/files/'.$filename.'.csl')){
$data = file_get_contents('files/'.$filename.'.csl', $data);
$data = unserialize($data);
// check that this is actually a CESIL file
if (isset($data[0]['label']) && isset($data[0]['cmd']) && isset($data[0]['goto'])){
if ($interactive) echo 'File '.$filename.' loaded successfully'.PHP_EOL;
return $data;
}else{
if ($interactive) echo 'File '.$filename.' not a valid CESIL file.'.PHP_EOL;
return '';
}
}else{
echo 'File not loaded'.PHP_EOL;
}
}
function save($filename, $data){
if (file_put_contents('files/'.$filename.'.csl', $data)){
echo 'File '.$filename.' saved successfully'.PHP_EOL;
}else{
echo 'File not saved'.PHP_EOL;
}
}
function check_jump($label,$data){
for ($i = 0; $i<count($data); $i++){
if (strtoupper($data[$i]['label'])==strtoupper(trim($label))){
return $i;
}
}
return 0;
}
function check_goto($split, $text){
// has a parameter been passed?
if (empty($text) && empty($split)) return '';
// select which to return
return (empty($text)) ? $split : $text;
}
function split_line($line){
$arr = explode(' ', strtoupper($line));
if (isset($arr[0])) $arr[0] = trim($arr[0]);
if (isset($arr[1])) $arr[1] = trim($arr[1]);
if (isset($arr[2])) $arr[2] = trim($arr[2]);
return $arr;
}
?>