2016-05-28 14:39:44 +00:00
|
|
|
/*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// (setq comment-start "//")
|
|
|
|
// (setq comment-end "")
|
|
|
|
/*
|
|
|
|
* The Maxwell Equations of Software -- John McCarthy page 13
|
|
|
|
* http://www.softwarepreservation.org/projects/LISP/book/LISP%201.5%20Programmers%20Manual.pdf
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <assert.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
2016-05-15 22:07:44 +00:00
|
|
|
#define DEBUG 0
|
2016-07-22 10:23:23 +00:00
|
|
|
#define MES_FULL 1
|
2016-05-15 22:07:44 +00:00
|
|
|
|
2016-07-22 15:13:51 +00:00
|
|
|
enum type {CHAR, MACRO, NUMBER, PAIR, STRING, SYMBOL, VALUES, VECTOR,
|
2016-07-10 20:43:23 +00:00
|
|
|
FUNCTION0, FUNCTION1, FUNCTION2, FUNCTION3, FUNCTIONn};
|
2016-05-28 14:39:44 +00:00
|
|
|
struct scm_t;
|
|
|
|
typedef struct scm_t* (*function0_t) (void);
|
|
|
|
typedef struct scm_t* (*function1_t) (struct scm_t*);
|
|
|
|
typedef struct scm_t* (*function2_t) (struct scm_t*, struct scm_t*);
|
|
|
|
typedef struct scm_t* (*function3_t) (struct scm_t*, struct scm_t*, struct scm_t*);
|
2016-07-10 20:43:23 +00:00
|
|
|
typedef struct scm_t* (*functionn_t) (struct scm_t*);
|
2016-05-28 14:39:44 +00:00
|
|
|
|
|
|
|
typedef struct scm_t {
|
|
|
|
enum type type;
|
|
|
|
union {
|
|
|
|
char *name;
|
|
|
|
struct scm_t* car;
|
2016-07-11 08:38:02 +00:00
|
|
|
int length;
|
2016-05-28 14:39:44 +00:00
|
|
|
};
|
|
|
|
union {
|
|
|
|
int value;
|
|
|
|
function0_t function0;
|
|
|
|
function1_t function1;
|
2016-05-29 11:44:03 +00:00
|
|
|
function2_t function2;
|
2016-05-28 14:39:44 +00:00
|
|
|
function3_t function3;
|
2016-07-10 20:43:23 +00:00
|
|
|
functionn_t functionn;
|
2016-05-28 14:39:44 +00:00
|
|
|
struct scm_t* cdr;
|
2016-07-22 15:13:51 +00:00
|
|
|
struct scm_t* macro;
|
2016-07-11 08:38:02 +00:00
|
|
|
struct scm_t** vector;
|
2016-05-28 14:39:44 +00:00
|
|
|
};
|
|
|
|
} scm;
|
|
|
|
|
2016-07-22 10:23:23 +00:00
|
|
|
#define MES_C 1
|
2016-07-09 21:12:25 +00:00
|
|
|
#include "mes.h"
|
|
|
|
|
|
|
|
scm *display_helper (scm*, bool, char*, bool);
|
2016-07-10 08:43:26 +00:00
|
|
|
bool
|
|
|
|
symbol_eq (scm *x, char *s)
|
|
|
|
{
|
2016-07-10 20:43:23 +00:00
|
|
|
return x->type == SYMBOL && !strcmp (x->name, s);
|
2016-07-10 08:43:26 +00:00
|
|
|
}
|
2016-07-09 21:12:25 +00:00
|
|
|
|
2016-07-10 20:43:23 +00:00
|
|
|
scm scm_nil = {SYMBOL, "()"};
|
|
|
|
scm scm_dot = {SYMBOL, "."};
|
|
|
|
scm scm_t = {SYMBOL, "#t"};
|
|
|
|
scm scm_f = {SYMBOL, "#f"};
|
|
|
|
scm scm_unspecified = {SYMBOL, "*unspecified*"};
|
2016-07-10 08:43:26 +00:00
|
|
|
|
2016-07-23 22:01:31 +00:00
|
|
|
scm symbol_closure = {SYMBOL, "*closure*"};
|
2016-07-22 10:23:23 +00:00
|
|
|
scm symbol_circ = {SYMBOL, "*circ*"};
|
2016-07-19 16:18:27 +00:00
|
|
|
scm symbol_lambda = {SYMBOL, "lambda"};
|
|
|
|
scm symbol_begin = {SYMBOL, "begin"};
|
|
|
|
scm symbol_list = {SYMBOL, "list"};
|
|
|
|
scm symbol_cond = {SYMBOL, "cond"};
|
|
|
|
scm symbol_quote = {SYMBOL, "quote"};
|
|
|
|
scm symbol_quasiquote = {SYMBOL, "quasiquote"};
|
|
|
|
scm symbol_unquote = {SYMBOL, "unquote"};
|
|
|
|
scm symbol_unquote_splicing = {SYMBOL, "unquote-splicing"};
|
|
|
|
|
|
|
|
scm symbol_call_with_values = {SYMBOL, "call-with-values"};
|
|
|
|
scm symbol_current_module = {SYMBOL, "current-module"};
|
|
|
|
scm symbol_define = {SYMBOL, "define"};
|
|
|
|
scm symbol_define_macro = {SYMBOL, "define-macro"};
|
|
|
|
scm symbol_set_x = {SYMBOL, "set!"};
|
2016-05-28 14:39:44 +00:00
|
|
|
|
|
|
|
// PRIMITIVES
|
2016-05-15 22:07:44 +00:00
|
|
|
|
|
|
|
scm *
|
2016-05-29 11:44:03 +00:00
|
|
|
atom_p (scm *x)
|
2016-05-15 22:07:44 +00:00
|
|
|
{
|
|
|
|
return x->type == PAIR ? &scm_f : &scm_t;
|
|
|
|
}
|
|
|
|
|
2016-05-28 14:39:44 +00:00
|
|
|
scm *
|
|
|
|
car (scm *x)
|
|
|
|
{
|
|
|
|
assert (x->type == PAIR);
|
|
|
|
return x->car;
|
|
|
|
}
|
|
|
|
|
|
|
|
scm *
|
|
|
|
cdr (scm *x)
|
|
|
|
{
|
|
|
|
assert (x->type == PAIR);
|
|
|
|
return x->cdr;
|
|
|
|
}
|
|
|
|
|
|
|
|
scm *
|
|
|
|
cons (scm *x, scm *y)
|
|
|
|
{
|
|
|
|
scm *p = malloc (sizeof (scm));
|
|
|
|
p->type = PAIR;
|
|
|
|
p->car = x;
|
|
|
|
p->cdr = y;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
scm *
|
|
|
|
eq_p (scm *x, scm *y)
|
|
|
|
{
|
|
|
|
return (x == y
|
2016-07-10 22:15:28 +00:00
|
|
|
|| (x->type == CHAR && y->type == CHAR
|
|
|
|
&& x->value == y->value)
|
2016-05-28 14:39:44 +00:00
|
|
|
|| (x->type == NUMBER && y->type == NUMBER
|
2016-05-15 22:07:44 +00:00
|
|
|
&& x->value == y->value)
|
|
|
|
// FIXME: alist lookup symbols
|
2016-05-29 11:44:03 +00:00
|
|
|
|| (atom_p (x) == &scm_t
|
2016-07-22 10:23:23 +00:00
|
|
|
&& atom_p (y) == &scm_t
|
2016-07-10 22:15:28 +00:00
|
|
|
&& x->type != CHAR
|
|
|
|
&& y->type != CHAR
|
2016-05-15 22:07:44 +00:00
|
|
|
&& x->type != NUMBER
|
2016-05-29 11:44:03 +00:00
|
|
|
&& y->type != NUMBER
|
2016-07-22 10:23:23 +00:00
|
|
|
&& x->type != STRING
|
|
|
|
&& y->type != STRING
|
2016-07-11 08:38:02 +00:00
|
|
|
&& x->type != VECTOR
|
|
|
|
&& y->type != VECTOR
|
2016-05-15 22:07:44 +00:00
|
|
|
&& !strcmp (x->name, y->name)))
|
2016-05-28 14:39:44 +00:00
|
|
|
? &scm_t : &scm_f;
|
|
|
|
}
|
|
|
|
|
2016-07-17 20:15:31 +00:00
|
|
|
scm *
|
2016-07-22 15:13:51 +00:00
|
|
|
macro_p (scm *x)
|
2016-07-17 20:15:31 +00:00
|
|
|
{
|
2016-07-22 15:13:51 +00:00
|
|
|
return x->type == MACRO ? &scm_t : &scm_f;
|
2016-07-17 20:15:31 +00:00
|
|
|
}
|
|
|
|
|
2016-05-28 14:39:44 +00:00
|
|
|
scm *
|
|
|
|
null_p (scm *x)
|
|
|
|
{
|
|
|
|
return eq_p (x, &scm_nil);
|
|
|
|
}
|
|
|
|
|
|
|
|
scm *
|
|
|
|
pair_p (scm *x)
|
|
|
|
{
|
|
|
|
return x->type == PAIR ? &scm_t : &scm_f;
|
|
|
|
}
|
|
|
|
|
2016-07-10 08:43:26 +00:00
|
|
|
scm *
|
|
|
|
set_cdr_x (scm *x, scm *e)
|
|
|
|
{
|
|
|
|
assert (x->type == PAIR);
|
|
|
|
x->cdr = e;
|
2016-07-10 11:47:56 +00:00
|
|
|
return &scm_unspecified;
|
|
|
|
}
|
|
|
|
|
|
|
|
scm *
|
|
|
|
set_x (scm *x, scm *e, scm *a)
|
|
|
|
{
|
2016-07-11 08:48:25 +00:00
|
|
|
return set_cdr_x (assq (x, a), e);
|
2016-07-10 08:43:26 +00:00
|
|
|
}
|
2016-05-28 14:39:44 +00:00
|
|
|
|
2016-07-10 08:43:26 +00:00
|
|
|
scm *
|
|
|
|
set_env_x (scm *x, scm *e, scm *a)
|
|
|
|
{
|
2016-07-11 08:48:25 +00:00
|
|
|
return set_cdr_x (assq (x, a), e);
|
2016-07-10 08:43:26 +00:00
|
|
|
}
|
2016-05-28 14:39:44 +00:00
|
|
|
|
|
|
|
scm *
|
|
|
|
quote (scm *x)
|
|
|
|
{
|
2016-07-19 16:18:27 +00:00
|
|
|
return cons (&symbol_quote, x);
|
2016-05-28 14:39:44 +00:00
|
|
|
}
|
|
|
|
|
2016-07-16 15:18:11 +00:00
|
|
|
scm *
|
|
|
|
quasiquote (scm *x)
|
|
|
|
{
|
2016-07-19 16:18:27 +00:00
|
|
|
return cons (&symbol_quasiquote, x);
|
2016-07-16 15:18:11 +00:00
|
|
|
}
|
|
|
|
|
2016-07-09 12:29:39 +00:00
|
|
|
scm *
|
2016-07-17 21:43:16 +00:00
|
|
|
unquote (scm *x) //int must not add to environment
|
2016-07-09 12:29:39 +00:00
|
|
|
{
|
2016-07-19 16:18:27 +00:00
|
|
|
return cons (&symbol_unquote, x);
|
2016-07-09 12:29:39 +00:00
|
|
|
}
|
2016-07-17 21:43:16 +00:00
|
|
|
scm *unquote (scm *x);
|
|
|
|
scm scm_unquote = {FUNCTION1, .name="unquote", .function1=&unquote};
|
2016-07-09 12:29:39 +00:00
|
|
|
|
|
|
|
scm *
|
2016-07-17 21:43:16 +00:00
|
|
|
unquote_splicing (scm *x) //int must not add to environment
|
2016-07-09 12:29:39 +00:00
|
|
|
{
|
2016-07-19 16:18:27 +00:00
|
|
|
return cons (&symbol_unquote_splicing, x);
|
2016-07-09 12:29:39 +00:00
|
|
|
}
|
2016-07-17 21:43:16 +00:00
|
|
|
scm *unquote_splicing (scm *x);
|
|
|
|
scm scm_unquote_splicing = {FUNCTION1, .name="unquote-splicing", .function1=&unquote_splicing};
|
2016-07-09 12:29:39 +00:00
|
|
|
|
2016-05-15 22:07:44 +00:00
|
|
|
//Library functions
|
2016-05-28 14:39:44 +00:00
|
|
|
|
2016-05-15 22:07:44 +00:00
|
|
|
// Derived, non-primitives
|
|
|
|
scm *caar (scm *x) {return car (car (x));}
|
|
|
|
scm *cadr (scm *x) {return car (cdr (x));}
|
|
|
|
scm *cdar (scm *x) {return cdr (car (x));}
|
|
|
|
scm *cddr (scm *x) {return cdr (cdr (x));}
|
|
|
|
scm *caadr (scm *x) {return car (car (cdr (x)));}
|
2016-05-28 14:39:44 +00:00
|
|
|
scm *caddr (scm *x) {return car (cdr (cdr (x)));}
|
2016-05-15 22:07:44 +00:00
|
|
|
scm *cdadr (scm *x) {return cdr (car (cdr (x)));}
|
2016-05-28 14:39:44 +00:00
|
|
|
scm *cadar (scm *x) {return car (cdr (car (x)));}
|
2016-05-15 22:07:44 +00:00
|
|
|
scm *cddar (scm *x) {return cdr (cdr (car (x)));}
|
|
|
|
scm *cdddr (scm *x) {return cdr (cdr (cdr (x)));}
|
2016-05-28 14:39:44 +00:00
|
|
|
|
|
|
|
scm *
|
|
|
|
pairlis (scm *x, scm *y, scm *a)
|
|
|
|
{
|
|
|
|
if (x == &scm_nil)
|
|
|
|
return a;
|
2016-07-08 16:02:06 +00:00
|
|
|
if (atom_p (x) == &scm_t)
|
|
|
|
return cons (cons (x, y), a);
|
2016-05-28 14:39:44 +00:00
|
|
|
return cons (cons (car (x), car (y)),
|
|
|
|
pairlis (cdr (x), cdr (y), a));
|
|
|
|
}
|
|
|
|
|
|
|
|
scm *
|
2016-07-11 08:48:25 +00:00
|
|
|
assq (scm *x, scm *a)
|
2016-05-28 14:39:44 +00:00
|
|
|
{
|
2016-05-15 22:07:44 +00:00
|
|
|
if (a == &scm_nil) {
|
|
|
|
#if DEBUG
|
|
|
|
printf ("alist miss: %s\n", x->name);
|
|
|
|
#endif
|
|
|
|
return &scm_f;
|
|
|
|
}
|
2016-05-28 14:39:44 +00:00
|
|
|
if (eq_p (caar (a), x) == &scm_t)
|
|
|
|
return car (a);
|
2016-07-11 08:48:25 +00:00
|
|
|
return assq (x, cdr (a));
|
2016-05-28 14:39:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
scm *
|
2016-07-18 20:43:16 +00:00
|
|
|
apply_env (scm *fn, scm *x, scm *a)
|
2016-05-28 14:39:44 +00:00
|
|
|
{
|
2016-05-15 22:07:44 +00:00
|
|
|
#if DEBUG
|
2016-07-22 10:23:23 +00:00
|
|
|
printf ("\napply_env fn=");
|
2016-05-28 14:39:44 +00:00
|
|
|
display (fn);
|
2016-07-08 16:02:06 +00:00
|
|
|
printf (" x=");
|
|
|
|
display (x);
|
2016-05-28 14:39:44 +00:00
|
|
|
puts ("");
|
2016-07-17 08:38:29 +00:00
|
|
|
#endif
|
|
|
|
scm *macro;
|
2016-05-29 11:44:03 +00:00
|
|
|
if (atom_p (fn) != &scm_f)
|
2016-05-28 14:39:44 +00:00
|
|
|
{
|
2016-07-19 19:37:39 +00:00
|
|
|
if (fn == &symbol_current_module) return a;
|
2016-07-19 16:18:27 +00:00
|
|
|
if (eq_p (fn, &symbol_call_with_values) == &scm_t)
|
2016-07-11 19:50:59 +00:00
|
|
|
return call (&scm_call_with_values_env, append2 (x, cons (a, &scm_nil)));
|
2016-05-15 22:07:44 +00:00
|
|
|
if (builtin_p (fn) == &scm_t)
|
2016-05-28 14:39:44 +00:00
|
|
|
return call (fn, x);
|
|
|
|
}
|
2016-07-23 22:01:31 +00:00
|
|
|
else if (car (fn) == &symbol_lambda) {
|
|
|
|
scm *p = pairlis (cadr (fn), x, a);
|
|
|
|
return eval (cons (&symbol_begin, cddr (fn)), cons (cons (&symbol_closure, p), p));
|
|
|
|
}
|
2016-07-19 19:37:39 +00:00
|
|
|
else if (car (fn) == &symbol_closure) {
|
|
|
|
scm *args = caddr (fn);
|
|
|
|
scm *body = cdddr (fn);
|
2016-07-22 10:23:23 +00:00
|
|
|
a = cdadr (fn);
|
2016-07-23 22:01:31 +00:00
|
|
|
a = cdr (a);
|
|
|
|
scm *p = pairlis (args, x, a);
|
|
|
|
return eval (cons (&symbol_begin, body), cons (cons (&symbol_closure, p), p));
|
2016-07-19 19:37:39 +00:00
|
|
|
}
|
2016-07-22 15:13:51 +00:00
|
|
|
else if ((macro = lookup_macro (car (fn), a)) != &scm_f) {
|
|
|
|
scm *r = apply_env (eval (macro, a), cdr (fn), a);
|
2016-07-19 19:37:39 +00:00
|
|
|
scm *e = eval (r, a);
|
|
|
|
return apply_env (e, x, a);
|
|
|
|
}
|
2016-07-22 07:00:32 +00:00
|
|
|
scm *efn = eval (fn, a);
|
|
|
|
if (efn->type == NUMBER || efn == &scm_f || efn == &scm_t) assert (!"apply bool");
|
|
|
|
return apply_env (efn, x, a);
|
2016-05-28 14:39:44 +00:00
|
|
|
}
|
|
|
|
|
2016-07-22 15:13:51 +00:00
|
|
|
scm *make_symbol (char const *s);
|
|
|
|
|
2016-05-28 14:39:44 +00:00
|
|
|
scm *
|
2016-07-18 20:43:16 +00:00
|
|
|
eval (scm *e, scm *a)
|
2016-05-28 14:39:44 +00:00
|
|
|
{
|
2016-07-08 16:02:06 +00:00
|
|
|
#if DEBUG
|
2016-07-22 10:23:23 +00:00
|
|
|
printf ("\neval e=");
|
2016-07-08 16:02:06 +00:00
|
|
|
display (e);
|
2016-07-23 22:01:31 +00:00
|
|
|
printf ("\na=");
|
|
|
|
display (a);
|
2016-07-08 16:02:06 +00:00
|
|
|
puts ("");
|
|
|
|
#endif
|
2016-07-19 19:37:39 +00:00
|
|
|
scm *macro;
|
2016-07-19 16:18:27 +00:00
|
|
|
if (e->type == SYMBOL) {
|
2016-07-11 08:48:25 +00:00
|
|
|
scm *y = assq (e, a);
|
2016-05-15 22:07:44 +00:00
|
|
|
if (y == &scm_f) {
|
2016-07-23 09:51:06 +00:00
|
|
|
//return e;
|
2016-05-15 22:07:44 +00:00
|
|
|
printf ("eval: no such symbol: %s\n", e->name);
|
2016-07-17 20:15:31 +00:00
|
|
|
assert (!"unknown symbol");
|
2016-05-15 22:07:44 +00:00
|
|
|
}
|
|
|
|
return cdr (y);
|
|
|
|
}
|
2016-07-19 16:18:27 +00:00
|
|
|
else if (pair_p (e) == &scm_f)
|
2016-05-28 14:39:44 +00:00
|
|
|
return e;
|
2016-05-29 11:44:03 +00:00
|
|
|
else if (atom_p (car (e)) == &scm_t)
|
2016-05-28 14:39:44 +00:00
|
|
|
{
|
2016-07-19 16:18:27 +00:00
|
|
|
if (car (e) == &symbol_quote)
|
2016-05-28 14:39:44 +00:00
|
|
|
return cadr (e);
|
2016-07-19 16:18:27 +00:00
|
|
|
if (car (e) == &symbol_begin)
|
|
|
|
{
|
|
|
|
scm *body = cdr (e);
|
2016-07-23 22:01:31 +00:00
|
|
|
if (body == &scm_nil) return &scm_unspecified;
|
|
|
|
e = car (body);
|
|
|
|
body = cdr (body);
|
|
|
|
scm *r = eval (e, a);
|
2016-07-19 16:18:27 +00:00
|
|
|
if (body == &scm_nil) return r;
|
|
|
|
return eval (cons (&symbol_begin, body), a);
|
|
|
|
}
|
2016-07-19 19:37:39 +00:00
|
|
|
if (car (e) == &symbol_lambda)
|
2016-07-23 22:01:31 +00:00
|
|
|
return make_closure (cadr (e), cddr (e), assq (&symbol_closure, a));
|
2016-07-19 19:37:39 +00:00
|
|
|
if (car (e) == &symbol_closure)
|
|
|
|
return e;
|
2016-07-19 16:18:27 +00:00
|
|
|
if (car (e) == &symbol_unquote)
|
2016-07-09 12:29:39 +00:00
|
|
|
return eval (cadr (e), a);
|
2016-07-19 16:18:27 +00:00
|
|
|
if (car (e) == &symbol_quasiquote)
|
2016-07-17 20:15:31 +00:00
|
|
|
return eval_quasiquote (cadr (e), add_unquoters (a));
|
2016-07-19 16:18:27 +00:00
|
|
|
if (car (e) == &symbol_cond)
|
2016-05-28 14:39:44 +00:00
|
|
|
return evcon (cdr (e), a);
|
2016-07-23 22:01:31 +00:00
|
|
|
if (eq_p (car (e), &symbol_define) == &scm_t)
|
|
|
|
return define (e, a);
|
2016-07-19 16:18:27 +00:00
|
|
|
if (eq_p (car (e), &symbol_define_macro) == &scm_t)
|
2016-07-22 15:13:51 +00:00
|
|
|
return define (e, a);
|
|
|
|
if ((macro = lookup_macro (car (e), a)) != &scm_f)
|
|
|
|
return eval (apply_env (macro, cdr (e), a), a);
|
2016-07-19 16:18:27 +00:00
|
|
|
if (car (e) == &symbol_set_x)
|
|
|
|
return set_env_x (cadr (e), eval (caddr (e), a), a);
|
2016-05-28 14:39:44 +00:00
|
|
|
}
|
2016-07-16 06:17:56 +00:00
|
|
|
return apply_env (car (e), evlis (cdr (e), a), a);
|
2016-05-28 14:39:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
scm *
|
2016-07-19 16:18:27 +00:00
|
|
|
evcon (scm *c, scm *a)
|
2016-05-28 14:39:44 +00:00
|
|
|
{
|
2016-07-17 10:57:06 +00:00
|
|
|
if (c == &scm_nil) return &scm_unspecified;
|
|
|
|
scm *clause = car (c);
|
|
|
|
scm *expr = eval (car (clause), a);
|
|
|
|
if (expr != &scm_f) {
|
|
|
|
if (cdr (clause) == &scm_nil)
|
|
|
|
return expr;
|
|
|
|
if (cddr (clause) == &scm_nil)
|
|
|
|
return eval (cadr (clause), a);
|
|
|
|
eval (cadr (clause), a);
|
2016-07-19 16:18:27 +00:00
|
|
|
return evcon (cons (cons (&scm_t, cddr (clause)), &scm_nil), a);
|
2016-05-15 22:07:44 +00:00
|
|
|
}
|
2016-07-19 16:18:27 +00:00
|
|
|
return evcon (cdr (c), a);
|
2016-05-29 11:44:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
scm *
|
2016-07-19 16:18:27 +00:00
|
|
|
evlis (scm *m, scm *a)
|
2016-05-29 11:44:03 +00:00
|
|
|
{
|
2016-07-19 16:18:27 +00:00
|
|
|
if (m == &scm_nil) return &scm_nil;
|
|
|
|
if (m->type != PAIR) return eval (m, a);
|
|
|
|
scm *e = eval (car (m), a);
|
|
|
|
return cons (e, evlis (cdr (m), a));
|
2016-05-28 14:39:44 +00:00
|
|
|
}
|
2016-05-29 11:44:03 +00:00
|
|
|
|
2016-05-28 14:39:44 +00:00
|
|
|
scm *
|
2016-07-19 16:18:27 +00:00
|
|
|
eval_quasiquote (scm *e, scm *a)
|
2016-05-28 14:39:44 +00:00
|
|
|
{
|
2016-07-19 16:18:27 +00:00
|
|
|
if (e == &scm_nil) return e;
|
|
|
|
else if (atom_p (e) == &scm_t) return e;
|
|
|
|
else if (eq_p (car (e), &symbol_unquote) == &scm_t)
|
|
|
|
return eval (cadr (e), a);
|
|
|
|
else if (e->type == PAIR && e->car->type == PAIR
|
|
|
|
&& eq_p (caar (e), &symbol_unquote_splicing) == &scm_t)
|
|
|
|
return append2 (eval (cadar (e), a), eval_quasiquote (cdr (e), a));
|
|
|
|
return cons (eval_quasiquote (car (e), a), eval_quasiquote (cdr (e), a));
|
2016-05-28 14:39:44 +00:00
|
|
|
}
|
2016-05-29 11:44:03 +00:00
|
|
|
|
|
|
|
//Helpers
|
2016-05-28 14:39:44 +00:00
|
|
|
|
|
|
|
scm *
|
2016-05-15 22:07:44 +00:00
|
|
|
builtin_p (scm *x)
|
2016-05-28 14:39:44 +00:00
|
|
|
{
|
|
|
|
return (x->type == FUNCTION0
|
|
|
|
|| x->type == FUNCTION1
|
|
|
|
|| x->type == FUNCTION2
|
2016-07-10 20:43:23 +00:00
|
|
|
|| x->type == FUNCTION3
|
|
|
|
|| x->type == FUNCTIONn)
|
2016-05-28 14:39:44 +00:00
|
|
|
? &scm_t : &scm_f;
|
|
|
|
}
|
2016-05-15 22:07:44 +00:00
|
|
|
|
2016-07-17 11:32:52 +00:00
|
|
|
scm *
|
|
|
|
boolean_p (scm *x)
|
|
|
|
{
|
|
|
|
return (x == &scm_t || x == &scm_f) ? &scm_t : &scm_f;
|
|
|
|
}
|
|
|
|
|
2016-07-10 22:15:28 +00:00
|
|
|
scm *
|
|
|
|
char_p (scm *x)
|
|
|
|
{
|
|
|
|
return x->type == CHAR ? &scm_t : &scm_f;
|
|
|
|
}
|
|
|
|
|
2016-05-15 22:07:44 +00:00
|
|
|
scm *
|
|
|
|
number_p (scm *x)
|
|
|
|
{
|
|
|
|
return x->type == NUMBER ? &scm_t : &scm_f;
|
|
|
|
}
|
2016-05-29 11:44:03 +00:00
|
|
|
|
2016-07-10 20:43:23 +00:00
|
|
|
scm *
|
|
|
|
string_p (scm *x)
|
|
|
|
{
|
|
|
|
return x->type == STRING ? &scm_t : &scm_f;
|
|
|
|
}
|
|
|
|
|
|
|
|
scm *
|
|
|
|
symbol_p (scm *x)
|
|
|
|
{
|
2016-07-17 11:16:10 +00:00
|
|
|
return (x->type == SYMBOL
|
|
|
|
&& x != &scm_nil
|
|
|
|
&& x != &scm_f
|
|
|
|
&& x != &scm_t) ? &scm_t : &scm_f;
|
2016-07-10 20:43:23 +00:00
|
|
|
}
|
|
|
|
|
2016-07-11 08:38:02 +00:00
|
|
|
scm *
|
|
|
|
vector_p (scm *x)
|
|
|
|
{
|
|
|
|
return x->type == VECTOR ? &scm_t : &scm_f;
|
|
|
|
}
|
|
|
|
|
2016-05-29 11:44:03 +00:00
|
|
|
scm *
|
|
|
|
display (scm *x)
|
|
|
|
{
|
|
|
|
return display_helper (x, false, "", false);
|
|
|
|
}
|
2016-05-28 14:39:44 +00:00
|
|
|
|
|
|
|
scm *
|
|
|
|
call (scm *fn, scm *x)
|
|
|
|
{
|
|
|
|
if (fn->type == FUNCTION0)
|
|
|
|
return fn->function0 ();
|
2016-07-11 17:32:11 +00:00
|
|
|
if (x->car->type == VALUES)
|
|
|
|
x = cons (x->car->cdr->car, &scm_nil);
|
2016-05-29 11:44:03 +00:00
|
|
|
if (fn->type == FUNCTION1)
|
2016-05-28 14:39:44 +00:00
|
|
|
return fn->function1 (car (x));
|
|
|
|
if (fn->type == FUNCTION2)
|
|
|
|
return fn->function2 (car (x), cadr (x));
|
|
|
|
if (fn->type == FUNCTION3)
|
|
|
|
return fn->function3 (car (x), cadr (x), caddr (x));
|
2016-07-10 20:43:23 +00:00
|
|
|
if (fn->type == FUNCTIONn)
|
|
|
|
return fn->functionn (x);
|
2016-05-28 14:39:44 +00:00
|
|
|
return &scm_unspecified;
|
|
|
|
}
|
|
|
|
|
|
|
|
scm *
|
2016-07-11 19:50:59 +00:00
|
|
|
append2 (scm *x, scm *y)
|
2016-05-28 14:39:44 +00:00
|
|
|
{
|
|
|
|
if (x == &scm_nil) return y;
|
|
|
|
assert (x->type == PAIR);
|
2016-07-11 19:50:59 +00:00
|
|
|
return cons (car (x), append2 (cdr (x), y));
|
2016-05-28 14:39:44 +00:00
|
|
|
}
|
|
|
|
|
2016-07-11 19:50:59 +00:00
|
|
|
scm *
|
|
|
|
append (scm *x/*...*/)
|
|
|
|
{
|
|
|
|
if (x == &scm_nil) return &scm_nil;
|
|
|
|
return append2 (car (x), append (cdr (x)));
|
|
|
|
}
|
|
|
|
|
2016-07-10 22:15:28 +00:00
|
|
|
scm *
|
|
|
|
make_char (int x)
|
|
|
|
{
|
|
|
|
scm *p = malloc (sizeof (scm));
|
|
|
|
p->type = CHAR;
|
|
|
|
p->value = x;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2016-07-22 15:13:51 +00:00
|
|
|
scm *
|
|
|
|
make_macro (scm *x) //int
|
|
|
|
{
|
|
|
|
scm *p = malloc (sizeof (scm));
|
|
|
|
p->type = MACRO;
|
|
|
|
p->macro = x;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2016-05-28 14:39:44 +00:00
|
|
|
scm *
|
2016-07-10 20:43:23 +00:00
|
|
|
make_number (int x)
|
2016-05-28 14:39:44 +00:00
|
|
|
{
|
|
|
|
scm *p = malloc (sizeof (scm));
|
2016-07-10 20:43:23 +00:00
|
|
|
p->type = NUMBER;
|
|
|
|
p->value = x;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
scm *
|
|
|
|
make_string (char const *s)
|
|
|
|
{
|
|
|
|
scm *p = malloc (sizeof (scm));
|
|
|
|
p->type = STRING;
|
2016-05-28 14:39:44 +00:00
|
|
|
p->name = strdup (s);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
scm *
|
2016-07-10 20:43:23 +00:00
|
|
|
make_symbol (char const *s)
|
2016-05-28 14:39:44 +00:00
|
|
|
{
|
2016-07-10 20:43:23 +00:00
|
|
|
// TODO: alist lookup symbols
|
2016-05-28 14:39:44 +00:00
|
|
|
scm *p = malloc (sizeof (scm));
|
2016-07-10 20:43:23 +00:00
|
|
|
p->type = SYMBOL;
|
|
|
|
p->name = strdup (s);
|
2016-05-28 14:39:44 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2016-07-11 08:38:02 +00:00
|
|
|
scm *
|
|
|
|
make_vector (int n)
|
|
|
|
{
|
|
|
|
scm *p = malloc (sizeof (scm));
|
|
|
|
p->type = VECTOR;
|
|
|
|
p->length = n;
|
|
|
|
p->vector = malloc (n * sizeof (scm*));
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2016-07-10 22:15:28 +00:00
|
|
|
scm *
|
|
|
|
string (scm *x/*...*/)
|
|
|
|
{
|
|
|
|
char buf[256] = "";
|
|
|
|
char *p = buf;
|
|
|
|
while (x != &scm_nil)
|
|
|
|
{
|
|
|
|
scm *s = car (x);
|
|
|
|
assert (s->type == CHAR);
|
|
|
|
*p++ = s->value;
|
|
|
|
x = cdr (x);
|
|
|
|
}
|
|
|
|
return make_string (buf);
|
|
|
|
}
|
|
|
|
|
2016-07-10 20:43:23 +00:00
|
|
|
scm *
|
|
|
|
string_append (scm *x/*...*/)
|
|
|
|
{
|
|
|
|
char buf[256] = "";
|
|
|
|
|
|
|
|
while (x != &scm_nil)
|
|
|
|
{
|
|
|
|
scm *s = car (x);
|
|
|
|
assert (s->type == STRING);
|
|
|
|
strcat (buf, s->name);
|
|
|
|
x = cdr (x);
|
|
|
|
}
|
|
|
|
return make_string (buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
scm *
|
|
|
|
string_length (scm *x)
|
|
|
|
{
|
|
|
|
assert (x->type == STRING);
|
|
|
|
return make_number (strlen (x->name));
|
|
|
|
}
|
|
|
|
|
2016-07-10 22:21:45 +00:00
|
|
|
scm *
|
|
|
|
length (scm *x)
|
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
while (x != &scm_nil)
|
|
|
|
{
|
|
|
|
n++;
|
|
|
|
x = cdr (x);
|
|
|
|
}
|
|
|
|
return make_number (n);
|
|
|
|
}
|
|
|
|
|
2016-07-23 12:39:33 +00:00
|
|
|
scm *
|
|
|
|
last_pair (scm *x)
|
|
|
|
{
|
|
|
|
//if (x != &scm_nil && cdr (x) != &scm_nil)
|
|
|
|
//return last_pair (cdr (x));
|
|
|
|
while (x != &scm_nil && cdr (x) != &scm_nil)
|
|
|
|
x = cdr (x);
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2016-07-11 08:38:02 +00:00
|
|
|
scm *
|
2016-07-19 16:18:27 +00:00
|
|
|
builtin_list (scm *x/*...*/)
|
2016-07-11 08:38:02 +00:00
|
|
|
{
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2016-07-11 17:32:11 +00:00
|
|
|
scm *
|
|
|
|
values (scm *x/*...*/)
|
|
|
|
{
|
|
|
|
scm *v = cons (0, x);
|
|
|
|
v->type = VALUES;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
scm *
|
|
|
|
call_with_values_env (scm *producer, scm *consumer, scm *a)
|
|
|
|
{
|
2016-07-18 20:43:16 +00:00
|
|
|
scm *v = apply_env (producer, &scm_nil, a);
|
2016-07-11 17:32:11 +00:00
|
|
|
if (v->type == VALUES)
|
|
|
|
v = v->cdr;
|
2016-07-18 20:43:16 +00:00
|
|
|
return apply_env (consumer, v, a);
|
2016-07-11 17:32:11 +00:00
|
|
|
}
|
|
|
|
|
2016-07-11 08:38:02 +00:00
|
|
|
scm *
|
|
|
|
vector_length (scm *x)
|
|
|
|
{
|
|
|
|
assert (x->type == VECTOR);
|
|
|
|
return make_number (x->length);
|
|
|
|
}
|
|
|
|
|
|
|
|
scm *
|
|
|
|
vector_ref (scm *x, scm *i)
|
|
|
|
{
|
|
|
|
assert (x->type == VECTOR);
|
|
|
|
assert (i->value < x->length);
|
|
|
|
return x->vector[i->value];
|
|
|
|
}
|
|
|
|
|
|
|
|
scm *
|
|
|
|
vector_set_x (scm *x, scm *i, scm *e)
|
|
|
|
{
|
|
|
|
assert (x->type == VECTOR);
|
|
|
|
assert (i->value < x->length);
|
|
|
|
x->vector[i->value] = e;
|
|
|
|
return &scm_unspecified;
|
|
|
|
}
|
|
|
|
|
2016-05-28 14:39:44 +00:00
|
|
|
scm *
|
2016-05-15 22:07:44 +00:00
|
|
|
lookup (char *x, scm *a)
|
2016-05-28 14:39:44 +00:00
|
|
|
{
|
|
|
|
if (isdigit (*x) || (*x == '-' && isdigit (*(x+1))))
|
|
|
|
return make_number (atoi (x));
|
2016-05-15 22:07:44 +00:00
|
|
|
|
2016-07-22 15:13:51 +00:00
|
|
|
if (!strcmp (x, scm_dot.name)) return &scm_dot;
|
|
|
|
if (!strcmp (x, scm_f.name)) return &scm_f;
|
2016-05-29 11:44:03 +00:00
|
|
|
if (!strcmp (x, scm_nil.name)) return &scm_nil;
|
2016-07-22 10:23:23 +00:00
|
|
|
if (!strcmp (x, scm_t.name)) return &scm_t;
|
2016-07-22 15:13:51 +00:00
|
|
|
if (!strcmp (x, scm_unspecified.name)) return &scm_unspecified;
|
2016-07-19 16:18:27 +00:00
|
|
|
|
|
|
|
if (!strcmp (x, symbol_begin.name)) return &symbol_begin;
|
2016-07-19 19:37:39 +00:00
|
|
|
if (!strcmp (x, symbol_closure.name)) return &symbol_closure;
|
2016-07-22 15:13:51 +00:00
|
|
|
if (!strcmp (x, symbol_cond.name)) return &symbol_cond;
|
|
|
|
if (!strcmp (x, symbol_current_module.name)) return &symbol_current_module;
|
2016-07-19 16:18:27 +00:00
|
|
|
if (!strcmp (x, symbol_lambda.name)) return &symbol_lambda;
|
2016-07-22 15:13:51 +00:00
|
|
|
if (!strcmp (x, symbol_quasiquote.name)) return &symbol_quasiquote;
|
2016-07-19 16:18:27 +00:00
|
|
|
if (!strcmp (x, symbol_quote.name)) return &symbol_quote;
|
2016-07-22 15:13:51 +00:00
|
|
|
if (!strcmp (x, symbol_set_x.name)) return &symbol_set_x;
|
|
|
|
if (!strcmp (x, symbol_unquote.name)) return &symbol_unquote;
|
|
|
|
if (!strcmp (x, symbol_unquote_splicing.name)) return &symbol_unquote_splicing;
|
2016-07-19 16:18:27 +00:00
|
|
|
|
2016-07-22 10:23:23 +00:00
|
|
|
if (!strcmp (x, scm_car.name)) return &scm_car;
|
|
|
|
if (!strcmp (x, scm_cdr.name)) return &scm_cdr;
|
|
|
|
if (!strcmp (x, scm_display.name)) return &scm_display;
|
|
|
|
if (!strcmp (x, scm_builtin_list.name)) return &scm_builtin_list;
|
|
|
|
|
2016-07-22 15:13:51 +00:00
|
|
|
if (*x == '\'') return &symbol_quote;
|
2016-07-19 16:18:27 +00:00
|
|
|
if (*x == '`') return &symbol_quasiquote;
|
|
|
|
if (*x == ',' && *(x+1) == '@') return &symbol_unquote_splicing;
|
|
|
|
if (*x == ',') return &symbol_unquote;
|
2016-07-09 12:29:39 +00:00
|
|
|
|
2016-07-10 20:43:23 +00:00
|
|
|
return make_symbol (x);
|
2016-05-29 11:44:03 +00:00
|
|
|
}
|
|
|
|
|
2016-07-09 12:29:39 +00:00
|
|
|
scm *
|
|
|
|
lookup_char (int c, scm *a)
|
|
|
|
{
|
|
|
|
char buf[2];
|
|
|
|
buf[0] = c;
|
|
|
|
buf[1] = 0;
|
|
|
|
return lookup (buf, a);
|
|
|
|
}
|
|
|
|
|
2016-05-29 11:44:03 +00:00
|
|
|
char *
|
2016-07-11 19:50:59 +00:00
|
|
|
list2str (scm *l) // char*
|
2016-05-29 11:44:03 +00:00
|
|
|
{
|
|
|
|
static char buf[256];
|
|
|
|
char *p = buf;
|
|
|
|
while (l != &scm_nil) {
|
|
|
|
scm *c = car (l);
|
|
|
|
assert (c->type == NUMBER);
|
|
|
|
*p++ = c->value;
|
|
|
|
l = cdr (l);
|
2016-05-28 14:39:44 +00:00
|
|
|
}
|
2016-05-29 11:44:03 +00:00
|
|
|
*p = 0;
|
|
|
|
return buf;
|
|
|
|
}
|
2016-05-28 14:39:44 +00:00
|
|
|
|
2016-07-11 08:38:02 +00:00
|
|
|
scm*
|
|
|
|
list_to_vector (scm *x)
|
|
|
|
{
|
|
|
|
int n = length (x)->value;
|
|
|
|
scm *v = make_vector (n);
|
|
|
|
scm **p = v->vector;
|
|
|
|
while (x != &scm_nil)
|
|
|
|
{
|
|
|
|
*p++ = car (x);
|
|
|
|
x = cdr (x);
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2016-07-18 20:43:16 +00:00
|
|
|
scm*
|
|
|
|
integer_to_char (scm *x)
|
|
|
|
{
|
|
|
|
assert (x->type == NUMBER);
|
|
|
|
return make_char (x->value);
|
|
|
|
}
|
|
|
|
|
|
|
|
scm*
|
|
|
|
char_to_integer (scm *x)
|
|
|
|
{
|
|
|
|
assert (x->type == CHAR);
|
|
|
|
return make_number (x->value);
|
|
|
|
}
|
|
|
|
|
2016-07-11 20:36:38 +00:00
|
|
|
scm*
|
|
|
|
number_to_string (scm *x)
|
|
|
|
{
|
|
|
|
assert (x->type == NUMBER);
|
|
|
|
char buf[256];
|
|
|
|
sprintf (buf,"%d", x->value);
|
|
|
|
return make_string (buf);
|
|
|
|
}
|
|
|
|
|
2016-07-18 20:43:16 +00:00
|
|
|
scm*
|
|
|
|
builtin_exit (scm *x)
|
|
|
|
{
|
|
|
|
assert (x->type == NUMBER);
|
|
|
|
exit (x->value);
|
|
|
|
}
|
|
|
|
|
2016-07-11 20:36:38 +00:00
|
|
|
scm*
|
|
|
|
string_to_symbol (scm *x)
|
|
|
|
{
|
|
|
|
assert (x->type == STRING);
|
|
|
|
return make_symbol (x->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
scm*
|
|
|
|
symbol_to_string (scm *x)
|
|
|
|
{
|
|
|
|
assert (x->type == SYMBOL);
|
|
|
|
return make_string (x->name);
|
|
|
|
}
|
|
|
|
|
2016-07-11 08:38:02 +00:00
|
|
|
scm*
|
|
|
|
vector_to_list (scm *v)
|
|
|
|
{
|
|
|
|
scm *x = &scm_nil;
|
|
|
|
for (int i = 0; i < v->length; i++)
|
2016-07-11 19:50:59 +00:00
|
|
|
x = append2 (x, cons (v->vector[i], &scm_nil));
|
2016-07-11 08:38:02 +00:00
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2016-05-15 22:07:44 +00:00
|
|
|
scm *
|
|
|
|
newline ()
|
|
|
|
{
|
|
|
|
puts ("");
|
|
|
|
return &scm_unspecified;
|
2016-05-28 14:39:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
scm *
|
2016-05-15 22:07:44 +00:00
|
|
|
display_helper (scm *x, bool cont, char *sep, bool quote)
|
2016-05-28 14:39:44 +00:00
|
|
|
{
|
|
|
|
scm *r;
|
2016-05-29 11:44:03 +00:00
|
|
|
printf ("%s", sep);
|
2016-07-10 22:15:28 +00:00
|
|
|
if (x->type == CHAR && x->value == 10) printf ("#\\%s", "newline");
|
|
|
|
else if (x->type == CHAR && x->value == 32) printf ("#\\%s", "space");
|
|
|
|
else if (x->type == CHAR) printf ("#\\%c", x->value);
|
2016-07-22 15:13:51 +00:00
|
|
|
else if (x->type == MACRO) {
|
|
|
|
printf ("(*macro* ");
|
|
|
|
display_helper (x->macro, cont, sep, quote);
|
|
|
|
printf (")");
|
|
|
|
}
|
2016-07-10 22:15:28 +00:00
|
|
|
else if (x->type == NUMBER) printf ("%d", x->value);
|
2016-05-28 14:39:44 +00:00
|
|
|
else if (x->type == PAIR) {
|
2016-07-22 10:23:23 +00:00
|
|
|
if (car (x) == &symbol_circ) {
|
|
|
|
printf ("(*circ* . #-1#)");
|
|
|
|
return &scm_unspecified;
|
|
|
|
}
|
2016-07-23 22:01:31 +00:00
|
|
|
if (car (x) == &symbol_closure) {
|
|
|
|
printf ("(*closure* . #-1#)");
|
|
|
|
return &scm_unspecified;
|
|
|
|
}
|
2016-05-28 14:39:44 +00:00
|
|
|
if (car (x) == &scm_quote) {
|
|
|
|
printf ("'");
|
2016-05-15 22:07:44 +00:00
|
|
|
return display_helper (car (cdr (x)), cont, "", true);
|
|
|
|
}
|
2016-07-10 22:17:12 +00:00
|
|
|
if (car (x) == &scm_quasiquote) {
|
2016-07-09 12:29:39 +00:00
|
|
|
printf ("`");
|
|
|
|
return display_helper (car (cdr (x)), cont, "", true);
|
|
|
|
}
|
2016-07-10 22:17:12 +00:00
|
|
|
if (car (x) == &scm_unquote) {
|
2016-07-09 12:29:39 +00:00
|
|
|
printf (",");
|
|
|
|
return display_helper (car (cdr (x)), cont, "", true);
|
|
|
|
}
|
2016-07-16 15:18:11 +00:00
|
|
|
if (car (x) == &scm_unquote_splicing) {
|
|
|
|
printf (",@");
|
|
|
|
return display_helper (car (cdr (x)), cont, "", true);
|
|
|
|
}
|
2016-05-28 14:39:44 +00:00
|
|
|
if (!cont) printf ("(");
|
|
|
|
display (car (x));
|
|
|
|
if (cdr (x)->type == PAIR)
|
2016-05-15 22:07:44 +00:00
|
|
|
display_helper (cdr (x), true, " ", false);
|
2016-05-28 14:39:44 +00:00
|
|
|
else if (cdr (x) != &scm_nil) {
|
|
|
|
printf (" . ");
|
|
|
|
display (cdr (x));
|
|
|
|
}
|
|
|
|
if (!cont) printf (")");
|
|
|
|
}
|
2016-07-11 08:38:02 +00:00
|
|
|
else if (x->type == VECTOR) {
|
2016-07-23 12:39:33 +00:00
|
|
|
printf ("#[%d](", x->length);
|
|
|
|
// for (int i = 0; i < x->length; i++)
|
|
|
|
// display_helper (x->vector[i], true, i ? " " : "", false);
|
2016-07-11 08:38:02 +00:00
|
|
|
printf (")");
|
|
|
|
}
|
2016-05-29 11:44:03 +00:00
|
|
|
else if (atom_p (x) == &scm_t) printf ("%s", x->name);
|
2016-05-28 14:39:44 +00:00
|
|
|
|
2016-07-17 20:15:31 +00:00
|
|
|
return &scm_unspecified;
|
2016-05-28 14:39:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// READ
|
2016-05-29 11:44:03 +00:00
|
|
|
|
2016-05-28 14:39:44 +00:00
|
|
|
int
|
2016-07-09 21:12:25 +00:00
|
|
|
ungetchar (int c) //int
|
2016-05-28 14:39:44 +00:00
|
|
|
{
|
|
|
|
return ungetc (c, stdin);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2016-07-09 21:12:25 +00:00
|
|
|
peekchar () //int
|
2016-05-28 14:39:44 +00:00
|
|
|
{
|
|
|
|
int c = getchar ();
|
|
|
|
ungetchar (c);
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
scm*
|
|
|
|
builtin_getchar ()
|
|
|
|
{
|
|
|
|
return make_number (getchar ());
|
|
|
|
}
|
|
|
|
|
|
|
|
scm*
|
|
|
|
builtin_peekchar ()
|
|
|
|
{
|
|
|
|
return make_number (peekchar ());
|
|
|
|
}
|
|
|
|
|
|
|
|
scm*
|
2016-07-09 21:12:25 +00:00
|
|
|
builtin_ungetchar (scm *c)
|
2016-05-28 14:39:44 +00:00
|
|
|
{
|
|
|
|
assert (c->type == NUMBER);
|
|
|
|
ungetchar (c->value);
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
readcomment (int c)
|
|
|
|
{
|
|
|
|
if (c == '\n') return c;
|
|
|
|
return readcomment (getchar ());
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
readblock (int c)
|
|
|
|
{
|
|
|
|
if (c == '!' && peekchar () == '#') return getchar ();
|
|
|
|
return readblock (getchar ());
|
|
|
|
}
|
|
|
|
|
|
|
|
scm *
|
|
|
|
readword (int c, char* w, scm *a)
|
|
|
|
{
|
|
|
|
if (c == EOF && !w) return &scm_nil;
|
|
|
|
if (c == '\n' && !w) return readword (getchar (), w, a);
|
2016-07-08 16:02:06 +00:00
|
|
|
if (c == '\n' && *w == '.' && w[1] == 0) return &scm_dot;
|
2016-05-15 22:07:44 +00:00
|
|
|
if (c == EOF || c == '\n') return lookup (w, a);
|
2016-05-28 14:39:44 +00:00
|
|
|
if (c == ' ') return readword ('\n', w, a);
|
2016-07-10 20:43:23 +00:00
|
|
|
if (c == '"' && !w) return readstring ();
|
|
|
|
if (c == '"') {ungetchar (c); return lookup (w, a);}
|
2016-07-11 08:38:02 +00:00
|
|
|
if (c == '(' && !w) return readlist (a);
|
2016-05-15 22:07:44 +00:00
|
|
|
if (c == '(') {ungetchar (c); return lookup (w, a);}
|
2016-05-28 14:39:44 +00:00
|
|
|
if (c == ')' && !w) {ungetchar (c); return &scm_nil;}
|
2016-05-15 22:07:44 +00:00
|
|
|
if (c == ')') {ungetchar (c); return lookup (w, a);}
|
2016-07-16 15:18:11 +00:00
|
|
|
if (c == ',' && peekchar () == '@') {getchar (); return cons (lookup (",@", a),
|
|
|
|
cons (readword (getchar (), w, a),
|
|
|
|
&scm_nil));}
|
2016-07-09 12:29:39 +00:00
|
|
|
if ((c == '\''
|
|
|
|
|| c == '`'
|
2016-07-19 16:18:27 +00:00
|
|
|
|| c == ',')
|
2016-07-09 12:29:39 +00:00
|
|
|
&& !w) {return cons (lookup_char (c, a),
|
2016-05-28 14:39:44 +00:00
|
|
|
cons (readword (getchar (), w, a),
|
|
|
|
&scm_nil));}
|
|
|
|
if (c == ';') {readcomment (c); return readword ('\n', w, a);}
|
2016-07-10 22:15:28 +00:00
|
|
|
if (c == '#' && peekchar () == '\\') {getchar (); return readchar ();}
|
2016-07-11 08:38:02 +00:00
|
|
|
if (c == '#' && !w && peekchar () == '(') {getchar (); return list_to_vector (readlist (a));}
|
|
|
|
if (c == '#' && peekchar () == '(') {ungetchar (c); return lookup (w, a);}
|
2016-05-28 14:39:44 +00:00
|
|
|
if (c == '#' && peekchar () == '!') {getchar (); readblock (getchar ()); return readword (getchar (), w, a);}
|
2016-05-29 11:44:03 +00:00
|
|
|
char buf[256] = {0};
|
|
|
|
char ch = c;
|
|
|
|
return readword (getchar (), strncat (w ? w : buf, &ch, 1), a);
|
2016-05-28 14:39:44 +00:00
|
|
|
}
|
|
|
|
|
2016-07-10 22:15:28 +00:00
|
|
|
scm *
|
|
|
|
readchar ()
|
|
|
|
{
|
|
|
|
int c = getchar ();
|
|
|
|
if (c >= '0' && c <= '7'
|
|
|
|
&& peekchar () >= '0' && peekchar () <= '7') {
|
|
|
|
c = c - '0';
|
|
|
|
while (peekchar () >= '0' && peekchar () <= '7') {
|
|
|
|
c <<= 3;
|
|
|
|
c += getchar () - '0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (c >= 'a' && c <= 'z'
|
|
|
|
&& peekchar () >= 'a' && peekchar () <= 'z') {
|
|
|
|
char buf[256];
|
|
|
|
char *p = buf;
|
|
|
|
*p++ = c;
|
|
|
|
while (peekchar () >= 'a' && peekchar () <= 'z') {
|
|
|
|
*p++ = getchar ();
|
|
|
|
}
|
|
|
|
*p = 0;
|
|
|
|
if (!strcmp (buf, "newline")) c = 10;
|
|
|
|
else if (!strcmp (buf, "space")) c = 32;
|
|
|
|
else {
|
|
|
|
printf ("char not supported: %s", buf);
|
|
|
|
assert (!"char not supported");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return make_char (c);
|
|
|
|
}
|
|
|
|
|
2016-07-10 20:43:23 +00:00
|
|
|
scm *
|
|
|
|
readstring ()
|
|
|
|
{
|
|
|
|
char buf[256];
|
|
|
|
char *p = buf;
|
|
|
|
int c = getchar ();
|
|
|
|
while (true) {
|
|
|
|
if (c == '"') break;
|
|
|
|
*p++ = c;
|
|
|
|
if (c == '\\' && peekchar () == '"') *p++ = getchar ();
|
|
|
|
if (c == EOF) assert (!"EOF in string");
|
|
|
|
c = getchar ();
|
|
|
|
}
|
|
|
|
*p = 0;
|
|
|
|
return make_string (buf);
|
|
|
|
}
|
|
|
|
|
2016-07-09 16:56:59 +00:00
|
|
|
int
|
|
|
|
eat_whitespace (int c)
|
|
|
|
{
|
|
|
|
while (c == ' ' || c == '\n') c = getchar ();
|
|
|
|
if (c == ';') return eat_whitespace (readcomment (c));
|
|
|
|
if (c == '#' && peekchar () == '!') {getchar (); readblock (getchar ()); return eat_whitespace (getchar ());}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2016-05-28 14:39:44 +00:00
|
|
|
scm *
|
2016-07-11 08:38:02 +00:00
|
|
|
readlist (scm *a)
|
2016-05-28 14:39:44 +00:00
|
|
|
{
|
|
|
|
int c = getchar ();
|
2016-07-09 16:56:59 +00:00
|
|
|
c = eat_whitespace (c);
|
2016-05-28 14:39:44 +00:00
|
|
|
if (c == ')') return &scm_nil;
|
|
|
|
scm *w = readword (c, 0, a);
|
2016-07-08 16:02:06 +00:00
|
|
|
if (w == &scm_dot)
|
2016-07-11 08:38:02 +00:00
|
|
|
return car (readlist (a));
|
|
|
|
return cons (w, readlist (a));
|
2016-05-28 14:39:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
scm *
|
2016-05-15 22:07:44 +00:00
|
|
|
readenv (scm *a)
|
2016-05-28 14:39:44 +00:00
|
|
|
{
|
2016-05-15 22:07:44 +00:00
|
|
|
return readword (getchar (), 0, a);
|
2016-05-28 14:39:44 +00:00
|
|
|
}
|
2016-05-29 11:44:03 +00:00
|
|
|
|
2016-07-16 11:24:38 +00:00
|
|
|
scm *
|
|
|
|
greater_p (scm *a, scm *b)
|
|
|
|
{
|
|
|
|
assert (a->type == NUMBER);
|
|
|
|
assert (b->type == NUMBER);
|
|
|
|
return a->value > b->value ? &scm_t : &scm_f;
|
|
|
|
}
|
|
|
|
|
2016-05-28 14:39:44 +00:00
|
|
|
scm *
|
|
|
|
less_p (scm *a, scm *b)
|
|
|
|
{
|
|
|
|
assert (a->type == NUMBER);
|
|
|
|
assert (b->type == NUMBER);
|
|
|
|
return a->value < b->value ? &scm_t : &scm_f;
|
|
|
|
}
|
|
|
|
|
|
|
|
scm *
|
2016-07-11 18:58:30 +00:00
|
|
|
minus (scm *x/*...*/)
|
2016-05-28 14:39:44 +00:00
|
|
|
{
|
2016-07-11 18:58:30 +00:00
|
|
|
scm *a = car (x);
|
2016-05-28 14:39:44 +00:00
|
|
|
assert (a->type == NUMBER);
|
2016-07-11 18:58:30 +00:00
|
|
|
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);
|
2016-07-10 10:52:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
scm *
|
2016-07-11 18:58:30 +00:00
|
|
|
plus (scm *x/*...*/)
|
2016-07-10 10:52:53 +00:00
|
|
|
{
|
2016-07-11 18:58:30 +00:00
|
|
|
int n = 0;
|
|
|
|
while (x != &scm_nil)
|
|
|
|
{
|
|
|
|
assert (x->car->type == NUMBER);
|
|
|
|
n += x->car->value;
|
|
|
|
x = cdr (x);
|
|
|
|
}
|
|
|
|
return make_number (n);
|
2016-07-10 10:52:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
scm *
|
2016-07-11 18:58:30 +00:00
|
|
|
divide (scm *x/*...*/)
|
2016-07-10 10:52:53 +00:00
|
|
|
{
|
2016-07-11 18:58:30 +00:00
|
|
|
int n = 1;
|
2016-07-18 20:59:33 +00:00
|
|
|
if (x != &scm_nil) {
|
|
|
|
assert (x->car->type == NUMBER);
|
|
|
|
n = x->car->value;
|
|
|
|
x = cdr (x);
|
|
|
|
}
|
2016-07-11 18:58:30 +00:00
|
|
|
while (x != &scm_nil)
|
|
|
|
{
|
|
|
|
assert (x->car->type == NUMBER);
|
|
|
|
n /= x->car->value;
|
|
|
|
x = cdr (x);
|
|
|
|
}
|
|
|
|
return make_number (n);
|
2016-07-10 10:52:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
scm *
|
2016-07-11 18:58:30 +00:00
|
|
|
multiply (scm *x/*...*/)
|
2016-07-10 10:52:53 +00:00
|
|
|
{
|
2016-07-11 18:58:30 +00:00
|
|
|
int n = 1;
|
|
|
|
while (x != &scm_nil)
|
|
|
|
{
|
|
|
|
assert (x->car->type == NUMBER);
|
|
|
|
n *= x->car->value;
|
|
|
|
x = cdr (x);
|
|
|
|
}
|
|
|
|
return make_number (n);
|
2016-07-10 10:52:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
scm *
|
|
|
|
is_p (scm *a, scm *b)
|
|
|
|
{
|
|
|
|
assert (a->type == NUMBER);
|
|
|
|
assert (b->type == NUMBER);
|
|
|
|
return a->value == b->value ? &scm_t : &scm_f;
|
2016-05-28 14:39:44 +00:00
|
|
|
}
|
|
|
|
|
2016-07-17 20:15:31 +00:00
|
|
|
scm *add_environment (scm *a, char *name, scm *x);
|
|
|
|
|
|
|
|
scm *
|
|
|
|
add_unquoters (scm *a)
|
|
|
|
{
|
|
|
|
a = add_environment (a, "unquote", &scm_unquote);
|
|
|
|
a = add_environment (a, "unquote-splicing", &scm_unquote_splicing);
|
|
|
|
return a;
|
|
|
|
}
|
2016-05-28 14:39:44 +00:00
|
|
|
|
|
|
|
scm *
|
2016-07-09 21:12:25 +00:00
|
|
|
add_environment (scm *a, char *name, scm *x)
|
2016-05-15 22:07:44 +00:00
|
|
|
{
|
2016-07-10 20:43:23 +00:00
|
|
|
return cons (cons (make_symbol (name), x), a);
|
2016-05-15 22:07:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
scm *
|
2016-07-09 21:12:25 +00:00
|
|
|
mes_environment ()
|
2016-05-15 22:07:44 +00:00
|
|
|
{
|
2016-05-28 14:39:44 +00:00
|
|
|
scm *a = &scm_nil;
|
2016-05-15 22:07:44 +00:00
|
|
|
|
2016-07-23 09:51:06 +00:00
|
|
|
a = cons (cons (&scm_f, &scm_f), a);
|
|
|
|
a = cons (cons (&scm_nil, &scm_nil), a);
|
|
|
|
a = cons (cons (&scm_t, &scm_t), a);
|
|
|
|
a = cons (cons (&scm_unspecified, &scm_unspecified), a);
|
2016-07-23 12:39:33 +00:00
|
|
|
a = cons (cons (&symbol_begin, &symbol_begin), a);
|
|
|
|
a = cons (cons (&symbol_quote, &scm_quote), a);
|
2016-07-23 09:51:06 +00:00
|
|
|
|
2016-07-23 22:01:31 +00:00
|
|
|
#if MES_FULL
|
2016-07-09 21:12:25 +00:00
|
|
|
#include "environment.i"
|
2016-07-23 22:01:31 +00:00
|
|
|
#else
|
|
|
|
a = add_environment (a, "display", &scm_display);
|
|
|
|
a = add_environment (a, "newline", &scm_newline);
|
|
|
|
#endif
|
|
|
|
a = cons (cons (&symbol_closure, a), a);
|
2016-05-28 14:39:44 +00:00
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2016-05-15 22:07:44 +00:00
|
|
|
scm *
|
2016-07-16 11:24:38 +00:00
|
|
|
make_lambda (scm *args, scm *body)
|
2016-05-15 22:07:44 +00:00
|
|
|
{
|
2016-07-19 16:18:27 +00:00
|
|
|
return cons (&symbol_lambda, cons (args, body));
|
2016-05-15 22:07:44 +00:00
|
|
|
}
|
|
|
|
|
2016-07-19 19:37:39 +00:00
|
|
|
scm *
|
|
|
|
make_closure (scm *args, scm *body, scm *a)
|
|
|
|
{
|
2016-07-23 22:01:31 +00:00
|
|
|
return cons (&symbol_closure, cons (cons (&symbol_circ, a), cons (args, body)));
|
2016-07-19 19:37:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-15 22:07:44 +00:00
|
|
|
scm *
|
|
|
|
define (scm *x, scm *a)
|
|
|
|
{
|
2016-07-22 15:13:51 +00:00
|
|
|
scm *e;
|
|
|
|
scm *name = cadr (x);
|
|
|
|
if (name->type != PAIR)
|
|
|
|
e = eval (caddr (x), cons (cons (cadr (x), cadr (x)), a));
|
2016-07-17 20:15:31 +00:00
|
|
|
else {
|
2016-07-22 15:13:51 +00:00
|
|
|
name = car (name);
|
2016-07-17 20:15:31 +00:00
|
|
|
scm *p = pairlis (cadr (x), cadr (x), a);
|
2016-07-22 15:13:51 +00:00
|
|
|
e = eval (make_lambda (cdadr (x), cddr (x)), p);
|
2016-07-17 20:15:31 +00:00
|
|
|
}
|
2016-07-22 15:13:51 +00:00
|
|
|
if (eq_p (car (x), &symbol_define_macro) == &scm_t)
|
|
|
|
e = make_macro (e);
|
2016-07-23 22:01:31 +00:00
|
|
|
scm *entry = cons (name, e);
|
|
|
|
scm *aa = cons (entry, &scm_nil);
|
|
|
|
set_cdr_x (aa, cdr (a));
|
|
|
|
set_cdr_x (a, aa);
|
|
|
|
scm *cl = assq (&symbol_closure, a);
|
|
|
|
set_cdr_x (cl, aa);
|
|
|
|
return entry;
|
2016-07-22 15:13:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
scm *
|
|
|
|
lookup_macro (scm *x, scm *a)
|
|
|
|
{
|
|
|
|
scm *m = assq (x, a);
|
|
|
|
if (m != &scm_f && macro_p (cdr (m)) != &scm_f)
|
|
|
|
return cdr (m)->macro;
|
|
|
|
return &scm_f;
|
2016-07-08 16:02:06 +00:00
|
|
|
}
|
|
|
|
|
2016-07-16 20:43:13 +00:00
|
|
|
scm *
|
|
|
|
read_file (scm *e, scm *a)
|
2016-05-28 14:39:44 +00:00
|
|
|
{
|
2016-07-16 20:43:13 +00:00
|
|
|
if (e == &scm_nil) return e;
|
|
|
|
return cons (e, read_file (readenv (a), a));
|
2016-05-29 11:44:03 +00:00
|
|
|
}
|
|
|
|
|
2016-07-16 20:43:13 +00:00
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
{
|
|
|
|
scm *a = mes_environment ();
|
2016-07-19 16:18:27 +00:00
|
|
|
display (eval (cons (&symbol_begin, read_file (readenv (a), a)), a));
|
2016-07-16 20:43:13 +00:00
|
|
|
newline ();
|
|
|
|
return 0;
|
|
|
|
}
|