add Compare functions

This commit is contained in:
Pekka Laiho 2020-05-31 19:02:21 +07:00
parent cc9fa06117
commit 4aa2a79a7b
3 changed files with 46 additions and 11 deletions

View File

@ -15,6 +15,7 @@ function ml_get_lisp(): array
$env = new MadLisp\Env();
(new MadLisp\Lib\Math())->register($env);
(new MadLisp\Lib\Compare())->register($env);
/*
$env->set('eval', function (...$args) use ($eval, $env) {

45
src/Lib/Compare.php Normal file
View File

@ -0,0 +1,45 @@
<?php
namespace MadLisp\Lib;
use MadLisp\CoreFunc;
use MadLisp\Env;
class Compare implements ILib
{
public function register(Env $env): void
{
// TODO: handle collections
$env->set('=', new CoreFunc('=', 'Return true if arguments are equal.', 2, 2,
fn ($a, $b) => $a == $b
));
$env->set('==', new CoreFunc('==', 'Return true if arguments are equal using strict comparison.', 2, 2,
fn ($a, $b) => $a === $b
));
$env->set('!=', new CoreFunc('!=', 'Return true if arguments are not equal.', 2, 2,
fn ($a, $b) => $a != $b
));
$env->set('!==', new CoreFunc('!==', 'Return true if arguments are not equal using strict comparison.', 2, 2,
fn ($a, $b) => $a !== $b
));
$env->set('<', new CoreFunc('<', 'Return true if first argument is less than second argument.', 2, 2,
fn ($a, $b) => $a < $b
));
$env->set('<=', new CoreFunc('<=', 'Return true if first argument is less or equal to second argument.', 2, 2,
fn ($a, $b) => $a <= $b
));
$env->set('>', new CoreFunc('>', 'Return true if first argument is greater than second argument.', 2, 2,
fn ($a, $b) => $a > $b
));
$env->set('>=', new CoreFunc('>=', 'Return true if first argument is greater or equal to second argument.', 2, 2,
fn ($a, $b) => $a >= $b
));
}
}

View File

@ -13,17 +13,6 @@ class Core implements ILib
{
public function register(Env $env): void
{
// arithmetic
// comparison
$env->set('=', fn ($a, $b) => $a == $b);
$env->set('<', fn ($a, $b) => $a < $b);
$env->set('>', fn ($a, $b) => $a > $b);
$env->set('<=', fn ($a, $b) => $a <= $b);
$env->set('>=', fn ($a, $b) => $a >= $b);
$env->set('!=', fn ($a, $b) => $a != $b);
// types
$env->set('type?', function ($a) {