5 class _Error
extends Exception
{
7 public function __construct($obj) {
8 parent
::__construct("Mal Error", 0, null);
16 function _equal_Q($a, $b) {
17 $ota = gettype($a) === "object" ?
get_class($a) : gettype($a);
18 $otb = gettype($b) === "object" ?
get_class($b) : gettype($b);
19 if (!($ota === $otb or (_sequential_Q($a) and _sequential_Q($b)))) {
21 } elseif (_symbol_Q($a)) {
22 #print "ota: $ota, otb: $otb\n";
23 return $a->value
=== $b->value
;
24 } elseif (_list_Q($a) or _vector_Q($a)) {
25 if ($a->count() !== $b->count()) { return false; }
26 for ($i=0; $i<$a->count(); $i++
) {
27 if (!_equal_Q($a[$i], $b[$i])) { return false; }
30 } elseif (_hash_map_Q($a)) {
31 if ($a->count() !== $b->count()) { return false; }
32 $hm1 = $a->getArrayCopy();
33 $hm2 = $b->getArrayCopy();
34 foreach (array_keys($hm1) as $k) {
35 if (!_equal_Q($hm1[$k], $hm2[$k])) { return false; }
43 function _sequential_Q($seq) { return _list_Q($seq) or _vector_Q($seq); }
47 function _nil_Q($obj) { return $obj === NULL; }
48 function _true_Q($obj) { return $obj === true; }
49 function _false_Q($obj) { return $obj === false; }
50 function _string_Q($obj) {
51 return is_string($obj) && strpos($obj, chr(0x7f)) !== 0;
53 function _number_Q($obj) { return is_int($obj); }
60 public function __construct($value) {
61 $this->value
= $value;
64 function _symbol($name) { return new SymbolClass($name); }
65 function _symbol_Q($obj) { return ($obj instanceof SymbolClass
); }
68 function _keyword($name) { return chr(0x7f).$name; }
69 function _keyword_Q($obj) {
70 return is_string($obj) && strpos($obj, chr(0x7f)) === 0;
78 public $type = 'native'; // 'native' or 'platform'
82 public $params = NULL;
83 public $ismacro = False;
84 public function __construct($func, $type,
85 $ast, $env, $params, $ismacro=False) {
91 $this->params
= $params;
92 $this->ismacro
= $ismacro;
94 public function __invoke() {
95 $args = func_get_args();
96 if ($this->type
=== 'native') {
97 $fn_env = new Env($this->env
,
98 $this->params
, $args);
100 return $evalf($this->ast
, $fn_env);
102 return call_user_func_array($this->func
, $args);
105 public function gen_env($args) {
106 return new Env($this->env
, $this->params
, $args);
108 public function apply($args) {
109 return call_user_func_array(array(&$this, '__invoke'),$args);
113 function _function($func, $type='platform',
114 $ast=NULL, $env=NULL, $params=NULL, $ismacro=False) {
115 return new FunctionClass($func, $type, $ast, $env, $params, $ismacro);
117 function _function_Q($obj) { return $obj instanceof FunctionClass
; }
118 function _fn_Q($obj) { return $obj instanceof Closure
; }
121 // Parent class of list, vector, hash-map
122 // http://www.php.net/manual/en/class.arrayobject.php
123 class SeqClass
extends ArrayObject
{
124 public function slice($start, $length=NULL) {
126 if ($start >= count($this)) {
129 $arr = array_slice($this->getArrayCopy(), $start, $length);
131 $sc->exchangeArray($arr);
138 class ListClass
extends SeqClass
{
143 $v = new ListClass();
144 $v->exchangeArray(func_get_args());
147 function _list_Q($obj) { return $obj instanceof ListClass
; }
151 class VectorClass
extends SeqClass
{
156 $v = new VectorClass();
157 $v->exchangeArray(func_get_args());
160 function _vector_Q($obj) { return $obj instanceof VectorClass
; }
164 class HashMapClass
extends ArrayObject
{
168 function _hash_map() {
169 $args = func_get_args();
170 if (count($args) %
2 === 1) {
171 throw new Exception("Odd number of hash map arguments");
173 $hm = new HashMapClass();
174 array_unshift($args, $hm);
175 return call_user_func_array('_assoc_BANG', $args);
177 function _hash_map_Q($obj) { return $obj instanceof HashMapClass
; }
179 function _assoc_BANG($hm) {
180 $args = func_get_args();
181 if (count($args) %
2 !== 1) {
182 throw new Exception("Odd number of assoc arguments");
184 for ($i=1; $i<count($args); $i+
=2) {
186 $vtoken = $args[$i+
1];
187 // TODO: support more than string keys
188 if (gettype($ktoken) !== "string") {
189 throw new Exception("expected hash-map key string, got: " . gettype($ktoken));
191 $hm[$ktoken] = $vtoken;
196 function _dissoc_BANG($hm) {
197 $args = func_get_args();
198 for ($i=1; $i<count($args); $i++
) {
200 if ($hm && $hm->offsetExists($ktoken)) {
210 public $value = NULL;
212 public function __construct($value) {
213 $this->value
= $value;
216 function _atom($val) { return new Atom($val); }
217 function _atom_Q($atm) { return $atm instanceof Atom
; }