From be2f3bc274072a4be11ce66dfb08062ead0a6d0a Mon Sep 17 00:00:00 2001 From: Jan Nieuwenhuizen Date: Sat, 22 Oct 2016 20:07:12 +0200 Subject: [PATCH] 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. --- GNUmakefile | 3 +- math.c | 145 ++++++++++++++++++++++++++++++++++++++++++++++++++++ mes.c | 129 ++-------------------------------------------- 3 files changed, 150 insertions(+), 127 deletions(-) create mode 100644 math.c diff --git a/GNUmakefile b/GNUmakefile index 094f820e..f8aa33dc 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -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 diff --git a/math.c b/math.c new file mode 100644 index 00000000..a28f89b8 --- /dev/null +++ b/math.c @@ -0,0 +1,145 @@ +/* -*-comment-start: "//";comment-end:""-*- + * Mes --- Maxwell Equations of Software + * Copyright © 2016 Jan Nieuwenhuizen + * + * 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 . + */ + +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); +} diff --git a/mes.c b/mes.c index 288c1db5..cba0d05b 100644 --- a/mes.c +++ b/mes.c @@ -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"