2016-10-17 16:26:07 +00:00
|
|
|
/* -*-comment-start: "//";comment-end:""-*-
|
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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <assert.h>
|
|
|
|
#include <ctype.h>
|
2016-07-24 13:26:49 +00:00
|
|
|
#include <limits.h>
|
2016-05-28 14:39:44 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
2016-05-15 22:07:44 +00:00
|
|
|
#define DEBUG 0
|
2016-10-22 17:26:12 +00:00
|
|
|
#define QUASIQUOTE 1
|
2016-10-30 15:01:34 +00:00
|
|
|
//#define QUASISYNTAX 0
|
2016-05-15 22:07:44 +00:00
|
|
|
|
Introduce reference type, use vectors of SCM.
* mes.c (type): Add REF.
(scm_t): Add ref, change vector to *scm_t. Update users.
(alloc): New function.
(cons, make_char, make_macro, make_number, make_string,
internal_make_symbol, make_vector): Use it.
(make_ref): New function.
(vector_entry): New function.
(make_vector, list_to_vector, vector_set_x): Use it.
(vector_ref): Dereference REF entry.
(display_helper): Handle REF.
* lib.c (vector_to_list): Handle REF.
* type.c (ref_p): New function.
* tests/vector.test (vector list): New test.
Bugfix vector-ref.
* mes.c (vector-ref): Make copies of simple values. Fixes lalr.
* tests/vector.test (vector-set! 3): New test.
2016-10-24 22:21:28 +00:00
|
|
|
enum type {CHAR, MACRO, NUMBER, PAIR, SCM, STRING, SYMBOL, REF, 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 {
|
2016-10-08 08:14:17 +00:00
|
|
|
char const *name;
|
Implement strings and symbols as list of characters [WAS: c-string].
* mes.c (scm_t): Add string field.
(make_string, internal_lookup_symbol, internal_make_symbol,
make_symbol, lookup, readword): Take scm*. Update callers.
(display_helper): Support string field.
(append_char): New function.
(readstring): Use it. Produce scm*.
(cstring_to_list): New function.
(add_environment, internal_make_symbol): Use it.
(list_of_char_equal_p): New function.
(internal_lookup_symbol): Use it.
* lib.c (list_ref): New function.
* string.c (string_ref): Use it.
(string, string_append, string_length, substring, number_to_string,
string_to_symbol, symbol_to_string): Update to list-of-characters
implementation.
2016-10-25 14:50:19 +00:00
|
|
|
struct scm_t* string;
|
2016-05-28 14:39:44 +00:00
|
|
|
struct scm_t* car;
|
Introduce reference type, use vectors of SCM.
* mes.c (type): Add REF.
(scm_t): Add ref, change vector to *scm_t. Update users.
(alloc): New function.
(cons, make_char, make_macro, make_number, make_string,
internal_make_symbol, make_vector): Use it.
(make_ref): New function.
(vector_entry): New function.
(make_vector, list_to_vector, vector_set_x): Use it.
(vector_ref): Dereference REF entry.
(display_helper): Handle REF.
* lib.c (vector_to_list): Handle REF.
* type.c (ref_p): New function.
* tests/vector.test (vector list): New test.
Bugfix vector-ref.
* mes.c (vector-ref): Make copies of simple values. Fixes lalr.
* tests/vector.test (vector-set! 3): New test.
2016-10-24 22:21:28 +00:00
|
|
|
struct scm_t* ref;
|
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-10-20 17:19:32 +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;
|
Introduce reference type, use vectors of SCM.
* mes.c (type): Add REF.
(scm_t): Add ref, change vector to *scm_t. Update users.
(alloc): New function.
(cons, make_char, make_macro, make_number, make_string,
internal_make_symbol, make_vector): Use it.
(make_ref): New function.
(vector_entry): New function.
(make_vector, list_to_vector, vector_set_x): Use it.
(vector_ref): Dereference REF entry.
(display_helper): Handle REF.
* lib.c (vector_to_list): Handle REF.
* type.c (ref_p): New function.
* tests/vector.test (vector list): New test.
Bugfix vector-ref.
* mes.c (vector-ref): Make copies of simple values. Fixes lalr.
* tests/vector.test (vector-set! 3): New test.
2016-10-24 22:21:28 +00:00
|
|
|
struct scm_t* vector;
|
2016-10-26 17:44:36 +00:00
|
|
|
int hits;
|
2016-05-28 14:39:44 +00:00
|
|
|
};
|
|
|
|
} scm;
|
|
|
|
|
2016-10-22 17:36:54 +00:00
|
|
|
#include "define.environment.h"
|
2016-10-22 18:51:32 +00:00
|
|
|
#include "lib.environment.h"
|
2016-10-22 18:07:12 +00:00
|
|
|
#include "math.environment.h"
|
2016-10-21 20:44:50 +00:00
|
|
|
#include "mes.environment.h"
|
2016-10-22 18:51:32 +00:00
|
|
|
#include "quasiquote.environment.h"
|
|
|
|
#include "string.environment.h"
|
|
|
|
#include "type.environment.h"
|
2016-07-09 21:12:25 +00:00
|
|
|
|
2016-10-21 20:44:50 +00:00
|
|
|
scm *display_ (FILE* f, scm *x);
|
2016-10-08 08:14:17 +00:00
|
|
|
scm *display_helper (FILE*, scm*, bool, char const*, bool);
|
2016-07-09 21:12:25 +00:00
|
|
|
|
2016-10-20 17:19:32 +00:00
|
|
|
scm scm_nil = {SCM, "()"};
|
|
|
|
scm scm_dot = {SCM, "."};
|
|
|
|
scm scm_f = {SCM, "#f"};
|
|
|
|
scm scm_t = {SCM, "#t"};
|
2016-10-19 22:11:48 +00:00
|
|
|
scm scm_undefined = {SCM, "*undefined*"};
|
2016-10-20 17:19:32 +00:00
|
|
|
scm scm_unspecified = {SCM, "*unspecified*"};
|
|
|
|
scm scm_closure = {SCM, "*closure*"};
|
|
|
|
scm scm_circular = {SCM, "*circular*"};
|
2016-10-20 16:43:33 +00:00
|
|
|
#if BOOT
|
|
|
|
scm scm_label = {
|
|
|
|
SCM, "label"};
|
|
|
|
#endif
|
2016-11-02 09:26:04 +00:00
|
|
|
scm scm_begin = {SCM, "begin"};
|
2016-10-20 17:19:32 +00:00
|
|
|
|
2016-11-02 09:26:04 +00:00
|
|
|
scm symbol_lambda = {SYMBOL, "lambda"};
|
|
|
|
scm symbol_begin = {SYMBOL, "begin"};
|
|
|
|
scm symbol_if = {SYMBOL, "if"};
|
|
|
|
scm symbol_define = {SYMBOL, "define"};
|
2016-10-20 17:19:32 +00:00
|
|
|
scm symbol_define_macro = {SCM, "define-macro"};
|
2016-11-02 09:26:04 +00:00
|
|
|
scm symbol_set_x = {SYMBOL, "set!"};
|
2016-10-20 17:19:32 +00:00
|
|
|
|
2016-07-19 16:18:27 +00:00
|
|
|
scm symbol_quote = {SYMBOL, "quote"};
|
|
|
|
scm symbol_quasiquote = {SYMBOL, "quasiquote"};
|
|
|
|
scm symbol_unquote = {SYMBOL, "unquote"};
|
|
|
|
scm symbol_unquote_splicing = {SYMBOL, "unquote-splicing"};
|
|
|
|
|
2016-07-23 22:40:37 +00:00
|
|
|
scm symbol_sc_expand = {SYMBOL, "sc-expand"};
|
2016-11-02 09:36:09 +00:00
|
|
|
scm symbol_expand_macro = {SYMBOL, "expand-macro"};
|
2016-10-30 15:16:20 +00:00
|
|
|
scm symbol_sc_expander_alist = {SYMBOL, "*sc-expander-alist*"};
|
|
|
|
scm symbol_noexpand = {SYMBOL, "noexpand"};
|
2016-07-23 22:40:37 +00:00
|
|
|
scm symbol_syntax = {SYMBOL, "syntax"};
|
|
|
|
scm symbol_quasisyntax = {SYMBOL, "quasisyntax"};
|
|
|
|
scm symbol_unsyntax = {SYMBOL, "unsyntax"};
|
|
|
|
scm symbol_unsyntax_splicing = {SYMBOL, "unsyntax-splicing"};
|
|
|
|
|
2016-07-19 16:18:27 +00:00
|
|
|
scm symbol_call_with_values = {SYMBOL, "call-with-values"};
|
|
|
|
scm symbol_current_module = {SYMBOL, "current-module"};
|
2016-10-20 17:19:32 +00:00
|
|
|
|
2016-05-28 14:39:44 +00:00
|
|
|
|
2016-07-24 21:41:16 +00:00
|
|
|
scm char_nul = {CHAR, .name="nul", .value=0};
|
|
|
|
scm char_backspace = {CHAR, .name="backspace", .value=8};
|
|
|
|
scm char_tab = {CHAR, .name="tab", .value=9};
|
|
|
|
scm char_newline = {CHAR, .name="newline", .value=10};
|
|
|
|
scm char_vt = {CHAR, .name="vt", .value=11};
|
|
|
|
scm char_page = {CHAR, .name="page", .value=12};
|
|
|
|
scm char_return = {CHAR, .name="return", .value=13};
|
|
|
|
scm char_space = {CHAR, .name="space", .value=32};
|
|
|
|
|
2016-05-28 14:39:44 +00:00
|
|
|
// PRIMITIVES
|
2016-05-15 22:07:44 +00:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
Introduce reference type, use vectors of SCM.
* mes.c (type): Add REF.
(scm_t): Add ref, change vector to *scm_t. Update users.
(alloc): New function.
(cons, make_char, make_macro, make_number, make_string,
internal_make_symbol, make_vector): Use it.
(make_ref): New function.
(vector_entry): New function.
(make_vector, list_to_vector, vector_set_x): Use it.
(vector_ref): Dereference REF entry.
(display_helper): Handle REF.
* lib.c (vector_to_list): Handle REF.
* type.c (ref_p): New function.
* tests/vector.test (vector list): New test.
Bugfix vector-ref.
* mes.c (vector-ref): Make copies of simple values. Fixes lalr.
* tests/vector.test (vector-set! 3): New test.
2016-10-24 22:21:28 +00:00
|
|
|
scm *
|
|
|
|
alloc (int n)
|
|
|
|
{
|
|
|
|
return (scm*)malloc (n * sizeof (scm));
|
|
|
|
}
|
|
|
|
|
2016-10-26 17:44:36 +00:00
|
|
|
scm *
|
|
|
|
make_cell (scm *type, scm *car, scm *cdr)
|
|
|
|
{
|
|
|
|
scm *x = alloc (1);
|
|
|
|
assert (type->type == NUMBER);
|
|
|
|
x->type = type->value;
|
|
|
|
if (type->value == CHAR || type->value == NUMBER) {
|
|
|
|
if (car) x->car = car->car;
|
|
|
|
if (cdr) x->cdr = cdr->cdr;
|
|
|
|
} else {
|
|
|
|
x->car = car;
|
|
|
|
x->cdr = cdr;
|
|
|
|
}
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2016-05-28 14:39:44 +00:00
|
|
|
scm *
|
|
|
|
cons (scm *x, scm *y)
|
|
|
|
{
|
2016-10-26 17:44:36 +00:00
|
|
|
scm t = {NUMBER, .value=PAIR};
|
|
|
|
return make_cell (&t, x, y);
|
2016-05-28 14:39:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
scm *
|
|
|
|
eq_p (scm *x, scm *y)
|
|
|
|
{
|
Move optional type predicates to type.c.
* mes.c (char_p, macro_p, number_p, pair_p, string_p, symbol_p,
vector_p, builtin_p, boolean_p): Move to type.c
* type.c: New file.
* GNUmakefile (mes.o): Depend on type snarf output.
* module/mes/loop-0.mes (cond, map, let, or, and not, evlis-env,
apply-env, eval-expand, uquote, add-unquoters, eval,
expand-macro-env, eval-begin-env, eval-if-env, sexp:define,
env:define, env:macro): Move to mes-0.mes.
* module/mes/mes-0.mes: New file.
* module/mes/type-0.mes: New file.
* scripts/include.mes: If BOOT, also include mes-0.mes. If TYPE0,
also include type-0.mes.
2016-10-22 10:16:19 +00:00
|
|
|
return (x == y
|
|
|
|
|| (x->type == CHAR && y->type == CHAR
|
|
|
|
&& x->value == y->value)
|
|
|
|
|| (x->type == NUMBER && y->type == NUMBER
|
|
|
|
&& x->value == y->value))
|
|
|
|
? &scm_t : &scm_f;
|
2016-05-28 14:39:44 +00:00
|
|
|
}
|
|
|
|
|
2016-07-24 22:06:18 +00:00
|
|
|
scm *
|
|
|
|
set_car_x (scm *x, scm *e)
|
|
|
|
{
|
|
|
|
assert (x->type == PAIR);
|
|
|
|
x->car = e;
|
|
|
|
return &scm_unspecified;
|
|
|
|
}
|
|
|
|
|
2016-07-10 08:43:26 +00:00
|
|
|
scm *
|
|
|
|
set_cdr_x (scm *x, scm *e)
|
|
|
|
{
|
|
|
|
assert (x->type == PAIR);
|
2016-10-19 22:11:48 +00:00
|
|
|
cache_invalidate (x->cdr);
|
2016-07-10 08:43:26 +00:00
|
|
|
x->cdr = e;
|
2016-07-10 11:47:56 +00:00
|
|
|
return &scm_unspecified;
|
|
|
|
}
|
|
|
|
|
|
|
|
scm *
|
2016-10-20 16:43:33 +00:00
|
|
|
set_env_x (scm *x, scm *e, scm *a)
|
2016-07-10 11:47:56 +00:00
|
|
|
{
|
2016-10-19 22:11:48 +00:00
|
|
|
cache_invalidate (x);
|
2016-10-30 14:38:27 +00:00
|
|
|
scm *p = assq (x, a);
|
|
|
|
if (p->type != PAIR)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "set!: unbound variable:");
|
|
|
|
display_ (stderr, x);
|
|
|
|
fprintf (stderr, "\n");
|
|
|
|
assert (!"unbound variable");
|
|
|
|
}
|
|
|
|
return set_cdr_x (p, 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-10-21 20:44:50 +00:00
|
|
|
scm *
|
|
|
|
quasisyntax (scm *x)
|
|
|
|
{
|
|
|
|
return cons (&symbol_quasisyntax, x);
|
|
|
|
}
|
|
|
|
|
2016-05-28 14:39:44 +00:00
|
|
|
scm *
|
|
|
|
pairlis (scm *x, scm *y, scm *a)
|
|
|
|
{
|
|
|
|
if (x == &scm_nil)
|
|
|
|
return a;
|
Move optional type predicates to type.c.
* mes.c (char_p, macro_p, number_p, pair_p, string_p, symbol_p,
vector_p, builtin_p, boolean_p): Move to type.c
* type.c: New file.
* GNUmakefile (mes.o): Depend on type snarf output.
* module/mes/loop-0.mes (cond, map, let, or, and not, evlis-env,
apply-env, eval-expand, uquote, add-unquoters, eval,
expand-macro-env, eval-begin-env, eval-if-env, sexp:define,
env:define, env:macro): Move to mes-0.mes.
* module/mes/mes-0.mes: New file.
* module/mes/type-0.mes: New file.
* scripts/include.mes: If BOOT, also include mes-0.mes. If TYPE0,
also include type-0.mes.
2016-10-22 10:16:19 +00:00
|
|
|
if (pair_p (x) == &scm_f)
|
2016-07-08 16:02:06 +00:00
|
|
|
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
|
|
|
{
|
Move optional type predicates to type.c.
* mes.c (char_p, macro_p, number_p, pair_p, string_p, symbol_p,
vector_p, builtin_p, boolean_p): Move to type.c
* type.c: New file.
* GNUmakefile (mes.o): Depend on type snarf output.
* module/mes/loop-0.mes (cond, map, let, or, and not, evlis-env,
apply-env, eval-expand, uquote, add-unquoters, eval,
expand-macro-env, eval-begin-env, eval-if-env, sexp:define,
env:define, env:macro): Move to mes-0.mes.
* module/mes/mes-0.mes: New file.
* module/mes/type-0.mes: New file.
* scripts/include.mes: If BOOT, also include mes-0.mes. If TYPE0,
also include type-0.mes.
2016-10-22 10:16:19 +00:00
|
|
|
while (a != &scm_nil && eq_p (x, a->car->car) == &scm_f) a = a->cdr;
|
2016-10-19 22:11:48 +00:00
|
|
|
return a != &scm_nil ? a->car : &scm_f;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define ENV_CACHE 1
|
2016-10-22 16:13:37 +00:00
|
|
|
#define CACHE_SIZE 30
|
2016-10-21 08:43:32 +00:00
|
|
|
#define ENV_HEAD 15
|
|
|
|
|
2016-10-19 22:11:48 +00:00
|
|
|
#if !ENV_CACHE
|
|
|
|
scm *
|
2016-10-21 20:44:50 +00:00
|
|
|
assq_ref_cache (scm *x, scm *a)
|
2016-10-19 22:11:48 +00:00
|
|
|
{
|
|
|
|
x = assq (x, a);
|
2016-10-30 14:39:36 +00:00
|
|
|
if (x == &scm_f) return &scm_undefined;
|
2016-10-19 22:11:48 +00:00
|
|
|
return x->cdr;
|
|
|
|
}
|
|
|
|
scm*cache_invalidate (scm*x){}
|
|
|
|
scm*cache_invalidate_range (scm*p,scm*a){}
|
|
|
|
scm*cache_save (scm*p){}
|
|
|
|
scm*cache_lookup (scm*x){}
|
|
|
|
|
|
|
|
#else // ENV_CACHE
|
|
|
|
|
|
|
|
scm *env_cache_cars[CACHE_SIZE];
|
|
|
|
scm *env_cache_cdrs[CACHE_SIZE];
|
|
|
|
int cache_threshold = 0;
|
|
|
|
scm *
|
|
|
|
cache_save (scm *p)
|
|
|
|
{
|
2016-10-26 17:44:36 +00:00
|
|
|
int n = p->car->hits;
|
2016-10-19 22:11:48 +00:00
|
|
|
if (n < cache_threshold) return &scm_unspecified;
|
|
|
|
int j = -1;
|
|
|
|
for (int i=0; i < CACHE_SIZE; i++) {
|
|
|
|
if (!env_cache_cars[i]) {
|
|
|
|
j = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (env_cache_cars[i] == p->car) return &scm_unspecified;
|
2016-10-26 17:44:36 +00:00
|
|
|
if (n > env_cache_cars[i]->hits) {
|
|
|
|
n = env_cache_cars[i]->hits;
|
2016-10-19 22:11:48 +00:00
|
|
|
j = i;
|
|
|
|
}
|
2016-10-12 19:14:06 +00:00
|
|
|
}
|
2016-10-19 22:11:48 +00:00
|
|
|
if (j >= 0) {
|
2016-10-26 17:44:36 +00:00
|
|
|
cache_threshold = p->car->hits;
|
2016-10-19 22:11:48 +00:00
|
|
|
env_cache_cars[j] = p->car;
|
|
|
|
env_cache_cdrs[j] = p->cdr;
|
|
|
|
}
|
|
|
|
return &scm_unspecified;
|
|
|
|
}
|
|
|
|
|
|
|
|
scm *
|
|
|
|
cache_lookup (scm *x)
|
|
|
|
{
|
|
|
|
for (int i=0; i < CACHE_SIZE; i++) {
|
|
|
|
if (!env_cache_cars[i]) break;
|
|
|
|
if (env_cache_cars[i] == x) return env_cache_cdrs[i];
|
2016-05-15 22:07:44 +00:00
|
|
|
}
|
2016-10-19 22:11:48 +00:00
|
|
|
return &scm_undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
scm *
|
|
|
|
cache_invalidate (scm *x)
|
|
|
|
{
|
|
|
|
for (int i=0; i < CACHE_SIZE; i++) {
|
|
|
|
if (env_cache_cars[i] == x) {
|
|
|
|
env_cache_cars[i] = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &scm_unspecified;
|
2016-05-28 14:39:44 +00:00
|
|
|
}
|
|
|
|
|
2016-10-19 22:11:48 +00:00
|
|
|
scm *
|
|
|
|
cache_invalidate_range (scm *p, scm *a)
|
|
|
|
{
|
|
|
|
do {
|
|
|
|
cache_invalidate (p->car->car);
|
|
|
|
p = p->cdr;
|
|
|
|
} while (p != a);
|
|
|
|
return &scm_unspecified;
|
|
|
|
}
|
|
|
|
|
|
|
|
scm *
|
|
|
|
assq_ref_cache (scm *x, scm *a)
|
|
|
|
{
|
2016-10-26 17:44:36 +00:00
|
|
|
x->hits++;
|
2016-10-19 22:11:48 +00:00
|
|
|
scm *c = cache_lookup (x);
|
|
|
|
if (c != &scm_undefined) return c;
|
2016-10-19 22:21:06 +00:00
|
|
|
int i = 0;
|
|
|
|
while (a != &scm_nil && x != a->car->car) {i++;a = a->cdr;}
|
2016-10-19 22:11:48 +00:00
|
|
|
if (a == &scm_nil) return &scm_undefined;
|
2016-10-22 16:13:37 +00:00
|
|
|
if (i>ENV_HEAD) cache_save (a->car);
|
2016-10-19 22:11:48 +00:00
|
|
|
return a->car->cdr;
|
|
|
|
}
|
|
|
|
#endif // ENV_CACHE
|
|
|
|
|
2016-10-12 19:14:06 +00:00
|
|
|
scm *
|
2016-10-20 16:43:33 +00:00
|
|
|
evlis_env (scm *m, scm *a)
|
2016-10-12 19:14:06 +00:00
|
|
|
{
|
|
|
|
if (m == &scm_nil) return &scm_nil;
|
2016-10-20 17:19:32 +00:00
|
|
|
if (m->type != PAIR) return builtin_eval (m, a);
|
|
|
|
scm *e = builtin_eval (car (m), a);
|
2016-10-20 16:43:33 +00:00
|
|
|
return cons (e, evlis_env (cdr (m), a));
|
2016-10-12 19:14:06 +00:00
|
|
|
}
|
|
|
|
|
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-10-08 15:00:32 +00:00
|
|
|
if (fn->type != PAIR)
|
2016-05-28 14:39:44 +00:00
|
|
|
{
|
2016-10-08 15:00:32 +00:00
|
|
|
if (fn == &scm_car) return x->car->car;
|
|
|
|
if (fn == &scm_cdr) return x->car->cdr;
|
2016-10-20 17:19:32 +00:00
|
|
|
if (builtin_p (fn) == &scm_t)
|
2016-10-08 15:00:32 +00:00
|
|
|
return call (fn, x);
|
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-10-08 15:00:32 +00:00
|
|
|
if (fn == &symbol_current_module) return a;
|
2016-05-28 14:39:44 +00:00
|
|
|
}
|
2016-11-02 09:26:04 +00:00
|
|
|
else if (fn->car == &symbol_lambda) {
|
2016-07-23 22:01:31 +00:00
|
|
|
scm *p = pairlis (cadr (fn), x, a);
|
2016-10-19 22:11:48 +00:00
|
|
|
cache_invalidate_range (p, a->cdr);
|
2016-11-02 09:26:04 +00:00
|
|
|
scm *r = begin_env (cddr (fn), cons (cons (&scm_closure, p), p));
|
2016-10-19 22:11:48 +00:00
|
|
|
cache_invalidate_range (p, a->cdr);
|
|
|
|
return r;
|
2016-07-23 22:01:31 +00:00
|
|
|
}
|
2016-10-20 17:19:32 +00:00
|
|
|
else if (fn->car == &scm_closure) {
|
2016-07-19 19:37:39 +00:00
|
|
|
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);
|
2016-10-19 22:11:48 +00:00
|
|
|
cache_invalidate_range (p, a->cdr);
|
2016-11-02 09:26:04 +00:00
|
|
|
scm *r = begin_env (body, cons (cons (&scm_closure, p), p));
|
2016-10-19 22:11:48 +00:00
|
|
|
cache_invalidate_range (p, a->cdr);
|
|
|
|
return r;
|
2016-07-19 19:37:39 +00:00
|
|
|
}
|
2016-10-20 16:43:33 +00:00
|
|
|
#if BOOT
|
|
|
|
else if (fn->car == &scm_label)
|
|
|
|
return apply_env (caddr (fn), x, cons (cons (cadr (fn), caddr (fn)), a));
|
|
|
|
#endif
|
2016-10-20 17:19:32 +00:00
|
|
|
scm *efn = builtin_eval (fn, a);
|
2016-10-18 07:21:49 +00:00
|
|
|
if (efn == &scm_f || efn == &scm_t) assert (!"apply bool");
|
|
|
|
if (efn->type == NUMBER) assert (!"apply number");
|
2016-10-20 17:19:32 +00:00
|
|
|
if (efn->type == STRING) assert (!"apply string");
|
2016-10-30 14:42:50 +00:00
|
|
|
if (efn == &scm_unspecified) assert (!"apply *unspecified*");
|
2016-07-22 07:00:32 +00:00
|
|
|
return apply_env (efn, x, a);
|
2016-05-28 14:39:44 +00:00
|
|
|
}
|
|
|
|
|
2016-07-24 11:27:05 +00:00
|
|
|
scm *
|
2016-10-20 17:19:32 +00:00
|
|
|
builtin_eval (scm *e, scm *a)
|
2016-07-24 11:27:05 +00:00
|
|
|
{
|
2016-10-20 17:19:32 +00:00
|
|
|
if (builtin_p (e) == &scm_t) return e;
|
Move optional type predicates to type.c.
* mes.c (char_p, macro_p, number_p, pair_p, string_p, symbol_p,
vector_p, builtin_p, boolean_p): Move to type.c
* type.c: New file.
* GNUmakefile (mes.o): Depend on type snarf output.
* module/mes/loop-0.mes (cond, map, let, or, and not, evlis-env,
apply-env, eval-expand, uquote, add-unquoters, eval,
expand-macro-env, eval-begin-env, eval-if-env, sexp:define,
env:define, env:macro): Move to mes-0.mes.
* module/mes/mes-0.mes: New file.
* module/mes/type-0.mes: New file.
* scripts/include.mes: If BOOT, also include mes-0.mes. If TYPE0,
also include type-0.mes.
2016-10-22 10:16:19 +00:00
|
|
|
if (e->type == SCM) return e;
|
2016-10-16 07:44:52 +00:00
|
|
|
|
|
|
|
e = expand_macro_env (e, a);
|
|
|
|
|
2016-07-19 16:18:27 +00:00
|
|
|
if (e->type == SYMBOL) {
|
2016-10-19 22:11:48 +00:00
|
|
|
scm *y = assq_ref_cache (e, a);
|
|
|
|
if (y == &scm_undefined) {
|
Implement strings and symbols as list of characters [WAS: c-string].
* mes.c (scm_t): Add string field.
(make_string, internal_lookup_symbol, internal_make_symbol,
make_symbol, lookup, readword): Take scm*. Update callers.
(display_helper): Support string field.
(append_char): New function.
(readstring): Use it. Produce scm*.
(cstring_to_list): New function.
(add_environment, internal_make_symbol): Use it.
(list_of_char_equal_p): New function.
(internal_lookup_symbol): Use it.
* lib.c (list_ref): New function.
* string.c (string_ref): Use it.
(string, string_append, string_length, substring, number_to_string,
string_to_symbol, symbol_to_string): Update to list-of-characters
implementation.
2016-10-25 14:50:19 +00:00
|
|
|
fprintf (stderr, "eval: unbound variable:");
|
|
|
|
display_ (stderr, e);
|
|
|
|
fprintf (stderr, "\n");
|
2016-07-24 11:27:05 +00:00
|
|
|
assert (!"unbound variable");
|
2016-05-15 22:07:44 +00:00
|
|
|
}
|
2016-10-19 22:11:48 +00:00
|
|
|
return y;
|
2016-05-15 22:07:44 +00:00
|
|
|
}
|
2016-10-08 15:00:32 +00:00
|
|
|
else if (e->type != PAIR)
|
2016-05-28 14:39:44 +00:00
|
|
|
return e;
|
2016-10-08 15:00:32 +00:00
|
|
|
else if (e->car->type != PAIR)
|
2016-05-28 14:39:44 +00:00
|
|
|
{
|
2016-10-30 15:16:20 +00:00
|
|
|
if (e->car->type == STRING && string_to_symbol (e->car) == &symbol_noexpand)
|
|
|
|
e = cadr (e);
|
|
|
|
else
|
|
|
|
e = sc_expand_env (e, a);
|
2016-10-08 15:00:32 +00:00
|
|
|
if (e->car == &symbol_quote)
|
2016-05-28 14:39:44 +00:00
|
|
|
return cadr (e);
|
2016-10-30 15:01:34 +00:00
|
|
|
#if QUASISYNTAX
|
2016-10-15 23:41:07 +00:00
|
|
|
if (e->car == &symbol_syntax)
|
|
|
|
return e;
|
2016-10-30 15:01:34 +00:00
|
|
|
#endif
|
2016-10-08 15:00:32 +00:00
|
|
|
if (e->car == &symbol_begin)
|
2016-11-02 09:26:04 +00:00
|
|
|
return begin_env (e, a);
|
|
|
|
if (e->car == &symbol_lambda)
|
2016-10-20 17:19:32 +00:00
|
|
|
return make_closure (cadr (e), cddr (e), assq (&scm_closure, a));
|
|
|
|
if (e->car == &scm_closure)
|
2016-07-19 19:37:39 +00:00
|
|
|
return e;
|
2016-10-08 15:00:32 +00:00
|
|
|
if (e->car == &symbol_if)
|
2016-10-20 17:19:32 +00:00
|
|
|
return builtin_if (cdr (e), a);
|
2016-10-20 16:43:33 +00:00
|
|
|
#if !BOOT
|
2016-10-08 15:00:32 +00:00
|
|
|
if (e->car == &symbol_define)
|
2016-11-02 09:26:04 +00:00
|
|
|
return define_env (e, a);
|
2016-10-08 15:00:32 +00:00
|
|
|
if (e->car == &symbol_define_macro)
|
2016-11-02 09:26:04 +00:00
|
|
|
return define_env (e, a);
|
2016-10-20 16:43:33 +00:00
|
|
|
#else
|
|
|
|
if (e->car == &symbol_define) {
|
Implement strings and symbols as list of characters [WAS: c-string].
* mes.c (scm_t): Add string field.
(make_string, internal_lookup_symbol, internal_make_symbol,
make_symbol, lookup, readword): Take scm*. Update callers.
(display_helper): Support string field.
(append_char): New function.
(readstring): Use it. Produce scm*.
(cstring_to_list): New function.
(add_environment, internal_make_symbol): Use it.
(list_of_char_equal_p): New function.
(internal_lookup_symbol): Use it.
* lib.c (list_ref): New function.
* string.c (string_ref): Use it.
(string, string_append, string_length, substring, number_to_string,
string_to_symbol, symbol_to_string): Update to list-of-characters
implementation.
2016-10-25 14:50:19 +00:00
|
|
|
fprintf (stderr, "C DEFINE: ");
|
|
|
|
display_ (stderr,
|
|
|
|
e->cdr->car->type == SYMBOL
|
|
|
|
? e->cdr->car->string
|
|
|
|
: e->cdr->car->car->string);
|
|
|
|
fprintf (stderr, "\n");
|
2016-10-20 16:43:33 +00:00
|
|
|
}
|
|
|
|
assert (e->car != &symbol_define);
|
|
|
|
assert (e->car != &symbol_define_macro);
|
|
|
|
#endif
|
2016-10-08 15:00:32 +00:00
|
|
|
if (e->car == &symbol_set_x)
|
2016-10-20 16:43:33 +00:00
|
|
|
return set_env_x (cadr (e), builtin_eval (caddr (e), a), a);
|
2016-10-22 17:26:12 +00:00
|
|
|
#if QUASIQUOTE
|
2016-10-08 15:00:32 +00:00
|
|
|
if (e->car == &symbol_unquote)
|
2016-10-20 17:19:32 +00:00
|
|
|
return builtin_eval (cadr (e), a);
|
2016-10-08 15:00:32 +00:00
|
|
|
if (e->car == &symbol_quasiquote)
|
2016-07-25 12:39:56 +00:00
|
|
|
return eval_quasiquote (cadr (e), add_unquoters (a));
|
2016-10-30 15:01:34 +00:00
|
|
|
#endif //QUASIQUOTE
|
|
|
|
#if QUASISYNTAX
|
2016-10-15 23:41:07 +00:00
|
|
|
if (e->car == &symbol_unsyntax)
|
2016-10-20 17:19:32 +00:00
|
|
|
return builtin_eval (cadr (e), a);
|
2016-10-15 23:41:07 +00:00
|
|
|
if (e->car == &symbol_quasisyntax)
|
|
|
|
return eval_quasisyntax (cadr (e), add_unsyntaxers (a));
|
2016-10-30 15:01:34 +00:00
|
|
|
#endif //QUASISYNTAX
|
2016-05-28 14:39:44 +00:00
|
|
|
}
|
2016-10-20 16:43:33 +00:00
|
|
|
return apply_env (e->car, evlis_env (e->cdr, a), a);
|
2016-10-08 15:00:32 +00:00
|
|
|
}
|
|
|
|
|
2016-05-28 14:39:44 +00:00
|
|
|
scm *
|
2016-10-16 07:44:52 +00:00
|
|
|
expand_macro_env (scm *e, scm *a)
|
|
|
|
{
|
|
|
|
scm *macro;
|
|
|
|
if (e->type == PAIR
|
|
|
|
&& (macro = lookup_macro (e->car, a)) != &scm_f)
|
|
|
|
return expand_macro_env (apply_env (macro, e->cdr, a), a);
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2016-10-30 15:16:20 +00:00
|
|
|
scm *
|
|
|
|
sc_expand_env (scm *e, scm *a)
|
|
|
|
{
|
|
|
|
scm *expanders;
|
|
|
|
scm *macro;
|
|
|
|
if (e->type == PAIR
|
|
|
|
&& car (e)->type == SYMBOL
|
2016-11-02 09:36:09 +00:00
|
|
|
|
|
|
|
&& car (e) != &symbol_lambda
|
|
|
|
&& car (e) != &symbol_set_x
|
|
|
|
&& car (e) != &symbol_if
|
|
|
|
&& car (e) != &symbol_begin
|
|
|
|
&& car (e) != &symbol_define
|
|
|
|
|
2016-10-30 15:16:20 +00:00
|
|
|
&& car (e) != &symbol_quasiquote
|
|
|
|
&& car (e) != &symbol_quote
|
|
|
|
&& car (e) != &symbol_unquote
|
|
|
|
&& car (e) != &symbol_unquote_splicing
|
|
|
|
&& ((expanders = assq_ref_cache (&symbol_sc_expander_alist, a)) != &scm_undefined)
|
|
|
|
&& ((macro = assq (car (e), expanders)) != &scm_f))
|
|
|
|
{
|
2016-11-02 09:36:09 +00:00
|
|
|
scm *sc_expand = assq_ref_cache (&symbol_expand_macro, a);
|
|
|
|
if (sc_expand != &scm_undefined && sc_expand != &scm_f)
|
|
|
|
{
|
|
|
|
e = apply_env (sc_expand, cons (e, &scm_nil), a);
|
|
|
|
return expand_macro_env (e, a);
|
|
|
|
}
|
2016-10-30 15:16:20 +00:00
|
|
|
}
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2016-10-16 07:44:52 +00:00
|
|
|
scm *
|
2016-11-02 09:26:04 +00:00
|
|
|
begin_env (scm *e, scm *a)
|
2016-05-28 14:39:44 +00:00
|
|
|
{
|
2016-10-12 19:14:06 +00:00
|
|
|
scm *r = &scm_unspecified;
|
|
|
|
while (e != &scm_nil) {
|
2016-10-20 17:19:32 +00:00
|
|
|
r = builtin_eval (e->car, a);
|
2016-10-12 19:14:06 +00:00
|
|
|
e = e->cdr;
|
2016-05-15 22:07:44 +00:00
|
|
|
}
|
2016-10-12 19:14:06 +00:00
|
|
|
return r;
|
2016-05-29 11:44:03 +00:00
|
|
|
}
|
|
|
|
|
2016-07-27 06:49:45 +00:00
|
|
|
scm *
|
2016-10-20 17:19:32 +00:00
|
|
|
builtin_if (scm *e, scm *a)
|
2016-07-27 06:49:45 +00:00
|
|
|
{
|
2016-10-20 17:19:32 +00:00
|
|
|
if (builtin_eval (car (e), a) != &scm_f)
|
|
|
|
return builtin_eval (cadr (e), a);
|
2016-07-27 06:49:45 +00:00
|
|
|
if (cddr (e) != &scm_nil)
|
2016-10-20 17:19:32 +00:00
|
|
|
return builtin_eval (caddr (e), a);
|
2016-07-27 06:49:45 +00:00
|
|
|
return &scm_unspecified;
|
|
|
|
}
|
|
|
|
|
2016-05-29 11:44:03 +00:00
|
|
|
//Helpers
|
2016-05-28 14:39:44 +00:00
|
|
|
|
2016-05-29 11:44:03 +00:00
|
|
|
scm *
|
2016-10-21 20:44:50 +00:00
|
|
|
display (scm *x) ///((args . n))
|
2016-05-29 11:44:03 +00:00
|
|
|
{
|
2016-08-12 12:17:20 +00:00
|
|
|
scm *e = car (x);
|
|
|
|
scm *p = cdr (x);
|
|
|
|
int fd = 1;
|
2016-10-26 17:44:36 +00:00
|
|
|
if (p->type == PAIR && p->car->type == NUMBER) fd = p->car->hits;
|
2016-08-12 12:17:20 +00:00
|
|
|
FILE *f = fd == 1 ? stdout : stderr;
|
|
|
|
return display_helper (f, e, false, "", false);
|
|
|
|
}
|
|
|
|
|
|
|
|
scm *
|
Move optional type predicates to type.c.
* mes.c (char_p, macro_p, number_p, pair_p, string_p, symbol_p,
vector_p, builtin_p, boolean_p): Move to type.c
* type.c: New file.
* GNUmakefile (mes.o): Depend on type snarf output.
* module/mes/loop-0.mes (cond, map, let, or, and not, evlis-env,
apply-env, eval-expand, uquote, add-unquoters, eval,
expand-macro-env, eval-begin-env, eval-if-env, sexp:define,
env:define, env:macro): Move to mes-0.mes.
* module/mes/mes-0.mes: New file.
* module/mes/type-0.mes: New file.
* scripts/include.mes: If BOOT, also include mes-0.mes. If TYPE0,
also include type-0.mes.
2016-10-22 10:16:19 +00:00
|
|
|
display_ (FILE* f, scm *x)
|
2016-08-12 12:17:20 +00:00
|
|
|
{
|
|
|
|
return display_helper (f, x, false, "", false);
|
2016-05-29 11:44:03 +00:00
|
|
|
}
|
2016-05-28 14:39:44 +00:00
|
|
|
|
|
|
|
scm *
|
|
|
|
call (scm *fn, scm *x)
|
|
|
|
{
|
|
|
|
if (fn->type == FUNCTION0)
|
|
|
|
return fn->function0 ();
|
2016-10-29 14:06:41 +00:00
|
|
|
if (x != &scm_nil && x->car->type == VALUES)
|
|
|
|
x = cons (x->car->cdr->car, x->cdr);
|
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));
|
2016-10-29 14:06:41 +00:00
|
|
|
if (x != &scm_nil && x->cdr->car->type == VALUES)
|
|
|
|
x = cons (x->car, cons (x->cdr->car->cdr->car, x->cdr));
|
2016-05-28 14:39:44 +00:00
|
|
|
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 *
|
2016-10-21 20:44:50 +00:00
|
|
|
append (scm *x) ///((args . n))
|
2016-07-11 19:50:59 +00:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2016-10-26 17:44:36 +00:00
|
|
|
scm t = {NUMBER, .value=CHAR};
|
|
|
|
scm n = {NUMBER, .value=x};
|
|
|
|
return make_cell (&t, &n, &n);
|
2016-07-10 22:15:28 +00:00
|
|
|
}
|
|
|
|
|
2016-07-22 15:13:51 +00:00
|
|
|
scm *
|
2016-10-20 16:43:33 +00:00
|
|
|
make_macro (scm *name, scm *x)
|
2016-07-22 15:13:51 +00:00
|
|
|
{
|
2016-10-26 17:44:36 +00:00
|
|
|
scm t = {NUMBER, .value=MACRO};
|
|
|
|
return make_cell (&t, name->string, x);
|
2016-07-22 15:13:51 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2016-10-26 17:44:36 +00:00
|
|
|
scm t = {NUMBER, .value=NUMBER};
|
|
|
|
scm n = {NUMBER, .value=x};
|
|
|
|
return make_cell (&t, &n, &n);
|
2016-07-10 20:43:23 +00:00
|
|
|
}
|
|
|
|
|
Introduce reference type, use vectors of SCM.
* mes.c (type): Add REF.
(scm_t): Add ref, change vector to *scm_t. Update users.
(alloc): New function.
(cons, make_char, make_macro, make_number, make_string,
internal_make_symbol, make_vector): Use it.
(make_ref): New function.
(vector_entry): New function.
(make_vector, list_to_vector, vector_set_x): Use it.
(vector_ref): Dereference REF entry.
(display_helper): Handle REF.
* lib.c (vector_to_list): Handle REF.
* type.c (ref_p): New function.
* tests/vector.test (vector list): New test.
Bugfix vector-ref.
* mes.c (vector-ref): Make copies of simple values. Fixes lalr.
* tests/vector.test (vector-set! 3): New test.
2016-10-24 22:21:28 +00:00
|
|
|
scm *
|
|
|
|
make_ref (scm *x)
|
|
|
|
{
|
2016-10-26 17:44:36 +00:00
|
|
|
scm t = {NUMBER, .value=REF};
|
|
|
|
return make_cell (&t, x, x);
|
Introduce reference type, use vectors of SCM.
* mes.c (type): Add REF.
(scm_t): Add ref, change vector to *scm_t. Update users.
(alloc): New function.
(cons, make_char, make_macro, make_number, make_string,
internal_make_symbol, make_vector): Use it.
(make_ref): New function.
(vector_entry): New function.
(make_vector, list_to_vector, vector_set_x): Use it.
(vector_ref): Dereference REF entry.
(display_helper): Handle REF.
* lib.c (vector_to_list): Handle REF.
* type.c (ref_p): New function.
* tests/vector.test (vector list): New test.
Bugfix vector-ref.
* mes.c (vector-ref): Make copies of simple values. Fixes lalr.
* tests/vector.test (vector-set! 3): New test.
2016-10-24 22:21:28 +00:00
|
|
|
}
|
|
|
|
|
2016-07-10 20:43:23 +00:00
|
|
|
scm *
|
Implement strings and symbols as list of characters [WAS: c-string].
* mes.c (scm_t): Add string field.
(make_string, internal_lookup_symbol, internal_make_symbol,
make_symbol, lookup, readword): Take scm*. Update callers.
(display_helper): Support string field.
(append_char): New function.
(readstring): Use it. Produce scm*.
(cstring_to_list): New function.
(add_environment, internal_make_symbol): Use it.
(list_of_char_equal_p): New function.
(internal_lookup_symbol): Use it.
* lib.c (list_ref): New function.
* string.c (string_ref): Use it.
(string, string_append, string_length, substring, number_to_string,
string_to_symbol, symbol_to_string): Update to list-of-characters
implementation.
2016-10-25 14:50:19 +00:00
|
|
|
make_string (scm *x)
|
2016-07-10 20:43:23 +00:00
|
|
|
{
|
2016-10-26 17:44:36 +00:00
|
|
|
scm t = {NUMBER, .value=STRING};
|
|
|
|
return make_cell (&t, x, 0);
|
Implement strings and symbols as list of characters [WAS: c-string].
* mes.c (scm_t): Add string field.
(make_string, internal_lookup_symbol, internal_make_symbol,
make_symbol, lookup, readword): Take scm*. Update callers.
(display_helper): Support string field.
(append_char): New function.
(readstring): Use it. Produce scm*.
(cstring_to_list): New function.
(add_environment, internal_make_symbol): Use it.
(list_of_char_equal_p): New function.
(internal_lookup_symbol): Use it.
* lib.c (list_ref): New function.
* string.c (string_ref): Use it.
(string, string_append, string_length, substring, number_to_string,
string_to_symbol, symbol_to_string): Update to list-of-characters
implementation.
2016-10-25 14:50:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
scm *
|
|
|
|
cstring_to_list (char const* s)
|
|
|
|
{
|
|
|
|
scm *p = &scm_nil;
|
|
|
|
while (s && *s)
|
|
|
|
p = append2 (p, cons (make_char (*s++), &scm_nil));
|
2016-05-28 14:39:44 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2016-10-08 06:41:30 +00:00
|
|
|
scm *symbols = 0;
|
|
|
|
|
|
|
|
scm *
|
Implement strings and symbols as list of characters [WAS: c-string].
* mes.c (scm_t): Add string field.
(make_string, internal_lookup_symbol, internal_make_symbol,
make_symbol, lookup, readword): Take scm*. Update callers.
(display_helper): Support string field.
(append_char): New function.
(readstring): Use it. Produce scm*.
(cstring_to_list): New function.
(add_environment, internal_make_symbol): Use it.
(list_of_char_equal_p): New function.
(internal_lookup_symbol): Use it.
* lib.c (list_ref): New function.
* string.c (string_ref): Use it.
(string, string_append, string_length, substring, number_to_string,
string_to_symbol, symbol_to_string): Update to list-of-characters
implementation.
2016-10-25 14:50:19 +00:00
|
|
|
list_of_char_equal_p (scm *a, scm *b)
|
|
|
|
{
|
|
|
|
while (a != &scm_nil && b != &scm_nil && a->car->value == b->car->value) {
|
|
|
|
assert (a->car->type == CHAR);
|
|
|
|
assert (b->car->type == CHAR);
|
|
|
|
a = a->cdr;
|
|
|
|
b = b->cdr;
|
|
|
|
}
|
|
|
|
return (a == &scm_nil && b == &scm_nil) ? &scm_t : &scm_f;
|
|
|
|
}
|
|
|
|
|
|
|
|
scm *
|
|
|
|
internal_lookup_symbol (scm *s)
|
2016-10-08 06:41:30 +00:00
|
|
|
{
|
|
|
|
scm *x = symbols;
|
Implement strings and symbols as list of characters [WAS: c-string].
* mes.c (scm_t): Add string field.
(make_string, internal_lookup_symbol, internal_make_symbol,
make_symbol, lookup, readword): Take scm*. Update callers.
(display_helper): Support string field.
(append_char): New function.
(readstring): Use it. Produce scm*.
(cstring_to_list): New function.
(add_environment, internal_make_symbol): Use it.
(list_of_char_equal_p): New function.
(internal_lookup_symbol): Use it.
* lib.c (list_ref): New function.
* string.c (string_ref): Use it.
(string, string_append, string_length, substring, number_to_string,
string_to_symbol, symbol_to_string): Update to list-of-characters
implementation.
2016-10-25 14:50:19 +00:00
|
|
|
while (x) {
|
2016-10-26 17:44:36 +00:00
|
|
|
// .string and .name is the same field; .name is used as a handy
|
|
|
|
// static field initializer. A string can only be mistaken for a
|
|
|
|
// cell with type == PAIR for the one character long, zero-padded
|
|
|
|
// #\etx.
|
Implement strings and symbols as list of characters [WAS: c-string].
* mes.c (scm_t): Add string field.
(make_string, internal_lookup_symbol, internal_make_symbol,
make_symbol, lookup, readword): Take scm*. Update callers.
(display_helper): Support string field.
(append_char): New function.
(readstring): Use it. Produce scm*.
(cstring_to_list): New function.
(add_environment, internal_make_symbol): Use it.
(list_of_char_equal_p): New function.
(internal_lookup_symbol): Use it.
* lib.c (list_ref): New function.
* string.c (string_ref): Use it.
(string, string_append, string_length, substring, number_to_string,
string_to_symbol, symbol_to_string): Update to list-of-characters
implementation.
2016-10-25 14:50:19 +00:00
|
|
|
if (x->car->string->type != PAIR)
|
|
|
|
x->car->string = cstring_to_list (x->car->name);
|
|
|
|
if (list_of_char_equal_p (x->car->string, s) == &scm_t) break;
|
|
|
|
x = x->cdr;
|
|
|
|
}
|
2016-10-08 06:41:30 +00:00
|
|
|
if (x) x = x->car;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
scm *
|
Implement strings and symbols as list of characters [WAS: c-string].
* mes.c (scm_t): Add string field.
(make_string, internal_lookup_symbol, internal_make_symbol,
make_symbol, lookup, readword): Take scm*. Update callers.
(display_helper): Support string field.
(append_char): New function.
(readstring): Use it. Produce scm*.
(cstring_to_list): New function.
(add_environment, internal_make_symbol): Use it.
(list_of_char_equal_p): New function.
(internal_lookup_symbol): Use it.
* lib.c (list_ref): New function.
* string.c (string_ref): Use it.
(string, string_append, string_length, substring, number_to_string,
string_to_symbol, symbol_to_string): Update to list-of-characters
implementation.
2016-10-25 14:50:19 +00:00
|
|
|
internal_make_symbol (scm *s)
|
2016-10-08 06:41:30 +00:00
|
|
|
{
|
2016-10-26 17:44:36 +00:00
|
|
|
scm t = {NUMBER, .value=SYMBOL};
|
|
|
|
scm *x = make_cell (&t, s, 0);
|
2016-10-08 06:41:30 +00:00
|
|
|
symbols = cons (x, symbols);
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2016-05-28 14:39:44 +00:00
|
|
|
scm *
|
Implement strings and symbols as list of characters [WAS: c-string].
* mes.c (scm_t): Add string field.
(make_string, internal_lookup_symbol, internal_make_symbol,
make_symbol, lookup, readword): Take scm*. Update callers.
(display_helper): Support string field.
(append_char): New function.
(readstring): Use it. Produce scm*.
(cstring_to_list): New function.
(add_environment, internal_make_symbol): Use it.
(list_of_char_equal_p): New function.
(internal_lookup_symbol): Use it.
* lib.c (list_ref): New function.
* string.c (string_ref): Use it.
(string, string_append, string_length, substring, number_to_string,
string_to_symbol, symbol_to_string): Update to list-of-characters
implementation.
2016-10-25 14:50:19 +00:00
|
|
|
make_symbol (scm *s)
|
2016-05-28 14:39:44 +00:00
|
|
|
{
|
2016-10-08 06:41:30 +00:00
|
|
|
scm *x = internal_lookup_symbol (s);
|
|
|
|
return x ? x : internal_make_symbol (s);
|
2016-05-28 14:39:44 +00:00
|
|
|
}
|
|
|
|
|
2016-07-11 08:38:02 +00:00
|
|
|
scm *
|
2016-10-21 08:51:09 +00:00
|
|
|
make_vector (scm *n)
|
2016-07-11 08:38:02 +00:00
|
|
|
{
|
2016-10-26 17:44:36 +00:00
|
|
|
scm t = {NUMBER, .value=VECTOR};
|
|
|
|
scm *v = alloc (n->value);
|
|
|
|
scm *x = make_cell (&t, (scm*)(long)n->value, v);
|
|
|
|
for (int i=0; i<n->value; i++) x->vector[i] = *vector_entry (&scm_unspecified);
|
|
|
|
return x;
|
2016-07-11 08:38:02 +00:00
|
|
|
}
|
|
|
|
|
2016-07-11 17:32:11 +00:00
|
|
|
scm *
|
2016-10-21 20:44:50 +00:00
|
|
|
values (scm *x) ///((args . n))
|
2016-07-11 17:32:11 +00:00
|
|
|
{
|
|
|
|
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);
|
Introduce reference type, use vectors of SCM.
* mes.c (type): Add REF.
(scm_t): Add ref, change vector to *scm_t. Update users.
(alloc): New function.
(cons, make_char, make_macro, make_number, make_string,
internal_make_symbol, make_vector): Use it.
(make_ref): New function.
(vector_entry): New function.
(make_vector, list_to_vector, vector_set_x): Use it.
(vector_ref): Dereference REF entry.
(display_helper): Handle REF.
* lib.c (vector_to_list): Handle REF.
* type.c (ref_p): New function.
* tests/vector.test (vector list): New test.
Bugfix vector-ref.
* mes.c (vector-ref): Make copies of simple values. Fixes lalr.
* tests/vector.test (vector-set! 3): New test.
2016-10-24 22:21:28 +00:00
|
|
|
scm *e = &x->vector[i->value];
|
|
|
|
if (e->type == REF) e = e->ref;
|
|
|
|
if (e->type == CHAR) e = make_char (e->value);
|
|
|
|
if (e->type == NUMBER) e = make_number (e->value);
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
scm *
|
|
|
|
vector_entry (scm *x) {
|
|
|
|
if (x->type == PAIR || x->type == SCM || x->type == STRING || x->type == SYMBOL || x->type == VECTOR) x = make_ref (x);
|
|
|
|
return x;
|
2016-07-11 08:38:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
scm *
|
|
|
|
vector_set_x (scm *x, scm *i, scm *e)
|
|
|
|
{
|
|
|
|
assert (x->type == VECTOR);
|
|
|
|
assert (i->value < x->length);
|
Introduce reference type, use vectors of SCM.
* mes.c (type): Add REF.
(scm_t): Add ref, change vector to *scm_t. Update users.
(alloc): New function.
(cons, make_char, make_macro, make_number, make_string,
internal_make_symbol, make_vector): Use it.
(make_ref): New function.
(vector_entry): New function.
(make_vector, list_to_vector, vector_set_x): Use it.
(vector_ref): Dereference REF entry.
(display_helper): Handle REF.
* lib.c (vector_to_list): Handle REF.
* type.c (ref_p): New function.
* tests/vector.test (vector list): New test.
Bugfix vector-ref.
* mes.c (vector-ref): Make copies of simple values. Fixes lalr.
* tests/vector.test (vector-set! 3): New test.
2016-10-24 22:21:28 +00:00
|
|
|
x->vector[i->value] = *vector_entry (e);
|
2016-07-11 08:38:02 +00:00
|
|
|
return &scm_unspecified;
|
|
|
|
}
|
|
|
|
|
2016-05-28 14:39:44 +00:00
|
|
|
scm *
|
Implement strings and symbols as list of characters [WAS: c-string].
* mes.c (scm_t): Add string field.
(make_string, internal_lookup_symbol, internal_make_symbol,
make_symbol, lookup, readword): Take scm*. Update callers.
(display_helper): Support string field.
(append_char): New function.
(readstring): Use it. Produce scm*.
(cstring_to_list): New function.
(add_environment, internal_make_symbol): Use it.
(list_of_char_equal_p): New function.
(internal_lookup_symbol): Use it.
* lib.c (list_ref): New function.
* string.c (string_ref): Use it.
(string, string_append, string_length, substring, number_to_string,
string_to_symbol, symbol_to_string): Update to list-of-characters
implementation.
2016-10-25 14:50:19 +00:00
|
|
|
lookup (scm *s, scm *a)
|
2016-05-28 14:39:44 +00:00
|
|
|
{
|
Implement strings and symbols as list of characters [WAS: c-string].
* mes.c (scm_t): Add string field.
(make_string, internal_lookup_symbol, internal_make_symbol,
make_symbol, lookup, readword): Take scm*. Update callers.
(display_helper): Support string field.
(append_char): New function.
(readstring): Use it. Produce scm*.
(cstring_to_list): New function.
(add_environment, internal_make_symbol): Use it.
(list_of_char_equal_p): New function.
(internal_lookup_symbol): Use it.
* lib.c (list_ref): New function.
* string.c (string_ref): Use it.
(string, string_append, string_length, substring, number_to_string,
string_to_symbol, symbol_to_string): Update to list-of-characters
implementation.
2016-10-25 14:50:19 +00:00
|
|
|
if (isdigit (s->car->value) || (s->car->value == '-' && s->cdr != &scm_nil)) {
|
|
|
|
scm *p = s;
|
|
|
|
int sign = 1;
|
|
|
|
if (s->car->value == '-') {
|
|
|
|
sign = -1;
|
|
|
|
p = s->cdr;
|
|
|
|
}
|
|
|
|
int n = 0;
|
|
|
|
while (p != &scm_nil && isdigit (p->car->value)) {
|
|
|
|
n *= 10;
|
|
|
|
n += p->car->value - '0';
|
|
|
|
p = p->cdr;
|
|
|
|
}
|
|
|
|
if (p == &scm_nil) return make_number (n * sign);
|
|
|
|
}
|
|
|
|
|
|
|
|
scm *x = internal_lookup_symbol (s);
|
2016-10-08 06:41:30 +00:00
|
|
|
if (x) return x;
|
2016-07-23 22:40:37 +00:00
|
|
|
|
Implement strings and symbols as list of characters [WAS: c-string].
* mes.c (scm_t): Add string field.
(make_string, internal_lookup_symbol, internal_make_symbol,
make_symbol, lookup, readword): Take scm*. Update callers.
(display_helper): Support string field.
(append_char): New function.
(readstring): Use it. Produce scm*.
(cstring_to_list): New function.
(add_environment, internal_make_symbol): Use it.
(list_of_char_equal_p): New function.
(internal_lookup_symbol): Use it.
* lib.c (list_ref): New function.
* string.c (string_ref): Use it.
(string, string_append, string_length, substring, number_to_string,
string_to_symbol, symbol_to_string): Update to list-of-characters
implementation.
2016-10-25 14:50:19 +00:00
|
|
|
if (s->cdr == &scm_nil) {
|
|
|
|
if (s->car->value == '\'') return &symbol_quote;
|
|
|
|
if (s->car->value == '`') return &symbol_quasiquote;
|
|
|
|
if (s->car->value == ',') return &symbol_unquote;
|
|
|
|
}
|
|
|
|
else if (s->cdr->cdr == &scm_nil) {
|
|
|
|
if (s->car->value == ',' && s->cdr->car->value == '@') return &symbol_unquote_splicing;
|
|
|
|
if (s->car->value == '#' && s->cdr->car->value == '\'') return &symbol_syntax;
|
|
|
|
if (s->car->value == '#' && s->cdr->car->value == '`') return &symbol_quasisyntax;
|
|
|
|
if (s->car->value == '#' && s->cdr->car->value == ',') return &symbol_unsyntax;
|
|
|
|
}
|
|
|
|
else if (s->cdr->cdr->cdr == &scm_nil) {
|
|
|
|
if (s->car->value == '#' && s->cdr->car->value == ',' && s->cdr->cdr->car->value == '@') return &symbol_unsyntax_splicing;
|
|
|
|
if (s->car->value == 'E' && s->cdr->car->value == 'O' && s->cdr->cdr->car->value == 'F') {
|
|
|
|
fprintf (stderr, "mes: got EOF\n");
|
|
|
|
return &scm_nil; // `EOF': eval program, which may read stdin
|
|
|
|
}
|
2016-07-24 16:28:45 +00:00
|
|
|
}
|
2016-07-27 06:49:45 +00:00
|
|
|
|
2016-10-08 06:41:30 +00:00
|
|
|
return internal_make_symbol (s);
|
2016-05-29 11:44:03 +00:00
|
|
|
}
|
|
|
|
|
2016-07-09 12:29:39 +00:00
|
|
|
scm *
|
|
|
|
lookup_char (int c, scm *a)
|
|
|
|
{
|
Implement strings and symbols as list of characters [WAS: c-string].
* mes.c (scm_t): Add string field.
(make_string, internal_lookup_symbol, internal_make_symbol,
make_symbol, lookup, readword): Take scm*. Update callers.
(display_helper): Support string field.
(append_char): New function.
(readstring): Use it. Produce scm*.
(cstring_to_list): New function.
(add_environment, internal_make_symbol): Use it.
(list_of_char_equal_p): New function.
(internal_lookup_symbol): Use it.
* lib.c (list_ref): New function.
* string.c (string_ref): Use it.
(string, string_append, string_length, substring, number_to_string,
string_to_symbol, symbol_to_string): Update to list-of-characters
implementation.
2016-10-25 14:50:19 +00:00
|
|
|
return lookup (cons (make_char (c), &scm_nil), a);
|
2016-07-09 12:29:39 +00:00
|
|
|
}
|
|
|
|
|
2016-10-21 20:44:50 +00:00
|
|
|
scm *
|
2016-07-11 08:38:02 +00:00
|
|
|
list_to_vector (scm *x)
|
|
|
|
{
|
2016-10-26 17:44:36 +00:00
|
|
|
scm n = {NUMBER, .value=length (x)->value};
|
|
|
|
scm *v = make_vector (&n);
|
Introduce reference type, use vectors of SCM.
* mes.c (type): Add REF.
(scm_t): Add ref, change vector to *scm_t. Update users.
(alloc): New function.
(cons, make_char, make_macro, make_number, make_string,
internal_make_symbol, make_vector): Use it.
(make_ref): New function.
(vector_entry): New function.
(make_vector, list_to_vector, vector_set_x): Use it.
(vector_ref): Dereference REF entry.
(display_helper): Handle REF.
* lib.c (vector_to_list): Handle REF.
* type.c (ref_p): New function.
* tests/vector.test (vector list): New test.
Bugfix vector-ref.
* mes.c (vector-ref): Make copies of simple values. Fixes lalr.
* tests/vector.test (vector-set! 3): New test.
2016-10-24 22:21:28 +00:00
|
|
|
scm *p = v->vector;
|
2016-07-11 08:38:02 +00:00
|
|
|
while (x != &scm_nil)
|
|
|
|
{
|
Introduce reference type, use vectors of SCM.
* mes.c (type): Add REF.
(scm_t): Add ref, change vector to *scm_t. Update users.
(alloc): New function.
(cons, make_char, make_macro, make_number, make_string,
internal_make_symbol, make_vector): Use it.
(make_ref): New function.
(vector_entry): New function.
(make_vector, list_to_vector, vector_set_x): Use it.
(vector_ref): Dereference REF entry.
(display_helper): Handle REF.
* lib.c (vector_to_list): Handle REF.
* type.c (ref_p): New function.
* tests/vector.test (vector list): New test.
Bugfix vector-ref.
* mes.c (vector-ref): Make copies of simple values. Fixes lalr.
* tests/vector.test (vector-set! 3): New test.
2016-10-24 22:21:28 +00:00
|
|
|
*p++ = *vector_entry (car (x));
|
2016-07-11 08:38:02 +00:00
|
|
|
x = cdr (x);
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2016-05-15 22:07:44 +00:00
|
|
|
scm *
|
2016-10-21 20:44:50 +00:00
|
|
|
newline (scm *p) ///((args . n))
|
2016-05-15 22:07:44 +00:00
|
|
|
{
|
2016-08-12 12:17:20 +00:00
|
|
|
int fd = 1;
|
|
|
|
if (p->type == PAIR && p->car->type == NUMBER) fd = p->car->value;
|
|
|
|
FILE *f = fd == 1 ? stdout : stderr;
|
|
|
|
fputs ("\n", f);
|
2016-05-15 22:07:44 +00:00
|
|
|
return &scm_unspecified;
|
2016-05-28 14:39:44 +00:00
|
|
|
}
|
|
|
|
|
2016-10-16 07:44:52 +00:00
|
|
|
scm *
|
2016-10-21 20:44:50 +00:00
|
|
|
force_output (scm *p) ///((args . n))
|
2016-10-16 07:44:52 +00:00
|
|
|
{
|
|
|
|
int fd = 1;
|
|
|
|
if (p->type == PAIR && p->car->type == NUMBER) fd = p->car->value;
|
|
|
|
FILE *f = fd == 1 ? stdout : stderr;
|
|
|
|
fflush (f);
|
|
|
|
}
|
|
|
|
|
2016-05-28 14:39:44 +00:00
|
|
|
scm *
|
2016-10-08 08:14:17 +00:00
|
|
|
display_helper (FILE* f, scm *x, bool cont, char const *sep, bool quote)
|
2016-05-28 14:39:44 +00:00
|
|
|
{
|
|
|
|
scm *r;
|
2016-08-12 12:17:20 +00:00
|
|
|
fprintf (f, "%s", sep);
|
|
|
|
if (x->type == CHAR && x->value == char_nul.value) fprintf (f, "#\\%s", char_nul.name);
|
|
|
|
else if (x->type == CHAR && x->value == char_backspace.value) fprintf (f, "#\\%s", char_backspace.name);
|
|
|
|
else if (x->type == CHAR && x->value == char_tab.value) fprintf (f, "#\\%s", char_tab.name);
|
|
|
|
else if (x->type == CHAR && x->value == char_newline.value) fprintf (f, "#\\%s", char_newline.name);
|
|
|
|
else if (x->type == CHAR && x->value == char_vt.value) fprintf (f, "#\\%s", char_vt.name);
|
|
|
|
else if (x->type == CHAR && x->value == char_page.value) fprintf (f, "#\\%s", char_page.name);
|
|
|
|
else if (x->type == CHAR && x->value == char_return.value) fprintf (f, "#\\%s", char_return.name);
|
|
|
|
else if (x->type == CHAR && x->value == char_space.value) fprintf (f, "#\\%s", char_space.name);
|
|
|
|
else if (x->type == CHAR) fprintf (f, "#\\%c", x->value);
|
2016-07-22 15:13:51 +00:00
|
|
|
else if (x->type == MACRO) {
|
2016-08-12 12:17:20 +00:00
|
|
|
fprintf (f, "(*macro* ");
|
|
|
|
display_helper (f, x->macro, cont, sep, quote);
|
|
|
|
fprintf (f, ")");
|
2016-07-22 15:13:51 +00:00
|
|
|
}
|
2016-08-12 12:17:20 +00:00
|
|
|
else if (x->type == NUMBER) fprintf (f, "%d", x->value);
|
2016-05-28 14:39:44 +00:00
|
|
|
else if (x->type == PAIR) {
|
2016-10-20 17:19:32 +00:00
|
|
|
if (car (x) == &scm_circular) {
|
2016-08-12 12:17:20 +00:00
|
|
|
fprintf (f, "(*circ* . #-1#)");
|
2016-07-22 10:23:23 +00:00
|
|
|
return &scm_unspecified;
|
|
|
|
}
|
2016-10-20 17:19:32 +00:00
|
|
|
if (car (x) == &scm_closure) {
|
2016-08-12 12:17:20 +00:00
|
|
|
fprintf (f, "(*closure* . #-1#)");
|
2016-07-23 22:01:31 +00:00
|
|
|
return &scm_unspecified;
|
|
|
|
}
|
2016-05-28 14:39:44 +00:00
|
|
|
if (car (x) == &scm_quote) {
|
2016-08-12 12:17:20 +00:00
|
|
|
fprintf (f, "'");
|
|
|
|
return display_helper (f, car (cdr (x)), cont, "", true);
|
2016-05-15 22:07:44 +00:00
|
|
|
}
|
2016-08-12 12:17:20 +00:00
|
|
|
if (!cont) fprintf (f, "(");
|
|
|
|
display_ (f, car (x));
|
2016-05-28 14:39:44 +00:00
|
|
|
if (cdr (x)->type == PAIR)
|
2016-08-12 12:17:20 +00:00
|
|
|
display_helper (f, cdr (x), true, " ", false);
|
2016-05-28 14:39:44 +00:00
|
|
|
else if (cdr (x) != &scm_nil) {
|
2016-08-12 12:17:20 +00:00
|
|
|
fprintf (f, " . ");
|
|
|
|
display_ (f, cdr (x));
|
2016-05-28 14:39:44 +00:00
|
|
|
}
|
2016-08-12 12:17:20 +00:00
|
|
|
if (!cont) fprintf (f, ")");
|
2016-05-28 14:39:44 +00:00
|
|
|
}
|
2016-07-11 08:38:02 +00:00
|
|
|
else if (x->type == VECTOR) {
|
2016-08-12 12:17:20 +00:00
|
|
|
fprintf (f, "#(", x->length);
|
2016-07-23 22:14:40 +00:00
|
|
|
for (int i = 0; i < x->length; i++) {
|
Introduce reference type, use vectors of SCM.
* mes.c (type): Add REF.
(scm_t): Add ref, change vector to *scm_t. Update users.
(alloc): New function.
(cons, make_char, make_macro, make_number, make_string,
internal_make_symbol, make_vector): Use it.
(make_ref): New function.
(vector_entry): New function.
(make_vector, list_to_vector, vector_set_x): Use it.
(vector_ref): Dereference REF entry.
(display_helper): Handle REF.
* lib.c (vector_to_list): Handle REF.
* type.c (ref_p): New function.
* tests/vector.test (vector list): New test.
Bugfix vector-ref.
* mes.c (vector-ref): Make copies of simple values. Fixes lalr.
* tests/vector.test (vector-set! 3): New test.
2016-10-24 22:21:28 +00:00
|
|
|
if (x->vector[i].type == VECTOR
|
|
|
|
|| (x->vector[i].type == REF
|
|
|
|
&& x->vector[i].ref->type == VECTOR))
|
2016-08-12 12:17:20 +00:00
|
|
|
fprintf (f, "%s#(...)", i ? " " : "");
|
2016-07-23 22:14:40 +00:00
|
|
|
else
|
Introduce reference type, use vectors of SCM.
* mes.c (type): Add REF.
(scm_t): Add ref, change vector to *scm_t. Update users.
(alloc): New function.
(cons, make_char, make_macro, make_number, make_string,
internal_make_symbol, make_vector): Use it.
(make_ref): New function.
(vector_entry): New function.
(make_vector, list_to_vector, vector_set_x): Use it.
(vector_ref): Dereference REF entry.
(display_helper): Handle REF.
* lib.c (vector_to_list): Handle REF.
* type.c (ref_p): New function.
* tests/vector.test (vector list): New test.
Bugfix vector-ref.
* mes.c (vector-ref): Make copies of simple values. Fixes lalr.
* tests/vector.test (vector-set! 3): New test.
2016-10-24 22:21:28 +00:00
|
|
|
display_helper (f, &x->vector[i], false, i ? " " : "", false);
|
2016-07-23 22:14:40 +00:00
|
|
|
}
|
2016-08-12 12:17:20 +00:00
|
|
|
fprintf (f, ")");
|
2016-07-11 08:38:02 +00:00
|
|
|
}
|
Introduce reference type, use vectors of SCM.
* mes.c (type): Add REF.
(scm_t): Add ref, change vector to *scm_t. Update users.
(alloc): New function.
(cons, make_char, make_macro, make_number, make_string,
internal_make_symbol, make_vector): Use it.
(make_ref): New function.
(vector_entry): New function.
(make_vector, list_to_vector, vector_set_x): Use it.
(vector_ref): Dereference REF entry.
(display_helper): Handle REF.
* lib.c (vector_to_list): Handle REF.
* type.c (ref_p): New function.
* tests/vector.test (vector list): New test.
Bugfix vector-ref.
* mes.c (vector-ref): Make copies of simple values. Fixes lalr.
* tests/vector.test (vector-set! 3): New test.
2016-10-24 22:21:28 +00:00
|
|
|
else if (x->type == REF) display_helper (f, x->ref, cont, "", true);
|
2016-10-18 07:08:42 +00:00
|
|
|
else if (builtin_p (x) == &scm_t) fprintf (f, "#<procedure %s>", x->name);
|
Implement strings and symbols as list of characters [WAS: c-string].
* mes.c (scm_t): Add string field.
(make_string, internal_lookup_symbol, internal_make_symbol,
make_symbol, lookup, readword): Take scm*. Update callers.
(display_helper): Support string field.
(append_char): New function.
(readstring): Use it. Produce scm*.
(cstring_to_list): New function.
(add_environment, internal_make_symbol): Use it.
(list_of_char_equal_p): New function.
(internal_lookup_symbol): Use it.
* lib.c (list_ref): New function.
* string.c (string_ref): Use it.
(string, string_append, string_length, substring, number_to_string,
string_to_symbol, symbol_to_string): Update to list-of-characters
implementation.
2016-10-25 14:50:19 +00:00
|
|
|
else if (x->type != PAIR && x->string) {
|
|
|
|
scm *p = x->string;
|
|
|
|
assert (p);
|
|
|
|
while (p != &scm_nil) {
|
|
|
|
assert (p->car->type == CHAR);
|
|
|
|
fputc (p->car->value, f);
|
|
|
|
p = p->cdr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (x->type != PAIR && x->name) fprintf (f, "%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-10-21 20:44:50 +00:00
|
|
|
ungetchar (int c)
|
2016-05-28 14:39:44 +00:00
|
|
|
{
|
|
|
|
return ungetc (c, stdin);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2016-10-21 20:44:50 +00:00
|
|
|
peekchar ()
|
2016-05-28 14:39:44 +00:00
|
|
|
{
|
|
|
|
int c = getchar ();
|
|
|
|
ungetchar (c);
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2016-10-21 20:44:50 +00:00
|
|
|
scm *
|
2016-10-20 17:19:32 +00:00
|
|
|
peek_char ()
|
2016-05-28 14:39:44 +00:00
|
|
|
{
|
2016-10-20 17:19:32 +00:00
|
|
|
return make_char (peekchar ());
|
2016-05-28 14:39:44 +00:00
|
|
|
}
|
|
|
|
|
2016-07-24 16:28:45 +00:00
|
|
|
scm *
|
|
|
|
read_char ()
|
2016-05-28 14:39:44 +00:00
|
|
|
{
|
2016-07-24 15:35:31 +00:00
|
|
|
return make_char (getchar ());
|
2016-05-28 14:39:44 +00:00
|
|
|
}
|
|
|
|
|
2016-08-12 12:17:20 +00:00
|
|
|
scm *
|
2016-10-21 20:44:50 +00:00
|
|
|
write_char (scm *x) ///((args . n))
|
2016-08-12 12:17:20 +00:00
|
|
|
{
|
|
|
|
scm *c = car (x);
|
|
|
|
scm *p = cdr (x);
|
|
|
|
int fd = 1;
|
|
|
|
if (p->type == PAIR && p->car->type == NUMBER) fd = p->car->value;
|
|
|
|
FILE *f = fd == 1 ? stdout : stderr;
|
|
|
|
assert (c->type == NUMBER || c->type == CHAR);
|
|
|
|
fputc (c->value, f);
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2016-10-21 20:44:50 +00:00
|
|
|
scm *
|
2016-10-20 17:19:32 +00:00
|
|
|
unget_char (scm *c)
|
2016-05-28 14:39:44 +00:00
|
|
|
{
|
2016-08-12 12:17:20 +00:00
|
|
|
assert (c->type == NUMBER || c->type == CHAR);
|
2016-05-28 14:39:44 +00:00
|
|
|
ungetchar (c->value);
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
readcomment (int c)
|
|
|
|
{
|
|
|
|
if (c == '\n') return c;
|
|
|
|
return readcomment (getchar ());
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
readblock (int c)
|
|
|
|
{
|
2016-10-20 17:19:32 +00:00
|
|
|
if (c == '!' && peekchar () == '#') return getchar ();
|
2016-05-28 14:39:44 +00:00
|
|
|
return readblock (getchar ());
|
|
|
|
}
|
|
|
|
|
|
|
|
scm *
|
Implement strings and symbols as list of characters [WAS: c-string].
* mes.c (scm_t): Add string field.
(make_string, internal_lookup_symbol, internal_make_symbol,
make_symbol, lookup, readword): Take scm*. Update callers.
(display_helper): Support string field.
(append_char): New function.
(readstring): Use it. Produce scm*.
(cstring_to_list): New function.
(add_environment, internal_make_symbol): Use it.
(list_of_char_equal_p): New function.
(internal_lookup_symbol): Use it.
* lib.c (list_ref): New function.
* string.c (string_ref): Use it.
(string, string_append, string_length, substring, number_to_string,
string_to_symbol, symbol_to_string): Update to list-of-characters
implementation.
2016-10-25 14:50:19 +00:00
|
|
|
readword (int c, scm *w, scm *a)
|
2016-05-28 14:39:44 +00:00
|
|
|
{
|
Implement strings and symbols as list of characters [WAS: c-string].
* mes.c (scm_t): Add string field.
(make_string, internal_lookup_symbol, internal_make_symbol,
make_symbol, lookup, readword): Take scm*. Update callers.
(display_helper): Support string field.
(append_char): New function.
(readstring): Use it. Produce scm*.
(cstring_to_list): New function.
(add_environment, internal_make_symbol): Use it.
(list_of_char_equal_p): New function.
(internal_lookup_symbol): Use it.
* lib.c (list_ref): New function.
* string.c (string_ref): Use it.
(string, string_append, string_length, substring, number_to_string,
string_to_symbol, symbol_to_string): Update to list-of-characters
implementation.
2016-10-25 14:50:19 +00:00
|
|
|
if (c == EOF && w == &scm_nil) return &scm_nil;
|
|
|
|
if (c == '\n' && w == &scm_nil) return readword (getchar (), w, a);
|
|
|
|
if (c == '\n' && w->car->value == '.' && w->cdr == &scm_nil) 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);
|
Implement strings and symbols as list of characters [WAS: c-string].
* mes.c (scm_t): Add string field.
(make_string, internal_lookup_symbol, internal_make_symbol,
make_symbol, lookup, readword): Take scm*. Update callers.
(display_helper): Support string field.
(append_char): New function.
(readstring): Use it. Produce scm*.
(cstring_to_list): New function.
(add_environment, internal_make_symbol): Use it.
(list_of_char_equal_p): New function.
(internal_lookup_symbol): Use it.
* lib.c (list_ref): New function.
* string.c (string_ref): Use it.
(string, string_append, string_length, substring, number_to_string,
string_to_symbol, symbol_to_string): Update to list-of-characters
implementation.
2016-10-25 14:50:19 +00:00
|
|
|
if (c == '"' && w == &scm_nil) return readstring ();
|
2016-07-10 20:43:23 +00:00
|
|
|
if (c == '"') {ungetchar (c); return lookup (w, a);}
|
Implement strings and symbols as list of characters [WAS: c-string].
* mes.c (scm_t): Add string field.
(make_string, internal_lookup_symbol, internal_make_symbol,
make_symbol, lookup, readword): Take scm*. Update callers.
(display_helper): Support string field.
(append_char): New function.
(readstring): Use it. Produce scm*.
(cstring_to_list): New function.
(add_environment, internal_make_symbol): Use it.
(list_of_char_equal_p): New function.
(internal_lookup_symbol): Use it.
* lib.c (list_ref): New function.
* string.c (string_ref): Use it.
(string, string_append, string_length, substring, number_to_string,
string_to_symbol, symbol_to_string): Update to list-of-characters
implementation.
2016-10-25 14:50:19 +00:00
|
|
|
if (c == '(' && w == &scm_nil) return readlist (a);
|
2016-05-15 22:07:44 +00:00
|
|
|
if (c == '(') {ungetchar (c); return lookup (w, a);}
|
Implement strings and symbols as list of characters [WAS: c-string].
* mes.c (scm_t): Add string field.
(make_string, internal_lookup_symbol, internal_make_symbol,
make_symbol, lookup, readword): Take scm*. Update callers.
(display_helper): Support string field.
(append_char): New function.
(readstring): Use it. Produce scm*.
(cstring_to_list): New function.
(add_environment, internal_make_symbol): Use it.
(list_of_char_equal_p): New function.
(internal_lookup_symbol): Use it.
* lib.c (list_ref): New function.
* string.c (string_ref): Use it.
(string, string_append, string_length, substring, number_to_string,
string_to_symbol, symbol_to_string): Update to list-of-characters
implementation.
2016-10-25 14:50:19 +00:00
|
|
|
if (c == ')' && w == &scm_nil) {ungetchar (c); return &scm_nil;}
|
2016-05-15 22:07:44 +00:00
|
|
|
if (c == ')') {ungetchar (c); return lookup (w, a);}
|
Implement strings and symbols as list of characters [WAS: c-string].
* mes.c (scm_t): Add string field.
(make_string, internal_lookup_symbol, internal_make_symbol,
make_symbol, lookup, readword): Take scm*. Update callers.
(display_helper): Support string field.
(append_char): New function.
(readstring): Use it. Produce scm*.
(cstring_to_list): New function.
(add_environment, internal_make_symbol): Use it.
(list_of_char_equal_p): New function.
(internal_lookup_symbol): Use it.
* lib.c (list_ref): New function.
* string.c (string_ref): Use it.
(string, string_append, string_length, substring, number_to_string,
string_to_symbol, symbol_to_string): Update to list-of-characters
implementation.
2016-10-25 14:50:19 +00:00
|
|
|
if (c == ',' && peekchar () == '@') {getchar (); return cons (lookup (symbol_unquote_splicing.string, a),
|
2016-10-20 17:19:32 +00:00
|
|
|
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 == ',')
|
Implement strings and symbols as list of characters [WAS: c-string].
* mes.c (scm_t): Add string field.
(make_string, internal_lookup_symbol, internal_make_symbol,
make_symbol, lookup, readword): Take scm*. Update callers.
(display_helper): Support string field.
(append_char): New function.
(readstring): Use it. Produce scm*.
(cstring_to_list): New function.
(add_environment, internal_make_symbol): Use it.
(list_of_char_equal_p): New function.
(internal_lookup_symbol): Use it.
* lib.c (list_ref): New function.
* string.c (string_ref): Use it.
(string, string_append, string_length, substring, number_to_string,
string_to_symbol, symbol_to_string): Update to list-of-characters
implementation.
2016-10-25 14:50:19 +00:00
|
|
|
&& w == &scm_nil) {return cons (lookup_char (c, a),
|
2016-05-28 14:39:44 +00:00
|
|
|
cons (readword (getchar (), w, a),
|
|
|
|
&scm_nil));}
|
Implement strings and symbols as list of characters [WAS: c-string].
* mes.c (scm_t): Add string field.
(make_string, internal_lookup_symbol, internal_make_symbol,
make_symbol, lookup, readword): Take scm*. Update callers.
(display_helper): Support string field.
(append_char): New function.
(readstring): Use it. Produce scm*.
(cstring_to_list): New function.
(add_environment, internal_make_symbol): Use it.
(list_of_char_equal_p): New function.
(internal_lookup_symbol): Use it.
* lib.c (list_ref): New function.
* string.c (string_ref): Use it.
(string, string_append, string_length, substring, number_to_string,
string_to_symbol, symbol_to_string): Update to list-of-characters
implementation.
2016-10-25 14:50:19 +00:00
|
|
|
if (c == '#' && peekchar () == ',' && w == &scm_nil) {
|
2016-07-23 22:40:37 +00:00
|
|
|
getchar ();
|
Implement strings and symbols as list of characters [WAS: c-string].
* mes.c (scm_t): Add string field.
(make_string, internal_lookup_symbol, internal_make_symbol,
make_symbol, lookup, readword): Take scm*. Update callers.
(display_helper): Support string field.
(append_char): New function.
(readstring): Use it. Produce scm*.
(cstring_to_list): New function.
(add_environment, internal_make_symbol): Use it.
(list_of_char_equal_p): New function.
(internal_lookup_symbol): Use it.
* lib.c (list_ref): New function.
* string.c (string_ref): Use it.
(string, string_append, string_length, substring, number_to_string,
string_to_symbol, symbol_to_string): Update to list-of-characters
implementation.
2016-10-25 14:50:19 +00:00
|
|
|
if (peekchar () == '@'){getchar (); return cons (lookup (symbol_unsyntax_splicing.string, a),
|
2016-07-23 22:40:37 +00:00
|
|
|
cons (readword (getchar (), w, a),
|
|
|
|
&scm_nil));}
|
Implement strings and symbols as list of characters [WAS: c-string].
* mes.c (scm_t): Add string field.
(make_string, internal_lookup_symbol, internal_make_symbol,
make_symbol, lookup, readword): Take scm*. Update callers.
(display_helper): Support string field.
(append_char): New function.
(readstring): Use it. Produce scm*.
(cstring_to_list): New function.
(add_environment, internal_make_symbol): Use it.
(list_of_char_equal_p): New function.
(internal_lookup_symbol): Use it.
* lib.c (list_ref): New function.
* string.c (string_ref): Use it.
(string, string_append, string_length, substring, number_to_string,
string_to_symbol, symbol_to_string): Update to list-of-characters
implementation.
2016-10-25 14:50:19 +00:00
|
|
|
return cons (lookup (symbol_unsyntax.string, a), cons (readword (getchar (), w, a), &scm_nil));
|
2016-07-23 22:40:37 +00:00
|
|
|
}
|
2016-10-30 22:30:22 +00:00
|
|
|
if (c == '#' && (peekchar () == '\'' || peekchar () == '`') && w == &scm_nil) {
|
|
|
|
c = getchar ();
|
|
|
|
return cons (lookup (cons (make_char ('#'), cons (make_char (c), &scm_nil)), a),
|
|
|
|
cons (readword (getchar (), w, a), &scm_nil));}
|
2016-07-24 16:28:45 +00:00
|
|
|
if (c == ';') {readcomment (c); return readword ('\n', w, a);}
|
2016-10-20 17:19:32 +00:00
|
|
|
if (c == '#' && peekchar () == 'x') {getchar (); return read_hex ();}
|
|
|
|
if (c == '#' && peekchar () == '\\') {getchar (); return read_character ();}
|
Implement strings and symbols as list of characters [WAS: c-string].
* mes.c (scm_t): Add string field.
(make_string, internal_lookup_symbol, internal_make_symbol,
make_symbol, lookup, readword): Take scm*. Update callers.
(display_helper): Support string field.
(append_char): New function.
(readstring): Use it. Produce scm*.
(cstring_to_list): New function.
(add_environment, internal_make_symbol): Use it.
(list_of_char_equal_p): New function.
(internal_lookup_symbol): Use it.
* lib.c (list_ref): New function.
* string.c (string_ref): Use it.
(string, string_append, string_length, substring, number_to_string,
string_to_symbol, symbol_to_string): Update to list-of-characters
implementation.
2016-10-25 14:50:19 +00:00
|
|
|
if (c == '#' && w == &scm_nil && peekchar () == '(') {getchar (); return list_to_vector (readlist (a));}
|
2016-10-20 17:19:32 +00:00
|
|
|
if (c == '#' && peekchar () == '(') {ungetchar (c); return lookup (w, a);}
|
|
|
|
if (c == '#' && peekchar () == '!') {getchar (); readblock (getchar ()); return readword (getchar (), w, a);}
|
Implement strings and symbols as list of characters [WAS: c-string].
* mes.c (scm_t): Add string field.
(make_string, internal_lookup_symbol, internal_make_symbol,
make_symbol, lookup, readword): Take scm*. Update callers.
(display_helper): Support string field.
(append_char): New function.
(readstring): Use it. Produce scm*.
(cstring_to_list): New function.
(add_environment, internal_make_symbol): Use it.
(list_of_char_equal_p): New function.
(internal_lookup_symbol): Use it.
* lib.c (list_ref): New function.
* string.c (string_ref): Use it.
(string, string_append, string_length, substring, number_to_string,
string_to_symbol, symbol_to_string): Update to list-of-characters
implementation.
2016-10-25 14:50:19 +00:00
|
|
|
return readword (getchar (), append2 (w, cons (make_char (c), &scm_nil)), a);
|
2016-05-28 14:39:44 +00:00
|
|
|
}
|
|
|
|
|
2016-08-12 12:17:20 +00:00
|
|
|
scm *
|
|
|
|
read_hex ()
|
|
|
|
{
|
|
|
|
int n = 0;
|
2016-10-20 17:19:32 +00:00
|
|
|
int c = peekchar ();
|
2016-08-12 12:17:20 +00:00
|
|
|
while ((c >= '0' && c <= '9')
|
|
|
|
|| (c >= 'A' && c <= 'F')
|
|
|
|
|| (c >= 'a' && c <= 'f')) {
|
|
|
|
n <<= 4;
|
|
|
|
if (c >= 'a') n += c - 'a' + 10;
|
|
|
|
else if (c >= 'A') n += c - 'A' + 10;
|
|
|
|
else n+= c - '0';
|
|
|
|
getchar ();
|
2016-10-20 17:19:32 +00:00
|
|
|
c = peekchar ();
|
2016-08-12 12:17:20 +00:00
|
|
|
}
|
|
|
|
return make_number (n);
|
|
|
|
}
|
|
|
|
|
2016-07-10 22:15:28 +00:00
|
|
|
scm *
|
2016-07-24 16:28:45 +00:00
|
|
|
read_character ()
|
2016-07-10 22:15:28 +00:00
|
|
|
{
|
|
|
|
int c = getchar ();
|
|
|
|
if (c >= '0' && c <= '7'
|
2016-10-20 17:19:32 +00:00
|
|
|
&& peekchar () >= '0' && peekchar () <= '7') {
|
2016-07-10 22:15:28 +00:00
|
|
|
c = c - '0';
|
2016-10-20 17:19:32 +00:00
|
|
|
while (peekchar () >= '0' && peekchar () <= '7') {
|
2016-07-10 22:15:28 +00:00
|
|
|
c <<= 3;
|
|
|
|
c += getchar () - '0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (c >= 'a' && c <= 'z'
|
2016-10-20 17:19:32 +00:00
|
|
|
&& peekchar () >= 'a' && peekchar () <= 'z') {
|
Implement strings and symbols as list of characters [WAS: c-string].
* mes.c (scm_t): Add string field.
(make_string, internal_lookup_symbol, internal_make_symbol,
make_symbol, lookup, readword): Take scm*. Update callers.
(display_helper): Support string field.
(append_char): New function.
(readstring): Use it. Produce scm*.
(cstring_to_list): New function.
(add_environment, internal_make_symbol): Use it.
(list_of_char_equal_p): New function.
(internal_lookup_symbol): Use it.
* lib.c (list_ref): New function.
* string.c (string_ref): Use it.
(string, string_append, string_length, substring, number_to_string,
string_to_symbol, symbol_to_string): Update to list-of-characters
implementation.
2016-10-25 14:50:19 +00:00
|
|
|
char buf[10];
|
2016-07-10 22:15:28 +00:00
|
|
|
char *p = buf;
|
|
|
|
*p++ = c;
|
2016-10-20 17:19:32 +00:00
|
|
|
while (peekchar () >= 'a' && peekchar () <= 'z') {
|
2016-07-10 22:15:28 +00:00
|
|
|
*p++ = getchar ();
|
|
|
|
}
|
|
|
|
*p = 0;
|
2016-07-24 21:41:16 +00:00
|
|
|
if (!strcmp (buf, char_nul.name)) c = char_nul.value;
|
|
|
|
else if (!strcmp (buf, char_backspace.name)) c = char_backspace.value;
|
|
|
|
else if (!strcmp (buf, char_tab.name)) c = char_tab.value;
|
|
|
|
else if (!strcmp (buf, char_newline.name)) c = char_newline.value;
|
|
|
|
else if (!strcmp (buf, char_vt.name)) c = char_vt.value;
|
|
|
|
else if (!strcmp (buf, char_page.name)) c = char_page.value;
|
|
|
|
else if (!strcmp (buf, char_return.name)) c = char_return.value;
|
|
|
|
else if (!strcmp (buf, char_space.name)) c = char_space.value;
|
2016-07-10 22:15:28 +00:00
|
|
|
else {
|
2016-07-24 10:06:05 +00:00
|
|
|
fprintf (stderr, "char not supported: %s\n", buf);
|
2016-07-10 22:15:28 +00:00
|
|
|
assert (!"char not supported");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return make_char (c);
|
|
|
|
}
|
|
|
|
|
Implement strings and symbols as list of characters [WAS: c-string].
* mes.c (scm_t): Add string field.
(make_string, internal_lookup_symbol, internal_make_symbol,
make_symbol, lookup, readword): Take scm*. Update callers.
(display_helper): Support string field.
(append_char): New function.
(readstring): Use it. Produce scm*.
(cstring_to_list): New function.
(add_environment, internal_make_symbol): Use it.
(list_of_char_equal_p): New function.
(internal_lookup_symbol): Use it.
* lib.c (list_ref): New function.
* string.c (string_ref): Use it.
(string, string_append, string_length, substring, number_to_string,
string_to_symbol, symbol_to_string): Update to list-of-characters
implementation.
2016-10-25 14:50:19 +00:00
|
|
|
scm *
|
|
|
|
append_char (scm *x, int i)
|
|
|
|
{
|
|
|
|
return append2 (x, cons (make_char (i), &scm_nil));
|
|
|
|
}
|
|
|
|
|
2016-07-10 20:43:23 +00:00
|
|
|
scm *
|
|
|
|
readstring ()
|
|
|
|
{
|
Implement strings and symbols as list of characters [WAS: c-string].
* mes.c (scm_t): Add string field.
(make_string, internal_lookup_symbol, internal_make_symbol,
make_symbol, lookup, readword): Take scm*. Update callers.
(display_helper): Support string field.
(append_char): New function.
(readstring): Use it. Produce scm*.
(cstring_to_list): New function.
(add_environment, internal_make_symbol): Use it.
(list_of_char_equal_p): New function.
(internal_lookup_symbol): Use it.
* lib.c (list_ref): New function.
* string.c (string_ref): Use it.
(string, string_append, string_length, substring, number_to_string,
string_to_symbol, symbol_to_string): Update to list-of-characters
implementation.
2016-10-25 14:50:19 +00:00
|
|
|
scm *p = &scm_nil;
|
2016-07-10 20:43:23 +00:00
|
|
|
int c = getchar ();
|
|
|
|
while (true) {
|
|
|
|
if (c == '"') break;
|
Implement strings and symbols as list of characters [WAS: c-string].
* mes.c (scm_t): Add string field.
(make_string, internal_lookup_symbol, internal_make_symbol,
make_symbol, lookup, readword): Take scm*. Update callers.
(display_helper): Support string field.
(append_char): New function.
(readstring): Use it. Produce scm*.
(cstring_to_list): New function.
(add_environment, internal_make_symbol): Use it.
(list_of_char_equal_p): New function.
(internal_lookup_symbol): Use it.
* lib.c (list_ref): New function.
* string.c (string_ref): Use it.
(string, string_append, string_length, substring, number_to_string,
string_to_symbol, symbol_to_string): Update to list-of-characters
implementation.
2016-10-25 14:50:19 +00:00
|
|
|
if (c == '\\' && peekchar () == '"') p = append_char (p, getchar ());
|
|
|
|
else if (c == '\\' && peekchar () == 'n') {getchar (); p = append_char (p, '\n');}
|
2016-08-13 16:42:11 +00:00
|
|
|
else if (c == EOF) assert (!"EOF in string");
|
Implement strings and symbols as list of characters [WAS: c-string].
* mes.c (scm_t): Add string field.
(make_string, internal_lookup_symbol, internal_make_symbol,
make_symbol, lookup, readword): Take scm*. Update callers.
(display_helper): Support string field.
(append_char): New function.
(readstring): Use it. Produce scm*.
(cstring_to_list): New function.
(add_environment, internal_make_symbol): Use it.
(list_of_char_equal_p): New function.
(internal_lookup_symbol): Use it.
* lib.c (list_ref): New function.
* string.c (string_ref): Use it.
(string, string_append, string_length, substring, number_to_string,
string_to_symbol, symbol_to_string): Update to list-of-characters
implementation.
2016-10-25 14:50:19 +00:00
|
|
|
else p = append_char (p, c);
|
2016-07-10 20:43:23 +00:00
|
|
|
c = getchar ();
|
|
|
|
}
|
Implement strings and symbols as list of characters [WAS: c-string].
* mes.c (scm_t): Add string field.
(make_string, internal_lookup_symbol, internal_make_symbol,
make_symbol, lookup, readword): Take scm*. Update callers.
(display_helper): Support string field.
(append_char): New function.
(readstring): Use it. Produce scm*.
(cstring_to_list): New function.
(add_environment, internal_make_symbol): Use it.
(list_of_char_equal_p): New function.
(internal_lookup_symbol): Use it.
* lib.c (list_ref): New function.
* string.c (string_ref): Use it.
(string, string_append, string_length, substring, number_to_string,
string_to_symbol, symbol_to_string): Update to list-of-characters
implementation.
2016-10-25 14:50:19 +00:00
|
|
|
return make_string (p);
|
2016-07-10 20:43:23 +00:00
|
|
|
}
|
|
|
|
|
2016-07-09 16:56:59 +00:00
|
|
|
int
|
|
|
|
eat_whitespace (int c)
|
|
|
|
{
|
2016-07-24 10:06:05 +00:00
|
|
|
while (c == ' ' || c == '\t' || c == '\n') c = getchar ();
|
2016-07-09 16:56:59 +00:00
|
|
|
if (c == ';') return eat_whitespace (readcomment (c));
|
2016-10-20 17:19:32 +00:00
|
|
|
if (c == '#' && peekchar () == '!') {getchar (); readblock (getchar ()); return eat_whitespace (getchar ());}
|
2016-07-09 16:56:59 +00:00
|
|
|
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;
|
Implement strings and symbols as list of characters [WAS: c-string].
* mes.c (scm_t): Add string field.
(make_string, internal_lookup_symbol, internal_make_symbol,
make_symbol, lookup, readword): Take scm*. Update callers.
(display_helper): Support string field.
(append_char): New function.
(readstring): Use it. Produce scm*.
(cstring_to_list): New function.
(add_environment, internal_make_symbol): Use it.
(list_of_char_equal_p): New function.
(internal_lookup_symbol): Use it.
* lib.c (list_ref): New function.
* string.c (string_ref): Use it.
(string, string_append, string_length, substring, number_to_string,
string_to_symbol, symbol_to_string): Update to list-of-characters
implementation.
2016-10-25 14:50:19 +00:00
|
|
|
scm *w = readword (c, &scm_nil, 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-10-16 07:44:52 +00:00
|
|
|
read_env (scm *a)
|
2016-05-28 14:39:44 +00:00
|
|
|
{
|
Implement strings and symbols as list of characters [WAS: c-string].
* mes.c (scm_t): Add string field.
(make_string, internal_lookup_symbol, internal_make_symbol,
make_symbol, lookup, readword): Take scm*. Update callers.
(display_helper): Support string field.
(append_char): New function.
(readstring): Use it. Produce scm*.
(cstring_to_list): New function.
(add_environment, internal_make_symbol): Use it.
(list_of_char_equal_p): New function.
(internal_lookup_symbol): Use it.
* lib.c (list_ref): New function.
* string.c (string_ref): Use it.
(string, string_append, string_length, substring, number_to_string,
string_to_symbol, symbol_to_string): Update to list-of-characters
implementation.
2016-10-25 14:50:19 +00:00
|
|
|
return readword (getchar (), &scm_nil, 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-10-08 08:14:17 +00:00
|
|
|
add_environment (scm *a, char const *name, scm *x)
|
2016-05-15 22:07:44 +00:00
|
|
|
{
|
Implement strings and symbols as list of characters [WAS: c-string].
* mes.c (scm_t): Add string field.
(make_string, internal_lookup_symbol, internal_make_symbol,
make_symbol, lookup, readword): Take scm*. Update callers.
(display_helper): Support string field.
(append_char): New function.
(readstring): Use it. Produce scm*.
(cstring_to_list): New function.
(add_environment, internal_make_symbol): Use it.
(list_of_char_equal_p): New function.
(internal_lookup_symbol): Use it.
* lib.c (list_ref): New function.
* string.c (string_ref): Use it.
(string, string_append, string_length, substring, number_to_string,
string_to_symbol, symbol_to_string): Update to list-of-characters
implementation.
2016-10-25 14:50:19 +00:00
|
|
|
return cons (cons (make_symbol (cstring_to_list (name)), x), a);
|
2016-05-15 22:07:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
scm *
|
2016-10-21 20:44:50 +00:00
|
|
|
mes_environment () ///((internal))
|
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-10-21 20:44:50 +00:00
|
|
|
#include "mes.symbols.i"
|
2016-10-08 06:41:30 +00:00
|
|
|
|
2016-10-20 16:43:33 +00:00
|
|
|
#if BOOT
|
|
|
|
symbols = cons (&scm_label, symbols);
|
|
|
|
a = cons (cons (&scm_label, &scm_t), a);
|
|
|
|
#endif
|
2016-11-02 09:26:04 +00:00
|
|
|
a = cons (cons (&symbol_begin, &scm_begin), a);
|
2016-10-20 16:43:33 +00:00
|
|
|
|
2016-10-22 18:18:03 +00:00
|
|
|
#include "string.environment.i"
|
2016-10-22 18:07:12 +00:00
|
|
|
#include "math.environment.i"
|
2016-10-22 18:51:32 +00:00
|
|
|
#include "lib.environment.i"
|
2016-10-21 20:44:50 +00:00
|
|
|
#include "mes.environment.i"
|
2016-10-22 17:36:54 +00:00
|
|
|
#include "define.environment.i"
|
Move optional type predicates to type.c.
* mes.c (char_p, macro_p, number_p, pair_p, string_p, symbol_p,
vector_p, builtin_p, boolean_p): Move to type.c
* type.c: New file.
* GNUmakefile (mes.o): Depend on type snarf output.
* module/mes/loop-0.mes (cond, map, let, or, and not, evlis-env,
apply-env, eval-expand, uquote, add-unquoters, eval,
expand-macro-env, eval-begin-env, eval-if-env, sexp:define,
env:define, env:macro): Move to mes-0.mes.
* module/mes/mes-0.mes: New file.
* module/mes/type-0.mes: New file.
* scripts/include.mes: If BOOT, also include mes-0.mes. If TYPE0,
also include type-0.mes.
2016-10-22 10:16:19 +00:00
|
|
|
#include "type.environment.i"
|
2016-10-21 20:44:50 +00:00
|
|
|
|
2016-10-30 15:16:20 +00:00
|
|
|
a = add_environment (a, "sc-expand", &scm_f);
|
|
|
|
|
2016-10-20 17:19:32 +00:00
|
|
|
a = cons (cons (&scm_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-11-02 09:26:04 +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-10-20 17:19:32 +00:00
|
|
|
return cons (&scm_closure, cons (cons (&scm_circular, a), cons (args, body)));
|
2016-07-19 19:37:39 +00:00
|
|
|
}
|
|
|
|
|
2016-10-20 17:19:32 +00:00
|
|
|
scm *
|
|
|
|
lookup_macro (scm *x, scm *a)
|
|
|
|
{
|
2016-10-19 22:11:48 +00:00
|
|
|
if (x->type != SYMBOL) return &scm_f;
|
|
|
|
scm *m = assq_ref_cache (x, a);
|
|
|
|
if (macro_p (m) == &scm_t) return m->macro;
|
2016-07-22 15:13:51 +00:00
|
|
|
return &scm_f;
|
2016-07-08 16:02:06 +00:00
|
|
|
}
|
|
|
|
|
2016-07-16 20:43:13 +00:00
|
|
|
scm *
|
2016-10-29 16:43:03 +00:00
|
|
|
read_file_env (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;
|
2016-10-29 16:43:03 +00:00
|
|
|
return cons (e, read_file_env (read_env (a), a));
|
|
|
|
}
|
|
|
|
|
|
|
|
scm *
|
|
|
|
load_file_env (scm *a)
|
|
|
|
{
|
2016-11-02 09:26:04 +00:00
|
|
|
return begin_env (read_file_env (read_env (a), a), a);
|
2016-05-29 11:44:03 +00:00
|
|
|
}
|
|
|
|
|
2016-10-22 18:18:03 +00:00
|
|
|
#include "type.c"
|
|
|
|
#include "define.c"
|
2016-10-22 18:51:32 +00:00
|
|
|
#include "lib.c"
|
2016-10-22 18:18:03 +00:00
|
|
|
#include "math.c"
|
|
|
|
#include "quasiquote.c"
|
|
|
|
#include "string.c"
|
|
|
|
|
2016-07-16 20:43:13 +00:00
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
{
|
2016-10-16 11:45:24 +00:00
|
|
|
if (argc > 1 && !strcmp (argv[1], "--help")) return puts ("Usage: mes < FILE\n");
|
2016-11-20 20:47:47 +00:00
|
|
|
if (argc > 1 && !strcmp (argv[1], "--version")) return puts ("Mes 0.1\n");
|
2016-07-16 20:43:13 +00:00
|
|
|
scm *a = mes_environment ();
|
2016-10-29 16:43:03 +00:00
|
|
|
display_ (stderr, load_file_env (a));
|
2016-08-12 12:17:20 +00:00
|
|
|
fputs ("", stderr);
|
2016-07-16 20:43:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|