Move arithmetic functions to math.c.

* mes.c (greater_p, less_p, is_p, minus, divide, modulo, multiply,
  logior): Move to math.c.
* math.c: New file.
* GNUmakefile (mes.o): Depend on math snarf output.
This commit is contained in:
Jan Nieuwenhuizen 2016-10-22 20:07:12 +02:00
parent 27247383d7
commit be2f3bc274
3 changed files with 150 additions and 127 deletions

View File

@ -25,11 +25,12 @@ all: mes
mes.o: mes.c
mes.o: mes.c mes.environment.h mes.environment.i mes.symbols.i
mes.o: define.c define.environment.h define.environment.i
mes.o: math.c math.environment.h math.environment.i
mes.o: quasiquote.c quasiquote.environment.h quasiquote.environment.i
mes.o: type.c type.environment.h type.environment.i
clean:
rm -f mes mes.o mes.environment.i mes.symbols.i mes.environment.h *.cat a.out
rm -f mes mes.o *.environment.i *.symbols.i *.environment.h *.cat a.out
distclean: clean
rm -f .config.make

145
math.c Normal file
View File

@ -0,0 +1,145 @@
/* -*-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/>.
*/
scm *
greater_p (scm *x) ///((name . ">") (args . n))
{
int n = INT_MAX;
while (x != &scm_nil)
{
assert (x->car->type == NUMBER);
if (x->car->value >= n) return &scm_f;
n = x->car->value;
x = cdr (x);
}
return &scm_t;
}
scm *
less_p (scm *x) ///((name . "<") (args . n))
{
int n = INT_MIN;
while (x != &scm_nil)
{
assert (x->car->type == NUMBER);
if (x->car->value <= n) return &scm_f;
n = x->car->value;
x = cdr (x);
}
return &scm_t;
}
scm *
is_p (scm *x) ///((name . "=") (args . n))
{
if (x == &scm_nil) return &scm_t;
assert (x->car->type == NUMBER);
int n = x->car->value;
x = cdr (x);
while (x != &scm_nil)
{
if (x->car->value != n) return &scm_f;
x = cdr (x);
}
return &scm_t;
}
scm *
minus (scm *x) ///((name . "-") (args . n))
{
scm *a = car (x);
assert (a->type == NUMBER);
int n = a->value;
x = cdr (x);
if (x == &scm_nil)
n = -n;
while (x != &scm_nil)
{
assert (x->car->type == NUMBER);
n -= x->car->value;
x = cdr (x);
}
return make_number (n);
}
scm *
plus (scm *x) ///((name . "+") (args . n))
{
int n = 0;
while (x != &scm_nil)
{
assert (x->car->type == NUMBER);
n += x->car->value;
x = cdr (x);
}
return make_number (n);
}
scm *
divide (scm *x) ///((name . "/") (args . n))
{
int n = 1;
if (x != &scm_nil) {
assert (x->car->type == NUMBER);
n = x->car->value;
x = cdr (x);
}
while (x != &scm_nil)
{
assert (x->car->type == NUMBER);
n /= x->car->value;
x = cdr (x);
}
return make_number (n);
}
scm *
modulo (scm *a, scm *b)
{
assert (a->type == NUMBER);
assert (b->type == NUMBER);
return make_number (a->value % b->value);
}
scm *
multiply (scm *x) ///((name . "*") (args . n))
{
int n = 1;
while (x != &scm_nil)
{
assert (x->car->type == NUMBER);
n *= x->car->value;
x = cdr (x);
}
return make_number (n);
}
scm *
logior (scm *x) ///((args . n))
{
int n = 0;
while (x != &scm_nil)
{
assert (x->car->type == NUMBER);
n |= x->car->value;
x = cdr (x);
}
return make_number (n);
}

129
mes.c
View File

@ -65,6 +65,7 @@ scm temp_number = {NUMBER, .name="nul", .value=0};
#include "type.environment.h"
#include "define.environment.h"
#include "quasiquote.environment.h"
#include "math.environment.h"
#include "mes.environment.h"
scm *display_ (FILE* f, scm *x);
@ -195,6 +196,7 @@ quasisyntax (scm *x)
#include "type.c"
#include "define.c"
#include "math.c"
#include "quasiquote.c"
//Library functions
@ -1156,132 +1158,6 @@ read_env (scm *a)
return readword (getchar (), 0, a);
}
scm *
greater_p (scm *x) ///((name . ">") (args . n))
{
int n = INT_MAX;
while (x != &scm_nil)
{
assert (x->car->type == NUMBER);
if (x->car->value >= n) return &scm_f;
n = x->car->value;
x = cdr (x);
}
return &scm_t;
}
scm *
less_p (scm *x) ///((name . "<") (args . n))
{
int n = INT_MIN;
while (x != &scm_nil)
{
assert (x->car->type == NUMBER);
if (x->car->value <= n) return &scm_f;
n = x->car->value;
x = cdr (x);
}
return &scm_t;
}
scm *
is_p (scm *x) ///((name . "=") (args . n))
{
if (x == &scm_nil) return &scm_t;
assert (x->car->type == NUMBER);
int n = x->car->value;
x = cdr (x);
while (x != &scm_nil)
{
if (x->car->value != n) return &scm_f;
x = cdr (x);
}
return &scm_t;
}
scm *
minus (scm *x) ///((name . "-") (args . n))
{
scm *a = car (x);
assert (a->type == NUMBER);
int n = a->value;
x = cdr (x);
if (x == &scm_nil)
n = -n;
while (x != &scm_nil)
{
assert (x->car->type == NUMBER);
n -= x->car->value;
x = cdr (x);
}
return make_number (n);
}
scm *
plus (scm *x) ///((name . "+") (args . n))
{
int n = 0;
while (x != &scm_nil)
{
assert (x->car->type == NUMBER);
n += x->car->value;
x = cdr (x);
}
return make_number (n);
}
scm *
divide (scm *x) ///((name . "/") (args . n))
{
int n = 1;
if (x != &scm_nil) {
assert (x->car->type == NUMBER);
n = x->car->value;
x = cdr (x);
}
while (x != &scm_nil)
{
assert (x->car->type == NUMBER);
n /= x->car->value;
x = cdr (x);
}
return make_number (n);
}
scm *
modulo (scm *a, scm *b)
{
assert (a->type == NUMBER);
assert (b->type == NUMBER);
return make_number (a->value % b->value);
}
scm *
multiply (scm *x) ///((name . "*") (args . n))
{
int n = 1;
while (x != &scm_nil)
{
assert (x->car->type == NUMBER);
n *= x->car->value;
x = cdr (x);
}
return make_number (n);
}
scm *
logior (scm *x) ///((args . n))
{
int n = 0;
while (x != &scm_nil)
{
assert (x->car->type == NUMBER);
n |= x->car->value;
x = cdr (x);
}
return make_number (n);
}
scm *
add_environment (scm *a, char const *name, scm *x)
{
@ -1308,6 +1184,7 @@ mes_environment () ///((internal))
a = cons (cons (&symbol_quote, &scm_quote), a);
a = cons (cons (&symbol_syntax, &scm_syntax), a);
#include "math.environment.i"
#include "mes.environment.i"
#include "define.environment.i"
#include "type.environment.i"