2016-10-22 18:07:12 +00:00
|
|
|
/* -*-comment-start: "//";comment-end:""-*-
|
|
|
|
* Mes --- Maxwell Equations of Software
|
|
|
|
* Copyright © 2016 Jan Nieuwenhuizen <janneke@gnu.org>
|
|
|
|
*
|
|
|
|
* This file is part of Mes.
|
|
|
|
*
|
|
|
|
* Mes 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 3 of the License, or (at
|
|
|
|
* your option) any later version.
|
|
|
|
*
|
|
|
|
* Mes 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 Mes. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2016-11-21 08:28:34 +00:00
|
|
|
SCM
|
|
|
|
greater_p (SCM x) ///((name . ">") (arity . n))
|
2016-10-22 18:07:12 +00:00
|
|
|
{
|
|
|
|
int n = INT_MAX;
|
2016-11-21 08:28:34 +00:00
|
|
|
while (x != cell_nil)
|
2016-10-22 18:07:12 +00:00
|
|
|
{
|
2016-11-21 08:30:59 +00:00
|
|
|
assert (TYPE (car (x)) == NUMBER);
|
|
|
|
if (VALUE (car (x)) >= n) return cell_f;
|
|
|
|
n = VALUE (car (x));
|
2016-10-22 18:07:12 +00:00
|
|
|
x = cdr (x);
|
|
|
|
}
|
2016-11-21 08:28:34 +00:00
|
|
|
return cell_t;
|
2016-10-22 18:07:12 +00:00
|
|
|
}
|
|
|
|
|
2016-11-21 08:28:34 +00:00
|
|
|
SCM
|
|
|
|
less_p (SCM x) ///((name . "<") (arity . n))
|
2016-10-22 18:07:12 +00:00
|
|
|
{
|
|
|
|
int n = INT_MIN;
|
2016-11-21 08:28:34 +00:00
|
|
|
while (x != cell_nil)
|
2016-10-22 18:07:12 +00:00
|
|
|
{
|
2016-11-21 08:30:59 +00:00
|
|
|
assert (TYPE (car (x)) == NUMBER);
|
|
|
|
if (VALUE (car (x)) <= n) return cell_f;
|
|
|
|
n = VALUE (car (x));
|
2016-10-22 18:07:12 +00:00
|
|
|
x = cdr (x);
|
|
|
|
}
|
2016-11-21 08:28:34 +00:00
|
|
|
return cell_t;
|
2016-10-22 18:07:12 +00:00
|
|
|
}
|
|
|
|
|
2016-11-21 08:28:34 +00:00
|
|
|
SCM
|
|
|
|
is_p (SCM x) ///((name . "=") (arity . n))
|
2016-10-22 18:07:12 +00:00
|
|
|
{
|
2016-11-21 08:28:34 +00:00
|
|
|
if (x == cell_nil) return cell_t;
|
2016-11-21 08:30:59 +00:00
|
|
|
assert (TYPE (car (x)) == NUMBER);
|
|
|
|
int n = VALUE (car (x));
|
2016-10-22 18:07:12 +00:00
|
|
|
x = cdr (x);
|
2016-11-21 08:28:34 +00:00
|
|
|
while (x != cell_nil)
|
2016-10-22 18:07:12 +00:00
|
|
|
{
|
2016-11-21 08:30:59 +00:00
|
|
|
if (VALUE (car (x)) != n) return cell_f;
|
2016-10-22 18:07:12 +00:00
|
|
|
x = cdr (x);
|
|
|
|
}
|
2016-11-21 08:28:34 +00:00
|
|
|
return cell_t;
|
2016-10-22 18:07:12 +00:00
|
|
|
}
|
|
|
|
|
2016-11-21 08:28:34 +00:00
|
|
|
SCM
|
|
|
|
minus (SCM x) ///((name . "-") (arity . n))
|
2016-10-22 18:07:12 +00:00
|
|
|
{
|
2016-11-21 08:28:34 +00:00
|
|
|
SCM a = car (x);
|
2016-11-21 08:30:59 +00:00
|
|
|
assert (TYPE (a) == NUMBER);
|
|
|
|
int n = VALUE (a);
|
2016-10-22 18:07:12 +00:00
|
|
|
x = cdr (x);
|
2016-11-21 08:28:34 +00:00
|
|
|
if (x == cell_nil)
|
2016-10-22 18:07:12 +00:00
|
|
|
n = -n;
|
2016-11-21 08:28:34 +00:00
|
|
|
while (x != cell_nil)
|
2016-10-22 18:07:12 +00:00
|
|
|
{
|
2016-11-21 08:30:59 +00:00
|
|
|
assert (TYPE (car (x)) == NUMBER);
|
|
|
|
n -= VALUE (car (x));
|
2016-10-22 18:07:12 +00:00
|
|
|
x = cdr (x);
|
|
|
|
}
|
|
|
|
return make_number (n);
|
|
|
|
}
|
|
|
|
|
2016-11-21 08:28:34 +00:00
|
|
|
SCM
|
|
|
|
plus (SCM x) ///((name . "+") (arity . n))
|
2016-10-22 18:07:12 +00:00
|
|
|
{
|
|
|
|
int n = 0;
|
2016-11-21 08:28:34 +00:00
|
|
|
while (x != cell_nil)
|
2016-10-22 18:07:12 +00:00
|
|
|
{
|
2016-11-21 08:30:59 +00:00
|
|
|
assert (TYPE (car (x)) == NUMBER);
|
|
|
|
n += VALUE (car (x));
|
2016-10-22 18:07:12 +00:00
|
|
|
x = cdr (x);
|
|
|
|
}
|
|
|
|
return make_number (n);
|
|
|
|
}
|
|
|
|
|
2016-11-21 08:28:34 +00:00
|
|
|
SCM
|
|
|
|
divide (SCM x) ///((name . "/") (arity . n))
|
2016-10-22 18:07:12 +00:00
|
|
|
{
|
|
|
|
int n = 1;
|
2016-11-21 08:28:34 +00:00
|
|
|
if (x != cell_nil) {
|
2016-11-21 08:30:59 +00:00
|
|
|
assert (TYPE (car (x)) == NUMBER);
|
|
|
|
n = VALUE (car (x));
|
2016-10-22 18:07:12 +00:00
|
|
|
x = cdr (x);
|
|
|
|
}
|
2016-11-21 08:28:34 +00:00
|
|
|
while (x != cell_nil)
|
2016-10-22 18:07:12 +00:00
|
|
|
{
|
2016-11-21 08:30:59 +00:00
|
|
|
assert (TYPE (car (x)) == NUMBER);
|
|
|
|
n /= VALUE (car (x));
|
2016-10-22 18:07:12 +00:00
|
|
|
x = cdr (x);
|
|
|
|
}
|
|
|
|
return make_number (n);
|
|
|
|
}
|
|
|
|
|
2016-11-21 08:28:34 +00:00
|
|
|
SCM
|
|
|
|
modulo (SCM a, SCM b)
|
2016-10-22 18:07:12 +00:00
|
|
|
{
|
2016-11-21 08:30:59 +00:00
|
|
|
assert (TYPE (a) == NUMBER);
|
|
|
|
assert (TYPE (b) == NUMBER);
|
|
|
|
return make_number (VALUE (a) % VALUE (b));
|
2016-10-22 18:07:12 +00:00
|
|
|
}
|
|
|
|
|
2016-11-21 08:28:34 +00:00
|
|
|
SCM
|
|
|
|
multiply (SCM x) ///((name . "*") (arity . n))
|
2016-10-22 18:07:12 +00:00
|
|
|
{
|
|
|
|
int n = 1;
|
2016-11-21 08:28:34 +00:00
|
|
|
while (x != cell_nil)
|
2016-10-22 18:07:12 +00:00
|
|
|
{
|
2016-11-21 08:30:59 +00:00
|
|
|
assert (TYPE (car (x)) == NUMBER);
|
|
|
|
n *= VALUE (car (x));
|
2016-10-22 18:07:12 +00:00
|
|
|
x = cdr (x);
|
|
|
|
}
|
|
|
|
return make_number (n);
|
|
|
|
}
|
|
|
|
|
2016-11-21 08:28:34 +00:00
|
|
|
SCM
|
|
|
|
logior (SCM x) ///((arity . n))
|
2016-10-22 18:07:12 +00:00
|
|
|
{
|
|
|
|
int n = 0;
|
2016-11-21 08:28:34 +00:00
|
|
|
while (x != cell_nil)
|
2016-10-22 18:07:12 +00:00
|
|
|
{
|
2016-11-21 08:30:59 +00:00
|
|
|
assert (TYPE (car (x)) == NUMBER);
|
|
|
|
n |= VALUE (car (x));
|
2016-10-22 18:07:12 +00:00
|
|
|
x = cdr (x);
|
|
|
|
}
|
|
|
|
return make_number (n);
|
|
|
|
}
|
2016-12-20 19:10:43 +00:00
|
|
|
|
|
|
|
SCM
|
|
|
|
ash (SCM n, SCM count)
|
|
|
|
{
|
|
|
|
assert (TYPE (n) == NUMBER);
|
|
|
|
assert (TYPE (count) == NUMBER);
|
|
|
|
int cn = VALUE (n);
|
|
|
|
int ccount = VALUE (count);
|
|
|
|
return make_number ((ccount < 0) ? cn >> -ccount : cn << ccount);
|
|
|
|
}
|