ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/microscheme/scheme.c
(Generate patch)

Comparing cvsroot/microscheme/scheme.c (file contents):
Revision 1.47 by root, Mon Nov 30 09:25:19 2015 UTC vs.
Revision 1.55 by root, Tue Dec 1 03:03:11 2015 UTC

18 * 18 *
19 */ 19 */
20 20
21#define EXPERIMENT 1 21#define EXPERIMENT 1
22 22
23#if 1
23#define PAGE_SIZE 4096 /* does not work on sparc/alpha */ 24#define PAGE_SIZE 4096 /* does not work on sparc/alpha */
24#include "malloc.c" 25#include "malloc.c"
26#endif
25 27
26#define SCHEME_SOURCE 28#define SCHEME_SOURCE
27#include "scheme-private.h" 29#include "scheme-private.h"
28#ifndef WIN32 30#ifndef WIN32
29# include <unistd.h> 31# include <unistd.h>
79 81
80#define BACKQUOTE '`' 82#define BACKQUOTE '`'
81#define WHITESPACE " \t\r\n\v\f" 83#define WHITESPACE " \t\r\n\v\f"
82#define DELIMITERS "()\";" WHITESPACE 84#define DELIMITERS "()\";" WHITESPACE
83 85
84#define NIL (&SCHEME_V->xNIL) //TODO: make this 0? 86#define NIL POINTER (&SCHEME_V->xNIL)
85#define S_T (&SCHEME_V->xT) //TODO: magic ptr value? 87#define S_T POINTER (&SCHEME_V->xT)
86#define S_F (&SCHEME_V->xF) //TODO: magic ptr value? 88#define S_F POINTER (&SCHEME_V->xF)
87#define S_SINK (&SCHEME_V->xsink) 89#define S_SINK POINTER (&SCHEME_V->xsink)
88#define S_EOF (&SCHEME_V->xEOF_OBJ) 90#define S_EOF POINTER (&SCHEME_V->xEOF_OBJ)
89 91
90#if !USE_MULTIPLICITY 92#if !USE_MULTIPLICITY
91static scheme sc; 93static scheme sc;
92#endif 94#endif
93 95
101 } 103 }
102 104
103 char *p = s; 105 char *p = s;
104 106
105 do { 107 do {
106 *p++ = '0' + n % base; 108 *p++ = "0123456789abcdef"[n % base];
107 n /= base; 109 n /= base;
108 } while (n); 110 } while (n);
109 111
110 *p-- = 0; 112 *p-- = 0;
111 113
121{ 123{
122 xbase (s, n, 10); 124 xbase (s, n, 10);
123} 125}
124 126
125static void 127static void
126xwrstr (const char *s) 128putnum (SCHEME_P_ long n)
127{
128 write (1, s, strlen (s));
129}
130
131static void
132xwrnum (long n)
133{ 129{
134 char buf[64]; 130 char buf[64];
135 131
136 xnum (buf, n); 132 xnum (buf, n);
137 xwrstr (buf); 133 putstr (SCHEME_A_ buf);
138} 134}
139 135
140static char 136static char
141xtoupper (char c) 137xtoupper (char c)
142{ 138{
192# define prompt "ts> " 188# define prompt "ts> "
193#endif 189#endif
194 190
195#ifndef InitFile 191#ifndef InitFile
196# define InitFile "init.scm" 192# define InitFile "init.scm"
197#endif
198
199#ifndef FIRST_CELLSEGS
200# define FIRST_CELLSEGS 3
201#endif 193#endif
202 194
203enum scheme_types 195enum scheme_types
204{ 196{
205 T_INTEGER, 197 T_INTEGER,
258static num num_op (enum num_op op, num a, num b); 250static num num_op (enum num_op op, num a, num b);
259static num num_intdiv (num a, num b); 251static num num_intdiv (num a, num b);
260static num num_rem (num a, num b); 252static num num_rem (num a, num b);
261static num num_mod (num a, num b); 253static num num_mod (num a, num b);
262 254
263#if USE_MATH
264static double round_per_R5RS (double x);
265#endif
266static int is_zero_rvalue (RVALUE x); 255static int is_zero_rvalue (RVALUE x);
267 256
268static num num_zero; 257static num num_zero;
269static num num_one; 258static num num_one;
270 259
260/* convert "pointer" to cell* / cell* to pointer */
261#define CELL(p) ((struct cell *)(p) + 0)
262#define POINTER(c) ((void *)((c) - 0))
263
271/* macros for cell operations */ 264/* macros for cell operations */
272#define typeflag(p) ((p)->flag + 0) 265#define typeflag(p) (CELL(p)->flag + 0)
273#define set_typeflag(p,v) ((p)->flag = (v)) 266#define set_typeflag(p,v) (CELL(p)->flag = (v))
274#define type(p) (typeflag (p) & T_MASKTYPE) 267#define type(p) (typeflag (p) & T_MASKTYPE)
275 268
276INTERFACE int 269INTERFACE int
277is_string (pointer p) 270is_string (pointer p)
278{ 271{
279 return type (p) == T_STRING; 272 return type (p) == T_STRING;
280} 273}
281 274
282#define strvalue(p) ((p)->object.string.svalue) 275#define strvalue(p) (CELL(p)->object.string.svalue)
283#define strlength(p) ((p)->object.string.length) 276#define strlength(p) (CELL(p)->object.string.length)
284 277
285INTERFACE int 278INTERFACE int
286is_vector (pointer p) 279is_vector (pointer p)
287{ 280{
288 return type (p) == T_VECTOR; 281 return type (p) == T_VECTOR;
289} 282}
290 283
291#define vecvalue(p) ((p)->object.vector.vvalue) 284#define vecvalue(p) (CELL(p)->object.vector.vvalue)
292#define veclength(p) ((p)->object.vector.length) 285#define veclength(p) (CELL(p)->object.vector.length)
293INTERFACE void fill_vector (pointer vec, uint32_t start, pointer obj); 286INTERFACE void fill_vector (pointer vec, uint32_t start, pointer obj);
294INTERFACE pointer vector_get (pointer vec, uint32_t ielem); 287INTERFACE pointer vector_get (pointer vec, uint32_t ielem);
295INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a); 288INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a);
296 289
297INTERFACE int 290INTERFACE int
323string_value (pointer p) 316string_value (pointer p)
324{ 317{
325 return strvalue (p); 318 return strvalue (p);
326} 319}
327 320
328#define ivalue_unchecked(p) (p)->object.ivalue 321#define ivalue_unchecked(p) CELL(p)->object.ivalue
329#define set_ivalue(p,v) (p)->object.ivalue = (v) 322#define set_ivalue(p,v) CELL(p)->object.ivalue = (v)
330 323
331#if USE_REAL 324#if USE_REAL
332#define rvalue_unchecked(p) (p)->object.rvalue 325#define rvalue_unchecked(p) CELL(p)->object.rvalue
333#define set_rvalue(p,v) (p)->object.rvalue = (v) 326#define set_rvalue(p,v) CELL(p)->object.rvalue = (v)
334#else 327#else
335#define rvalue_unchecked(p) (p)->object.ivalue 328#define rvalue_unchecked(p) CELL(p)->object.ivalue
336#define set_rvalue(p,v) (p)->object.ivalue = (v) 329#define set_rvalue(p,v) CELL(p)->object.ivalue = (v)
337#endif 330#endif
338 331
339INTERFACE long 332INTERFACE long
340charvalue (pointer p) 333charvalue (pointer p)
341{ 334{
342 return ivalue_unchecked (p); 335 return ivalue_unchecked (p);
343} 336}
344 337
338#define port(p) CELL(p)->object.port
339#define set_port(p,v) port(p) = (v)
345INTERFACE int 340INTERFACE int
346is_port (pointer p) 341is_port (pointer p)
347{ 342{
348 return type (p) == T_PORT; 343 return type (p) == T_PORT;
349} 344}
350 345
351INTERFACE int 346INTERFACE int
352is_inport (pointer p) 347is_inport (pointer p)
353{ 348{
354 return is_port (p) && p->object.port->kind & port_input; 349 return is_port (p) && port (p)->kind & port_input;
355} 350}
356 351
357INTERFACE int 352INTERFACE int
358is_outport (pointer p) 353is_outport (pointer p)
359{ 354{
360 return is_port (p) && p->object.port->kind & port_output; 355 return is_port (p) && port (p)->kind & port_output;
361} 356}
362 357
363INTERFACE int 358INTERFACE int
364is_pair (pointer p) 359is_pair (pointer p)
365{ 360{
366 return type (p) == T_PAIR; 361 return type (p) == T_PAIR;
367} 362}
368 363
369#define car(p) ((p)->object.cons.car + 0) 364#define car(p) (POINTER (CELL(p)->object.cons.car))
370#define cdr(p) ((p)->object.cons.cdr + 0) 365#define cdr(p) (POINTER (CELL(p)->object.cons.cdr))
371 366
372static pointer caar (pointer p) { return car (car (p)); } 367static pointer caar (pointer p) { return car (car (p)); }
373static pointer cadr (pointer p) { return car (cdr (p)); } 368static pointer cadr (pointer p) { return car (cdr (p)); }
374static pointer cdar (pointer p) { return cdr (car (p)); } 369static pointer cdar (pointer p) { return cdr (car (p)); }
375static pointer cddr (pointer p) { return cdr (cdr (p)); } 370static pointer cddr (pointer p) { return cdr (cdr (p)); }
379static pointer cdaar (pointer p) { return cdr (car (car (p))); } 374static pointer cdaar (pointer p) { return cdr (car (car (p))); }
380 375
381INTERFACE void 376INTERFACE void
382set_car (pointer p, pointer q) 377set_car (pointer p, pointer q)
383{ 378{
384 p->object.cons.car = q; 379 CELL(p)->object.cons.car = CELL (q);
385} 380}
386 381
387INTERFACE void 382INTERFACE void
388set_cdr (pointer p, pointer q) 383set_cdr (pointer p, pointer q)
389{ 384{
390 p->object.cons.cdr = q; 385 CELL(p)->object.cons.cdr = CELL (q);
391} 386}
392 387
393INTERFACE pointer 388INTERFACE pointer
394pair_car (pointer p) 389pair_car (pointer p)
395{ 390{
670 665
671static int file_push (SCHEME_P_ const char *fname); 666static int file_push (SCHEME_P_ const char *fname);
672static void file_pop (SCHEME_P); 667static void file_pop (SCHEME_P);
673static int file_interactive (SCHEME_P); 668static int file_interactive (SCHEME_P);
674ecb_inline int is_one_of (const char *s, int c); 669ecb_inline int is_one_of (const char *s, int c);
675static int alloc_cellseg (SCHEME_P_ int n); 670static int alloc_cellseg (SCHEME_P);
676ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); 671ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b);
677static void finalize_cell (SCHEME_P_ pointer a); 672static void finalize_cell (SCHEME_P_ pointer a);
678static int count_consecutive_cells (pointer x, int needed); 673static int count_consecutive_cells (pointer x, int needed);
679static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); 674static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all);
680static pointer mk_number (SCHEME_P_ const num n); 675static pointer mk_number (SCHEME_P_ const num n);
874 } 869 }
875 870
876 return ret; 871 return ret;
877} 872}
878 873
879#if USE_MATH
880
881/* Round to nearest. Round to even if midway */
882static double
883round_per_R5RS (double x)
884{
885 double fl = floor (x);
886 double ce = ceil (x);
887 double dfl = x - fl;
888 double dce = ce - x;
889
890 if (dfl > dce)
891 return ce;
892 else if (dfl < dce)
893 return fl;
894 else
895 {
896 if (fmod (fl, 2) == 0) /* I imagine this holds */
897 return fl;
898 else
899 return ce;
900 }
901}
902#endif
903
904static int 874static int
905is_zero_rvalue (RVALUE x) 875is_zero_rvalue (RVALUE x)
906{ 876{
907 return x == 0; 877 return x == 0;
908#if 0 878#if 0
914#endif 884#endif
915} 885}
916 886
917/* allocate new cell segment */ 887/* allocate new cell segment */
918static int 888static int
919alloc_cellseg (SCHEME_P_ int n) 889alloc_cellseg (SCHEME_P)
920{ 890{
921 pointer newp; 891 struct cell *newp;
922 pointer last; 892 struct cell *last;
923 pointer p; 893 struct cell *p;
924 char *cp; 894 char *cp;
925 long i; 895 long i;
926 int k; 896 int k;
927 897
928 static int segsize = CELL_SEGSIZE >> 1; 898 static int segsize = CELL_SEGSIZE >> 1;
929 segsize <<= 1; 899 segsize <<= 1;
930 900
931 for (k = 0; k < n; k++)
932 {
933 if (SCHEME_V->last_cell_seg >= CELL_NSEGMENT - 1)
934 return k;
935
936 cp = malloc (segsize * sizeof (struct cell)); 901 cp = malloc (segsize * sizeof (struct cell));
937 902
938 if (!cp && USE_ERROR_CHECKING) 903 if (!cp && USE_ERROR_CHECKING)
939 return k; 904 return k;
940 905
941 i = ++SCHEME_V->last_cell_seg; 906 i = ++SCHEME_V->last_cell_seg;
942 SCHEME_V->alloc_seg[i] = cp; 907 SCHEME_V->alloc_seg[i] = cp;
943 908
944 newp = (pointer)cp; 909 newp = (struct cell *)cp;
945 SCHEME_V->cell_seg[i] = newp; 910 SCHEME_V->cell_seg[i] = newp;
946 SCHEME_V->cell_segsize[i] = segsize; 911 SCHEME_V->cell_segsize[i] = segsize;
947 SCHEME_V->fcells += segsize; 912 SCHEME_V->fcells += segsize;
948 last = newp + segsize - 1; 913 last = newp + segsize - 1;
949 914
950 for (p = newp; p <= last; p++) 915 for (p = newp; p <= last; p++)
951 { 916 {
917 pointer cp = POINTER (p);
952 set_typeflag (p, T_PAIR); 918 set_typeflag (cp, T_PAIR);
953 set_car (p, NIL); 919 set_car (cp, NIL);
954 set_cdr (p, p + 1); 920 set_cdr (cp, POINTER (p + 1));
955 } 921 }
956 922
957 set_cdr (last, SCHEME_V->free_cell); 923 set_cdr (POINTER (last), SCHEME_V->free_cell);
958 SCHEME_V->free_cell = newp; 924 SCHEME_V->free_cell = POINTER (newp);
959 }
960 925
961 return n; 926 return 1;
962} 927}
963 928
964/* get new cell. parameter a, b is marked by gc. */ 929/* get new cell. parameter a, b is marked by gc. */
965ecb_inline pointer 930ecb_inline pointer
966get_cell_x (SCHEME_P_ pointer a, pointer b) 931get_cell_x (SCHEME_P_ pointer a, pointer b)
977 gc (SCHEME_A_ a, b); 942 gc (SCHEME_A_ a, b);
978 943
979 if (SCHEME_V->fcells < min_to_be_recovered || SCHEME_V->free_cell == NIL) 944 if (SCHEME_V->fcells < min_to_be_recovered || SCHEME_V->free_cell == NIL)
980 { 945 {
981 /* if only a few recovered, get more to avoid fruitless gc's */ 946 /* if only a few recovered, get more to avoid fruitless gc's */
982 if (!alloc_cellseg (SCHEME_A_ 1) && SCHEME_V->free_cell == NIL) 947 if (!alloc_cellseg (SCHEME_A) && SCHEME_V->free_cell == NIL)
983 { 948 {
984#if USE_ERROR_CHECKING 949#if USE_ERROR_CHECKING
985 SCHEME_V->no_memory = 1; 950 SCHEME_V->no_memory = 1;
986 return S_SINK; 951 return S_SINK;
987#endif 952#endif
999 } 964 }
1000} 965}
1001 966
1002/* To retain recent allocs before interpreter knows about them - 967/* To retain recent allocs before interpreter knows about them -
1003 Tehom */ 968 Tehom */
1004
1005static void 969static void
1006push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) 970push_recent_alloc (SCHEME_P_ pointer recent, pointer extra)
1007{ 971{
1008 pointer holder = get_cell_x (SCHEME_A_ recent, extra); 972 pointer holder = get_cell_x (SCHEME_A_ recent, extra);
1009 973
1040 return S_SINK; 1004 return S_SINK;
1041 1005
1042 /* Record it as a vector so that gc understands it. */ 1006 /* Record it as a vector so that gc understands it. */
1043 set_typeflag (v, T_VECTOR | T_ATOM); 1007 set_typeflag (v, T_VECTOR | T_ATOM);
1044 1008
1045 v->object.vector.vvalue = e; 1009 CELL(v)->object.vector.vvalue = e;
1046 v->object.vector.length = len; 1010 CELL(v)->object.vector.length = len;
1047 fill_vector (v, 0, init); 1011 fill_vector (v, 0, init);
1048 push_recent_alloc (SCHEME_A_ v, NIL); 1012 push_recent_alloc (SCHEME_A_ v, NIL);
1049 1013
1050 return v; 1014 return v;
1051} 1015}
1060static void 1024static void
1061check_cell_alloced (pointer p, int expect_alloced) 1025check_cell_alloced (pointer p, int expect_alloced)
1062{ 1026{
1063 /* Can't use putstr(SCHEME_A_ str) because callers have no access to sc. */ 1027 /* Can't use putstr(SCHEME_A_ str) because callers have no access to sc. */
1064 if (typeflag (p) & !expect_alloced) 1028 if (typeflag (p) & !expect_alloced)
1065 xwrstr ("Cell is already allocated!\n"); 1029 putstr (SCHEME_A_ "Cell is already allocated!\n");
1066 1030
1067 if (!(typeflag (p)) & expect_alloced) 1031 if (!(typeflag (p)) & expect_alloced)
1068 xwrstr ("Cell is not allocated!\n"); 1032 putstr (SCHEME_A_ "Cell is not allocated!\n");
1069} 1033}
1070 1034
1071static void 1035static void
1072check_range_alloced (pointer p, int n, int expect_alloced) 1036check_range_alloced (pointer p, int n, int expect_alloced)
1073{ 1037{
1201 1165
1202/* returns the new symbol */ 1166/* returns the new symbol */
1203static pointer 1167static pointer
1204oblist_add_by_name (SCHEME_P_ const char *name) 1168oblist_add_by_name (SCHEME_P_ const char *name)
1205{ 1169{
1206 pointer x = mk_string (SCHEME_A_ name); 1170 pointer x = generate_symbol (SCHEME_A_ name);
1207 set_typeflag (x, T_SYMBOL);
1208 setimmutable (x);
1209 SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); 1171 SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist);
1210 return x; 1172 return x;
1211} 1173}
1212 1174
1213static pointer 1175static pointer
1223mk_port (SCHEME_P_ port *p) 1185mk_port (SCHEME_P_ port *p)
1224{ 1186{
1225 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1187 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1226 1188
1227 set_typeflag (x, T_PORT | T_ATOM); 1189 set_typeflag (x, T_PORT | T_ATOM);
1228 x->object.port = p; 1190 set_port (x, p);
1229 1191
1230 return x; 1192 return x;
1231} 1193}
1232#endif 1194#endif
1233 1195
1234pointer 1196pointer
1235mk_foreign_func (SCHEME_P_ foreign_func f) 1197mk_foreign_func (SCHEME_P_ foreign_func f)
1236{ 1198{
1237 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1199 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1238 1200
1239 set_typeflag (x, (T_FOREIGN | T_ATOM)); 1201 set_typeflag (x, T_FOREIGN | T_ATOM);
1240 x->object.ff = f; 1202 CELL(x)->object.ff = f;
1241 1203
1242 return x; 1204 return x;
1243} 1205}
1244 1206
1245INTERFACE pointer 1207INTERFACE pointer
1246mk_character (SCHEME_P_ int c) 1208mk_character (SCHEME_P_ int c)
1247{ 1209{
1248 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1210 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1249 1211
1250 set_typeflag (x, (T_CHARACTER | T_ATOM)); 1212 set_typeflag (x, T_CHARACTER | T_ATOM);
1251 set_ivalue (x, c & 0xff); 1213 set_ivalue (x, c & 0xff);
1252 1214
1253 return x; 1215 return x;
1254} 1216}
1255 1217
1256/* get number atom (integer) */ 1218/* get number atom (integer) */
1257INTERFACE pointer 1219INTERFACE pointer
1258mk_integer (SCHEME_P_ long n) 1220mk_integer (SCHEME_P_ long n)
1259{ 1221{
1222 pointer p = 0;
1223 pointer *pp = &p;
1224
1225#if USE_INTCACHE
1226 if (n >= INTCACHE_MIN && n <= INTCACHE_MAX)
1227 pp = &SCHEME_V->intcache[n - INTCACHE_MIN];
1228#endif
1229
1230 if (!*pp)
1231 {
1260 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1232 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1261 1233
1262 set_typeflag (x, (T_INTEGER | T_ATOM)); 1234 set_typeflag (x, T_INTEGER | T_ATOM);
1235 setimmutable (x); /* shouldn't do anythi9ng, doesn't cost anything */
1263 set_ivalue (x, n); 1236 set_ivalue (x, n);
1264 1237
1238 *pp = x;
1239 }
1240
1265 return x; 1241 return *pp;
1266} 1242}
1267 1243
1268INTERFACE pointer 1244INTERFACE pointer
1269mk_real (SCHEME_P_ RVALUE n) 1245mk_real (SCHEME_P_ RVALUE n)
1270{ 1246{
1271#if USE_REAL 1247#if USE_REAL
1272 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1248 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1273 1249
1274 set_typeflag (x, (T_REAL | T_ATOM)); 1250 set_typeflag (x, T_REAL | T_ATOM);
1275 set_rvalue (x, n); 1251 set_rvalue (x, n);
1276 1252
1277 return x; 1253 return x;
1278#else 1254#else
1279 return mk_integer (SCHEME_A_ n); 1255 return mk_integer (SCHEME_A_ n);
1622 1598
1623/* garbage collection. parameter a, b is marked. */ 1599/* garbage collection. parameter a, b is marked. */
1624static void 1600static void
1625gc (SCHEME_P_ pointer a, pointer b) 1601gc (SCHEME_P_ pointer a, pointer b)
1626{ 1602{
1627 pointer p;
1628 int i; 1603 int i;
1629 1604
1630 if (SCHEME_V->gc_verbose) 1605 if (SCHEME_V->gc_verbose)
1631 putstr (SCHEME_A_ "gc..."); 1606 putstr (SCHEME_A_ "gc...");
1632 1607
1648 /* Mark recent objects the interpreter doesn't know about yet. */ 1623 /* Mark recent objects the interpreter doesn't know about yet. */
1649 mark (car (S_SINK)); 1624 mark (car (S_SINK));
1650 /* Mark any older stuff above nested C calls */ 1625 /* Mark any older stuff above nested C calls */
1651 mark (SCHEME_V->c_nest); 1626 mark (SCHEME_V->c_nest);
1652 1627
1628#if USE_INTCACHE
1629 /* mark intcache */
1630 for (i = INTCACHE_MIN; i <= INTCACHE_MAX; ++i)
1631 if (SCHEME_V->intcache[i - INTCACHE_MIN])
1632 mark (SCHEME_V->intcache[i - INTCACHE_MIN]);
1633#endif
1634
1653 /* mark variables a, b */ 1635 /* mark variables a, b */
1654 mark (a); 1636 mark (a);
1655 mark (b); 1637 mark (b);
1656 1638
1657 /* garbage collect */ 1639 /* garbage collect */
1658 clrmark (NIL); 1640 clrmark (NIL);
1659 SCHEME_V->fcells = 0; 1641 SCHEME_V->fcells = 0;
1660 SCHEME_V->free_cell = NIL; 1642 SCHEME_V->free_cell = NIL;
1661 1643
1662 if (SCHEME_V->gc_verbose) 1644 if (SCHEME_V->gc_verbose)
1663 xwrstr ("freeing..."); 1645 putstr (SCHEME_A_ "freeing...");
1664 1646
1665 uint32_t total = 0; 1647 uint32_t total = 0;
1666 1648
1667 /* Here we scan the cells to build the free-list. */ 1649 /* Here we scan the cells to build the free-list. */
1668 for (i = SCHEME_V->last_cell_seg; i >= 0; i--) 1650 for (i = SCHEME_V->last_cell_seg; i >= 0; i--)
1669 { 1651 {
1670 pointer end = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i]; 1652 struct cell *end = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i];
1653 struct cell *p;
1671 total += SCHEME_V->cell_segsize [i]; 1654 total += SCHEME_V->cell_segsize [i];
1672 1655
1673 for (p = SCHEME_V->cell_seg[i]; p < end; ++p) 1656 for (p = SCHEME_V->cell_seg[i]; p < end; ++p)
1674 { 1657 {
1658 pointer c = POINTER (p);
1659
1675 if (is_mark (p)) 1660 if (is_mark (c))
1676 clrmark (p); 1661 clrmark (c);
1677 else 1662 else
1678 { 1663 {
1679 /* reclaim cell */ 1664 /* reclaim cell */
1680 if (typeflag (p) != T_PAIR) 1665 if (typeflag (c) != T_PAIR)
1681 { 1666 {
1682 finalize_cell (SCHEME_A_ p); 1667 finalize_cell (SCHEME_A_ c);
1683 set_typeflag (p, T_PAIR); 1668 set_typeflag (c, T_PAIR);
1684 set_car (p, NIL); 1669 set_car (c, NIL);
1685 } 1670 }
1686 1671
1687 ++SCHEME_V->fcells; 1672 ++SCHEME_V->fcells;
1688 set_cdr (p, SCHEME_V->free_cell); 1673 set_cdr (c, SCHEME_V->free_cell);
1689 SCHEME_V->free_cell = p; 1674 SCHEME_V->free_cell = c;
1690 } 1675 }
1691 } 1676 }
1692 } 1677 }
1693 1678
1694 if (SCHEME_V->gc_verbose) 1679 if (SCHEME_V->gc_verbose)
1695 { 1680 {
1696 xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" out of "); xwrnum (total); xwrstr (" cells were recovered.\n"); 1681 putstr (SCHEME_A_ "done: "); putnum (SCHEME_A_ SCHEME_V->fcells); putstr (SCHEME_A_ " out of "); putnum (SCHEME_A_ total); putstr (SCHEME_A_ " cells were recovered.\n");
1697 } 1682 }
1698} 1683}
1699 1684
1700static void 1685static void
1701finalize_cell (SCHEME_P_ pointer a) 1686finalize_cell (SCHEME_P_ pointer a)
1706 else if (is_vector (a)) 1691 else if (is_vector (a))
1707 free (vecvalue (a)); 1692 free (vecvalue (a));
1708#if USE_PORTS 1693#if USE_PORTS
1709 else if (is_port (a)) 1694 else if (is_port (a))
1710 { 1695 {
1711 if (a->object.port->kind & port_file && a->object.port->rep.stdio.closeit) 1696 if (port(a)->kind & port_file && port (a)->rep.stdio.closeit)
1712 port_close (SCHEME_A_ a, port_input | port_output); 1697 port_close (SCHEME_A_ a, port_input | port_output);
1713 1698
1714 free (a->object.port); 1699 free (port (a));
1715 } 1700 }
1716#endif 1701#endif
1717} 1702}
1718 1703
1719/* ========== Routines for Reading ========== */ 1704/* ========== Routines for Reading ========== */
1735 SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; 1720 SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1;
1736 SCHEME_V->load_stack[SCHEME_V->file_i].kind = port_file | port_input; 1721 SCHEME_V->load_stack[SCHEME_V->file_i].kind = port_file | port_input;
1737 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.file = fin; 1722 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.file = fin;
1738 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.closeit = 1; 1723 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.closeit = 1;
1739 SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; 1724 SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0;
1740 SCHEME_V->loadport->object.port = SCHEME_V->load_stack + SCHEME_V->file_i; 1725 set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i);
1741 1726
1742#if SHOW_ERROR_LINE 1727#if SHOW_ERROR_LINE
1743 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line = 0; 1728 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line = 0;
1744 1729
1745 if (fname) 1730 if (fname)
1762 SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; 1747 SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i];
1763#if USE_PORTS 1748#if USE_PORTS
1764 port_close (SCHEME_A_ SCHEME_V->loadport, port_input); 1749 port_close (SCHEME_A_ SCHEME_V->loadport, port_input);
1765#endif 1750#endif
1766 SCHEME_V->file_i--; 1751 SCHEME_V->file_i--;
1767 SCHEME_V->loadport->object.port = SCHEME_V->load_stack + SCHEME_V->file_i; 1752 set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i);
1768 } 1753 }
1769} 1754}
1770 1755
1771static int 1756static int
1772file_interactive (SCHEME_P) 1757file_interactive (SCHEME_P)
1773{ 1758{
1774#if USE_PORTS 1759#if USE_PORTS
1775 return SCHEME_V->file_i == 0 1760 return SCHEME_V->file_i == 0
1776 && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO 1761 && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO
1777 && (SCHEME_V->inport->object.port->kind & port_file); 1762 && (port (SCHEME_V->inport)->kind & port_file);
1778#else 1763#else
1779 return 0; 1764 return 0;
1780#endif 1765#endif
1781} 1766}
1782 1767
1916} 1901}
1917 1902
1918static void 1903static void
1919port_close (SCHEME_P_ pointer p, int flag) 1904port_close (SCHEME_P_ pointer p, int flag)
1920{ 1905{
1921 port *pt = p->object.port; 1906 port *pt = port (p);
1922 1907
1923 pt->kind &= ~flag; 1908 pt->kind &= ~flag;
1924 1909
1925 if ((pt->kind & (port_input | port_output)) == 0) 1910 if ((pt->kind & (port_input | port_output)) == 0)
1926 { 1911 {
1947/* get new character from input file */ 1932/* get new character from input file */
1948static int 1933static int
1949inchar (SCHEME_P) 1934inchar (SCHEME_P)
1950{ 1935{
1951 int c; 1936 int c;
1952 port *pt; 1937 port *pt = port (SCHEME_V->inport);
1953
1954 pt = SCHEME_V->inport->object.port;
1955 1938
1956 if (pt->kind & port_saw_EOF) 1939 if (pt->kind & port_saw_EOF)
1957 return EOF; 1940 return EOF;
1958 1941
1959 c = basic_inchar (pt); 1942 c = basic_inchar (pt);
2026 port *pt; 2009 port *pt;
2027 2010
2028 if (c == EOF) 2011 if (c == EOF)
2029 return; 2012 return;
2030 2013
2031 pt = SCHEME_V->inport->object.port; 2014 pt = port (SCHEME_V->inport);
2032 pt->unget = c; 2015 pt->unget = c;
2033#else 2016#else
2034 if (c == EOF) 2017 if (c == EOF)
2035 return; 2018 return;
2036 2019
2064 2047
2065INTERFACE void 2048INTERFACE void
2066putstr (SCHEME_P_ const char *s) 2049putstr (SCHEME_P_ const char *s)
2067{ 2050{
2068#if USE_PORTS 2051#if USE_PORTS
2069 port *pt = SCHEME_V->outport->object.port; 2052 port *pt = port (SCHEME_V->outport);
2070 2053
2071 if (pt->kind & port_file) 2054 if (pt->kind & port_file)
2072 write (pt->rep.stdio.file, s, strlen (s)); 2055 write (pt->rep.stdio.file, s, strlen (s));
2073 else 2056 else
2074 for (; *s; s++) 2057 for (; *s; s++)
2076 *pt->rep.string.curr++ = *s; 2059 *pt->rep.string.curr++ = *s;
2077 else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt)) 2060 else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt))
2078 *pt->rep.string.curr++ = *s; 2061 *pt->rep.string.curr++ = *s;
2079 2062
2080#else 2063#else
2081 xwrstr (s); 2064 write (pt->rep.stdio.file, s, strlen (s));
2082#endif 2065#endif
2083} 2066}
2084 2067
2085static void 2068static void
2086putchars (SCHEME_P_ const char *s, int len) 2069putchars (SCHEME_P_ const char *s, int len)
2087{ 2070{
2088#if USE_PORTS 2071#if USE_PORTS
2089 port *pt = SCHEME_V->outport->object.port; 2072 port *pt = port (SCHEME_V->outport);
2090 2073
2091 if (pt->kind & port_file) 2074 if (pt->kind & port_file)
2092 write (pt->rep.stdio.file, s, len); 2075 write (pt->rep.stdio.file, s, len);
2093 else 2076 else
2094 { 2077 {
2108 2091
2109INTERFACE void 2092INTERFACE void
2110putcharacter (SCHEME_P_ int c) 2093putcharacter (SCHEME_P_ int c)
2111{ 2094{
2112#if USE_PORTS 2095#if USE_PORTS
2113 port *pt = SCHEME_V->outport->object.port; 2096 port *pt = port (SCHEME_V->outport);
2114 2097
2115 if (pt->kind & port_file) 2098 if (pt->kind & port_file)
2116 { 2099 {
2117 char cc = c; 2100 char cc = c;
2118 write (pt->rep.stdio.file, &cc, 1); 2101 write (pt->rep.stdio.file, &cc, 1);
3060{ 3043{
3061 int nframes = (uintptr_t)SCHEME_V->dump; 3044 int nframes = (uintptr_t)SCHEME_V->dump;
3062 struct dump_stack_frame *next_frame; 3045 struct dump_stack_frame *next_frame;
3063 3046
3064 /* enough room for the next frame? */ 3047 /* enough room for the next frame? */
3065 if (nframes >= SCHEME_V->dump_size) 3048 if (ecb_expect_false (nframes >= SCHEME_V->dump_size))
3066 { 3049 {
3067 SCHEME_V->dump_size += STACK_GROWTH; 3050 SCHEME_V->dump_size += STACK_GROWTH;
3068 SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size); 3051 SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size);
3069 } 3052 }
3070 3053
3326 s_return (S_T); 3309 s_return (S_T);
3327#endif 3310#endif
3328 case OP_LOAD: /* load */ 3311 case OP_LOAD: /* load */
3329 if (file_interactive (SCHEME_A)) 3312 if (file_interactive (SCHEME_A))
3330 { 3313 {
3331 xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n"); 3314 putstr (SCHEME_A_ "Loading "); putstr (SCHEME_A_ strvalue (car (args))); putstr (SCHEME_A_ "\n");
3332 //D fprintf (SCHEME_V->outport->object.port->rep.stdio.file, "Loading %s\n", strvalue (car (args))); 3315 //D fprintf (port (SCHEME_V->outport)->rep.stdio.file, "Loading %s\n", strvalue (car (args)));
3333 } 3316 }
3334 3317
3335 if (!file_push (SCHEME_A_ strvalue (car (args)))) 3318 if (!file_push (SCHEME_A_ strvalue (car (args))))
3336 Error_1 ("unable to open", car (args)); 3319 Error_1 ("unable to open", car (args));
3337 else 3320 else
3341 } 3324 }
3342 3325
3343 case OP_T0LVL: /* top level */ 3326 case OP_T0LVL: /* top level */
3344 3327
3345 /* If we reached the end of file, this loop is done. */ 3328 /* If we reached the end of file, this loop is done. */
3346 if (SCHEME_V->loadport->object.port->kind & port_saw_EOF) 3329 if (port (SCHEME_V->loadport)->kind & port_saw_EOF)
3347 { 3330 {
3348 if (SCHEME_V->file_i == 0) 3331 if (SCHEME_V->file_i == 0)
3349 { 3332 {
3350 SCHEME_V->args = NIL; 3333 SCHEME_V->args = NIL;
3351 s_goto (OP_QUIT); 3334 s_goto (OP_QUIT);
3429#endif 3412#endif
3430 if (is_symbol (SCHEME_V->code)) /* symbol */ 3413 if (is_symbol (SCHEME_V->code)) /* symbol */
3431 { 3414 {
3432 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1); 3415 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1);
3433 3416
3434 if (x != NIL) 3417 if (x == NIL)
3435 s_return (slot_value_in_env (x));
3436 else
3437 Error_1 ("eval: unbound variable:", SCHEME_V->code); 3418 Error_1 ("eval: unbound variable:", SCHEME_V->code);
3419
3420 s_return (slot_value_in_env (x));
3438 } 3421 }
3439 else if (is_pair (SCHEME_V->code)) 3422 else if (is_pair (SCHEME_V->code))
3440 { 3423 {
3441 x = car (SCHEME_V->code); 3424 x = car (SCHEME_V->code);
3442 3425
3519 s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ 3502 s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */
3520 else if (is_foreign (SCHEME_V->code)) 3503 else if (is_foreign (SCHEME_V->code))
3521 { 3504 {
3522 /* Keep nested calls from GC'ing the arglist */ 3505 /* Keep nested calls from GC'ing the arglist */
3523 push_recent_alloc (SCHEME_A_ args, NIL); 3506 push_recent_alloc (SCHEME_A_ args, NIL);
3524 x = SCHEME_V->code->object.ff (SCHEME_A_ args); 3507 x = CELL(SCHEME_V->code)->object.ff (SCHEME_A_ args);
3525 3508
3526 s_return (x); 3509 s_return (x);
3527 } 3510 }
3528 else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */ 3511 else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */
3529 { 3512 {
3566 3549
3567 case OP_DOMACRO: /* do macro */ 3550 case OP_DOMACRO: /* do macro */
3568 SCHEME_V->code = SCHEME_V->value; 3551 SCHEME_V->code = SCHEME_V->value;
3569 s_goto (OP_EVAL); 3552 s_goto (OP_EVAL);
3570 3553
3571#if 1
3572
3573 case OP_LAMBDA: /* lambda */ 3554 case OP_LAMBDA: /* lambda */
3574 /* If the hook is defined, apply it to SCHEME_V->code, otherwise 3555 /* If the hook is defined, apply it to SCHEME_V->code, otherwise
3575 set SCHEME_V->value fall thru */ 3556 set SCHEME_V->value fall thru */
3576 { 3557 {
3577 pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1); 3558 pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1);
3583 SCHEME_V->code = slot_value_in_env (f); 3564 SCHEME_V->code = slot_value_in_env (f);
3584 s_goto (OP_APPLY); 3565 s_goto (OP_APPLY);
3585 } 3566 }
3586 3567
3587 SCHEME_V->value = SCHEME_V->code; 3568 SCHEME_V->value = SCHEME_V->code;
3588 /* Fallthru */
3589 } 3569 }
3570 /* Fallthru */
3590 3571
3591 case OP_LAMBDA1: 3572 case OP_LAMBDA1:
3592 s_return (mk_closure (SCHEME_A_ SCHEME_V->value, SCHEME_V->envir)); 3573 s_return (mk_closure (SCHEME_A_ SCHEME_V->value, SCHEME_V->envir));
3593
3594#else
3595
3596 case OP_LAMBDA: /* lambda */
3597 s_return (mk_closure (SCHEME_A_ SCHEME_V->code, SCHEME_V->envir));
3598
3599#endif
3600 3574
3601 case OP_MKCLOSURE: /* make-closure */ 3575 case OP_MKCLOSURE: /* make-closure */
3602 x = car (args); 3576 x = car (args);
3603 3577
3604 if (car (x) == SCHEME_V->LAMBDA) 3578 if (car (x) == SCHEME_V->LAMBDA)
4025 3999
4026 switch (op) 4000 switch (op)
4027 { 4001 {
4028#if USE_MATH 4002#if USE_MATH
4029 case OP_INEX2EX: /* inexact->exact */ 4003 case OP_INEX2EX: /* inexact->exact */
4030 {
4031 if (is_integer (x)) 4004 if (!is_integer (x))
4032 s_return (x); 4005 {
4033
4034 RVALUE r = rvalue_unchecked (x); 4006 RVALUE r = rvalue_unchecked (x);
4035 4007
4036 if (r == (RVALUE)(IVALUE)r) 4008 if (r == (RVALUE)(IVALUE)r)
4037 s_return (mk_integer (SCHEME_A_ rvalue_unchecked (x))); 4009 x = mk_integer (SCHEME_A_ rvalue_unchecked (x));
4038 else 4010 else
4039 Error_1 ("inexact->exact: not integral:", x); 4011 Error_1 ("inexact->exact: not integral:", x);
4040 } 4012 }
4013
4014 s_return (x);
4041 4015
4042 case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); 4016 case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x))));
4043 case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x)))); 4017 case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x))
4018 / (cadr (args) == NIL ? 1 : log (rvalue (cadr (args))))));
4044 case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); 4019 case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x))));
4045 case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x)))); 4020 case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x))));
4046 case OP_TAN: s_return (mk_real (SCHEME_A_ tan (rvalue (x)))); 4021 case OP_TAN: s_return (mk_real (SCHEME_A_ tan (rvalue (x))));
4047 case OP_ASIN: s_return (mk_real (SCHEME_A_ asin (rvalue (x)))); 4022 case OP_ASIN: s_return (mk_real (SCHEME_A_ asin (rvalue (x))));
4048 case OP_ACOS: s_return (mk_real (SCHEME_A_ acos (rvalue (x)))); 4023 case OP_ACOS: s_return (mk_real (SCHEME_A_ acos (rvalue (x))));
4049 4024
4050 case OP_ATAN: 4025 case OP_ATAN:
4026 s_return (mk_real (SCHEME_A_
4051 if (cdr (args) == NIL) 4027 cdr (args) == NIL
4052 s_return (mk_real (SCHEME_A_ atan (rvalue (x)))); 4028 ? atan (rvalue (x))
4053 else 4029 : atan2 (rvalue (x), rvalue (cadr (args)))));
4054 {
4055 pointer y = cadr (args);
4056 s_return (mk_real (SCHEME_A_ atan2 (rvalue (x), rvalue (y))));
4057 }
4058 4030
4059 case OP_SQRT: 4031 case OP_SQRT:
4060 s_return (mk_real (SCHEME_A_ sqrt (rvalue (x)))); 4032 s_return (mk_real (SCHEME_A_ sqrt (rvalue (x))));
4061 4033
4062 case OP_EXPT: 4034 case OP_EXPT:
4089 s_return (mk_real (SCHEME_A_ result)); 4061 s_return (mk_real (SCHEME_A_ result));
4090 else 4062 else
4091 s_return (mk_integer (SCHEME_A_ result)); 4063 s_return (mk_integer (SCHEME_A_ result));
4092 } 4064 }
4093 4065
4094 case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); 4066 case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x))));
4095 case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x)))); 4067 case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x))));
4096
4097 case OP_TRUNCATE:
4098 {
4099 RVALUE n = rvalue (x);
4100 s_return (mk_real (SCHEME_A_ n > 0 ? floor (n) : ceil (n)));
4101 }
4102
4103 case OP_ROUND:
4104 if (is_integer (x))
4105 s_return (x);
4106
4107 s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x)))); 4068 case OP_TRUNCATE: s_return (mk_real (SCHEME_A_ trunc (rvalue (x))));
4069 case OP_ROUND: s_return (mk_real (SCHEME_A_ nearbyint (rvalue (x))));
4108#endif 4070#endif
4109 4071
4110 case OP_ADD: /* + */ 4072 case OP_ADD: /* + */
4111 v = num_zero; 4073 v = num_zero;
4112 4074
4632 } 4594 }
4633 else 4595 else
4634 s_return (SCHEME_V->code); 4596 s_return (SCHEME_V->code);
4635 4597
4636 case OP_SAVE_FORCED: /* Save forced value replacing promise */ 4598 case OP_SAVE_FORCED: /* Save forced value replacing promise */
4637 memcpy (SCHEME_V->code, SCHEME_V->value, sizeof (struct cell)); 4599 *CELL (SCHEME_V->code) = *CELL (SCHEME_V->value);
4638 s_return (SCHEME_V->value); 4600 s_return (SCHEME_V->value);
4639 4601
4640#if USE_PORTS 4602#if USE_PORTS
4641 4603
4642 case OP_WRITE: /* write */ 4604 case OP_WRITE: /* write */
4787 SCHEME_V->gc_verbose = (a != S_F); 4749 SCHEME_V->gc_verbose = (a != S_F);
4788 s_retbool (was); 4750 s_retbool (was);
4789 } 4751 }
4790 4752
4791 case OP_NEWSEGMENT: /* new-segment */ 4753 case OP_NEWSEGMENT: /* new-segment */
4754#if 0
4792 if (!is_pair (args) || !is_number (a)) 4755 if (!is_pair (args) || !is_number (a))
4793 Error_0 ("new-segment: argument must be a number"); 4756 Error_0 ("new-segment: argument must be a number");
4794 4757#endif
4795 alloc_cellseg (SCHEME_A_ ivalue (a)); 4758 s_retbool (alloc_cellseg (SCHEME_A));
4796
4797 s_return (S_T);
4798 4759
4799 case OP_OBLIST: /* oblist */ 4760 case OP_OBLIST: /* oblist */
4800 s_return (oblist_all_symbols (SCHEME_A)); 4761 s_return (oblist_all_symbols (SCHEME_A));
4801 4762
4802#if USE_PORTS 4763#if USE_PORTS
4872 s_return (p == NIL ? S_F : p); 4833 s_return (p == NIL ? S_F : p);
4873 } 4834 }
4874 4835
4875 case OP_GET_OUTSTRING: /* get-output-string */ 4836 case OP_GET_OUTSTRING: /* get-output-string */
4876 { 4837 {
4877 port *p; 4838 port *p = port (a);
4878 4839
4879 if ((p = a->object.port)->kind & port_string) 4840 if (p->kind & port_string)
4880 { 4841 {
4881 off_t size; 4842 off_t size;
4882 char *str; 4843 char *str;
4883 4844
4884 size = p->rep.string.curr - p->rep.string.start + 1; 4845 size = p->rep.string.curr - p->rep.string.start + 1;
4989 int res; 4950 int res;
4990 4951
4991 if (is_pair (args)) 4952 if (is_pair (args))
4992 p = car (args); 4953 p = car (args);
4993 4954
4994 res = p->object.port->kind & port_string; 4955 res = port (p)->kind & port_string;
4995 4956
4996 s_retbool (res); 4957 s_retbool (res);
4997 } 4958 }
4998 4959
4999 case OP_SET_INPORT: /* set-input-port */ 4960 case OP_SET_INPORT: /* set-input-port */
5539 if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0)) 5500 if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0))
5540 return; 5501 return;
5541 5502
5542 if (SCHEME_V->no_memory && USE_ERROR_CHECKING) 5503 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
5543 { 5504 {
5544 xwrstr ("No memory!\n"); 5505 putstr (SCHEME_A_ "No memory!\n");
5545 return; 5506 return;
5546 } 5507 }
5547 } 5508 }
5548} 5509}
5549 5510
5662ecb_cold int 5623ecb_cold int
5663scheme_init (SCHEME_P) 5624scheme_init (SCHEME_P)
5664{ 5625{
5665 int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); 5626 int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]);
5666 pointer x; 5627 pointer x;
5628
5629 /* this memset is not strictly correct, as we assume (intcache)
5630 * that memset 0 will also set pointers to 0, but memset does
5631 * of course not guarantee that. screw such systems.
5632 */
5633 memset (SCHEME_V, 0, sizeof (*SCHEME_V));
5667 5634
5668 num_set_fixnum (num_zero, 1); 5635 num_set_fixnum (num_zero, 1);
5669 num_set_ivalue (num_zero, 0); 5636 num_set_ivalue (num_zero, 0);
5670 num_set_fixnum (num_one, 1); 5637 num_set_fixnum (num_one, 1);
5671 num_set_ivalue (num_one, 1); 5638 num_set_ivalue (num_one, 1);
5683 SCHEME_V->save_inport = NIL; 5650 SCHEME_V->save_inport = NIL;
5684 SCHEME_V->loadport = NIL; 5651 SCHEME_V->loadport = NIL;
5685 SCHEME_V->nesting = 0; 5652 SCHEME_V->nesting = 0;
5686 SCHEME_V->interactive_repl = 0; 5653 SCHEME_V->interactive_repl = 0;
5687 5654
5688 if (alloc_cellseg (SCHEME_A_ FIRST_CELLSEGS) != FIRST_CELLSEGS) 5655 if (!alloc_cellseg (SCHEME_A))
5689 { 5656 {
5690#if USE_ERROR_CHECKING 5657#if USE_ERROR_CHECKING
5691 SCHEME_V->no_memory = 1; 5658 SCHEME_V->no_memory = 1;
5692 return 0; 5659 return 0;
5693#endif 5660#endif
6027 int isfile = 1; 5994 int isfile = 1;
6028 system ("ps v $PPID");//D 5995 system ("ps v $PPID");//D
6029 5996
6030 if (argc == 2 && strcmp (argv[1], "-?") == 0) 5997 if (argc == 2 && strcmp (argv[1], "-?") == 0)
6031 { 5998 {
6032 xwrstr ("Usage: tinyscheme -?\n"); 5999 putstr (SCHEME_A_ "Usage: tinyscheme -?\n");
6033 xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); 6000 putstr (SCHEME_A_ "or: tinyscheme [<file1> <file2> ...]\n");
6034 xwrstr ("followed by\n"); 6001 putstr (SCHEME_A_ "followed by\n");
6035 xwrstr (" -1 <file> [<arg1> <arg2> ...]\n"); 6002 putstr (SCHEME_A_ " -1 <file> [<arg1> <arg2> ...]\n");
6036 xwrstr (" -c <Scheme commands> [<arg1> <arg2> ...]\n"); 6003 putstr (SCHEME_A_ " -c <Scheme commands> [<arg1> <arg2> ...]\n");
6037 xwrstr ("assuming that the executable is named tinyscheme.\n"); 6004 putstr (SCHEME_A_ "assuming that the executable is named tinyscheme.\n");
6038 xwrstr ("Use - as filename for stdin.\n"); 6005 putstr (SCHEME_A_ "Use - as filename for stdin.\n");
6039 return 1; 6006 return 1;
6040 } 6007 }
6041 6008
6042 if (!scheme_init (SCHEME_A)) 6009 if (!scheme_init (SCHEME_A))
6043 { 6010 {
6044 xwrstr ("Could not initialize!\n"); 6011 putstr (SCHEME_A_ "Could not initialize!\n");
6045 return 2; 6012 return 2;
6046 } 6013 }
6047 6014
6048# if USE_PORTS 6015# if USE_PORTS
6049 scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); 6016 scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO);
6094 fin = open (file_name, O_RDONLY); 6061 fin = open (file_name, O_RDONLY);
6095#endif 6062#endif
6096 6063
6097 if (isfile && fin < 0) 6064 if (isfile && fin < 0)
6098 { 6065 {
6099 xwrstr ("Could not open file "); xwrstr (file_name); xwrstr ("\n"); 6066 putstr (SCHEME_A_ "Could not open file "); putstr (SCHEME_A_ file_name); putstr (SCHEME_A_ "\n");
6100 } 6067 }
6101 else 6068 else
6102 { 6069 {
6103 if (isfile) 6070 if (isfile)
6104 scheme_load_named_file (SCHEME_A_ fin, file_name); 6071 scheme_load_named_file (SCHEME_A_ fin, file_name);
6108#if USE_PORTS 6075#if USE_PORTS
6109 if (!isfile || fin != STDIN_FILENO) 6076 if (!isfile || fin != STDIN_FILENO)
6110 { 6077 {
6111 if (SCHEME_V->retcode != 0) 6078 if (SCHEME_V->retcode != 0)
6112 { 6079 {
6113 xwrstr ("Errors encountered reading "); xwrstr (file_name); xwrstr ("\n"); 6080 putstr (SCHEME_A_ "Errors encountered reading "); putstr (SCHEME_A_ file_name); putstr (SCHEME_A_ "\n");
6114 } 6081 }
6115 6082
6116 if (isfile) 6083 if (isfile)
6117 close (fin); 6084 close (fin);
6118 } 6085 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines