/
scheme.php
452 lines (394 loc) · 12.1 KB
/
scheme.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
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
<?php
/*
lisphp is a lisp interperter written to take scheme r5 and turn it into php4+
Copyright (C) 2008 shaun gilchrist
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
define('__HSHt',true);
define('__HSHf',false);
define('N',"\n");
define('n',"\n");
require_once('system.inc.php');
function ipos($arr){
return is_array($arr) ? pos($arr) : $arr;
}
class Scheme{
var $strings = Array();
var $vars = Array();
var $methods;
var $scope;
var $macros = Array();
var $reserved = Array(
"'" => 'quote ',
'`' => 'quasiquote ',
',@' => 'unquotesplicing ',
',' => 'unquote ',
'(:=' => '(define',
'*' => '__MUL',
'+' => '__PLU',
'/' => '__DIV',
'?' => '__QUE',
'!' => '__EXC',
'#(' => '(__list ',
'(list' => '(__list',
'( list' => '(__list',
'#' => '__HSH',
'$' => '__DOL',
'^' => '__CAR',
'%' => '__PER',
'@' => '__ATS',
'&' => '__AMP',
'-' => '__DSH',
'<' => '__LST',
'>' => '__GRT',
'=' => '__EQL',
'\\' => '__SLS',
':' => '__COL',
'~' => '__TIL',
'.' => '__DOT',
'|' => '__PIP',
'[' => '__SQL',
']' => '__SQR',
'((' => '(iapply (',
'( (' => '(iapply (',
')(' => ') (');
function __iapply($args){
$method = array_shift($args);
if(is_array($method)){
$method = $this->to_code($method);
}
$alist = Array();
foreach($args as $a){
$alist[] = $this->to_code($a);
}
return "call_user_func_array(".
"{$method},Array(".implode(',',$alist)."))";
}
function add_var($var){
if(!isset($this->vars[$this->scope])) $this->vars[$this->scope] = Array();
$this->vars[$this->scope][$var] = $var;
return $this;
}
function get_var($var){
if(isset($this->vars[$this->scope]) && isset($this->vars[$this->scope][$var])) return true;
return false;
}
function get_method($meth){
return false;
}
function __define($args){
if(D) puts('DEFINE CALLED WITH',$args,N);
$sig = array_shift($args);
if(!is_array($sig)){
//DEFINE VAR
if(D)puts('DEFINE VAR');
$this->add_var($sig);
$body = $this->to_code($args);
if(is_array($body)){
$body = implode('',$body);
}
return "$".$sig." = ".$body;
} else {
//DEFINE METHOD
if(D)puts('DEFINE METHOD');
$name = array_shift($sig);
$alist = Array();
foreach($sig as $arg){
$this->add_var($arg);
$alist[] = '$'.$arg;
}
if(D)puts('ARGS ARE NOW:',$alist);
$body = $this->to_code($args);
if(D)puts('+++++BODY',$body);
if(is_array($body)){
$body = implode(';',$body).';';
} else {
$body = 'return '.$body.';';
}
$func = "function $name(".implode(',',$alist)."){".$body."}";
if(D)puts('COMPILED TO:',$func);
return $func;
}
}
function __lambda($args){
if(D)puts("LAMBDA TIME",$args);
$sig = array_shift($args);
$alist = Array();
foreach($sig as $arg)$alist[] = '$'.$arg;
$body = $this->to_code($args);
if(is_array($body) && is_array(pos($body))){
$body = implode(';',pos($body)).';';
} else {
$body = 'return '.ipos($body).';';
}
return "create_function('".implode(',',$alist)."','".
str_replace($sig, $alist, $body)."')";
}
function __let($args){
//expand to __lambda!
}
function __if($args){
if(D)puts("IF CALLED WITH ",$args);
$pred = $this->to_code(array_shift($args));
$r1 = $this->to_code(array_shift($args));
$r2 = $this->to_code(array_shift($args));
if(is_array($r1)){
$if = "if({$pred}){\n".implode(";\n",$r1).";\n}";
if($r2) $if .= "else{\n".implode(";\n",$r2).";\n}";
} else {
$r2 = $r2 ? $r2 : false;
$if = "({$pred} ? {$r1} : {$r2})";
}
if(D)puts("IF:",$if);
return $if;
}
function __cond($args){
if(D)puts('COND CALLED WITH',$args);
//transform into nested ternary!!!
$body = Array();
foreach($args as $clause){
//CHECK FOR ELSE
if(pos($clause) == 'else'){
array_shift($clause);
$body[] = '{'.$this->to_code($clause).';}';
} else {
array_shift($clause); //get rid of iapply as this is special form
if(D)puts("CLAUSE ",$clause);
$body[] = 'if('.$this->to_code(array_shift($clause)).
'){ return '.$this->to_code($clause).';}';
}
}
return Array(implode(' else ',$body));
}
function __begin($args,$buildreturn=false){
if(!$buildreturn) return $args;
$last = array_pop($args);
$args[] = 'return '.$last;
return $args;
}
function __quote($args){
if(D) puts('QUOTE:',$args);
if(is_scalar($args)) return "'$args'";
$args = $this->to_code($args);
return is_array($args) ? var_export($args,1) : (is_string($args) ? "'{$args}'" : $args);
}
function __and($args){
}
function __or($args){
}
function __not($args){
}
public function parse($string){
$this->scope = 0;
$this->vars[0] = array_keys($GLOBALS);
//strip comments!! i.e. ;comment, ;;;comment etc. up til the new line!
if(D)puts($string);
//strip the strings!
$max = strlen($string);
while(($start = strpos($string, '"')) !== false){
$end = false;
$pos = $start;
$skip = false;
while(!$end || $pos < $max){
$char = $string{++$pos};
if($char == '"' && !$skip){
$sub = substr($string, $start, ($pos-$start)+1);
puts($sub);
$key = count($this->strings);
$string = str_replace($sub, '~~STRING_'.$key.'~~', $string);
$this->strings[$key] = $sub;
$end = $pos;
break;
}
$skip = ($char =='\\');
}
}
eval('$x = Array('.str_replace(
'(',
'Array(',
preg_replace(
'/([a-z0-9_]+)/i',
'\'${1}\'',
str_replace(
' ',
',',
str_replace(
array_keys($this->reserved),
$this->reserved,
trim(
ereg_replace(
' +',
' ',
str_replace(
Array("\r\n","\r","\n","\t"),
' ',
$string))))))).');');
$x = $this->to_quote($x);
$x = $this->find_macros($x);
$x = $this->expand_macros($x);
if(D) puts($x);
$code = $this->to_code($x);
$x = is_array($code) ? implode(";\n", $this->to_code($x)) : $code.";\n";
foreach($this->strings as $key => $val){
$x = str_replace('__TIL__TILSTRING_'.$key.'__TIL__TIL', $val, $x);
}
return $x;
}
function to_quote($ast,$tab=0){
$tree = Array();
$skip = Array();
$quote_types = Array('quote','quasiquote','unquote','unquotesplicing');
foreach($ast as $key => $val){
if(in_array($key, $skip)) continue;
if(D) puts(str_repeat("\t",$tab)."TEST $key and $val");
if(in_array($val, $quote_types) && isset($ast[$key+1])){
$val = Array('__'.$val, is_array($ast[$key+1]) ? $this->to_quote($ast[$key+1],$tab+1) : $ast[$key+1]);
$skip[] = $key+1;
} else if(is_array($val)){
$val = $this->to_quote($val,$tab+1);
}
$tree[] = $val;
}
return $tree;
}
function find_macros($ast){
//find macros! add to list of macros and remove from AST
//let-syntax, letrec-syntax, define-syntax, syntax-rules
//assumes macros are only on "base level"
foreach($ast as $key => $leaf){
if(pos($leaf) == 'define__DSHmacro'){
$this->macros[array_shift($leaf[1])] = Array(
'args' => $leaf[1],
'body' => $leaf[2][1]); //**ASSUMING [2][1] IS A QUASIQUOTE? CHECK FOR THIS
unset($ast[$key]);
}
}
return $ast;
}
function macro_replace_vars($branch, $vars){
$new_branch = Array();
foreach($branch as $leaf){
if(D) puts('check leaf:',$leaf);
if(is_array($leaf)){
if(pos($leaf) == '__unquote'){
$new_branch[] = $vars[$leaf[1]];
} else if(pos($leaf) == '__unquotesplicing'){
foreach($vars[$leaf[1]] as $part){
$new_branch[] = $part;
}
} else {
$new_branch[] = $this->macro_replace_vars($leaf, $vars);
}
} else {
$new_branch[] = $leaf;
}
}
if(D) puts("\n","NEW BRANCH:",$new_branch,"\n");
return $new_branch;
}
function expand_macros($ast){
$new_tree = Array();
if(D) puts("\n", "PRE-EXPAND AST",$ast,"\n");
foreach($ast as $key => $leaf){
//**CHECK FOR RECURSIVE EXPANDING
if(isset($this->macros[pos($leaf)])){
$macro = $this->macros[pos($leaf)];
if(D) puts('MACRO DEFINITION:',$macro);
if(D) puts('FOUND MACRO - EXPAND');
if(D) puts('EXPAND THIS:',$leaf);
//shift off macro name
$macro_name = array_shift($leaf);
//populate vars for macro
$vars = Array();
$dotted = false;
foreach($macro['args'] as $arg){
if($arg == '__DOT')
$dotted = true;
else
$vars[$arg] = $dotted ? $leaf : array_shift($leaf);
}
//**CHECK FOR MISSING EXPECTED VARS, THROW EXCEPTION!
if(D) puts("\n",'VARS:',$vars,"\n");
$leaf = $this->macro_replace_vars($macro['body'], $vars);
}
$new_tree[] = $leaf;
}
if(D) puts("\n","NEW POST MACRO TREE",$new_tree,"\n");
return $new_tree;
}
function __call($meth, $args){
if(D)puts("CALLED FOR:",$meth);
if(D)puts("WITH ARGS:",$args);
$alist = is_array($args) ? pos($args) : $args;
if(D)puts('SO APPLY',$meth,' TO ',$alist,N);
$call = $meth.'('.implode(',',$alist).')';
if(D)puts('GENERATED:',$call);
return $call;
}
function to_code($ast,$t='-'){
if(D)puts($t.'AST:',$ast);
if(is_array($ast) && count($ast) == 1 && !$this->get_method(pos($ast))) $ast = pos($ast);
//IS IT AN ATOM?
if(!is_array($ast)){
//is it a float or negative number?
$f = str_replace(Array('__DOT','__DSH'),Array('.','-'),$ast);
if(is_numeric($f)) return $f;
return $this->get_var($ast) && !is_numeric($ast) ? '$'.$ast:$ast;
}
//IS IT A SINGLE LIST?
if(!is_array(pos($ast))){
$ast = Array($ast);
}
//CREATE PARSED AST
$code = Array();
$special_forms = Array('__quote', 'lambda', 'define', 'cond', 'if', 'and', 'or', 'not', 'iapply');
foreach($ast as $node){
if(D)puts(N,$t.'CHECK:',$node,N);
$method = array_shift($node);
if(in_array($method, $special_forms)){
$args = $node;
$method = '__'.$method;
} else {
$args = Array();
foreach((array)$node as $arg){
$args[] = $this->to_code($arg);
}
if(D)puts('BLIND',$args);
}
$code[] = $this->$method(empty($args) ? NULL : $args);
}
return count($code) == 1 ? array_shift($code) : $code;
}
}
if(isset($argv)){
$file = array_pop($argv);
if($file == '-d'){
define('D', true);
$file = array_pop($argv);
}
if(!defined('D'))define('D',false);
if(file_exists($file)) {
$contents = file_get_contents($file);
if(!empty($contents)) {
$S = new Scheme();
$code = $S->parse($contents).';'.N;
if(D)puts($code);
eval($code);
} else {
echo "file was empty".N;
}
} else {
echo "Could not find a file to parse.".N;
}
}
?>