support for object and resource types

This commit is contained in:
Pekka Laiho 2020-06-19 13:46:52 +07:00
parent 599ebd303f
commit 838313fa9c
3 changed files with 27 additions and 0 deletions

View File

@ -275,6 +275,8 @@ vector? | Return true if the argument is a vector.
seq? | Return true if the argument is a sequence (list or vector).
hash? | Return true if the argument is a hash-map.
symbol? | Return true if the argument is a symbol.
object? | Return true if the argument is an object.
resource? | Return true if the argument is a resource.
bool? | Return true if the argument is a boolean value (strict comparison).
true? | Return true if the argument evaluates to true (non-strict comparison).
false? | Return true if the argument evaluates to false (non-strict comparison).

View File

@ -1,6 +1,7 @@
<?php
namespace MadLisp\Lib;
use MadLisp\Collection;
use MadLisp\CoreFunc;
use MadLisp\Env;
use MadLisp\Hash;
@ -54,6 +55,10 @@ class Types implements ILib
return 'hash';
} elseif ($a instanceof Symbol) {
return 'symbol';
} elseif (is_object($a)) {
return 'object';
} elseif (is_resource($a)) {
return 'resource';
} elseif ($a === true || $a === false) {
return 'bool';
} elseif ($a === null) {
@ -92,6 +97,21 @@ class Types implements ILib
fn ($a) => $a instanceof Symbol
));
$env->set('object?', new CoreFunc('object?', 'Return true if argument is an object.', 1, 1,
function ($a) {
// Skip classes which have their own types
if ($a instanceof Func || $a instanceof Collection || $a instanceof Symbol) {
return false;
}
return is_object($a);
}
));
$env->set('resource?', new CoreFunc('resource?', 'Return true if argument is a resource.', 1, 1,
fn ($a) => is_resource($a)
));
$env->set('bool?', new CoreFunc('bool?', 'Return true if argument is a boolean.', 1, 1,
fn ($a) => $a === true || $a === false
));

View File

@ -21,6 +21,11 @@ class Printer
array_keys($a->getData()), array_values($a->getData()))) . '}';
} elseif ($a instanceof Symbol) {
return $a->getName();
} elseif (is_object($a)) {
$class = get_class($a);
return "<object<$class>>";
} elseif (is_resource($a)) {
return '<resource>';
} elseif ($a === true) {
return 'true';
} elseif ($a === false) {