/
libfructose.php
2402 lines (2303 loc) · 64.7 KB
/
libfructose.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
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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
Copyright (c) 2011 Charlie Somerville
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
$GLOBALS['_stack'] = array();
$GLOBALS['_lambda_objs'] = array();
$GLOBALS['_locals'] = new stdClass;
$GLOBALS['_locals']->self = new F_Object;
$GLOBALS['_gthis'] = $_locals->self;
$GLOBALS['_globals'] = array();
class ReturnFromBlock extends Exception
{
public $val;
function __construct($val)
{
$this->val = $val;
}
}
class ErrorCarrier extends Exception
{
public $val;
function __construct($val)
{
$this->val = $val;
}
}
function _exception_handler($ex)
{
header("HTTP/1.1 500 Internal Server Error");
?>
<!DOCTYPE html>
<html>
<head>
<title>Runtime Error</title>
<style>
body {
font-family:Tahoma, Verdana, Arial, Helvetica, sans-serif;
}
h1 {
padding:16px;
background-color:#ff8e55;
color:#441700;
}
div {
font-family:Consolas, Monaco, monospace;
font-size:12px;
line-height:18px;
}
</style>
</head>
<body>
<h1>Runtime Error in <?php echo htmlspecialchars($_SERVER['FRUCTOSE_SRC']); ?></h1>
<div>
Unhandled exception: <?php echo htmlspecialchars(get_class($ex->val)); ?><br />
<?php echo htmlspecialchars($ex->val->F_to_s(NULL)->__STRING); ?><br/>
<?php
foreach($ex->getTrace() as $frame)
{
echo "at " . htmlspecialchars($frame['function']) . ' in ' . htmlspecialchars($frame['file']) . ' on line ' . $frame['line'] . "<br />";
}
?>
</div>
</body>
</html>
<?php
exit;
}
set_exception_handler('_exception_handler');
$_operator_lookup["!"] = "__operator_not";
$_operator_lookup["~"] = "__operator_bitwisenot";
$_operator_lookup["+@"] = "__operator_unaryplus";
$_operator_lookup["**"] = "__operator_exp";
$_operator_lookup["-@"] = "__operator_unaryminus";
$_operator_lookup["*"] = "__operator_mul";
$_operator_lookup["/"] = "__operator_div";
$_operator_lookup["%"] = "__operator_mod";
$_operator_lookup["+"] = "__operator_add";
$_operator_lookup["-"] = "__operator_sub";
$_operator_lookup["<<"] = "__operator_lshift";
$_operator_lookup[">>"] = "__operator_rshift";
$_operator_lookup["&"] = "__operator_bitwiseand";
$_operator_lookup["|"] = "__operator_bitwiseor";
$_operator_lookup["^"] = "__operator_xor";
$_operator_lookup["<"] = "__operator_lt";
$_operator_lookup["<="] = "__operator_lte";
$_operator_lookup[">"] = "__operator_gt";
$_operator_lookup[">="] = "__operator_gte";
$_operator_lookup["=="] = "__operator_eq";
$_operator_lookup["==="] = "__operator_stricteq";
$_operator_lookup["!="] = "__operator_neq";
$_operator_lookup["=~"] = "__operator_match";
$_operator_lookup["!~"] = "__operator_notmatch";
$_operator_lookup["<=>"] = "__operator_spaceship";
$_operator_lookup["[]"] = "__operator_arrayget";
$_operator_lookup["[]="] = "__operator_arrayset";
function _rmethod_to_php($method)
{
global $_operator_lookup;
if(isset($_operator_lookup[$method]))
return $_operator_lookup[$method];
return "F_" . str_replace("?", "_QUES_", str_replace("!", "_EXCL", str_replace("=", "__set", $method)));
}
function _isTruthy($obj)
{
$class = get_class($obj);
return $class !== 'F_NilClass' && $class !== 'F_FalseClass';
}
class F_Object
{
public $_instance_vars = array();
public $_dyn_methods = array();
public static $_dyn_global_methods = array();
public static $_class_vars = array();
public $_tainted = FALSE;
public $_untrusted = FALSE;
public function F_to_s($block)
{
return $this->F_class(NULL)->F_to_s(NULL);
}
public function F_puts($block,$o)
{
$str = $o->F_to_s(NULL);
if(_isTruthy($str->F_tainted_QUES_(NULL)))
throw new ErrorCarrier(F_SecurityError::SF_new(NULL, F_String::__from_string("Attempted to puts tainted data")));
echo $str->__STRING . "\n";
return new F_NilClass;
}
public function F_require($block, $str)
{
$path = $str->F_to_s(NULL)->__STRING;
foreach(array('%s','%s.php','%s.fruc.php','lib/%s','lib/%s.php','lib/%s.fruc.php') as $pathfmt)
{
$p = sprintf($pathfmt, $path);
if(@file_get_contents($p, FILE_USE_INCLUDE_PATH) !== FALSE)
{
require_once $p;
return new F_TrueClass;
}
}
if(function_exists("_fructose_dynamic_include"))
{
foreach(explode(PATH_SEPARATOR, get_include_path()) as $path_root)
{
if(file_exists($path_root . "/" . $path))
return _fructose_dynamic_include(realpath($path . ".fruc"));
}
}
return new F_FalseClass;
}
public function F_class($block)
{
return F_Symbol::__from_string(get_class($this));
}
public function F_respond_to_QUES_($block, $sym, $include_private = NULL)
{
if($include_private === NULL)
$include_private = new F_FalseClass;
if(method_exists($this, _rmethod_to_php($sym->__SYMBOL)))
return new F_TrueClass;
if(method_exists($this, "F_respond_to_missing_QUES_"))
return $this->F_respond_to_missing_QUES_($sym, $include_private);
return new F_FalseClass;
}
public function F_send($block, $sym)
{
$args = func_get_args();
array_splice($args, 1, 1);
return call_user_func_array(array($this, _rmethod_to_php($sym->__SYMBOL)), $args);
}
public function __call($name, $args)
{
if(isset($this->_dyn_methods[$name]))
return call_user_func_array($this->_dyn_methods[$name], $args);
if(isset(F_Object::$_dyn_global_methods[$name]))
return call_user_func_array(F_Object::$_dyn_global_methods[$name], $args);
if(get_class($this) === 'F_Object' && function_exists($name))
return call_user_func_array($name, $args);
throw new ErrorCarrier(F_NoMethodError::SF_new(NULL, F_String::__from_string("No such method " . substr(get_class($this), 2) . "#" . substr($name, 2))));
}
public function F_define_method($block, $sym)
{
$this->_dyn_methods[_rmethod_to_php($sym->F_to_s(NULL)->__STRING)] = $block;
}
public function __add_method($name, $fn)
{
$this->_dyn_methods[$name] = $fn;
}
public function F_define_global_method($block, $sym)
{
F_Object::$_dyn_global_methods[_rmethod_to_php($sym->F_to_s(NULL)->__STRING)] = $block;
return $this;
}
public static function __add_global_method($name, $fn)
{
F_Object::$_dyn_global_methods[$name] = $fn;
return $this;
}
public function __operator_notmatch($block, $operand)
{
// foo ^ true == !foo
return $this->__operator_match(NULL, $operand)->__operator_xor(new F_TrueClass);
}
public function __operator_stricteq($block, $operand)
{
return $this->__operator_eq(NULL, $operand);
}
public function __operator_neq($block, $operand)
{
return F_TrueClass::__from_bool(! _isTruthy($this->__operator_eq(NULL, $operand)));
}
public function F_clone($block)
{
$new = clone $this;
if(_isTruthy($new->F_respond_to_QUES_(NULL, F_Symbol::__from_string("initialize_copy"))))
$new->F_initialize_copy(NULL, $this);
return $new;
}
public function F_dup($block)
{
$new = clone $this;
if(_isTruthy($new->F_respond_to_QUES_(NULL, F_Symbol::__from_string("initialize_copy"))))
$new->F_initialize_copy(NULL, $this);
return $new;
}
public function __operator_eq($block, $operand)
{
return F_TrueClass::__from_bool($this === $operand);
}
public function F_equal_QUES_($block, $operand)
{
return F_TrueClass::__from_bool($this === $operand);
}
public function F_inspect($block)
{
return $this->F_to_s(NULL);
}
public function F_instance_of_QUES_($block, $sym)
{
return F_TrueClass::__from_bool(get_class($this) === _rmethod_to_php($sym->__SYMBOL));
}
public function F_is_a_QUES_($block, $sym)
{
return F_TrueClass::__from_bool(is_a($this, _rmethod_to_php($sym->__SYMBOL)));
}
public function F_nil_QUES_($block)
{
return new F_FalseClass;
}
public function F_taint($block)
{
$this->_tainted = TRUE;
return $this;
}
public function F_untaint($block)
{
$this->_tainted = FALSE;
return $this;
}
public function F_tainted_QUES_($block)
{
return F_TrueClass::__from_bool($this->_tainted);
}
public function F_tap($block)
{
$block(NULL, $this);
return $this;
}
public function F_trust($block)
{
$this->_untrusted = FALSE;
return $this;
}
public function F_untrust($block)
{
$this->_untrusted = TRUE;
return $this;
}
public function F_untrusted_QUES_($block)
{
return F_TrueClass::__from_bool($this->_untrusted);
}
public function F_raise($block, $err)
{
if(is_a($err, 'F_Error'))
throw new ErrorCarrier($err);
throw new ErrorCarrier(F_Error::SF_new(NULL, $err));
}
}
class F_Error extends F_Object
{
public static function SF_new($block, $msg = NULL)
{
$err = new F_Error;
$err->__MESSAGE = $msg !== NULL ? $msg->F_to_s(NULL) : new F_NilClass;
return $err;
}
public function F_message($block)
{
return $this->__MESSAGE;
}
public function F_message__set($block, $val)
{
return ($this->__MESSAGE = $val);
}
public function F_to_s($block)
{
return _isTruthy($this->__MESSAGE) ? $this->__MESSAGE : $this->F_class(NULL);
}
public function F_inspect($block)
{
$ex = $this->F_class(NULL)->F_to_s(NULL);
$ex->__operator_lshift(NULL, F_String::__from_string(": "));
$ex->__operator_lshift(NULL, $this->__MESSAGE);
return $ex;
}
}
class F_NoMethodError extends F_Error
{
public static function SF_new($block, $msg = NULL)
{
$err = new F_NoMethodError;
$err->__MESSAGE = $msg !== NULL ? $msg->F_to_s(NULL) : new F_NilClass;
return $err;
}
}
class F_StopIteration extends F_Error
{
public static function SF_new($block, $msg = NULL)
{
$err = new F_StopIteration;
$err->__MESSAGE = $msg !== NULL ? $msg->F_to_s(NULL) : new F_NilClass;
return $err;
}
}
class F_IOError extends F_Error
{
public static function SF_new($block, $msg = NULL)
{
$err = new F_IOError;
$err->__MESSAGE = $msg !== NULL ? $msg->F_to_s(NULL) : new F_NilClass;
return $err;
}
}
class F_SecurityError extends F_Error
{
public static function SF_new($block, $msg = NULL)
{
$err = new F_SecurityError;
$err->__MESSAGE = $msg !== NULL ? $msg->F_to_s(NULL) : new F_NilClass;
return $err;
}
}
class F_File extends F_Enumerable
{
public static function SF_new($block, $filename, $m = NULL)
{
$mode = $m === NULL ? "r" : $m->F_to_s(NULL)->__STRING;
$file = new F_File;
$file->__HANDLE = fopen($filename->F_to_s(NULL)->__STRING, $mode);
$file->__CLOSED = FALSE;
if($block === NULL)
return $file;
$block(NULL, $file);
if(!$file->__CLOSED)
$file->F_close(NULL);
}
public static function SF_absolute_path($block, $filename)
{
$p = realpath($filename->F_to_s(NULL)->__STRING);
if($p === FALSE || $p === NULL)
return new F_NilClass;
return F_String::__from_string($p);
}
public static function SF_exist_QUES_($block, $filename)
{
return F_TrueClass::__from_bool(file_exists($filename->F_to_s(NULL)->__STRING));
}
public function F_close($block)
{
if($this->__CLOSED)
throw new ErrorCarrier(F_IOError::SF_new(NULL, F_String::__from_string("File already closed")));
fclose($this->__HANDLE);
$file->__CLOSED = TRUE;
return new F_NilClass;
}
public function F_closed_QUES_($block)
{
return F_TrueClass::__from_bool($this->__CLOSED);
}
public function __operator_lshift($block, $obj)
{
if($this->__CLOSED)
throw new ErrorCarrier(F_IOError::SF_new(NULL, F_String::__from_string("File closed")));
fwrite($this->__HANDLE, $obj->F_to_s(NULL)->__STRING);
return $this;
}
public function F_each($block)
{
if($this->__CLOSED)
throw new ErrorCarrier(F_IOError::SF_new(NULL, F_String::__from_string("File closed")));
if($block !== NULL)
{
while(($line = fgets($this->__HANDLE)) !== FALSE)
$block(NULL, F_String::__from_string($line));
return new F_NilClass;
}
while(($line = fgets($this->__HANDLE)) !== FALSE)
$lines[] = F_String::__from_string($line);
return F_Enumerator::__from_array($lines);
}
public function F_eof_QUES_($block)
{
if($this->__CLOSED)
throw new ErrorCarrier(F_IOError::SF_new(NULL, F_String::__from_string("File closed")));
return F_TrueClass::__from_bool(feof($this->__HANDLE));
}
public function F_flush($block)
{
if($this->__CLOSED)
throw new ErrorCarrier(F_IOError::SF_new(NULL, F_String::__from_string("File closed")));
fflush($this->__HANDLE);
return new F_NilClass;
}
public function F_getc($block)
{
if($this->__CLOSED)
throw new ErrorCarrier(F_IOError::SF_new(NULL, F_String::__from_string("File closed")));
if(feof($this->__HANDLE))
return new F_NilClass;
return F_String::__from_string(fgetc($this->__HANDLE));
}
public function F_gets($block)
{
if($this->__CLOSED)
throw new ErrorCarrier(F_IOError::SF_new(NULL, F_String::__from_string("File closed")));
if(feof($this->__HANDLE))
return new F_NilClass;
return F_String::__from_string(fgets($this->__HANDLE));
}
public function F_tell($block)
{
if($this->__CLOSED)
throw new ErrorCarrier(F_IOError::SF_new(NULL, F_String::__from_string("File closed")));
return F_Number::__from_number(ftell($this->__HANDLE));
}
public function F_read($block, $length, $buffer = NULL)
{
if($this->__CLOSED)
throw new ErrorCarrier(F_IOError::SF_new(NULL, F_String::__from_string("File closed")));
if(feof($this->__HANDLE))
return new F_NilClass;
$buff = F_String::__from_string(fread($this->__HANDLE, $length->__NUMBER));
if($buffer !== NULL)
{
$buffer->__operator_lshift(NULL, $buff);
return $buffer;
}
return $buff;
}
public function F_rewind($block)
{
if($this->__CLOSED)
throw new ErrorCarrier(F_IOError::SF_new(NULL, F_String::__from_string("File closed")));
fseek($this->__HANDLE, 0);
return F_Number::__from_number(0);
}
public function F_seek($block, $offset, $whence = NULL)
{
if($this->__CLOSED)
throw new ErrorCarrier(F_IOError::SF_new(NULL, F_String::__from_string("File closed")));
$w = SEEK_SET;
if(_isTruthy($whence->__operator_eq(NULL, F_Symbol::__from_string("cur"))))
$w = SEEK_CUR;
elseif(_isTruthy($whence->__operator_eq(NULL, F_Symbol::__from_string("end"))))
$w = SEEK_END;
fseek($this->__HANDLE, $offset->__NUMBER, $w);
return F_Number::__from_number(0);
}
public function F_stat($block)
{
if($this->__CLOSED)
throw new ErrorCarrier(F_IOError::SF_new(NULL, F_String::__from_string("File closed")));
$stats = array();
foreach(fstat($this->__HANDLE) as $k=>$v)
{
$stats[] = F_Symbol::__from_string($k);
$stats[] = F_Number::__from_number($v);
}
return F_Hash::__from_flatpairs($stats);
}
public function F_write($block, $obj)
{
if($this->__CLOSED)
throw new ErrorCarrier(F_IOError::SF_new(NULL, F_String::__from_string("File closed")));
fwrite($this->__HANDLE, $obj->F_to_s(NULL)->__STRING);
return $this;
}
}
class F_Enumerator extends F_Object
{
public static function __from_array($array)
{
$e = new F_Enumerator;
$e->__ARRAY = $array;
$e->__INDEX = 0;
return $e;
}
public function F_next($block)
{
if($this->__INDEX === count($this->__ARRAY))
throw new ErrorCarrier(F_StopIteration::SF_new(NULL));
return $this->__ARRAY[$this->__INDEX++];
}
public function F_peek($block)
{
return $this->__ARRAY[$this->__INDEX];
}
}
class F_Proc extends F_Object
{
public static function SF_new($block)
{
$p = new F_Proc;
$p->__BLOCK = $block;
return $p;
}
public function __operator_eq($block, $operand)
{
return F_TrueClass::__from_bool($this->__BLOCK === $operand->__BLOCK);
}
public function __operator_stricteq($block, $operand)
{
$blockfn = $this->__BLOCK;
return $blockfn(NULL, $operand);
}
public function F_call($block)
{
$args = func_get_args();
return call_user_func_array($this->__BLOCK, $args);
}
public function F_to_s($block)
{
return F_String::__from_string($this->__BLOCK);
}
}
class F_Random extends F_Object
{
static $seed = 0;
public static function SF_seed($block, $seed = NULL)
{
$n = $seed !== NULL ? (int)$seed->__NUMBER : time();
mt_srand($n);
$old_seed = F_Random::$seed;
F_Random::$seed = $n;
return F_Number::__from_number($old_seed);
}
public static function SF_rand($max = NULL)
{
$m = $max !== NULL ? $max->__NUMBER : 1.0;
return F_Number::__from_number((mt_rand() / mt_getrandmax()) * $m);
}
}
class F_Regexp extends F_Object
{
public static $_matches = array();
public static function _get_match($n)
{
$count = 0;
foreach(F_Regexp::$_matches as $k=>$v)
if(is_int($k))
$count++;
if($n < 0)
$n += $count;
if($n < 0)
return new F_NilClass;
if($n >= $count)
return new F_NilClass;
$val = F_Regexp::$_matches[$n];
if(is_array($val))
$val = $val[0];
return F_String::__from_string($val);
}
public static function SF_escape($block, $str)
{
return F_String::__from_string(preg_quote($str->F_to_s(NULL)->__STRING));
}
public static function __from_string($str)
{
$r = new F_Regexp;
$r->__REGEXP = $str;
return $r;
}
public static function SF_new($block, $regexp, $options = NULL)
{
$opts = "";
if($options !== NULL)
$opts = $options->F_to_s(NULL)->__STRING;
$r = new F_Regexp;
$r->__REGEXP = '/' . str_replace('/', '\/', $regexp->F_to_s(NULL)->__STRING) . '/' . $opts;
return $r;
}
public function __operator_eq($block, $operand)
{
return F_TrueClass::__from_bool(is_a($operand, 'F_Regexp') && $this->__REGEXP === $operand->__REGEXP);
}
public function __operator_stricteq($block, $operand)
{
return $this->__operator_match(NULL, $operand);
}
public function __operator_match($block, $operand)
{
if(preg_match($this->__REGEXP, $operand->F_to_s(NULL)->__STRING, F_Regexp::$_matches, PREG_OFFSET_CAPTURE) === 0)
return new F_NilClass;
return F_Number::__from_number(F_Regexp::$_matches[0][1]);
}
public function F_casefold_QUES_($block)
{
return F_TrueClass::__from_bool(preg_match('/\/[a-z]*i[a-z]*$/', $this->__REGEXP, F_Regexp::$_matches));
}
public function F_to_s($block)
{
return F_String::__from_string($this->__REGEXP);
}
public function F_source($block)
{
preg_match('/^\/(.*)\/[a-z]*$/', $this->__REGEXP, F_Regexp::$_matches);
return F_String::__from_string(F_Regexp::$_matches[0]);
}
public function F_match($block, $str)
{
if(preg_match($this->__REGEXP, $str->F_to_s(NULL)->__STRING, F_Regexp::$_matches, PREG_OFFSET_CAPTURE) === 0)
return new F_NilClass;
return F_MatchData::__from_array(F_Regexp::$_matches, $this);
}
}
class F_MatchData extends F_Object
{
public static function __from_array($matches, $regexp)
{
$md = new F_MatchData;
$md->__MATCHES = $matches;
$md->__REGEXP = $regexp;
return $md;
}
public function __operator_arrayget($block, $idx)
{
$count = 0;
foreach($this->__MATCHES as $k=>$v)
if(is_int($k))
$count++;
if(is_a($idx, 'F_Number'))
{
$index = $idx->__NUMBER;
if($index < 0)
$index += $count;
if($index < 0)
return new F_NilClass;
if($index >= $count)
return new F_NilClass;
return F_String::__from_string($this->__MATCHES[$index][0]);
}
$index = $idx->F_to_s(NULL)->__STRING;
if(!isset($this->__MATCHES[$index]))
return new F_NilClass;
return F_String::__from_string($this->__MATCHES[$index][0]);
}
public function F_begin($block, $idx)
{
$count = 0;
foreach($this->__MATCHES as $k=>$v)
if(is_int($k))
$count++;
if(is_a($idx, 'F_Number'))
{
$index = $idx->__NUMBER;
if($index < 0)
$index += $count;
if($index < 0)
return new F_NilClass;
if($index >= $count)
return new F_NilClass;
return F_Number::__from_number($this->__MATCHES[$index][1]);
}
$index = $idx->F_to_s(NULL)->__STRING;
if(!isset($this->__MATCHES[$index]))
return new F_NilClass;
return F_Number::__from_number($this->__MATCHES[$index][1]);
}
public function F_captures($block)
{
$caps = array_map(create_function('$a','return F_String::__from_string($a[0]);'), $this->__MATCHES);
array_shift($caps);
return F_Array::__from_array($caps);
}
public function F_end($block, $idx)
{
$count = 0;
foreach($this->__MATCHES as $k=>$v)
if(is_int($k))
$count++;
if(is_a($idx, 'F_Number'))
{
$index = $idx->__NUMBER;
if($index < 0)
$index += $count;
if($index < 0)
return new F_NilClass;
if($index >= $count)
return new F_NilClass;
return F_Number::__from_number($this->__MATCHES[$index][1] + strlen($this->__MATCHES[$index][0]));
}
$index = $idx->F_to_s(NULL)->__STRING;
if(!isset($this->__MATCHES[$index]))
return new F_NilClass;
return F_Number::__from_number($this->__MATCHES[$index][1] + strlen($this->__MATCHES[$index][0]));
}
public function F_size($block)
{
$count = 0;
foreach($this->__MATCHES as $k=>$v)
if(is_int($k))
$count++;
return F_Number::__from_number($count);
}
public function F_regexp($block)
{
return $this->__REGEXP;
}
public function F_to_a($block)
{
return $this->F_captures(NULL);
}
public function F_to_s($block)
{
return F_String::__from_string($this->__MATCHES[0][0]);
}
}
class F_Time extends F_Object
{
public static function SF_new($block)
{
$t = new F_Time;
$t->__TIME = time();
return $t;
}
public static function SF_now($block)
{
return F_Time::SF_new(NULL);
}
public static function SF_strtotime($block, $string)
{
$t = new F_Time;
$t->__TIME = strtotime($string->F_to_s(NULL)->__STRING);
return $t;
}
public function F_inspect($block)
{
return F_String::__from_string(strftime('%Y-%m-%d %H:%M:%S %z', $this->__TIME));
}
public function F_strftime($block, $string)
{
return F_String::__from_string(strftime($string->F_to_s(NULL)->__STRING, $this->__TIME));
}
public function F_to_s($block)
{
return $this->F_inspect(NULL);
}
}
class F_Enumerable extends F_Object
{
public static $_states = array();
public function F_all_QUES_($block)
{
if($block === NULL)
$block = create_function('','$a = func_get_args(); return $a[1];');
$state = count(F_Enumerable::$_states);
F_Enumerable::$_states[$state] = true;
$this->F_each(create_function('',sprintf('$a = func_get_args(); $f = "%s"; if(! _isTruthy($f(NULL, $a[1]))) { F_Enumerable::$_states[%d] = false; }', $block, $state)));
return F_TrueClass::__from_bool(F_Enumerable::$_states[$state]);
}
public function F_any_QUES_($block)
{
if($block === NULL)
$block = create_function('','$a = func_get_args(); return $a[1];');
$state = count(F_Enumerable::$_states);
F_Enumerable::$_states[$state] = false;
$this->F_each(create_function('',sprintf('$a = func_get_args(); $f = "%s"; if(_isTruthy($f(NULL, $a[1]))) { F_Enumerable::$_states[%d] = true; }', $block, $state)));
return F_TrueClass::__from_bool(F_Enumerable::$_states[$state]);
}
public function F_collect($block)
{
$state = count(F_Enumerable::$_states);
F_Enumerable::$_states[$state] = array();
$this->F_each(create_function('',sprintf('$a = func_get_args(); $f = "%s"; F_Enumerable::$_states[%d][] = $f(NULL, $a[1]);', $block, $state)));
return F_Array::__from_array(F_Enumerable::$_states[$state]);
}
public function F_each_with_index($block)
{
$state = count(F_Enumerable::$_states);
F_Enumerable::$_states[$state] = F_Number::__from_number(0);
$this->F_each(create_function('',sprintf('$a = func_get_args(); $f = "%s"; $f(NULL, $a[1], F_Enumerable::$_states[%d]); F_Enumerable::$_states[%d] = F_Enumerable::$_states[%d]->F_succ(NULL);', $block, $state)));
return new F_NilClass;
}
public function F_count($block, $item = NULL)
{
if($block === NULL && $item === NULL)
{
if(_isTruthy($this->F_respond_to_QUES_(NULL, F_Symbol::__from_string('size'))))
return $this->F_size(NULL);
$state = count(F_Enumerable::$_states);
F_Enumerable::$_states[$state] = 0;
$this->F_each(create_function('',sprintf('F_Enumerable::$_states[%d]++;', $state)));
return F_Number::__from_number(F_Enumerable::$_states[$state]);
}
if($item !== NULL)
{
$state = count(F_Enumerable::$_states);
F_Enumerable::$_states[$state] = array('item' => $item, 'count' => 0);
$this->F_each(create_function('',sprintf('$a = func_get_args(); if(_isTruthy(F_Enumerable::$_states[%d]["item"]->__operator_eq(NULL, $a[1]))) { F_Enumerable::$_states[%d]["count"]++; }', $state, $state)));
return F_Number::__from_number(F_Enumerable::$_states[$state]['count']);
}
$state = count(F_Enumerable::$_states);
F_Enumerable::$_states[$state] = 0;
$this->F_each(create_function('',sprintf('$a = func_get_args(); $f = "%s"; if(_isTruthy($f(NULL, $a[1]))) { F_Enumerable::$_states[%d]++; }', $block, $state)));
return F_Number::__from_number(F_Enumerable::$_states[$state]);
}
public function F_find($block)
{
try
{
$this->F_each(create_function('',sprintf('$a = func_get_args(); $f = "%s"; if(_isTruthy($f(NULL, $a[1]))) { throw new ReturnFromBlock($a[1]); }', $block)));
}
catch(ReturnFromBlock $rfb)
{
return $rfb->val;
}
return new F_NilClass;
}
public function F_drop($block, $n)
{
$state = count(F_Enumerable::$_states);
F_Enumerable::$_states[$state] = array('n' => $n->__NUMBER, 'arr' => array());
$this->F_each(create_function('',sprintf('$a = func_get_args(); $state = %d;
if(--F_Enumerable::$_states[$state]["n"] < 0)
{
F_Enumerable::$_states[$state]["arr"][] = $a[1];
}', $state)));
return F_Array::__from_array(F_Enumerable::$_states[$state]['arr']);
}
public function F_drop_while($block)
{
$state = count(F_Enumerable::$_states);
F_Enumerable::$_states[$state] = array('dropping' => true, 'arr' => array());
$this->F_each(create_function('',sprintf('$a = func_get_args(); $f = "%s";
if(!F_Enumerable::$_states[%d]["dropping"] || !_isTruthy($f(NULL,$a[1]))) {
F_Enumerable::$_states[%d]["dropping"] = false;
F_Enumerable::$_states[%d]["arr"][] = $a[1];
}', $block, $state, $state, $state)));
return F_Array::__from_array(F_Enumerable::$_states[$state]['arr']);
}
public function F_to_a($block)
{
$state = count(F_Enumerable::$_states);
F_Enumerable::$_states[$state] = array();
$this->F_each(create_function('',sprintf('$a = func_get_args(); F_Enumerable::$_states[%d][] = $a[1];', $state)));
return F_Array::__from_array(F_Enumerable::$_states[$state]);
}
public function F_select($block)
{
$state = count(F_Enumerable::$_states);
F_Enumerable::$_states[$state] = array();
$this->F_each(create_function('',sprintf('$a = func_get_args(); $f = "%s"; if(_isTruthy($f(NULL, $a[1]))) { F_Enumerable::$_states[%d][] = $a[1]; }', $block, $state)));
return F_Array::__from_array(F_Enumerable::$_states[$state]);
}
public function F_first($block)
{
try
{
$this->F_each(create_function('','$a = func_get_args(); throw new ReturnFromBlock($a[1]);'));
}
catch(ReturnFromBlock $rfb)
{
return $rfb->val;
}
return new F_NilClass;
}
public function F_include_QUES_($block, $obj)
{
$state = count(F_Enumerable::$_states);
F_Enumerable::$_states[$state] = $obj;
try
{
$this->F_each(create_function('',sprintf('$a = func_get_args(); if(_isTruthy($a[1]->__operator_eq(NULL, F_Enumerable::$_states[%d]))) { throw new ReturnFromBlock($a[1]); }', $state, $obj)));
}
catch(ReturnFromBlock $rfb)
{
return $rfb->val;
}
return new F_NilClass;
}
public function F_reduce($block, $sym = NULL)
{
$state = count(F_Enumerable::$_states);
F_Enumerable::$_states[$state] = NULL;
if($block === NULL && $sym !== NULL)
$block = create_function('',sprintf('$a = func_get_args(); return call_user_func(array($a[1],"%s"), NULL, $a[2]);', _rmethod_to_php($sym->__SYMBOL)));
elseif($block === NULL)
{
// @TODO
// throw some exception
}
elseif($sym !== NULL)
F_Enumerable::$_states[$state] = $sym;
$this->F_each(create_function('',sprintf('$a = func_get_args(); $state = %d; $f = "%s";
if(F_Enumerable::$_states[$state] === NULL)
{
F_Enumerable::$_states[$state] = $a[1];
}
else
{
F_Enumerable::$_states[$state] = $f(NULL, F_Enumerable::$_states[$state], $a[1]);
}', $state, $block)));
return F_Enumerable::$_states[$state] === NULL ? new F_NilClass : F_Enumerable::$_states[$state];
}
public function _F_minmax($block, $compare)
{
if($block === NULL)
$block = create_function('', '$a = func_get_args(); return $a[1]->__operator_spaceship(NULL, $a[2]);');
$state = count(F_Enumerable::$_states);
F_Enumerable::$_states[$state] = NULL;
$this->F_each(create_function('',sprintf('$a = func_get_args(); $state = %d; $f = "%s";
if(F_Enumerable::$_states[$state] === NULL)
{
F_Enumerable::$_states[$state] = $a[1];
}
else
{
if($f(NULL, F_Enumerable::$_states[$state], $a[1])->__NUMBER ' . $compare . ' 0)
{
F_Enumerable::$_states[$state] = $a[1];
}
}', $state, $block)));