diff --git a/include/mes/builtins.h b/include/mes/builtins.h index 9f617181..fa0104a4 100644 --- a/include/mes/builtins.h +++ b/include/mes/builtins.h @@ -174,4 +174,4 @@ SCM vector_set_x (SCM x, SCM i, SCM e); SCM list_to_vector (SCM x); SCM vector_to_list (SCM v); -#endif //__MES_BUILTINS_H +#endif /* __MES_BUILTINS_H */ diff --git a/include/mes/cc.h b/include/mes/cc.h new file mode 100644 index 00000000..19949c7c --- /dev/null +++ b/include/mes/cc.h @@ -0,0 +1,42 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2016,2017,2018,2019 Jan (janneke) Nieuwenhuizen + * + * This file is part of GNU Mes. + * + * GNU 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. + * + * GNU 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 GNU Mes. If not, see . + */ + +#ifndef __MES_CC_H +#define __MES_CC_H + +typedef long SCM; + +#if __MESC__ +typedef long FUNCTION; +typedef long function0_t; +typedef long function1_t; +typedef long function2_t; +typedef long function3_t; +typedef long functionn_t; +#else // !__MESC__ +typedef SCM (*FUNCTION) (void); +typedef SCM (*function0_t) (void); +typedef SCM (*function1_t) (SCM); +typedef SCM (*function2_t) (SCM, SCM); +typedef SCM (*function3_t) (SCM, SCM, SCM); +typedef SCM (*functionn_t) (SCM); +#endif // !__MESC__ + +#endif //__MES_CC_H diff --git a/include/mes/constants.h b/include/mes/constants.h index a5cb28cb..2840535c 100644 --- a/include/mes/constants.h +++ b/include/mes/constants.h @@ -307,7 +307,31 @@ // CONSTANT STRUCT_PRINTER 1 #define STRUCT_PRINTER 1 +// CONSTANT FRAME_SIZE 5 #define FRAME_SIZE 5 +// CONSTANT FRAME_PROCEDURE 4 #define FRAME_PROCEDURE 4 -#endif //__MES_CONSTANTS_H +// CONSTANT STDIN 0 +// CONSTANT STDOUT 1 +// CONSTANT STDERR 2 + +/* Unknown type 1 +// CONSTANT EOF -1 +*/ + +// CONSTANT O_RDONLY 0 +// CONSTANT O_WRONLY 1 +// CONSTANT O_CREAT 0x40 +// CONSTANT O_TRUNC 0x200 + +// CONSTANT PATH_MAX 1024 +// CONSTANT __FILEDES_MAX 512 + +// CONSTANT S_IRUSR 00400 +// CONSTANT S_IWUSR 00200 + +// CONSTANT CLOCK_PROCESS_CPUTIME_ID 2 + + +#endif /* __MES_CONSTANTS_H */ diff --git a/include/mes/m2.h b/include/mes/m2.h new file mode 100644 index 00000000..b469973f --- /dev/null +++ b/include/mes/m2.h @@ -0,0 +1,112 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2016,2017,2018,2019 Jan (janneke) Nieuwenhuizen + * + * This file is part of GNU Mes. + * + * GNU 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. + * + * GNU 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 GNU Mes. If not, see . + */ + +#ifndef __MES_M2_H +#define __MES_M2_H + +char **environ; +int __stdin; +int __stdout; +int __stderr; + +int __ungetc_p (int filedes); +int eputs (char *s); +int oputs (char *s); +int puts (char *s); +size_t strlen (char *s); +ssize_t _write (); +ssize_t write (int filedes, void *buffer, size_t size); +void __ungetc_clear (int filedes); +void __ungetc_init (); +void __ungetc_set (int filedes, int c); + +/* Recieved SCM in program +struct timespec +{ + long tv_sec; + long tv_nsec; +}; + +struct timeval +{ + long tv_sec; + long tv_usec; +}; + +*/ + +#define struct_size 12 +#define CELL(x) ((x*struct_size)+g_cells) +#define TYPE(x) ((x*struct_size)+g_cells)->type +#define CAR(x) ((x*struct_size)+g_cells)->car +#define CDR(x) ((x*struct_size)+g_cells)->cdr + +#define NTYPE(x) ((x*struct_size)+g_news)->type +#define NCAR(x) ((x*struct_size)+g_news)->car +#define NCDR(x) ((x*struct_size)+g_news)->cdr + + +#define BYTES(x) ((x*struct_size)+g_cells)->car +#define LENGTH(x) ((x*struct_size)+g_cells)->car +#define MACRO(x) ((x*struct_size)+g_cells)->car +#define PORT(x) ((x*struct_size)+g_cells)->car +#define REF(x) ((x*struct_size)+g_cells)->car +#define VARIABLE(x) ((x*struct_size)+g_cells)->car + +#define CLOSURE(x) ((x*struct_size)+g_cells)->cdr +#define CONTINUATION(x) ((x*struct_size)+g_cells)->cdr + +#define CBYTES(x) (((x*struct_size)+g_cells) + 8) + +#define NAME(x) ((x*struct_size)+g_cells)->cdr +#define STRING(x) ((x*struct_size)+g_cells)->cdr +#define STRUCT(x) ((x*struct_size)+g_cells)->cdr +#define VALUE(x) ((x*struct_size)+g_cells)->cdr +#define VECTOR(x) ((x*struct_size)+g_cells)->cdr + +#define NLENGTH(x) ((x*struct_size)+g_news)->car + +#define NCBYTES(x) (((x*struct_size)+g_news) + 8) +#define NVALUE(x) ((x*struct_size)+g_news)->cdr +#define NSTRING(x) ((x*struct_size)+g_news)->cdr +#define NVECTOR(x) ((x*struct_size)+g_news)->cdr + +#define CSTRING(x) CBYTES (STRING (x)) + +#define MAKE_BYTES0(x) make_bytes (x, strlen (x)) +#define NAME_SYMBOL(symbol,name) {size_t s = strlen (name); CAR (symbol) = s; CDR (symbol) = make_bytes (name, s);} + +#define MAKE_CHAR(n) make_cell__ (TCHAR, 0, n) +#define MAKE_CONTINUATION(n) make_cell__ (TCONTINUATION, n, g_stack) +#define MAKE_NUMBER(n) make_cell__ (TNUMBER, 0, n) +#define MAKE_REF(n) make_cell__ (TREF, n, 0) +#define MAKE_STRING0(x) make_string (x, strlen (x)) +#define MAKE_STRING_PORT(x) make_cell__ (TPORT, -length__ (g_ports) - 2, x) +#define MAKE_MACRO(name, x) make_cell__ (TMACRO, x, STRING (name)) + +#define CAAR(x) CAR (CAR (x)) +#define CADR(x) CAR (CDR (x)) +#define CDAR(x) CDR (CAR (x)) +#define CDDR(x) CDR (CDR (x)) +#define CADAR(x) CAR (CDR (CAR (x))) +#define CADDR(x) CAR (CDR (CDR (x))) +#define CDADAR(x) CAR (CDR (CAR (CDR (x)))) + +#endif /* __MES_M2_H */ diff --git a/include/mes/mes.h b/include/mes/mes.h index c8719b47..422be601 100644 --- a/include/mes/mes.h +++ b/include/mes/mes.h @@ -22,8 +22,7 @@ #define __MES_MES_H #include - -typedef long SCM; +#include "mes/cc.h" struct scm { @@ -32,64 +31,53 @@ struct scm SCM cdr; }; -#if __MESC__ -typedef long function0_t; -typedef long function1_t; -typedef long function2_t; -typedef long function3_t; -typedef long functionn_t; -#else // !__MESC__ -typedef SCM (*function0_t) (void); -typedef SCM (*function1_t) (SCM); -typedef SCM (*function2_t) (SCM, SCM); -typedef SCM (*function3_t) (SCM, SCM, SCM); -typedef SCM (*functionn_t) (SCM); -#endif // !__MESC__ +/* mes */ +int g_debug; +char *g_buf; +SCM g_continuations; +SCM g_symbols; +SCM g_symbol_max; -// mes -extern int g_debug; -extern char *g_buf; -extern SCM g_continuations; -extern SCM g_symbols; -extern SCM g_symbol_max; +/* a/env */ +SCM r0; +/* param 1 */ +SCM r1; +/* save 2 */ +SCM r2; +/* continuation */ +SCM r3; +/* current-module */ +SCM m0; +/* macro */ +SCM g_macros; +SCM g_ports; -// a/env -extern SCM r0; -// param 1 -extern SCM r1; -// save 2 -extern SCM r2; -// continuation -extern SCM r3; -// current-module -extern SCM m0; -// macro -extern SCM g_macros; -extern SCM g_ports; +/* gc */ +long ARENA_SIZE; +long MAX_ARENA_SIZE; +long STACK_SIZE; +long JAM_SIZE; +long GC_SAFETY; +long MAX_STRING; +char *g_arena; +long g_free; +SCM g_stack; +SCM *g_stack_array; +struct scm *g_cells; +struct scm *g_news; -// gc -extern long ARENA_SIZE; -extern long MAX_ARENA_SIZE; -extern long STACK_SIZE; -extern long JAM_SIZE; -extern long GC_SAFETY; -extern long MAX_STRING; -extern char *g_arena; -extern long g_free; -extern SCM g_stack; -extern SCM *g_stack_array; -extern struct scm *g_cells; -extern struct scm *g_news; +char **__execl_c_argv; +char *__getcwd_buf; +char *__open_boot_buf; +char *__open_boot_file_name; +char *__setenv_buf; +char *__reader_read_char_buf; SCM alloc (long n); SCM apply (SCM f, SCM x, SCM a); SCM apply_builtin (SCM fn, SCM x); SCM builtin_name (SCM builtin); -#if __MESC__ -long builtin_function (SCM builtin); -#else -SCM (*builtin_function (SCM builtin)) (SCM); -#endif +FUNCTION builtin_function (SCM builtin); SCM cstring_to_list (char const *s); SCM cstring_to_symbol (char const *s); SCM fdisplay_ (SCM, int, int); @@ -122,4 +110,4 @@ void assert_max_string (size_t i, char const *msg, char *string); #include "mes/constants.h" #include "mes/macros.h" -#endif //__MES_MES_H +#endif /* __MES_MES_H */ diff --git a/simple.make b/simple.make index 10d7228c..a2dac45c 100644 --- a/simple.make +++ b/simple.make @@ -28,6 +28,7 @@ MES = bin/mes-gcc #MES_CPU = x86 M2_PLANET = M2-Planet +M2_PLANET_FLAGS = --architecture amd64 CFLAGS:= \ $(CFLAGS) \ @@ -105,6 +106,7 @@ bin/mes-gcc: $(MAKEFILES) $(GCC_SOURCES) $(INCLUDES) | bin M2_PLANET_INCLUDES = \ include/mes/mes.h \ include/mes/m2.h \ + include/mes/builtins.h \ include/mes/constants.h M2_PLANET_PREFIX = ../M2-Planet @@ -112,19 +114,54 @@ M2_PLANET_SOURCES = \ $(M2_PLANET_PREFIX)/test/common_amd64/functions/exit.c \ $(M2_PLANET_PREFIX)/test/common_amd64/functions/malloc.c \ $(M2_PLANET_PREFIX)/functions/calloc.c \ - $(M2_PLANET_INCLUDES) \ + $(M2_PLANET_INCLUDES:%.h=%.h.m2) \ $(SOURCES:%.c=%.c.m2) -%.c.m2: %.c $(MAKEFILES) - @sed -r 's@^(#include.*)@/* \1 */@' $< \ +%.h.m2: %.h $(MAKEFILES) + @sed -r \ + -e 's,^//,@@,' \ + -e 's@^(#include.*)@/* \1 */@' \ + $< \ | $(CC) -E -I include \ + -D __M2_PLANET__=1 \ -D FUNCTION0=FUNCTION \ -D FUNCTION1=FUNCTION \ -D FUNCTION2=FUNCTION \ -D FUNCTION3=FUNCTION \ -D FUNCTIONN=FUNCTION \ -D const= \ - -o $@ -x c - + -D long=SCM \ + -D size_t=SCM \ + -D ssize_t=SCM \ + -D unsigned=SCM \ + -include mes/m2.h \ + -x c - \ + | sed -r \ + -e 's,^@@,//,' \ + > $@ \ + +%.c.m2: %.c $(MAKEFILES) + @sed -r \ + -e 's,^//,@@,' \ + -e 's@^(#include.*)@/* \1 */@' \ + $< \ + | $(CC) -E -I include \ + -D __M2_PLANET__=1 \ + -D FUNCTION0=FUNCTION \ + -D FUNCTION1=FUNCTION \ + -D FUNCTION2=FUNCTION \ + -D FUNCTION3=FUNCTION \ + -D FUNCTIONN=FUNCTION \ + -D const= \ + -D long=SCM \ + -D size_t=SCM \ + -D ssize_t=SCM \ + -D unsigned=SCM \ + -include mes/m2.h \ + -x c - \ + | sed -r \ + -e 's,^@@,//,' \ + > $@ bin/mes-m2: $(MAKEFILES) $(M2_PLANET_SOURCES) $(M2_PLANET_INCLUDES) | bin $(M2_PLANET) $(M2_PLANET_FLAGS) $(M2_PLANET_SOURCES:%=-f %) -o $@ || rm -f $@ diff --git a/src/builtins.c b/src/builtins.c index 1fdf89f0..c5f6d12d 100644 --- a/src/builtins.c +++ b/src/builtins.c @@ -22,7 +22,7 @@ #include "mes/mes.h" SCM -make_builtin_type () ///(internal)) +make_builtin_type () /*:((internal)) */ { SCM record_type = cell_symbol_record_type; SCM fields = cell_nil; @@ -57,23 +57,18 @@ builtin_arity (SCM builtin) return struct_ref_ (builtin, 4); } -#if __MESC__ -long +FUNCTION builtin_function (SCM builtin) { return VALUE (struct_ref_ (builtin, 5)); } -#else -SCM (*builtin_function (SCM builtin)) (SCM) -{ - return (function1_t) VALUE (struct_ref_ (builtin, 5)); -} -#endif SCM builtin_p (SCM x) { - return (TYPE (x) == TSTRUCT && struct_ref_ (x, 2) == cell_symbol_builtin) ? cell_t : cell_f; + if (TYPE (x) == TSTRUCT && struct_ref_ (x, 2) == cell_symbol_builtin) + return cell_t; + return cell_f; } SCM @@ -88,9 +83,10 @@ builtin_printer (SCM builtin) else { fdputc ('(', __stdout); - for (int i = 0; i < arity; i++) + int i; + for (i = 0; i < arity; i = i + 1) { - if (i) + if (i != 0) fdputc (' ', __stdout); fdputc ('_', __stdout); } @@ -99,7 +95,7 @@ builtin_printer (SCM builtin) } SCM -init_builtin (SCM builtin_type, char const *name, int arity, SCM (*function) (SCM), SCM a) +init_builtin (SCM builtin_type, char const *name, int arity, FUNCTION function, SCM a) { SCM s = cstring_to_symbol (name); return acons (s, @@ -108,165 +104,166 @@ init_builtin (SCM builtin_type, char const *name, int arity, SCM (*function) (SC } SCM -mes_builtins (SCM a) ///((internal)) +mes_builtins (SCM a) /**((internal))*/ { // TODO minimal: cons, car, cdr, list, null_p, eq_p minus, plus // display_, display_error_, getenv SCM builtin_type = make_builtin_type (); - /* src/builtins.c */ - a = init_builtin (builtin_type, "make-builtin-type", 0, (function1_t) & make_builtin_type, a); - a = init_builtin (builtin_type, "make-builtin", 4, (function1_t) & make_builtin, a); - a = init_builtin (builtin_type, "builtin-name", 1, (function1_t) & builtin_name, a); - a = init_builtin (builtin_type, "builtin-arity", 1, (function1_t) & builtin_arity, a); - a = init_builtin (builtin_type, "builtin?", 1, (function1_t) & builtin_p, a); - a = init_builtin (builtin_type, "builtin-printer", 1, (function1_t) & builtin_printer, a); - /* src/gc.c */ - a = init_builtin (builtin_type, "gc-check", 0, (function1_t) & gc_check, a); - a = init_builtin (builtin_type, "gc", 0, (function1_t) & gc, a); - /* src/hash.c */ - a = init_builtin (builtin_type, "hashq", 2, (function1_t) & hashq, a); - a = init_builtin (builtin_type, "hash", 2, (function1_t) & hash, a); - a = init_builtin (builtin_type, "hashq-get-handle", 3, (function1_t) & hashq_get_handle, a); - a = init_builtin (builtin_type, "hashq-ref", 3, (function1_t) & hashq_ref, a); - a = init_builtin (builtin_type, "hash-ref", 3, (function1_t) & hash_ref, a); - a = init_builtin (builtin_type, "hashq-set!", 3, (function1_t) & hashq_set_x, a); - a = init_builtin (builtin_type, "hash-set!", 3, (function1_t) & hash_set_x, a); - a = init_builtin (builtin_type, "hash-table-printer", 1, (function1_t) & hash_table_printer, a); - a = init_builtin (builtin_type, "make-hash-table", 1, (function1_t) & make_hash_table, a); - /* src/lib.c */ - a = init_builtin (builtin_type, "core:display", 1, (function1_t) & display_, a); - a = init_builtin (builtin_type, "core:display-error", 1, (function1_t) & display_error_, a); - a = init_builtin (builtin_type, "core:display-port", 2, (function1_t) & display_port_, a); - a = init_builtin (builtin_type, "core:write", 1, (function1_t) & write_, a); - a = init_builtin (builtin_type, "core:write-error", 1, (function1_t) & write_error_, a); - a = init_builtin (builtin_type, "core:write-port", 2, (function1_t) & write_port_, a); - a = init_builtin (builtin_type, "exit", 1, (function1_t) & exit_, a); - a = init_builtin (builtin_type, "frame-printer", 1, (function1_t) & frame_printer, a); - a = init_builtin (builtin_type, "make-stack", -1, (function1_t) & make_stack, a); - a = init_builtin (builtin_type, "stack-length", 1, (function1_t) & stack_length, a); - a = init_builtin (builtin_type, "stack-ref", 2, (function1_t) & stack_ref, a); - a = init_builtin (builtin_type, "xassq", 2, (function1_t) & xassq, a); - a = init_builtin (builtin_type, "memq", 2, (function1_t) & memq, a); - a = init_builtin (builtin_type, "equal2?", 2, (function1_t) & equal2_p, a); - a = init_builtin (builtin_type, "last-pair", 1, (function1_t) & last_pair, a); - a = init_builtin (builtin_type, "pair?", 1, (function1_t) & pair_p, a); - /* src/math.c */ - a = init_builtin (builtin_type, ">", -1, (function1_t) & greater_p, a); - a = init_builtin (builtin_type, "<", -1, (function1_t) & less_p, a); - a = init_builtin (builtin_type, "=", -1, (function1_t) & is_p, a); - a = init_builtin (builtin_type, "-", -1, (function1_t) & minus, a); - a = init_builtin (builtin_type, "+", -1, (function1_t) & plus, a); - a = init_builtin (builtin_type, "/", -1, (function1_t) & divide, a); - a = init_builtin (builtin_type, "modulo", 2, (function1_t) & modulo, a); - a = init_builtin (builtin_type, "*", -1, (function1_t) & multiply, a); - a = init_builtin (builtin_type, "logand", -1, (function1_t) & logand, a); - a = init_builtin (builtin_type, "logior", -1, (function1_t) & logior, a); - a = init_builtin (builtin_type, "lognot", 1, (function1_t) & lognot, a); - a = init_builtin (builtin_type, "logxor", -1, (function1_t) & logxor, a); - a = init_builtin (builtin_type, "ash", 2, (function1_t) & ash, a); - /* src/mes.c */ - a = init_builtin (builtin_type, "core:make-cell", 3, (function1_t) & make_cell_, a); - a = init_builtin (builtin_type, "core:type", 1, (function1_t) & type_, a); - a = init_builtin (builtin_type, "core:car", 1, (function1_t) & car_, a); - a = init_builtin (builtin_type, "core:cdr", 1, (function1_t) & cdr_, a); - a = init_builtin (builtin_type, "cons", 2, (function1_t) & cons, a); - a = init_builtin (builtin_type, "car", 1, (function1_t) & car, a); - a = init_builtin (builtin_type, "cdr", 1, (function1_t) & cdr, a); - a = init_builtin (builtin_type, "list", -1, (function1_t) & list, a); - a = init_builtin (builtin_type, "null?", 1, (function1_t) & null_p, a); - a = init_builtin (builtin_type, "eq?", 2, (function1_t) & eq_p, a); - a = init_builtin (builtin_type, "values", -1, (function1_t) & values, a); - a = init_builtin (builtin_type, "acons", 3, (function1_t) & acons, a); - a = init_builtin (builtin_type, "length", 1, (function1_t) & length, a); - a = init_builtin (builtin_type, "error", 2, (function1_t) & error, a); - a = init_builtin (builtin_type, "append2", 2, (function1_t) & append2, a); - a = init_builtin (builtin_type, "append-reverse", 2, (function1_t) & append_reverse, a); - a = init_builtin (builtin_type, "core:reverse!", 2, (function1_t) & reverse_x_, a); - a = init_builtin (builtin_type, "pairlis", 3, (function1_t) & pairlis, a); - a = init_builtin (builtin_type, "assq", 2, (function1_t) & assq, a); - a = init_builtin (builtin_type, "assoc", 2, (function1_t) & assoc, a); - a = init_builtin (builtin_type, "set-car!", 2, (function1_t) & set_car_x, a); - a = init_builtin (builtin_type, "set-cdr!", 2, (function1_t) & set_cdr_x, a); - a = init_builtin (builtin_type, "set-env!", 3, (function1_t) & set_env_x, a); - a = init_builtin (builtin_type, "macro-get-handle", 1, (function1_t) & macro_get_handle, a); - a = init_builtin (builtin_type, "add-formals", 2, (function1_t) & add_formals, a); - a = init_builtin (builtin_type, "eval-apply", 0, (function1_t) & eval_apply, a); - /* src/module.c */ - a = init_builtin (builtin_type, "make-module-type", 0, (function1_t) & make_module_type, a); - a = init_builtin (builtin_type, "module-printer", 1, (function1_t) & module_printer, a); - a = init_builtin (builtin_type, "module-variable", 2, (function1_t) & module_variable, a); - a = init_builtin (builtin_type, "module-ref", 2, (function1_t) & module_ref, a); - a = init_builtin (builtin_type, "module-define!", 3, (function1_t) & module_define_x, a); - /* src/posix.c */ - a = init_builtin (builtin_type, "peek-byte", 0, (function1_t) & peek_byte, a); - a = init_builtin (builtin_type, "read-byte", 0, (function1_t) & read_byte, a); - a = init_builtin (builtin_type, "unread-byte", 1, (function1_t) & unread_byte, a); - a = init_builtin (builtin_type, "peek-char", 0, (function1_t) & peek_char, a); - a = init_builtin (builtin_type, "read-char", -1, (function1_t) & read_char, a); - a = init_builtin (builtin_type, "unread-char", 1, (function1_t) & unread_char, a); - a = init_builtin (builtin_type, "write-char", -1, (function1_t) & write_char, a); - a = init_builtin (builtin_type, "write-byte", -1, (function1_t) & write_byte, a); - a = init_builtin (builtin_type, "getenv", 1, (function1_t) & getenv_, a); - a = init_builtin (builtin_type, "setenv", 2, (function1_t) & setenv_, a); - a = init_builtin (builtin_type, "access?", 2, (function1_t) & access_p, a); - a = init_builtin (builtin_type, "current-input-port", 0, (function1_t) & current_input_port, a); - a = init_builtin (builtin_type, "open-input-file", 1, (function1_t) & open_input_file, a); - a = init_builtin (builtin_type, "open-input-string", 1, (function1_t) & open_input_string, a); - a = init_builtin (builtin_type, "set-current-input-port", 1, (function1_t) & set_current_input_port, a); - a = init_builtin (builtin_type, "current-output-port", 0, (function1_t) & current_output_port, a); - a = init_builtin (builtin_type, "current-error-port", 0, (function1_t) & current_error_port, a); - a = init_builtin (builtin_type, "open-output-file", -1, (function1_t) & open_output_file, a); - a = init_builtin (builtin_type, "set-current-output-port", 1, (function1_t) & set_current_output_port, a); - a = init_builtin (builtin_type, "set-current-error-port", 1, (function1_t) & set_current_error_port, a); - a = init_builtin (builtin_type, "chmod", 2, (function1_t) & chmod_, a); - a = init_builtin (builtin_type, "isatty?", 1, (function1_t) & isatty_p, a); - a = init_builtin (builtin_type, "primitive-fork", 0, (function1_t) & primitive_fork, a); - a = init_builtin (builtin_type, "execl", 2, (function1_t) & execl_, a); - a = init_builtin (builtin_type, "core:waitpid", 2, (function1_t) & waitpid_, a); - a = init_builtin (builtin_type, "current-time", 0, (function1_t) & current_time, a); - a = init_builtin (builtin_type, "gettimeofday", 0, (function1_t) & gettimeofday_, a); - a = init_builtin (builtin_type, "get-internal-run-time", 0, (function1_t) & get_internal_run_time, a); - a = init_builtin (builtin_type, "getcwd", 0, (function1_t) & getcwd_, a); - a = init_builtin (builtin_type, "dup", 1, (function1_t) & dup_, a); - a = init_builtin (builtin_type, "dup2", 2, (function1_t) & dup2_, a); - a = init_builtin (builtin_type, "delete-file", 1, (function1_t) & delete_file, a); - /* src/reader.c */ - a = init_builtin (builtin_type, "core:read-input-file-env", 2, (function1_t) & read_input_file_env_, a); - a = init_builtin (builtin_type, "read-input-file-env", 1, (function1_t) & read_input_file_env, a); - a = init_builtin (builtin_type, "read-env", 1, (function1_t) & read_env, a); - a = init_builtin (builtin_type, "reader-read-sexp", 3, (function1_t) & reader_read_sexp, a); - a = init_builtin (builtin_type, "reader-read-character", 0, (function1_t) & reader_read_character, a); - a = init_builtin (builtin_type, "reader-read-binary", 0, (function1_t) & reader_read_binary, a); - a = init_builtin (builtin_type, "reader-read-octal", 0, (function1_t) & reader_read_octal, a); - a = init_builtin (builtin_type, "reader-read-hex", 0, (function1_t) & reader_read_hex, a); - a = init_builtin (builtin_type, "reader-read-string", 0, (function1_t) & reader_read_string, a); - /* src/string.c */ - a = init_builtin (builtin_type, "string=?", 2, (function1_t) & string_equal_p, a); - a = init_builtin (builtin_type, "symbol->string", 1, (function1_t) & symbol_to_string, a); - a = init_builtin (builtin_type, "symbol->keyword", 1, (function1_t) & symbol_to_keyword, a); - a = init_builtin (builtin_type, "keyword->string", 1, (function1_t) & keyword_to_string, a); - a = init_builtin (builtin_type, "string->symbol", 1, (function1_t) & string_to_symbol, a); - a = init_builtin (builtin_type, "make-symbol", 1, (function1_t) & make_symbol, a); - a = init_builtin (builtin_type, "string->list", 1, (function1_t) & string_to_list, a); - a = init_builtin (builtin_type, "list->string", 1, (function1_t) & list_to_string, a); - a = init_builtin (builtin_type, "read-string", -1, (function1_t) & read_string, a); - a = init_builtin (builtin_type, "string-append", -1, (function1_t) & string_append, a); - a = init_builtin (builtin_type, "string-length", 1, (function1_t) & string_length, a); - a = init_builtin (builtin_type, "string-ref", 2, (function1_t) & string_ref, a); - /* src/struct.c */ - a = init_builtin (builtin_type, "make-struct", 3, (function1_t) & make_struct, a); - a = init_builtin (builtin_type, "struct-length", 1, (function1_t) & struct_length, a); - a = init_builtin (builtin_type, "struct-ref", 2, (function1_t) & struct_ref, a); - a = init_builtin (builtin_type, "struct-set!", 3, (function1_t) & struct_set_x, a); - /* src/vector.c */ - a = init_builtin (builtin_type, "core:make-vector", 1, (function1_t) & make_vector_, a); - a = init_builtin (builtin_type, "vector-length", 1, (function1_t) & vector_length, a); - a = init_builtin (builtin_type, "vector-ref", 2, (function1_t) & vector_ref, a); - a = init_builtin (builtin_type, "vector-entry", 1, (function1_t) & vector_entry, a); - a = init_builtin (builtin_type, "vector-set!", 3, (function1_t) & vector_set_x, a); - a = init_builtin (builtin_type, "list->vector", 1, (function1_t) & list_to_vector, a); - a = init_builtin (builtin_type, "vector->list", 1, (function1_t) & vector_to_list, a); + /* src/builtins.mes */ + a = init_builtin (builtin_type, "make-builtin-type", 0, &make_builtin_type, a); + a = init_builtin (builtin_type, "make-builtin", 4, &make_builtin, a); + a = init_builtin (builtin_type, "builtin-name", 1, &builtin_name, a); + a = init_builtin (builtin_type, "builtin-arity", 1, &builtin_arity, a); + a = init_builtin (builtin_type, "builtin?", 1, &builtin_p, a); + a = init_builtin (builtin_type, "builtin-printer", 1, &builtin_printer, a); + /* src/gc.mes */ + a = init_builtin (builtin_type, "gc-check", 0, &gc_check, a); + a = init_builtin (builtin_type, "gc", 0, &gc, a); + /* src/hash.mes */ + a = init_builtin (builtin_type, "hashq", 2, &hashq, a); + a = init_builtin (builtin_type, "hash", 2, &hash, a); + a = init_builtin (builtin_type, "hashq-get-handle", 3, &hashq_get_handle, a); + a = init_builtin (builtin_type, "hashq-ref", 3, &hashq_ref, a); + a = init_builtin (builtin_type, "hash-ref", 3, &hash_ref, a); + a = init_builtin (builtin_type, "hashq-set!", 3, &hashq_set_x, a); + a = init_builtin (builtin_type, "hash-set!", 3, &hash_set_x, a); + a = init_builtin (builtin_type, "hash-table-printer", 1, &hash_table_printer, a); + a = init_builtin (builtin_type, "make-hash-table", 1, &make_hash_table, a); + /* src/lib.mes */ + a = init_builtin (builtin_type, "core:display", 1, &display_, a); + a = init_builtin (builtin_type, "core:display-error", 1, &display_error_, a); + a = init_builtin (builtin_type, "core:display-port", 2, &display_port_, a); + a = init_builtin (builtin_type, "core:write", 1, &write_, a); + a = init_builtin (builtin_type, "core:write-error", 1, &write_error_, a); + a = init_builtin (builtin_type, "core:write-port", 2, &write_port_, a); + a = init_builtin (builtin_type, "exit", 1, &exit_, a); + a = init_builtin (builtin_type, "frame-printer", 1, &frame_printer, a); + a = init_builtin (builtin_type, "make-stack", -1, &make_stack, a); + a = init_builtin (builtin_type, "stack-length", 1, &stack_length, a); + a = init_builtin (builtin_type, "stack-ref", 2, &stack_ref, a); + a = init_builtin (builtin_type, "xassq", 2, &xassq, a); + a = init_builtin (builtin_type, "memq", 2, &memq, a); + a = init_builtin (builtin_type, "equal2?", 2, &equal2_p, a); + a = init_builtin (builtin_type, "last-pair", 1, &last_pair, a); + a = init_builtin (builtin_type, "pair?", 1, &pair_p, a); + /* src/math.mes */ + a = init_builtin (builtin_type, ">", -1, &greater_p, a); + a = init_builtin (builtin_type, "<", -1, &less_p, a); + a = init_builtin (builtin_type, "=", -1, &is_p, a); + a = init_builtin (builtin_type, "-", -1, &minus, a); + a = init_builtin (builtin_type, "+", -1, &plus, a); + a = init_builtin (builtin_type, "/", -1, ÷, a); + a = init_builtin (builtin_type, "modulo", 2, &modulo, a); + a = init_builtin (builtin_type, "*", -1, &multiply, a); + a = init_builtin (builtin_type, "logand", -1, &logand, a); + a = init_builtin (builtin_type, "logior", -1, &logior, a); + a = init_builtin (builtin_type, "lognot", 1, &lognot, a); + a = init_builtin (builtin_type, "logxor", -1, &logxor, a); + a = init_builtin (builtin_type, "ash", 2, &ash, a); + /* src/mes.mes */ + a = init_builtin (builtin_type, "core:make-cell", 3, &make_cell_, a); + a = init_builtin (builtin_type, "core:type", 1, &type_, a); + a = init_builtin (builtin_type, "core:car", 1, &car_, a); + a = init_builtin (builtin_type, "core:cdr", 1, &cdr_, a); + a = init_builtin (builtin_type, "cons", 2, &cons, a); + a = init_builtin (builtin_type, "car", 1, &car, a); + a = init_builtin (builtin_type, "cdr", 1, &cdr, a); + a = init_builtin (builtin_type, "list", -1, &list, a); + a = init_builtin (builtin_type, "null?", 1, &null_p, a); + a = init_builtin (builtin_type, "eq?", 2, &eq_p, a); + a = init_builtin (builtin_type, "values", -1, &values, a); + a = init_builtin (builtin_type, "acons", 3, &acons, a); + a = init_builtin (builtin_type, "length", 1, &length, a); + a = init_builtin (builtin_type, "error", 2, &error, a); + a = init_builtin (builtin_type, "append2", 2, &append2, a); + a = init_builtin (builtin_type, "append-reverse", 2, &append_reverse, a); + a = init_builtin (builtin_type, "core:reverse!", 2, &reverse_x_, a); + a = init_builtin (builtin_type, "pairlis", 3, &pairlis, a); + a = init_builtin (builtin_type, "assq", 2, &assq, a); + a = init_builtin (builtin_type, "assoc", 2, &assoc, a); + a = init_builtin (builtin_type, "set-car!", 2, &set_car_x, a); + a = init_builtin (builtin_type, "set-cdr!", 2, &set_cdr_x, a); + a = init_builtin (builtin_type, "set-env!", 3, &set_env_x, a); + a = init_builtin (builtin_type, "macro-get-handle", 1, ¯o_get_handle, a); + a = init_builtin (builtin_type, "add-formals", 2, &add_formals, a); + a = init_builtin (builtin_type, "eval-apply", 0, &eval_apply, a); + /* src/module.mes */ + a = init_builtin (builtin_type, "make-module-type", 0, &make_module_type, a); + a = init_builtin (builtin_type, "module-printer", 1, &module_printer, a); + a = init_builtin (builtin_type, "module-variable", 2, &module_variable, a); + a = init_builtin (builtin_type, "module-ref", 2, &module_ref, a); + a = init_builtin (builtin_type, "module-define!", 3, &module_define_x, a); + /* src/posix.mes */ + a = init_builtin (builtin_type, "peek-byte", 0, &peek_byte, a); + a = init_builtin (builtin_type, "read-byte", 0, &read_byte, a); + a = init_builtin (builtin_type, "unread-byte", 1, &unread_byte, a); + a = init_builtin (builtin_type, "peek-char", 0, &peek_char, a); + a = init_builtin (builtin_type, "read-char", -1, &read_char, a); + a = init_builtin (builtin_type, "unread-char", 1, &unread_char, a); + a = init_builtin (builtin_type, "write-char", -1, &write_char, a); + a = init_builtin (builtin_type, "write-byte", -1, &write_byte, a); + a = init_builtin (builtin_type, "getenv", 1, &getenv_, a); + a = init_builtin (builtin_type, "setenv", 2, &setenv_, a); + a = init_builtin (builtin_type, "access?", 2, &access_p, a); + a = init_builtin (builtin_type, "current-input-port", 0, ¤t_input_port, a); + a = init_builtin (builtin_type, "open-input-file", 1, &open_input_file, a); + a = init_builtin (builtin_type, "open-input-string", 1, &open_input_string, a); + a = init_builtin (builtin_type, "set-current-input-port", 1, &set_current_input_port, a); + a = init_builtin (builtin_type, "current-output-port", 0, ¤t_output_port, a); + a = init_builtin (builtin_type, "current-error-port", 0, ¤t_error_port, a); + a = init_builtin (builtin_type, "open-output-file", -1, &open_output_file, a); + a = init_builtin (builtin_type, "set-current-output-port", 1, &set_current_output_port, a); + a = init_builtin (builtin_type, "set-current-error-port", 1, &set_current_error_port, a); + a = init_builtin (builtin_type, "chmod", 2, &chmod_, a); + a = init_builtin (builtin_type, "isatty?", 1, &isatty_p, a); + a = init_builtin (builtin_type, "primitive-fork", 0, &primitive_fork, a); + a = init_builtin (builtin_type, "execl", 2, &execl_, a); + a = init_builtin (builtin_type, "core:waitpid", 2, &waitpid_, a); + a = init_builtin (builtin_type, "current-time", 0, ¤t_time, a); + a = init_builtin (builtin_type, "gettimeofday", 0, &gettimeofday_, a); + a = init_builtin (builtin_type, "get-internal-run-time", 0, &get_internal_run_time, a); + a = init_builtin (builtin_type, "getcwd", 0, &getcwd_, a); + a = init_builtin (builtin_type, "dup", 1, &dup_, a); + a = init_builtin (builtin_type, "dup2", 2, &dup2_, a); + a = init_builtin (builtin_type, "delete-file", 1, &delete_file, a); + /* src/reader.mes */ + a = init_builtin (builtin_type, "core:read-input-file-env", 2, &read_input_file_env_, a); + a = init_builtin (builtin_type, "read-input-file-env", 1, &read_input_file_env, a); + a = init_builtin (builtin_type, "read-env", 1, &read_env, a); + a = init_builtin (builtin_type, "reader-read-sexp", 3, &reader_read_sexp, a); + a = init_builtin (builtin_type, "reader-read-character", 0, &reader_read_character, a); + a = init_builtin (builtin_type, "reader-read-binary", 0, &reader_read_binary, a); + a = init_builtin (builtin_type, "reader-read-octal", 0, &reader_read_octal, a); + a = init_builtin (builtin_type, "reader-read-hex", 0, &reader_read_hex, a); + a = init_builtin (builtin_type, "reader-read-string", 0, &reader_read_string, a); + /* src/strings.mes */ + a = init_builtin (builtin_type, "string=?", 2, &string_equal_p, a); + a = init_builtin (builtin_type, "symbol->string", 1, &symbol_to_string, a); + a = init_builtin (builtin_type, "symbol->keyword", 1, &symbol_to_keyword, a); + a = init_builtin (builtin_type, "keyword->string", 1, &keyword_to_string, a); + a = init_builtin (builtin_type, "string->symbol", 1, &string_to_symbol, a); + a = init_builtin (builtin_type, "make-symbol", 1, &make_symbol, a); + a = init_builtin (builtin_type, "string->list", 1, &string_to_list, a); + a = init_builtin (builtin_type, "list->string", 1, &list_to_string, a); + a = init_builtin (builtin_type, "read-string", -1, &read_string, a); + a = init_builtin (builtin_type, "string-append", -1, &string_append, a); + a = init_builtin (builtin_type, "string-length", 1, &string_length, a); + a = init_builtin (builtin_type, "string-ref", 2, &string_ref, a); + /* src/struct.mes */ + a = init_builtin (builtin_type, "make-struct", 3, &make_struct, a); + a = init_builtin (builtin_type, "struct-length", 1, &struct_length, a); + a = init_builtin (builtin_type, "struct-ref", 2, &struct_ref, a); + a = init_builtin (builtin_type, "struct-set!", 3, &struct_set_x, a); + /* src/vector.mes */ + a = init_builtin (builtin_type, "core:make-vector", 1, &make_vector_, a); + a = init_builtin (builtin_type, "vector-length", 1, &vector_length, a); + a = init_builtin (builtin_type, "vector-ref", 2, &vector_ref, a); + a = init_builtin (builtin_type, "vector-entry", 1, &vector_entry, a); + a = init_builtin (builtin_type, "vector-set!", 3, &vector_set_x, a); + a = init_builtin (builtin_type, "list->vector", 1, &list_to_vector, a); + a = init_builtin (builtin_type, "vector->list", 1, &vector_to_list, a); + return a; } diff --git a/src/mes.c b/src/mes.c index d8a4c78e..ee961111 100644 --- a/src/mes.c +++ b/src/mes.c @@ -23,36 +23,16 @@ #include #include +#include #include #include #include -char g_datadir[1024]; -int g_debug; -char *g_buf; -SCM g_continuations; -SCM g_symbols; -SCM g_symbol_max; - -// a/env -SCM r0; -// param 1 -SCM r1; -// save 2 -SCM r2; -// continuation -SCM r3; -// current-module -SCM m0; -// macro -SCM g_macros; -SCM g_ports; - SCM alloc (long n) { SCM x = g_free; - g_free += n; + g_free = g_free + n; if (g_free > ARENA_SIZE) assert (!"alloc: out of memory"); return x; @@ -74,16 +54,28 @@ make_cell_ (SCM type, SCM car, SCM cdr) assert (TYPE (type) == TNUMBER); long t = VALUE (type); if (t == TCHAR || t == TNUMBER) - return make_cell__ (t, car ? CAR (car) : 0, cdr ? CDR (cdr) : 0); + { + if (car != 0) + car = CAR (car); + else + car = 0; + if (cdr != 0) + cdr = CDR (cdr); + else + cdr = 0; + return make_cell__ (t, car, cdr); + } return make_cell__ (t, car, cdr); } SCM -assoc_string (SCM x, SCM a) ///((internal)) +assoc_string (SCM x, SCM a) /*:((internal)) */ { while (a != cell_nil && (TYPE (CAAR (a)) != TSTRING || string_equal_p (x, CAAR (a)) == cell_f)) a = CDR (a); - return a != cell_nil ? CAR (a) : cell_f; + if (a != cell_nil) + return CAR (a); + return cell_f; } SCM @@ -119,23 +111,25 @@ type_ (SCM x) SCM car_ (SCM x) { - return (TYPE (x) != TCONTINUATION && (TYPE (CAR (x)) == TPAIR // FIXME: this is weird - || TYPE (CAR (x)) == TREF - || TYPE (CAR (x)) == TSPECIAL - || TYPE (CAR (x)) == TSYMBOL - || TYPE (CAR (x)) == TSTRING)) ? CAR (x) : MAKE_NUMBER (CAR (x)); + if (TYPE (x) != TCONTINUATION && (TYPE (CAR (x)) == TPAIR // FIXME: this is weird + || TYPE (CAR (x)) == TREF + || TYPE (CAR (x)) == TSPECIAL + || TYPE (CAR (x)) == TSYMBOL || TYPE (CAR (x)) == TSTRING)) + return CAR (x); + return MAKE_NUMBER (CAR (x)); } SCM cdr_ (SCM x) { - return (TYPE (x) != TCHAR - && TYPE (x) != TNUMBER - && TYPE (x) != TPORT - && (TYPE (CDR (x)) == TPAIR - || TYPE (CDR (x)) == TREF - || TYPE (CDR (x)) == TSPECIAL - || TYPE (CDR (x)) == TSYMBOL || TYPE (CDR (x)) == TSTRING)) ? CDR (x) : MAKE_NUMBER (CDR (x)); + if (TYPE (x) != TCHAR + && TYPE (x) != TNUMBER + && TYPE (x) != TPORT + && (TYPE (CDR (x)) == TPAIR + || TYPE (CDR (x)) == TREF + || TYPE (CDR (x)) == TSPECIAL || TYPE (CDR (x)) == TSYMBOL || TYPE (CDR (x)) == TSTRING)) + return CDR (x); + return MAKE_NUMBER (CDR (x)); } SCM @@ -165,7 +159,7 @@ cdr (SCM x) } SCM -list (SCM x) ///((arity . n)) +list (SCM x) /*:((arity . n)) */ { return x; } @@ -173,22 +167,26 @@ list (SCM x) ///((arity . n)) SCM null_p (SCM x) { - return x == cell_nil ? cell_t : cell_f; + if (x == cell_nil) + return cell_t; + return cell_f; } SCM eq_p (SCM x, SCM y) { - return (x == y - || ((TYPE (x) == TKEYWORD && TYPE (y) == TKEYWORD - && string_equal_p (x, y) == cell_t)) - || (TYPE (x) == TCHAR && TYPE (y) == TCHAR - && VALUE (x) == VALUE (y)) - || (TYPE (x) == TNUMBER && TYPE (y) == TNUMBER && VALUE (x) == VALUE (y))) ? cell_t : cell_f; + if (x == y + || ((TYPE (x) == TKEYWORD && TYPE (y) == TKEYWORD + && string_equal_p (x, y) == cell_t)) + || (TYPE (x) == TCHAR && TYPE (y) == TCHAR + && VALUE (x) == VALUE (y)) + || (TYPE (x) == TNUMBER && TYPE (y) == TNUMBER && VALUE (x) == VALUE (y))) + return cell_t; + return cell_f; } SCM -values (SCM x) ///((arity . n)) +values (SCM x) /*:((arity . n)) */ { SCM v = cons (0, x); TYPE (v) = TVALUES; @@ -202,12 +200,12 @@ acons (SCM key, SCM value, SCM alist) } long -length__ (SCM x) ///((internal)) +length__ (SCM x) /*:((internal)) */ { long n = 0; while (x != cell_nil) { - n++; + n = n + 1; if (TYPE (x) != TPAIR) return -1; x = CDR (x); @@ -237,9 +235,9 @@ error (SCM key, SCM x) exit (1); } -// extra lib +/* extra lib */ SCM -assert_defined (SCM x, SCM e) ///((internal)) +assert_defined (SCM x, SCM e) /*:((internal)) */ { if (e == cell_undefined) return error (cell_symbol_unbound_variable, x); @@ -247,9 +245,13 @@ assert_defined (SCM x, SCM e) ///((internal)) } SCM -check_formals (SCM f, SCM formals, SCM args) ///((internal)) +check_formals (SCM f, SCM formals, SCM args) /*:((internal)) */ { - long flen = (TYPE (formals) == TNUMBER) ? VALUE (formals) : length__ (formals); + long flen; + if (TYPE (formals) == TNUMBER) + flen = VALUE (formals); + else + flen = length__ (formals); long alen = length__ (args); if (alen != flen && alen != -1 && flen != -1) { @@ -267,7 +269,7 @@ check_formals (SCM f, SCM formals, SCM args) ///((internal)) } SCM -check_apply (SCM f, SCM e) ///((internal)) +check_apply (SCM f, SCM e) /*:((internal)) */ { char *type = 0; if (f == cell_f || f == cell_t) @@ -289,7 +291,7 @@ check_apply (SCM f, SCM e) ///((internal)) if (TYPE (f) == TBROKEN_HEART) type = "<3"; - if (type) + if (type != 0) { char *s = "cannot apply: "; eputs (s); @@ -384,7 +386,9 @@ assq (SCM x, SCM a) /* pointer equality, e.g. on strings. */ while (a != cell_nil && x != CAAR (a)) a = CDR (a); - return a != cell_nil ? CAR (a) : cell_f; + if (a != cell_nil) + return CAR (a); + return cell_f; } SCM @@ -394,7 +398,9 @@ assoc (SCM x, SCM a) return assoc_string (x, a); while (a != cell_nil && equal2_p (x, CAAR (a)) == cell_f) a = CDR (a); - return a != cell_nil ? CAR (a) : cell_f; + if (a != cell_nil) + return CAR (a); + return cell_f; } SCM @@ -429,7 +435,7 @@ set_env_x (SCM x, SCM e, SCM a) } SCM -call_lambda (SCM e, SCM x, SCM aa, SCM a) ///((internal)) +call_lambda (SCM e, SCM x, SCM aa, SCM a) /*:((internal)) */ { SCM cl = cons (cons (cell_closure, x), x); r1 = e; @@ -438,13 +444,13 @@ call_lambda (SCM e, SCM x, SCM aa, SCM a) ///((internal)) } SCM -make_closure_ (SCM args, SCM body, SCM a) ///((internal)) +make_closure_ (SCM args, SCM body, SCM a) /*:((internal)) */ { return make_cell__ (TCLOSURE, cell_f, cons (cons (cell_circular, a), cons (args, body))); } SCM -make_variable_ (SCM var) ///((internal)) +make_variable_ (SCM var) /*:((internal)) */ { return make_cell__ (TVARIABLE, var, 0); } @@ -458,7 +464,7 @@ macro_get_handle (SCM name) } SCM -get_macro (SCM name) ///((internal)) +get_macro (SCM name) /*:((internal)) */ { SCM m = macro_get_handle (name); if (m != cell_f) @@ -467,13 +473,13 @@ get_macro (SCM name) ///((internal)) } SCM -macro_set_x (SCM name, SCM value) ///((internal)) +macro_set_x (SCM name, SCM value) /*:((internal)) */ { return hashq_set_x (g_macros, name, value); } SCM -push_cc (SCM p1, SCM p2, SCM a, SCM c) ///((internal)) +push_cc (SCM p1, SCM p2, SCM a, SCM c) /*:((internal)) */ { SCM x = r3; r3 = c; @@ -499,7 +505,7 @@ add_formals (SCM formals, SCM x) } int -formal_p (SCM x, SCM formals) /// ((internal)) +formal_p (SCM x, SCM formals) /*:((internal)) */ { if (TYPE (formals) == TSYMBOL) { @@ -516,7 +522,7 @@ formal_p (SCM x, SCM formals) /// ((internal)) } SCM -expand_variable_ (SCM x, SCM formals, int top_p) ///((internal)) +expand_variable_ (SCM x, SCM formals, int top_p) /*:((internal)) */ { while (TYPE (x) == TPAIR) { @@ -570,7 +576,7 @@ expand_variable_ (SCM x, SCM formals, int top_p) ///((internal)) } SCM -expand_variable (SCM x, SCM formals) ///((internal)) +expand_variable (SCM x, SCM formals) /*:((internal)) */ { return expand_variable_ (x, formals, 1); } @@ -691,7 +697,7 @@ apply: if (t == TSTRUCT && builtin_p (CAR (r1)) == cell_t) { check_formals (CAR (r1), builtin_arity (CAR (r1)), CDR (r1)); - r1 = apply_builtin (CAR (r1), CDR (r1)); /// FIXME: move into eval_apply + r1 = apply_builtin (CAR (r1), CDR (r1)); goto vm_return; } else if (t == TCLOSURE) @@ -710,9 +716,9 @@ apply: else if (t == TCONTINUATION) { v = CONTINUATION (CAR (r1)); - if (LENGTH (v)) + if (LENGTH (v) != 0) { - for (t = 0; t < LENGTH (v); t++) + for (t = 0; t < LENGTH (v); t = t + 1) g_stack_array[STACK_SIZE - LENGTH (v) + t] = vector_ref_ (v, t); g_stack = STACK_SIZE - LENGTH (v); } @@ -855,12 +861,12 @@ eval: { global_p = CAAR (r0) != cell_closure; macro_p = CAR (r1) == cell_symbol_define_macro; - if (global_p) + if (global_p != 0) { name = CADR (r1); if (TYPE (CADR (r1)) == TPAIR) name = CAR (name); - if (macro_p) + if (macro_p != 0) { entry = assq (name, g_macros); if (entry == cell_f) @@ -891,17 +897,17 @@ eval: push_cc (r1, r2, p, cell_vm_eval_define); goto eval; } - eval_define:; + eval_define: name = CADR (r2); if (TYPE (CADR (r2)) == TPAIR) name = CAR (name); - if (macro_p) + if (macro_p != 0) { entry = macro_get_handle (name); r1 = MAKE_MACRO (name, r1); set_cdr_x (entry, r1); } - else if (global_p) + else if (global_p != 0) { entry = module_variable (r0, name); set_cdr_x (entry, r1); @@ -1094,7 +1100,7 @@ begin_expand: goto eval; // FIXME: expand too?! begin_expand_primitive_load: if (TYPE (r1) == TNUMBER && VALUE (r1) == 0) - ; + 0; else if (TYPE (r1) == TSTRING) input = set_current_input_port (open_input_file (r1)); else if (TYPE (r1) == TPORT) @@ -1158,9 +1164,10 @@ if_expr: call_with_current_continuation: gc_push_frame (); - x = MAKE_CONTINUATION (g_continuations++); + x = MAKE_CONTINUATION (g_continuations); + g_continuations = g_continuations + 1; v = make_vector__ (STACK_SIZE - g_stack); - for (t = g_stack; t < STACK_SIZE; t++) + for (t = g_stack; t < STACK_SIZE; t = t + 1) vector_set_x_ (v, t - g_stack, g_stack_array[t]); CONTINUATION (x) = v; gc_pop_frame (); @@ -1168,7 +1175,7 @@ call_with_current_continuation: goto apply; call_with_current_continuation2: v = make_vector__ (STACK_SIZE - g_stack); - for (t = g_stack; t < STACK_SIZE; t++) + for (t = g_stack; t < STACK_SIZE; t = t + 1) vector_set_x_ (v, t - g_stack, g_stack_array[t]); CONTINUATION (r2) = v; goto vm_return; @@ -1190,7 +1197,7 @@ vm_return: } SCM -apply (SCM f, SCM x, SCM a) ///((internal)) +apply (SCM f, SCM x, SCM a) /*:((internal)) */ { push_cc (cons (f, x), cell_unspecified, r0, cell_unspecified); r3 = cell_vm_apply; @@ -1198,9 +1205,8 @@ apply (SCM f, SCM x, SCM a) ///((internal)) } SCM -mes_g_stack (SCM a) ///((internal)) +mes_g_stack (SCM a) /*:((internal)) */ { - //g_stack = g_free + ARENA_SIZE; g_stack = STACK_SIZE; r0 = a; r1 = MAKE_CHAR (0); @@ -1221,12 +1227,17 @@ init_symbol (long x, long type, char const *name) } SCM -mes_symbols () ///((internal)) +mes_symbols () /*:((internal)) */ { g_free = cell_symbol_test + 1; g_symbol_max = g_free; g_symbols = make_hash_table_ (500); + int size = VALUE (struct_ref_ (g_symbols, 3)); + // Weird: m2-planet exits 67 here...[printing size = 100] + // if (size == 0) exit (66); + // if (!size) exit (67); + init_symbol (cell_nil, TSPECIAL, "()"); init_symbol (cell_f, TSPECIAL, "#f"); init_symbol (cell_t, TSPECIAL, "#t"); @@ -1393,7 +1404,7 @@ mes_symbols () ///((internal)) } SCM -mes_environment (int argc, char *argv[]) +mes_environment (int argc, char **argv) { SCM a = mes_symbols (); @@ -1419,7 +1430,8 @@ mes_environment (int argc, char *argv[]) #if !MES_MINI SCM lst = cell_nil; - for (int i = argc - 1; i >= 0; i--) + int i; + for (i = argc - 1; i >= 0; i = i - 1) lst = cons (MAKE_STRING0 (argv[i]), lst); a = acons (cell_symbol_argv, lst, a); #endif @@ -1428,7 +1440,7 @@ mes_environment (int argc, char *argv[]) } SCM -apply_builtin (SCM fn, SCM x) ///((internal)) +apply_builtin (SCM fn, SCM x) /*:((internal)) */ { int arity = VALUE (builtin_arity (fn)); if ((arity > 0 || arity == -1) && x != cell_nil && TYPE (CAR (x)) == TVALUES) @@ -1437,7 +1449,8 @@ apply_builtin (SCM fn, SCM x) ///((internal)) x = cons (CAR (x), cons (CDADAR (x), CDR (x))); #if __M2_PLANET__ - FUNCTION fp = builtin_function (fn) if (arity == 0) + FUNCTION fp = builtin_function (fn); + if (arity == 0) return fp (); else if (arity == 1) return fp (CAR (x)); @@ -1450,31 +1463,26 @@ apply_builtin (SCM fn, SCM x) ///((internal)) #else // !__M2_PLANET__ if (arity == 0) { - //function0_t fp = f->function; SCM (*fp) (void) = (function0_t) builtin_function (fn); return fp (); } else if (arity == 1) { - //function1_t fp = f->function; SCM (*fp) (SCM) = (function1_t) builtin_function (fn); return fp (CAR (x)); } else if (arity == 2) { - //function2_t fp = f->function; SCM (*fp) (SCM, SCM) = (function2_t) builtin_function (fn); return fp (CAR (x), CADR (x)); } else if (arity == 3) { - //function3_t fp = f->function; SCM (*fp) (SCM, SCM, SCM) = (function3_t) builtin_function (fn); return fp (CAR (x), CADR (x), CAR (CDDR (x))); } else if (arity == -1) { - //functionn_t fp = f->function; SCM (*fp) (SCM) = (function1_t) builtin_function (fn); return fp (x); } @@ -1495,7 +1503,7 @@ try_open_boot (char *file_name, char const *boot, char const *location) eputs ("\n"); } int fd = mes_open (file_name, O_RDONLY, 0); - if (g_debug && fd > 0) + if (g_debug != 0 && fd > 0) { eputs ("mes: read boot-0: "); eputs (file_name); @@ -1508,8 +1516,8 @@ void open_boot () { __stdin = -1; - char boot[1024]; - char file_name[1024]; + char *boot = __open_boot_buf; + char *file_name = __open_boot_file_name; strcpy (g_datadir, "."); if (getenv ("MES_BOOT")) strcpy (boot, getenv ("MES_BOOT")); @@ -1556,7 +1564,7 @@ open_boot () } SCM -read_boot () ///((internal)) +read_boot () /*:((internal)) */ { r2 = read_input_file_env (r0); __stdin = STDIN; @@ -1564,8 +1572,17 @@ read_boot () ///((internal)) } void -init () +init (char **envp) { + environ = envp; + __execl_c_argv = malloc (1024 * sizeof (char *)); /* POSIX minimum: 4096 */ + __getcwd_buf = malloc (PATH_MAX); + __open_boot_buf = malloc (PATH_MAX); + __open_boot_file_name = malloc (PATH_MAX); + __reader_read_char_buf = malloc (10); + __setenv_buf = malloc (1024); + g_datadir = malloc (1024); + char *p; if (p = getenv ("MES_DEBUG")) g_debug = atoi (p); @@ -1575,9 +1592,9 @@ init () } int -main (int argc, char *argv[]) +main (int argc, char **argv, char **envp) { - init (); + init (envp); SCM a = mes_environment (argc, argv); a = mes_builtins (a); @@ -1605,12 +1622,12 @@ main (int argc, char *argv[]) } r3 = cell_vm_begin_expand; r1 = eval_apply (); - if (g_debug) + if (g_debug != 0) { write_error_ (r1); eputs ("\n"); } - if (g_debug) + if (g_debug != 0) { if (g_debug > 5) module_printer (m0);