2018-03-04 23:13:08 +03:00
|
|
|
/* Runtime library */
|
|
|
|
|
|
2021-09-28 03:02:05 +03:00
|
|
|
# define _GNU_SOURCE 1
|
|
|
|
|
|
|
|
|
|
# include "runtime.h"
|
2023-03-27 10:09:54 +02:00
|
|
|
# include "runtime_common.h"
|
|
|
|
|
# include "gc.h"
|
2018-04-30 17:18:41 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
# define __ENABLE_GC__
|
2019-10-16 01:13:52 +03:00
|
|
|
# ifndef __ENABLE_GC__
|
|
|
|
|
# define alloc malloc
|
|
|
|
|
# endif
|
|
|
|
|
|
2023-03-27 10:09:54 +02:00
|
|
|
//# define DEBUG_PRINT 1
|
2019-10-16 01:13:52 +03:00
|
|
|
|
|
|
|
|
# ifdef __ENABLE_GC__
|
|
|
|
|
|
2019-04-24 16:02:29 +03:00
|
|
|
/* GC extern invariant for built-in functions */
|
2019-04-24 17:34:53 +03:00
|
|
|
extern void __pre_gc ();
|
2019-04-24 16:02:29 +03:00
|
|
|
extern void __post_gc ();
|
2019-10-16 01:13:52 +03:00
|
|
|
|
|
|
|
|
# else
|
|
|
|
|
|
|
|
|
|
# define __pre_gc __pre_gc_subst
|
|
|
|
|
# define __post_gc __post_gc_subst
|
|
|
|
|
|
|
|
|
|
void __pre_gc_subst () {}
|
|
|
|
|
void __post_gc_subst () {}
|
|
|
|
|
|
|
|
|
|
# endif
|
2019-04-24 16:02:29 +03:00
|
|
|
/* end */
|
2018-11-30 16:18:12 +03:00
|
|
|
|
2020-01-04 21:50:14 +03:00
|
|
|
static void vfailure (char *s, va_list args) {
|
2023-04-26 14:22:14 +02:00
|
|
|
fprintf (stderr, "*** FAILURE: ");
|
|
|
|
|
vfprintf (stderr, s, args); // vprintf (char *, va_list) <-> printf (char *, ...)
|
|
|
|
|
exit (255);
|
2020-01-04 21:50:14 +03:00
|
|
|
}
|
|
|
|
|
|
2021-09-28 03:02:05 +03:00
|
|
|
void failure (char *s, ...) {
|
2023-04-26 14:22:14 +02:00
|
|
|
va_list args;
|
2020-01-04 21:50:14 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
va_start (args, s);
|
|
|
|
|
vfailure (s, args);
|
2020-01-04 21:50:14 +03:00
|
|
|
}
|
|
|
|
|
|
2021-01-25 01:22:56 +03:00
|
|
|
void Lassert (void *f, char *s, ...) {
|
2023-04-26 14:22:14 +02:00
|
|
|
if (!UNBOX(f)) {
|
|
|
|
|
va_list args;
|
2021-01-25 01:22:56 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
va_start (args, s);
|
|
|
|
|
vfailure (s, args);
|
|
|
|
|
}
|
2021-01-25 01:22:56 +03:00
|
|
|
}
|
|
|
|
|
|
2020-01-24 17:05:53 +03:00
|
|
|
# define ASSERT_BOXED(memo, x) \
|
|
|
|
|
do if (UNBOXED(x)) failure ("boxed value expected in %s\n", memo); while (0)
|
|
|
|
|
# define ASSERT_UNBOXED(memo, x) \
|
|
|
|
|
do if (!UNBOXED(x)) failure ("unboxed value expected in %s\n", memo); while (0)
|
|
|
|
|
# define ASSERT_STRING(memo, x) \
|
2023-03-27 10:09:54 +02:00
|
|
|
do if (!UNBOXED(x) && TAG(TO_DATA(x)->data_header) \
|
2020-09-01 17:23:36 +03:00
|
|
|
!= STRING_TAG) failure ("string value expected in %s\n", memo); while (0)
|
2018-10-25 03:15:24 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
//extern void* alloc (size_t);
|
2020-09-01 20:31:34 +03:00
|
|
|
extern void* Bsexp (int n, ...);
|
|
|
|
|
extern int LtagHash (char*);
|
2020-01-14 05:15:19 +03:00
|
|
|
|
2020-01-26 07:58:11 +03:00
|
|
|
void *global_sysargs;
|
|
|
|
|
|
2023-03-27 10:09:54 +02:00
|
|
|
// Gets a raw data_header
|
2020-08-10 20:55:10 +03:00
|
|
|
extern int LkindOf (void *p) {
|
2023-04-26 14:22:14 +02:00
|
|
|
if (UNBOXED(p)) return UNBOXED_TAG;
|
|
|
|
|
|
|
|
|
|
return TAG(TO_DATA(p)->data_header);
|
2020-08-02 23:56:21 +03:00
|
|
|
}
|
|
|
|
|
|
2020-08-04 15:48:20 +03:00
|
|
|
// Compare sexprs tags
|
|
|
|
|
extern int LcompareTags (void *p, void *q) {
|
2023-04-26 14:22:14 +02:00
|
|
|
data *pd, *qd;
|
|
|
|
|
|
|
|
|
|
ASSERT_BOXED ("compareTags, 0", p);
|
|
|
|
|
ASSERT_BOXED ("compareTags, 1", q);
|
|
|
|
|
|
|
|
|
|
pd = TO_DATA(p);
|
|
|
|
|
qd = TO_DATA(q);
|
|
|
|
|
|
|
|
|
|
if (TAG(pd->data_header) == SEXP_TAG && TAG(qd->data_header) == SEXP_TAG) {
|
|
|
|
|
return
|
|
|
|
|
#ifndef DEBUG_PRINT
|
|
|
|
|
BOX((TO_SEXP(p)->tag) - (TO_SEXP(q)->tag));
|
|
|
|
|
#else
|
|
|
|
|
BOX((GET_SEXP_TAG(TO_SEXP(p)->data_header)) - (GET_SEXP_TAG(TO_SEXP(p)->data_header)));
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
else failure ("not a sexpr in compareTags: %d, %d\n", TAG(pd->data_header), TAG(qd->data_header));
|
|
|
|
|
|
|
|
|
|
return 0; // never happens
|
2020-08-04 15:48:20 +03:00
|
|
|
}
|
|
|
|
|
|
2020-01-14 05:15:19 +03:00
|
|
|
// Functional synonym for built-in operator ":";
|
|
|
|
|
void* Ls__Infix_58 (void *p, void *q) {
|
2023-04-26 14:22:14 +02:00
|
|
|
void *res;
|
2020-01-14 05:15:19 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
__pre_gc ();
|
2020-01-14 05:15:19 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
push_extra_root(&p);
|
|
|
|
|
push_extra_root(&q);
|
|
|
|
|
res = Bsexp (BOX(3), p, q, LtagHash ("cons")); //BOX(848787));
|
|
|
|
|
pop_extra_root(&q);
|
|
|
|
|
pop_extra_root(&p);
|
2020-01-14 05:15:19 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
__post_gc ();
|
|
|
|
|
|
|
|
|
|
return res;
|
2020-01-14 05:15:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Functional synonym for built-in operator "!!";
|
|
|
|
|
int Ls__Infix_3333 (void *p, void *q) {
|
2023-04-26 14:22:14 +02:00
|
|
|
ASSERT_UNBOXED("captured !!:1", p);
|
|
|
|
|
ASSERT_UNBOXED("captured !!:2", q);
|
2020-01-14 05:15:19 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
return BOX(UNBOX(p) || UNBOX(q));
|
2020-01-14 05:15:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Functional synonym for built-in operator "&&";
|
|
|
|
|
int Ls__Infix_3838 (void *p, void *q) {
|
2023-04-26 14:22:14 +02:00
|
|
|
ASSERT_UNBOXED("captured &&:1", p);
|
|
|
|
|
ASSERT_UNBOXED("captured &&:2", q);
|
2020-01-14 05:15:19 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
return BOX(UNBOX(p) && UNBOX(q));
|
2020-01-14 05:15:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Functional synonym for built-in operator "==";
|
|
|
|
|
int Ls__Infix_6161 (void *p, void *q) {
|
2023-04-26 14:22:14 +02:00
|
|
|
return BOX(p == q);
|
2020-01-14 05:15:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Functional synonym for built-in operator "!=";
|
|
|
|
|
int Ls__Infix_3361 (void *p, void *q) {
|
2023-04-26 14:22:14 +02:00
|
|
|
ASSERT_UNBOXED("captured !=:1", p);
|
|
|
|
|
ASSERT_UNBOXED("captured !=:2", q);
|
2020-01-14 05:15:19 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
return BOX(UNBOX(p) != UNBOX(q));
|
2020-01-14 05:15:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Functional synonym for built-in operator "<=";
|
|
|
|
|
int Ls__Infix_6061 (void *p, void *q) {
|
2023-04-26 14:22:14 +02:00
|
|
|
ASSERT_UNBOXED("captured <=:1", p);
|
|
|
|
|
ASSERT_UNBOXED("captured <=:2", q);
|
2020-01-14 05:15:19 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
return BOX(UNBOX(p) <= UNBOX(q));
|
2020-01-14 05:15:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Functional synonym for built-in operator "<";
|
|
|
|
|
int Ls__Infix_60 (void *p, void *q) {
|
2023-04-26 14:22:14 +02:00
|
|
|
ASSERT_UNBOXED("captured <:1", p);
|
|
|
|
|
ASSERT_UNBOXED("captured <:2", q);
|
2020-01-14 05:15:19 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
return BOX(UNBOX(p) < UNBOX(q));
|
2020-01-14 05:15:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Functional synonym for built-in operator ">=";
|
|
|
|
|
int Ls__Infix_6261 (void *p, void *q) {
|
2023-04-26 14:22:14 +02:00
|
|
|
ASSERT_UNBOXED("captured >=:1", p);
|
|
|
|
|
ASSERT_UNBOXED("captured >=:2", q);
|
2020-01-14 05:15:19 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
return BOX(UNBOX(p) >= UNBOX(q));
|
2020-01-14 05:15:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Functional synonym for built-in operator ">";
|
|
|
|
|
int Ls__Infix_62 (void *p, void *q) {
|
2023-04-26 14:22:14 +02:00
|
|
|
ASSERT_UNBOXED("captured >:1", p);
|
|
|
|
|
ASSERT_UNBOXED("captured >:2", q);
|
2020-01-14 05:15:19 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
return BOX(UNBOX(p) > UNBOX(q));
|
2020-01-14 05:15:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Functional synonym for built-in operator "+";
|
|
|
|
|
int Ls__Infix_43 (void *p, void *q) {
|
2023-04-26 14:22:14 +02:00
|
|
|
ASSERT_UNBOXED("captured +:1", p);
|
|
|
|
|
ASSERT_UNBOXED("captured +:2", q);
|
2020-01-14 05:15:19 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
return BOX(UNBOX(p) + UNBOX(q));
|
2020-01-14 05:15:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Functional synonym for built-in operator "-";
|
|
|
|
|
int Ls__Infix_45 (void *p, void *q) {
|
2023-04-26 14:22:14 +02:00
|
|
|
if (UNBOXED(p)) {
|
|
|
|
|
ASSERT_UNBOXED("captured -:2", q);
|
|
|
|
|
return BOX(UNBOX(p) - UNBOX(q));
|
|
|
|
|
}
|
2020-01-14 05:15:19 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
ASSERT_BOXED("captured -:1", q);
|
|
|
|
|
return BOX(p - q);
|
2020-01-14 05:15:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Functional synonym for built-in operator "*";
|
|
|
|
|
int Ls__Infix_42 (void *p, void *q) {
|
2023-04-26 14:22:14 +02:00
|
|
|
ASSERT_UNBOXED("captured *:1", p);
|
|
|
|
|
ASSERT_UNBOXED("captured *:2", q);
|
2020-01-14 05:15:19 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
return BOX(UNBOX(p) * UNBOX(q));
|
2020-01-14 05:15:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Functional synonym for built-in operator "/";
|
|
|
|
|
int Ls__Infix_47 (void *p, void *q) {
|
2023-04-26 14:22:14 +02:00
|
|
|
ASSERT_UNBOXED("captured /:1", p);
|
|
|
|
|
ASSERT_UNBOXED("captured /:2", q);
|
2020-01-14 05:15:19 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
return BOX(UNBOX(p) / UNBOX(q));
|
2020-01-14 05:15:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Functional synonym for built-in operator "%";
|
|
|
|
|
int Ls__Infix_37 (void *p, void *q) {
|
2023-04-26 14:22:14 +02:00
|
|
|
ASSERT_UNBOXED("captured %:1", p);
|
|
|
|
|
ASSERT_UNBOXED("captured %:2", q);
|
2020-01-14 05:15:19 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
return BOX(UNBOX(p) % UNBOX(q));
|
2020-01-14 05:15:19 +03:00
|
|
|
}
|
2018-11-29 18:27:59 +03:00
|
|
|
|
2021-01-31 22:57:12 +03:00
|
|
|
extern int Llength (void *p) {
|
2023-04-26 14:22:14 +02:00
|
|
|
ASSERT_BOXED(".length", p);
|
2023-05-23 13:40:46 +02:00
|
|
|
return BOX(LEN(TO_DATA(p)->data_header));
|
2018-04-30 17:18:41 +03:00
|
|
|
}
|
|
|
|
|
|
2020-09-01 17:23:36 +03:00
|
|
|
static char* chars = "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'";
|
|
|
|
|
|
2020-09-01 20:31:34 +03:00
|
|
|
extern char* de_hash (int);
|
|
|
|
|
|
2020-09-01 17:23:36 +03:00
|
|
|
extern int LtagHash (char *s) {
|
2023-04-26 14:22:14 +02:00
|
|
|
char *p;
|
|
|
|
|
int h = 0, limit = 0;
|
|
|
|
|
|
|
|
|
|
p = s;
|
|
|
|
|
|
|
|
|
|
while (*p && limit++ <= 4) {
|
|
|
|
|
char *q = chars;
|
|
|
|
|
int pos = 0;
|
2020-09-01 17:23:36 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
for (; *q && *q != *p; q++, pos++);
|
2020-09-01 17:23:36 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
if (*q) h = (h << 6) | pos;
|
|
|
|
|
else failure ("tagHash: character not found: %c\n", *p);
|
2020-09-01 17:23:36 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
p++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (strcmp (s, de_hash (h)) != 0) {
|
|
|
|
|
failure ("%s <-> %s\n", s, de_hash(h));
|
|
|
|
|
}
|
2020-09-01 17:23:36 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
return BOX(h);
|
2020-09-01 17:23:36 +03:00
|
|
|
}
|
|
|
|
|
|
2018-10-31 20:10:50 +03:00
|
|
|
char* de_hash (int n) {
|
2023-04-26 14:22:14 +02:00
|
|
|
// static char *chars = (char*) BOX (NULL);
|
|
|
|
|
static char buf[6] = {0,0,0,0,0,0};
|
|
|
|
|
char *p = (char *) BOX (NULL);
|
|
|
|
|
p = &buf[5];
|
2019-04-24 17:34:53 +03:00
|
|
|
|
|
|
|
|
#ifdef DEBUG_PRINT
|
2023-04-26 14:22:14 +02:00
|
|
|
indent++; print_indent ();
|
2023-03-27 10:09:54 +02:00
|
|
|
printf ("de_hash: data_header: %d\n", n); fflush (stdout);
|
2019-04-24 17:34:53 +03:00
|
|
|
#endif
|
2018-10-31 20:10:50 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
*p-- = 0;
|
|
|
|
|
|
|
|
|
|
while (n != 0) {
|
2019-04-24 17:34:53 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2023-04-26 14:22:14 +02:00
|
|
|
print_indent ();
|
2019-04-24 17:34:53 +03:00
|
|
|
printf ("char: %c\n", chars [n & 0x003F]); fflush (stdout);
|
|
|
|
|
#endif
|
2023-04-26 14:22:14 +02:00
|
|
|
*p-- = chars [n & 0x003F];
|
|
|
|
|
n = n >> 6;
|
|
|
|
|
}
|
2020-02-07 19:42:24 +03:00
|
|
|
|
|
|
|
|
#ifdef DEBUG_PRINT
|
2023-04-26 14:22:14 +02:00
|
|
|
indent--;
|
2020-02-07 19:42:24 +03:00
|
|
|
#endif
|
2023-04-26 14:22:14 +02:00
|
|
|
|
|
|
|
|
return ++p;
|
2018-10-31 20:10:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
2023-04-26 14:22:14 +02:00
|
|
|
char *contents;
|
|
|
|
|
int ptr;
|
|
|
|
|
int len;
|
2018-10-31 20:10:50 +03:00
|
|
|
} StringBuf;
|
|
|
|
|
|
|
|
|
|
static StringBuf stringBuf;
|
|
|
|
|
|
|
|
|
|
# define STRINGBUF_INIT 128
|
|
|
|
|
|
|
|
|
|
static void createStringBuf () {
|
2023-04-26 14:22:14 +02:00
|
|
|
stringBuf.contents = (char*) malloc (STRINGBUF_INIT);
|
|
|
|
|
memset(stringBuf.contents, 0, STRINGBUF_INIT);
|
|
|
|
|
stringBuf.ptr = 0;
|
|
|
|
|
stringBuf.len = STRINGBUF_INIT;
|
2018-10-31 20:10:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void deleteStringBuf () {
|
2023-04-26 14:22:14 +02:00
|
|
|
free (stringBuf.contents);
|
2018-10-31 20:10:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void extendStringBuf () {
|
2023-04-26 14:22:14 +02:00
|
|
|
int len = stringBuf.len << 1;
|
2018-10-31 20:10:50 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
stringBuf.contents = (char*) realloc (stringBuf.contents, len);
|
|
|
|
|
stringBuf.len = len;
|
2018-10-31 20:10:50 +03:00
|
|
|
}
|
|
|
|
|
|
2019-12-21 02:34:56 +03:00
|
|
|
static void vprintStringBuf (char *fmt, va_list args) {
|
2023-04-26 14:22:14 +02:00
|
|
|
int written = 0,
|
|
|
|
|
rest = 0;
|
|
|
|
|
char *buf = (char*) BOX(NULL);
|
|
|
|
|
va_list vsnargs;
|
2021-12-03 03:56:58 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
again:
|
|
|
|
|
va_copy (vsnargs, args);
|
2021-12-03 03:56:58 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
buf = &stringBuf.contents[stringBuf.ptr];
|
|
|
|
|
rest = stringBuf.len - stringBuf.ptr;
|
2018-10-31 20:10:50 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
written = vsnprintf (buf, rest, fmt, vsnargs);
|
|
|
|
|
|
|
|
|
|
va_end(vsnargs);
|
|
|
|
|
|
|
|
|
|
if (written >= rest) {
|
|
|
|
|
extendStringBuf ();
|
|
|
|
|
goto again;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stringBuf.ptr += written;
|
2018-10-31 20:10:50 +03:00
|
|
|
}
|
|
|
|
|
|
2019-12-21 02:34:56 +03:00
|
|
|
static void printStringBuf (char *fmt, ...) {
|
2023-04-26 14:22:14 +02:00
|
|
|
va_list args;
|
2019-12-21 02:34:56 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
va_start (args, fmt);
|
|
|
|
|
vprintStringBuf (fmt, args);
|
2019-12-21 02:34:56 +03:00
|
|
|
}
|
|
|
|
|
|
2018-10-31 20:10:50 +03:00
|
|
|
static void printValue (void *p) {
|
2023-04-26 14:22:14 +02:00
|
|
|
data *a = (data*) BOX(NULL);
|
|
|
|
|
int i = BOX(0);
|
2023-05-23 13:40:46 +02:00
|
|
|
if (UNBOXED(p)) {
|
|
|
|
|
printStringBuf ("%d", UNBOX(p));
|
|
|
|
|
} else {
|
2023-04-26 14:22:14 +02:00
|
|
|
if (!is_valid_heap_pointer(p)) {
|
|
|
|
|
printStringBuf ("0x%x", p);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-10-31 20:10:50 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
a = TO_DATA(p);
|
|
|
|
|
|
|
|
|
|
switch (TAG(a->data_header)) {
|
|
|
|
|
case STRING_TAG:
|
|
|
|
|
printStringBuf ("\"%s\"", a->contents);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case CLOSURE_TAG:
|
|
|
|
|
printStringBuf ("<closure ");
|
|
|
|
|
for (i = 0; i < LEN(a->data_header); i++) {
|
|
|
|
|
if (i) printValue ((void*)((int*) a->contents)[i]);
|
|
|
|
|
else printStringBuf ("0x%x", (void*)((int*) a->contents)[i]);
|
|
|
|
|
if (i != LEN(a->data_header) - 1) printStringBuf (", ");
|
|
|
|
|
}
|
|
|
|
|
printStringBuf (">");
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ARRAY_TAG:
|
|
|
|
|
printStringBuf ("[");
|
|
|
|
|
for (i = 0; i < LEN(a->data_header); i++) {
|
|
|
|
|
printValue ((void*)((int*) a->contents)[i]);
|
|
|
|
|
if (i != LEN(a->data_header) - 1) printStringBuf (", ");
|
|
|
|
|
}
|
|
|
|
|
printStringBuf ("]");
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SEXP_TAG: {
|
|
|
|
|
char * tag = de_hash (TO_SEXP(p)->tag);
|
|
|
|
|
if (strcmp (tag, "cons") == 0) {
|
|
|
|
|
data *b = a;
|
|
|
|
|
printStringBuf ("{");
|
2023-05-23 13:40:46 +02:00
|
|
|
while (LEN(b->data_header)) {
|
2023-04-26 14:22:14 +02:00
|
|
|
printValue ((void*)((int*) b->contents)[0]);
|
|
|
|
|
b = (data*)((int*) b->contents)[1];
|
|
|
|
|
if (! UNBOXED(b)) {
|
|
|
|
|
printStringBuf (", ");
|
|
|
|
|
b = TO_DATA(b);
|
|
|
|
|
}
|
|
|
|
|
else break;
|
|
|
|
|
}
|
|
|
|
|
printStringBuf ("}");
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
printStringBuf ("%s", tag);
|
|
|
|
|
if (LEN(a->data_header)) {
|
|
|
|
|
printStringBuf (" (");
|
|
|
|
|
for (i = 0; i < LEN(a->data_header); i++) {
|
|
|
|
|
printValue ((void*)((int*) a->contents)[i]);
|
|
|
|
|
if (i != LEN(a->data_header) - 1) printStringBuf (", ");
|
|
|
|
|
}
|
|
|
|
|
printStringBuf (")");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
printStringBuf ("*** invalid data_header: 0x%x ***", TAG(a->data_header));
|
|
|
|
|
}
|
2018-10-31 20:10:50 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-21 02:34:56 +03:00
|
|
|
static void stringcat (void *p) {
|
2023-04-26 14:22:14 +02:00
|
|
|
data *a;
|
|
|
|
|
int i;
|
2019-12-21 02:34:56 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
if (UNBOXED(p)) ;
|
|
|
|
|
else {
|
|
|
|
|
a = TO_DATA(p);
|
|
|
|
|
|
|
|
|
|
switch (TAG(a->data_header)) {
|
|
|
|
|
case STRING_TAG:
|
|
|
|
|
printStringBuf ("%s", a->contents);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SEXP_TAG: {
|
|
|
|
|
char * tag = de_hash (TO_SEXP(p)->tag);
|
2023-05-23 13:40:46 +02:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
if (strcmp (tag, "cons") == 0) {
|
|
|
|
|
data *b = a;
|
|
|
|
|
|
2023-05-23 13:40:46 +02:00
|
|
|
while (LEN(b->data_header)) {
|
2023-04-26 14:22:14 +02:00
|
|
|
stringcat ((void*)((int*) b->contents)[0]);
|
|
|
|
|
b = (data*)((int*) b->contents)[1];
|
|
|
|
|
if (! UNBOXED(b)) {
|
|
|
|
|
b = TO_DATA(b);
|
|
|
|
|
}
|
|
|
|
|
else break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else printStringBuf ("*** non-list data_header: %s ***", tag);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
printStringBuf ("*** invalid data_header: 0x%x ***", TAG(a->data_header));
|
|
|
|
|
}
|
2019-12-21 02:34:56 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-31 15:34:34 +03:00
|
|
|
extern int Luppercase (void *v) {
|
2023-04-26 14:22:14 +02:00
|
|
|
ASSERT_UNBOXED("Luppercase:1", v);
|
|
|
|
|
return BOX(toupper ((int) UNBOX(v)));
|
2021-10-31 15:34:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern int Llowercase (void *v) {
|
2023-04-26 14:22:14 +02:00
|
|
|
ASSERT_UNBOXED("Llowercase:1", v);
|
|
|
|
|
return BOX(tolower ((int) UNBOX(v)));
|
2021-10-31 15:34:34 +03:00
|
|
|
}
|
|
|
|
|
|
2019-12-31 00:59:28 +03:00
|
|
|
extern int LmatchSubString (char *subj, char *patt, int pos) {
|
2023-04-26 14:22:14 +02:00
|
|
|
data *p = TO_DATA(patt), *s = TO_DATA(subj);
|
|
|
|
|
int n;
|
|
|
|
|
|
|
|
|
|
ASSERT_STRING("matchSubString:1", subj);
|
|
|
|
|
ASSERT_STRING("matchSubString:2", patt);
|
|
|
|
|
ASSERT_UNBOXED("matchSubString:3", pos);
|
2019-12-31 00:59:28 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
n = LEN (p->data_header);
|
|
|
|
|
|
|
|
|
|
if (n + UNBOX(pos) > LEN(s->data_header))
|
|
|
|
|
return BOX(0);
|
2020-03-13 19:41:14 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
return BOX(strncmp (subj + UNBOX(pos), patt, n) == 0);
|
2019-12-31 00:59:28 +03:00
|
|
|
}
|
|
|
|
|
|
2020-01-03 01:38:49 +03:00
|
|
|
extern void* Lsubstring (void *subj, int p, int l) {
|
2023-04-26 14:22:14 +02:00
|
|
|
data *d = TO_DATA(subj);
|
|
|
|
|
int pp = UNBOX (p), ll = UNBOX (l);
|
2020-02-07 19:42:24 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
ASSERT_STRING("substring:1", subj);
|
|
|
|
|
ASSERT_UNBOXED("substring:2", p);
|
|
|
|
|
ASSERT_UNBOXED("substring:3", l);
|
2020-01-03 01:38:49 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
if (pp + ll <= LEN(d->data_header)) {
|
|
|
|
|
data *r;
|
2020-01-03 01:38:49 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
__pre_gc ();
|
2020-01-03 01:38:49 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
push_extra_root (&subj);
|
|
|
|
|
r = (data*) alloc_string(ll);
|
|
|
|
|
pop_extra_root (&subj);
|
|
|
|
|
|
|
|
|
|
strncpy (r->contents, (char*) subj + pp, ll);
|
|
|
|
|
|
|
|
|
|
__post_gc ();
|
|
|
|
|
|
|
|
|
|
return r->contents;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
failure ("substring: index out of bounds (position=%d, length=%d, \
|
2023-03-27 10:09:54 +02:00
|
|
|
subject length=%d)", pp, ll, LEN(d->data_header));
|
2020-01-03 01:38:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern struct re_pattern_buffer *Lregexp (char *regexp) {
|
2023-04-26 14:22:14 +02:00
|
|
|
regex_t *b = (regex_t*) malloc (sizeof (regex_t));
|
|
|
|
|
|
|
|
|
|
/* printf ("regexp: %s,\test_small_tree_compaction%x\n", regexp, b); */
|
2020-05-04 02:45:34 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
memset (b, 0, sizeof (regex_t));
|
2020-01-03 01:38:49 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
int n = (int) re_compile_pattern (regexp, strlen (regexp), b);
|
|
|
|
|
|
|
|
|
|
if (n != 0) {
|
|
|
|
|
failure ("%", strerror (n));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return b;
|
2020-01-03 01:38:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern int LregexpMatch (struct re_pattern_buffer *b, char *s, int pos) {
|
2023-04-26 14:22:14 +02:00
|
|
|
int res;
|
2020-05-04 02:45:34 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
ASSERT_BOXED("regexpMatch:1", b);
|
|
|
|
|
ASSERT_STRING("regexpMatch:2", s);
|
|
|
|
|
ASSERT_UNBOXED("regexpMatch:3", pos);
|
2020-05-04 02:45:34 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
res = re_match (b, s, LEN(TO_DATA(s)->data_header), UNBOX(pos), 0);
|
|
|
|
|
|
|
|
|
|
/* printf ("regexpMatch %x: %s, res=%d\n", b, s+UNBOX(pos), res); */
|
2020-05-04 02:45:34 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
if (res) {
|
|
|
|
|
return BOX (res);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return BOX (res);
|
2020-01-03 01:38:49 +03:00
|
|
|
}
|
|
|
|
|
|
2020-01-28 03:01:54 +03:00
|
|
|
extern void* Bstring (void*);
|
|
|
|
|
|
2020-01-15 21:42:59 +03:00
|
|
|
void *Lclone (void *p) {
|
2023-04-26 14:22:14 +02:00
|
|
|
data *obj;
|
|
|
|
|
sexp *sobj;
|
|
|
|
|
void* res;
|
|
|
|
|
int n;
|
2020-01-28 16:24:26 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2023-04-26 14:22:14 +02:00
|
|
|
register int * ebp asm ("ebp");
|
2020-02-07 19:42:24 +03:00
|
|
|
indent++; print_indent ();
|
2020-01-28 16:24:26 +03:00
|
|
|
printf ("Lclone arg: %p %p\n", &p, p); fflush (stdout);
|
|
|
|
|
#endif
|
2023-04-26 14:22:14 +02:00
|
|
|
__pre_gc ();
|
2020-01-15 21:42:59 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
if (UNBOXED(p)) return p;
|
|
|
|
|
else {
|
|
|
|
|
data *a = TO_DATA(p);
|
|
|
|
|
int t = TAG(a->data_header), l = LEN(a->data_header);
|
|
|
|
|
|
|
|
|
|
push_extra_root (&p);
|
|
|
|
|
switch (t) {
|
|
|
|
|
case STRING_TAG:
|
2020-01-28 16:24:26 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2023-04-26 14:22:14 +02:00
|
|
|
print_indent ();
|
2020-02-07 19:42:24 +03:00
|
|
|
printf ("Lclone: string1 &p=%p p=%p\n", &p, p); fflush (stdout);
|
2020-01-28 16:24:26 +03:00
|
|
|
#endif
|
2023-04-26 14:22:14 +02:00
|
|
|
res = Bstring (TO_DATA(p)->contents);
|
2020-01-28 18:06:09 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2023-04-26 14:22:14 +02:00
|
|
|
print_indent ();
|
2020-01-28 18:06:09 +03:00
|
|
|
printf ("Lclone: string2 %p %p\n", &p, p); fflush (stdout);
|
2020-02-07 19:42:24 +03:00
|
|
|
#endif
|
2023-04-26 14:22:14 +02:00
|
|
|
break;
|
2020-01-15 21:42:59 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
case ARRAY_TAG:
|
|
|
|
|
#ifdef DEBUG_PRINT
|
|
|
|
|
print_indent ();
|
|
|
|
|
printf ("Lclone: array &p=%p p=%p ebp=%p\n", &p, p, ebp); fflush (stdout);
|
|
|
|
|
#endif
|
|
|
|
|
obj = (data *) alloc_array(l);
|
|
|
|
|
memcpy(obj, TO_DATA(p), array_size(l));
|
|
|
|
|
res = (void *) obj->contents;
|
|
|
|
|
break;
|
|
|
|
|
case CLOSURE_TAG:
|
2020-01-28 16:24:26 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2023-04-26 14:22:14 +02:00
|
|
|
print_indent ();
|
|
|
|
|
printf ("Lclone: closure &p=%p p=%p ebp=%p\n", &p, p, ebp); fflush (stdout);
|
2020-01-28 16:24:26 +03:00
|
|
|
#endif
|
2023-04-26 14:22:14 +02:00
|
|
|
obj = (data *) alloc_closure(l);
|
|
|
|
|
memcpy (obj, TO_DATA(p), closure_size(l));
|
|
|
|
|
res = (void*) (obj->contents);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SEXP_TAG:
|
2020-01-28 16:24:26 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2023-04-26 14:22:14 +02:00
|
|
|
print_indent (); printf ("Lclone: sexp\n"); fflush (stdout);
|
2020-01-28 16:24:26 +03:00
|
|
|
#endif
|
2023-04-26 14:22:14 +02:00
|
|
|
sobj = (sexp*) alloc_sexp(l);
|
|
|
|
|
memcpy (sobj, TO_SEXP(p), sexp_size(l));
|
|
|
|
|
res = (void*) sobj->contents.contents;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
failure ("invalid data_header %d in clone *****\n", t);
|
|
|
|
|
}
|
|
|
|
|
pop_extra_root (&p);
|
2020-01-15 21:42:59 +03:00
|
|
|
}
|
2020-01-28 16:24:26 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2023-04-26 14:22:14 +02:00
|
|
|
print_indent (); printf ("Lclone ends1\n"); fflush (stdout);
|
2020-01-28 16:24:26 +03:00
|
|
|
#endif
|
2020-01-15 21:42:59 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
__post_gc ();
|
2020-01-28 16:24:26 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2023-04-26 14:22:14 +02:00
|
|
|
print_indent ();
|
2020-01-28 16:24:26 +03:00
|
|
|
printf ("Lclone ends2\n"); fflush (stdout);
|
2020-02-07 19:42:24 +03:00
|
|
|
indent--;
|
2020-01-28 16:24:26 +03:00
|
|
|
#endif
|
2023-04-26 14:22:14 +02:00
|
|
|
return res;
|
2020-01-15 21:42:59 +03:00
|
|
|
}
|
|
|
|
|
|
2020-01-20 03:38:43 +03:00
|
|
|
# define HASH_DEPTH 3
|
2020-03-13 19:41:14 +03:00
|
|
|
# define HASH_APPEND(acc, x) (((acc + (unsigned) x) << (WORD_SIZE / 2)) | ((acc + (unsigned) x) >> (WORD_SIZE / 2)))
|
2020-01-15 21:42:59 +03:00
|
|
|
|
|
|
|
|
int inner_hash (int depth, unsigned acc, void *p) {
|
2023-04-26 14:22:14 +02:00
|
|
|
if (depth > HASH_DEPTH) return acc;
|
2020-01-15 21:42:59 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
if (UNBOXED(p)) return HASH_APPEND(acc, UNBOX(p));
|
|
|
|
|
else if (is_valid_heap_pointer (p)) {
|
|
|
|
|
data *a = TO_DATA(p);
|
|
|
|
|
int t = TAG(a->data_header), l = LEN(a->data_header), i;
|
2020-01-15 21:42:59 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
acc = HASH_APPEND(acc, t);
|
|
|
|
|
acc = HASH_APPEND(acc, l);
|
2020-01-15 21:42:59 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
switch (t) {
|
|
|
|
|
case STRING_TAG: {
|
|
|
|
|
char *p = a->contents;
|
2020-01-15 21:42:59 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
while (*p) {
|
|
|
|
|
int n = (int) *p++;
|
|
|
|
|
acc = HASH_APPEND(acc, n);
|
|
|
|
|
}
|
2020-01-15 21:42:59 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
return acc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case CLOSURE_TAG:
|
|
|
|
|
acc = HASH_APPEND(acc, ((void**) a->contents)[0]);
|
|
|
|
|
i = 1;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ARRAY_TAG:
|
|
|
|
|
i = 0;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SEXP_TAG: {
|
2020-01-15 21:42:59 +03:00
|
|
|
#ifndef DEBUG_PRINT
|
2023-04-26 14:22:14 +02:00
|
|
|
int ta = TO_SEXP(p)->tag;
|
2020-01-15 21:42:59 +03:00
|
|
|
#else
|
2023-04-26 14:22:14 +02:00
|
|
|
int ta = GET_SEXP_TAG(TO_SEXP(p)->data_header);
|
2020-01-15 21:42:59 +03:00
|
|
|
#endif
|
2023-04-26 14:22:14 +02:00
|
|
|
acc = HASH_APPEND(acc, ta);
|
|
|
|
|
i = 0;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-01-15 21:42:59 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
default:
|
|
|
|
|
failure ("invalid data_header %d in hash *****\n", t);
|
|
|
|
|
}
|
2020-01-15 21:42:59 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
for (; i<l; i++)
|
|
|
|
|
acc = inner_hash (depth+1, acc, ((void**) a->contents)[i]);
|
2020-01-15 21:42:59 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
return acc;
|
|
|
|
|
}
|
|
|
|
|
else return HASH_APPEND(acc, p);
|
2020-01-15 21:42:59 +03:00
|
|
|
}
|
|
|
|
|
|
2020-02-23 01:36:30 +03:00
|
|
|
extern void* LstringInt (char *b) {
|
2023-04-26 14:22:14 +02:00
|
|
|
int n;
|
|
|
|
|
sscanf (b, "%d", &n);
|
|
|
|
|
return (void*) BOX(n);
|
2020-02-23 01:36:30 +03:00
|
|
|
}
|
|
|
|
|
|
2020-01-15 21:42:59 +03:00
|
|
|
extern int Lhash (void *p) {
|
2023-04-26 14:22:14 +02:00
|
|
|
return BOX(0x3fffff & inner_hash (0, 0, p));
|
2020-08-06 14:56:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern int LflatCompare (void *p, void *q) {
|
2023-04-26 14:22:14 +02:00
|
|
|
if (UNBOXED(p)) {
|
|
|
|
|
if (UNBOXED(q)) {
|
|
|
|
|
return BOX (UNBOX(p) - UNBOX(q));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return -1;
|
2020-08-06 14:56:41 +03:00
|
|
|
}
|
2023-04-26 14:22:14 +02:00
|
|
|
else if (~UNBOXED(q)) {
|
|
|
|
|
return BOX(p - q);
|
|
|
|
|
}
|
|
|
|
|
else BOX(1);
|
2020-01-15 21:42:59 +03:00
|
|
|
}
|
|
|
|
|
|
2019-12-21 02:34:56 +03:00
|
|
|
extern int Lcompare (void *p, void *q) {
|
2019-12-20 00:23:35 +03:00
|
|
|
# define COMPARE_AND_RETURN(x,y) do if (x != y) return BOX(x - y); while (0)
|
2020-05-04 02:45:34 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
if (p == q) return BOX(0);
|
2020-05-04 02:45:34 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
if (UNBOXED(p)) {
|
|
|
|
|
if (UNBOXED(q)) return BOX(UNBOX(p) - UNBOX(q));
|
|
|
|
|
else return BOX(-1);
|
|
|
|
|
}
|
|
|
|
|
else if (UNBOXED(q)) return BOX(1);
|
|
|
|
|
else {
|
|
|
|
|
if (is_valid_heap_pointer (p)) {
|
|
|
|
|
if (is_valid_heap_pointer (q)) {
|
|
|
|
|
data *a = TO_DATA(p), *b = TO_DATA(q);
|
|
|
|
|
int ta = TAG(a->data_header), tb = TAG(b->data_header);
|
|
|
|
|
int la = LEN(a->data_header), lb = LEN(b->data_header);
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
COMPARE_AND_RETURN (ta, tb);
|
|
|
|
|
|
|
|
|
|
switch (ta) {
|
|
|
|
|
case STRING_TAG:
|
|
|
|
|
return BOX(strcmp (a->contents, b->contents));
|
|
|
|
|
|
|
|
|
|
case CLOSURE_TAG:
|
|
|
|
|
COMPARE_AND_RETURN (((void**) a->contents)[0], ((void**) b->contents)[0]);
|
|
|
|
|
COMPARE_AND_RETURN (la, lb);
|
|
|
|
|
i = 1;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ARRAY_TAG:
|
|
|
|
|
COMPARE_AND_RETURN (la, lb);
|
|
|
|
|
i = 0;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SEXP_TAG: {
|
|
|
|
|
#ifndef DEBUG_PRINT
|
|
|
|
|
int ta = TO_SEXP(p)->tag, tb = TO_SEXP(q)->tag;
|
|
|
|
|
#else
|
|
|
|
|
int ta = GET_SEXP_TAG(TO_SEXP(p)->data_header), tb = GET_SEXP_TAG(TO_SEXP(q)->data_header);
|
|
|
|
|
#endif
|
|
|
|
|
COMPARE_AND_RETURN (ta, tb);
|
|
|
|
|
COMPARE_AND_RETURN (la, lb);
|
|
|
|
|
i = 0;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
failure ("invalid data_header %d in compare *****\n", ta);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (; i<la; i++) {
|
|
|
|
|
int c = Lcompare (((void**) a->contents)[i], ((void**) b->contents)[i]);
|
|
|
|
|
if (c != BOX(0)) return BOX(c);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return BOX(0);
|
|
|
|
|
}
|
|
|
|
|
else return BOX(-1);
|
2020-05-04 02:45:34 +03:00
|
|
|
}
|
2023-04-26 14:22:14 +02:00
|
|
|
else if (is_valid_heap_pointer (q)) return BOX(1);
|
|
|
|
|
else return BOX (p - q);
|
2020-05-04 02:45:34 +03:00
|
|
|
}
|
2019-12-20 00:23:35 +03:00
|
|
|
}
|
|
|
|
|
|
2018-04-30 17:18:41 +03:00
|
|
|
extern void* Belem (void *p, int i) {
|
2023-04-26 14:22:14 +02:00
|
|
|
data *a = (data *)BOX(NULL);
|
|
|
|
|
|
|
|
|
|
ASSERT_BOXED(".elem:1", p);
|
|
|
|
|
ASSERT_UNBOXED(".elem:2", i);
|
|
|
|
|
|
|
|
|
|
a = TO_DATA(p);
|
|
|
|
|
i = UNBOX(i);
|
2020-01-04 21:50:14 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
if (TAG(a->data_header) == STRING_TAG) {
|
|
|
|
|
return (void*) BOX(a->contents[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (void*) ((int*) a->contents)[i];
|
2018-04-30 17:18:41 +03:00
|
|
|
}
|
|
|
|
|
|
2020-01-16 06:59:34 +03:00
|
|
|
extern void* LmakeArray (int length) {
|
2023-04-26 14:22:14 +02:00
|
|
|
data *r;
|
|
|
|
|
int n, *p;
|
2020-01-16 06:59:34 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
ASSERT_UNBOXED("makeArray:1", length);
|
2020-01-16 06:59:34 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
__pre_gc ();
|
|
|
|
|
|
|
|
|
|
n = UNBOX(length);
|
|
|
|
|
r = (data*) alloc_array(n);
|
2020-01-16 06:59:34 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
p = (int*) r->contents;
|
|
|
|
|
while (n--) *p++ = BOX(0);
|
2020-01-16 06:59:34 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
__post_gc ();
|
2020-01-16 06:59:34 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
return r->contents;
|
2020-01-16 06:59:34 +03:00
|
|
|
}
|
|
|
|
|
|
2019-12-21 02:34:56 +03:00
|
|
|
extern void* LmakeString (int length) {
|
2023-04-26 14:22:14 +02:00
|
|
|
int n = UNBOX(length);
|
|
|
|
|
data *r;
|
|
|
|
|
|
|
|
|
|
ASSERT_UNBOXED("makeString", length);
|
|
|
|
|
|
|
|
|
|
__pre_gc () ;
|
2018-12-06 23:04:45 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
r = (data*) alloc_string(n); // '\0' in the end of the string is taken into account
|
2018-04-30 17:18:41 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
__post_gc();
|
2018-12-12 12:42:04 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
return r->contents;
|
2018-04-30 17:18:41 +03:00
|
|
|
}
|
|
|
|
|
|
2019-12-21 02:34:56 +03:00
|
|
|
extern void* Bstring (void *p) {
|
2023-04-26 14:22:14 +02:00
|
|
|
int n = strlen (p);
|
|
|
|
|
void *s = NULL;
|
|
|
|
|
|
|
|
|
|
__pre_gc ();
|
2020-01-28 16:24:26 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2023-04-26 14:22:14 +02:00
|
|
|
indent++; print_indent ();
|
|
|
|
|
printf ("Bstring: call LmakeString %s %p %p %p %i\n", p, &p, p, s, n);
|
|
|
|
|
fflush(stdout);
|
2020-01-28 16:24:26 +03:00
|
|
|
#endif
|
2023-04-26 14:22:14 +02:00
|
|
|
push_extra_root (&p);
|
|
|
|
|
s = LmakeString (BOX(n));
|
|
|
|
|
pop_extra_root(&p);
|
2020-01-28 16:24:26 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2023-04-26 14:22:14 +02:00
|
|
|
print_indent ();
|
|
|
|
|
printf ("\tBstring: call strncpy: %p %p %p %i\n", &p, p, s, n); fflush(stdout);
|
2020-01-28 16:24:26 +03:00
|
|
|
#endif
|
2023-05-23 13:40:46 +02:00
|
|
|
strncpy ((char*) &TO_DATA(s)->contents, p, n + 1); // +1 because of '\0' in the end of C-strings
|
2020-01-28 16:24:26 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2023-04-26 14:22:14 +02:00
|
|
|
print_indent ();
|
|
|
|
|
printf ("\tBstring: ends\n"); fflush(stdout);
|
|
|
|
|
indent--;
|
2020-01-28 16:24:26 +03:00
|
|
|
#endif
|
2023-04-26 14:22:14 +02:00
|
|
|
__post_gc ();
|
|
|
|
|
|
|
|
|
|
return s;
|
2019-12-21 02:34:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern void* Lstringcat (void *p) {
|
2023-04-26 14:22:14 +02:00
|
|
|
void *s;
|
|
|
|
|
|
|
|
|
|
/* ASSERT_BOXED("stringcat", p); */
|
2020-01-28 03:01:54 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
__pre_gc ();
|
|
|
|
|
|
|
|
|
|
createStringBuf ();
|
|
|
|
|
stringcat (p);
|
2019-12-21 02:34:56 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
push_extra_root(&p);
|
|
|
|
|
s = Bstring (stringBuf.contents);
|
|
|
|
|
pop_extra_root(&p);
|
2019-12-21 02:34:56 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
deleteStringBuf ();
|
|
|
|
|
|
|
|
|
|
__post_gc ();
|
2019-12-21 02:34:56 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
return s;
|
2019-12-21 02:34:56 +03:00
|
|
|
}
|
|
|
|
|
|
2021-01-31 22:57:12 +03:00
|
|
|
extern void* Lstring (void *p) {
|
2023-04-26 14:22:14 +02:00
|
|
|
void *s = (void *) BOX (NULL);
|
|
|
|
|
|
|
|
|
|
__pre_gc () ;
|
2018-12-12 12:42:04 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
createStringBuf ();
|
|
|
|
|
printValue (p);
|
2018-10-31 20:10:50 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
push_extra_root(&p);
|
|
|
|
|
s = Bstring (stringBuf.contents);
|
|
|
|
|
pop_extra_root(&p);
|
2018-10-31 20:10:50 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
deleteStringBuf ();
|
2018-12-12 12:42:04 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
__post_gc ();
|
|
|
|
|
|
|
|
|
|
return s;
|
2018-10-31 20:10:50 +03:00
|
|
|
}
|
|
|
|
|
|
2020-02-13 18:56:27 +03:00
|
|
|
extern void* Bclosure (int bn, void *entry, ...) {
|
2023-04-26 14:22:14 +02:00
|
|
|
va_list args;
|
|
|
|
|
int i, ai;
|
|
|
|
|
register int * ebp asm ("ebp");
|
|
|
|
|
size_t *argss;
|
|
|
|
|
data *r;
|
|
|
|
|
int n = UNBOX(bn);
|
|
|
|
|
|
|
|
|
|
__pre_gc ();
|
2019-10-16 01:13:52 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2023-04-26 14:22:14 +02:00
|
|
|
indent++; print_indent ();
|
2019-10-16 01:13:52 +03:00
|
|
|
printf ("Bclosure: create n = %d\n", n); fflush(stdout);
|
|
|
|
|
#endif
|
2023-04-26 14:22:14 +02:00
|
|
|
argss = (ebp + 12);
|
|
|
|
|
for (i = 0; i<n; i++, argss++) {
|
|
|
|
|
push_extra_root ((void**)argss);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r = (data*) alloc_closure(n + 1);
|
|
|
|
|
|
|
|
|
|
((void**) r->contents)[0] = entry;
|
|
|
|
|
|
|
|
|
|
va_start(args, entry);
|
|
|
|
|
|
|
|
|
|
for (i = 0; i<n; i++) {
|
|
|
|
|
ai = va_arg(args, int);
|
|
|
|
|
((int*)r->contents)[i+1] = ai;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
|
|
__post_gc();
|
|
|
|
|
|
|
|
|
|
argss--;
|
|
|
|
|
for (i = 0; i<n; i++, argss--) {
|
|
|
|
|
pop_extra_root ((void**)argss);
|
|
|
|
|
}
|
2020-02-07 19:42:24 +03:00
|
|
|
|
|
|
|
|
#ifdef DEBUG_PRINT
|
2023-04-26 14:22:14 +02:00
|
|
|
print_indent ();
|
|
|
|
|
printf ("Bclosure: ends\n", n); fflush(stdout);
|
|
|
|
|
indent--;
|
2020-02-07 19:42:24 +03:00
|
|
|
#endif
|
|
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
return r->contents;
|
2019-10-16 01:13:52 +03:00
|
|
|
}
|
|
|
|
|
|
2020-02-13 18:56:27 +03:00
|
|
|
extern void* Barray (int bn, ...) {
|
2023-04-26 14:22:14 +02:00
|
|
|
va_list args;
|
|
|
|
|
int i, ai;
|
|
|
|
|
data *r;
|
|
|
|
|
int n = UNBOX(bn);
|
|
|
|
|
|
|
|
|
|
__pre_gc ();
|
|
|
|
|
|
2018-12-05 19:06:07 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2023-04-26 14:22:14 +02:00
|
|
|
indent++; print_indent ();
|
2019-04-24 17:34:53 +03:00
|
|
|
printf ("Barray: create n = %d\n", n); fflush(stdout);
|
2018-12-05 19:06:07 +03:00
|
|
|
#endif
|
2023-04-26 14:22:14 +02:00
|
|
|
r = (data*) alloc_array(n);
|
|
|
|
|
|
|
|
|
|
va_start(args, bn);
|
|
|
|
|
|
|
|
|
|
for (i = 0; i<n; i++) {
|
|
|
|
|
ai = va_arg(args, int);
|
|
|
|
|
((int*)r->contents)[i] = ai;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
|
|
__post_gc();
|
2020-02-07 19:42:24 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2023-04-26 14:22:14 +02:00
|
|
|
indent--;
|
2020-02-07 19:42:24 +03:00
|
|
|
#endif
|
2023-05-23 13:40:46 +02:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
return r->contents;
|
2018-04-30 17:18:41 +03:00
|
|
|
}
|
|
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
#ifdef DEBUG_VERSION
|
|
|
|
|
extern memory_chunk heap;
|
|
|
|
|
#endif
|
|
|
|
|
|
2020-02-13 18:56:27 +03:00
|
|
|
extern void* Bsexp (int bn, ...) {
|
2023-04-26 14:22:14 +02:00
|
|
|
va_list args;
|
|
|
|
|
int i;
|
|
|
|
|
int ai;
|
|
|
|
|
size_t *p;
|
|
|
|
|
sexp *r;
|
|
|
|
|
data *d;
|
|
|
|
|
int n = UNBOX(bn);
|
|
|
|
|
|
|
|
|
|
__pre_gc () ;
|
|
|
|
|
|
2018-12-12 12:42:04 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2023-04-26 14:22:14 +02:00
|
|
|
indent++; print_indent ();
|
2019-04-24 17:34:53 +03:00
|
|
|
printf("Bsexp: allocate %zu!\n",sizeof(int) * (n+1)); fflush (stdout);
|
2018-12-12 12:42:04 +03:00
|
|
|
#endif
|
2023-04-26 14:22:14 +02:00
|
|
|
int fields_cnt = n - 1;
|
|
|
|
|
r = (sexp*) alloc_sexp(fields_cnt);
|
|
|
|
|
d = &(r->contents);
|
|
|
|
|
r->tag = 0;
|
|
|
|
|
|
|
|
|
|
va_start(args, bn);
|
|
|
|
|
|
|
|
|
|
for (i=0; i<n-1; i++) {
|
|
|
|
|
ai = va_arg(args, int);
|
|
|
|
|
|
|
|
|
|
#ifdef DEBUG_VERSION
|
|
|
|
|
if (!UNBOXED(ai)) {
|
|
|
|
|
assert(is_valid_heap_pointer((size_t *) ai));
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
p = (size_t*) ai;
|
|
|
|
|
((int*)d->contents)[i] = ai;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r->tag = UNBOX(va_arg(args, int));
|
2019-04-24 16:02:29 +03:00
|
|
|
|
2019-04-24 17:34:53 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2023-04-26 14:22:14 +02:00
|
|
|
r->data_header = SEXP_TAG | ((r->data_header) << 3);
|
|
|
|
|
print_indent ();
|
|
|
|
|
printf("Bsexp: ends\n"); fflush (stdout);
|
|
|
|
|
indent--;
|
2019-04-24 17:34:53 +03:00
|
|
|
#endif
|
|
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
va_end(args);
|
2018-04-30 17:18:41 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
__post_gc();
|
2018-12-12 12:42:04 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
return d->contents;
|
2018-04-30 17:18:41 +03:00
|
|
|
}
|
|
|
|
|
|
2018-11-05 20:17:11 +03:00
|
|
|
extern int Btag (void *d, int t, int n) {
|
2023-04-26 14:22:14 +02:00
|
|
|
data *r;
|
|
|
|
|
|
|
|
|
|
if (UNBOXED(d)) return BOX(0);
|
|
|
|
|
else {
|
|
|
|
|
r = TO_DATA(d);
|
2019-04-24 17:34:53 +03:00
|
|
|
#ifndef DEBUG_PRINT
|
2023-04-26 14:22:14 +02:00
|
|
|
return BOX(TAG(r->data_header) == SEXP_TAG && TO_SEXP(d)->tag == UNBOX(t) && LEN(r->data_header) == UNBOX(n));
|
2019-04-24 17:34:53 +03:00
|
|
|
#else
|
2023-04-26 14:22:14 +02:00
|
|
|
return BOX(TAG(r->data_header) == SEXP_TAG &&
|
|
|
|
|
GET_SEXP_TAG(TO_SEXP(d)->data_header) == UNBOX(test_small_tree_compaction) && LEN(r->data_header) == UNBOX(n));
|
2019-04-24 17:34:53 +03:00
|
|
|
#endif
|
2023-04-26 14:22:14 +02:00
|
|
|
}
|
2018-04-30 17:18:41 +03:00
|
|
|
}
|
2018-11-06 00:21:38 +03:00
|
|
|
|
2023-05-23 13:40:46 +02:00
|
|
|
int get_tag(data *d) {
|
|
|
|
|
// printf("%")
|
|
|
|
|
return TAG(d->data_header);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int get_len(data *d) {
|
|
|
|
|
return LEN(d->data_header);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-06 00:21:38 +03:00
|
|
|
extern int Barray_patt (void *d, int n) {
|
2023-04-26 14:22:14 +02:00
|
|
|
data *r;
|
|
|
|
|
|
|
|
|
|
if (UNBOXED(d)) return BOX(0);
|
|
|
|
|
else {
|
|
|
|
|
r = TO_DATA(d);
|
2023-05-23 13:40:46 +02:00
|
|
|
return BOX(get_tag(r) == ARRAY_TAG && get_len(r) == UNBOX(n));
|
2023-04-26 14:22:14 +02:00
|
|
|
}
|
2018-11-06 00:21:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern int Bstring_patt (void *x, void *y) {
|
2023-04-26 14:22:14 +02:00
|
|
|
data *rx = (data *) BOX (NULL),
|
|
|
|
|
*ry = (data *) BOX (NULL);
|
|
|
|
|
|
|
|
|
|
ASSERT_STRING(".string_patt:2", y);
|
|
|
|
|
|
|
|
|
|
if (UNBOXED(x)) return BOX(0);
|
|
|
|
|
else {
|
|
|
|
|
rx = TO_DATA(x); ry = TO_DATA(y);
|
|
|
|
|
|
|
|
|
|
if (TAG(rx->data_header) != STRING_TAG) return BOX(0);
|
2018-11-06 00:21:38 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
return BOX(strcmp (rx->contents, ry->contents) == 0 ? 1 : 0); // TODO: ???
|
|
|
|
|
}
|
2018-11-06 00:21:38 +03:00
|
|
|
}
|
|
|
|
|
|
2019-10-16 01:13:52 +03:00
|
|
|
extern int Bclosure_tag_patt (void *x) {
|
2023-04-26 14:22:14 +02:00
|
|
|
if (UNBOXED(x)) return BOX(0);
|
|
|
|
|
|
|
|
|
|
return BOX(TAG(TO_DATA(x)->data_header) == CLOSURE_TAG);
|
2019-10-16 01:13:52 +03:00
|
|
|
}
|
|
|
|
|
|
2018-11-06 00:21:38 +03:00
|
|
|
extern int Bboxed_patt (void *x) {
|
2023-05-23 13:40:46 +02:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
return BOX(UNBOXED(x) ? 0 : 1);
|
2018-11-06 00:21:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern int Bunboxed_patt (void *x) {
|
2023-04-26 14:22:14 +02:00
|
|
|
return BOX(UNBOXED(x) ? 1 : 0);
|
2018-11-06 00:21:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern int Barray_tag_patt (void *x) {
|
2023-04-26 14:22:14 +02:00
|
|
|
if (UNBOXED(x)) return BOX(0);
|
|
|
|
|
|
|
|
|
|
return BOX(TAG(TO_DATA(x)->data_header) == ARRAY_TAG);
|
2018-11-06 00:21:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern int Bstring_tag_patt (void *x) {
|
2023-04-26 14:22:14 +02:00
|
|
|
if (UNBOXED(x)) return BOX(0);
|
|
|
|
|
|
|
|
|
|
return BOX(TAG(TO_DATA(x)->data_header) == STRING_TAG);
|
2018-11-06 00:21:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern int Bsexp_tag_patt (void *x) {
|
2023-04-26 14:22:14 +02:00
|
|
|
if (UNBOXED(x)) return BOX(0);
|
|
|
|
|
|
|
|
|
|
return BOX(TAG(TO_DATA(x)->data_header) == SEXP_TAG);
|
2018-11-06 00:21:38 +03:00
|
|
|
}
|
2018-04-30 17:18:41 +03:00
|
|
|
|
2019-04-11 16:24:57 +03:00
|
|
|
extern void* Bsta (void *v, int i, void *x) {
|
2023-04-26 14:22:14 +02:00
|
|
|
if (UNBOXED(i)) {
|
|
|
|
|
ASSERT_BOXED(".sta:3", x);
|
|
|
|
|
// ASSERT_UNBOXED(".sta:2", i);
|
2020-11-24 23:11:17 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
if (TAG(TO_DATA(x)->data_header) == STRING_TAG)((char*) x)[UNBOX(i)] = (char) UNBOX(v);
|
|
|
|
|
else ((int*) x)[UNBOX(i)] = (int) v;
|
|
|
|
|
|
|
|
|
|
return v;
|
|
|
|
|
}
|
2020-11-24 23:11:17 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
* (void**) x = v;
|
2019-04-11 16:24:57 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
return v;
|
2019-04-10 22:15:08 +03:00
|
|
|
}
|
2018-04-30 17:18:41 +03:00
|
|
|
|
2019-12-21 02:34:56 +03:00
|
|
|
static void fix_unboxed (char *s, va_list va) {
|
2023-04-26 14:22:14 +02:00
|
|
|
size_t *p = (size_t*)va;
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
|
|
while (*s) {
|
|
|
|
|
if (*s == '%') {
|
|
|
|
|
size_t n = p [i];
|
|
|
|
|
if (UNBOXED (n)) {
|
|
|
|
|
p[i] = UNBOX(n);
|
|
|
|
|
}
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
s++;
|
2019-12-18 18:44:01 +03:00
|
|
|
}
|
2019-12-21 02:34:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern void Lfailure (char *s, ...) {
|
2023-04-26 14:22:14 +02:00
|
|
|
va_list args;
|
|
|
|
|
|
|
|
|
|
va_start (args, s);
|
|
|
|
|
fix_unboxed (s, args);
|
|
|
|
|
vfailure (s, args);
|
2018-04-30 17:18:41 +03:00
|
|
|
}
|
|
|
|
|
|
2019-12-24 03:59:05 +03:00
|
|
|
extern void Bmatch_failure (void *v, char *fname, int line, int col) {
|
2023-04-26 14:22:14 +02:00
|
|
|
createStringBuf ();
|
|
|
|
|
printValue (v);
|
|
|
|
|
failure ("match failure at %s:%d:%d, value '%s'\n",
|
|
|
|
|
fname, UNBOX(line), UNBOX(col), stringBuf.contents);
|
2019-12-24 03:59:05 +03:00
|
|
|
}
|
|
|
|
|
|
2020-01-03 01:38:49 +03:00
|
|
|
extern void* /*Lstrcat*/ Li__Infix_4343 (void *a, void *b) {
|
2023-04-26 14:22:14 +02:00
|
|
|
data *da = (data*) BOX (NULL);
|
|
|
|
|
data *db = (data*) BOX (NULL);
|
|
|
|
|
data *d = (data*) BOX (NULL);
|
|
|
|
|
|
|
|
|
|
ASSERT_STRING("++:1", a);
|
|
|
|
|
ASSERT_STRING("++:2", b);
|
|
|
|
|
|
|
|
|
|
da = TO_DATA(a);
|
|
|
|
|
db = TO_DATA(b);
|
|
|
|
|
|
|
|
|
|
__pre_gc () ;
|
2018-12-06 23:04:45 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
push_extra_root (&a);
|
|
|
|
|
push_extra_root (&b);
|
|
|
|
|
d = alloc_string(LEN(da->data_header) + LEN(db->data_header));
|
|
|
|
|
pop_extra_root (&b);
|
|
|
|
|
pop_extra_root (&a);
|
2018-12-06 23:04:45 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
da = TO_DATA(a);
|
|
|
|
|
db = TO_DATA(b);
|
2020-02-07 19:42:24 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
strncpy (d->contents , da->contents, LEN(da->data_header));
|
|
|
|
|
strncpy (d->contents + LEN(da->data_header), db->contents, LEN(db->data_header));
|
2018-05-25 09:53:10 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
d->contents[LEN(da->data_header) + LEN(db->data_header)] = 0;
|
2018-05-25 09:53:10 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
__post_gc();
|
2018-05-25 09:53:10 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
return d->contents;
|
2018-05-25 09:53:10 +03:00
|
|
|
}
|
|
|
|
|
|
2019-12-21 02:34:56 +03:00
|
|
|
extern void* Lsprintf (char * fmt, ...) {
|
2023-04-26 14:22:14 +02:00
|
|
|
va_list args;
|
|
|
|
|
void *s;
|
|
|
|
|
|
|
|
|
|
ASSERT_STRING("sprintf:1", fmt);
|
2019-12-21 02:34:56 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
va_start (args, fmt);
|
|
|
|
|
fix_unboxed (fmt, args);
|
2019-12-21 02:34:56 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
createStringBuf ();
|
2019-12-21 02:34:56 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
vprintStringBuf (fmt, args);
|
2019-12-21 02:34:56 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
__pre_gc ();
|
2019-12-21 02:34:56 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
push_extra_root ((void**)&fmt);
|
|
|
|
|
s = Bstring (stringBuf.contents);
|
|
|
|
|
pop_extra_root ((void**)&fmt);
|
2019-12-21 02:34:56 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
__post_gc ();
|
|
|
|
|
|
|
|
|
|
deleteStringBuf ();
|
|
|
|
|
|
|
|
|
|
return s;
|
2019-12-21 02:34:56 +03:00
|
|
|
}
|
|
|
|
|
|
2020-03-13 19:41:14 +03:00
|
|
|
extern void* LgetEnv (char *var) {
|
2023-04-26 14:22:14 +02:00
|
|
|
char *e = getenv (var);
|
|
|
|
|
void *s;
|
|
|
|
|
|
|
|
|
|
if (e == NULL)
|
|
|
|
|
return (void*) BOX(0); // TODO add (void*) cast?
|
2020-03-13 19:41:14 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
__pre_gc ();
|
2020-03-13 19:41:14 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
s = Bstring (e);
|
2020-03-13 19:41:14 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
__post_gc ();
|
2020-03-13 19:41:14 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
return s;
|
2020-03-13 19:41:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern int Lsystem (char *cmd) {
|
2023-04-26 14:22:14 +02:00
|
|
|
return BOX (system (cmd));
|
2020-03-13 19:41:14 +03:00
|
|
|
}
|
|
|
|
|
|
2018-10-23 23:18:00 +03:00
|
|
|
extern void Lfprintf (FILE *f, char *s, ...) {
|
2023-04-26 14:22:14 +02:00
|
|
|
va_list args = (va_list) BOX (NULL);
|
|
|
|
|
|
|
|
|
|
ASSERT_BOXED("fprintf:1", f);
|
|
|
|
|
ASSERT_STRING("fprintf:2", s);
|
|
|
|
|
|
|
|
|
|
va_start (args, s);
|
|
|
|
|
fix_unboxed (s, args);
|
2018-04-30 17:18:41 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
if (vfprintf (f, s, args) < 0) {
|
|
|
|
|
failure ("fprintf (...): %s\n", strerror (errno));
|
|
|
|
|
}
|
2019-12-21 02:34:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern void Lprintf (char *s, ...) {
|
2023-04-26 14:22:14 +02:00
|
|
|
va_list args = (va_list) BOX (NULL);
|
|
|
|
|
|
|
|
|
|
ASSERT_STRING("printf:1", s);
|
2019-12-21 02:34:56 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
va_start (args, s);
|
|
|
|
|
fix_unboxed (s, args);
|
2020-01-04 21:50:14 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
if (vprintf (s, args) < 0) {
|
|
|
|
|
failure ("fprintf (...): %s\n", strerror (errno));
|
|
|
|
|
}
|
2020-01-15 05:24:35 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
fflush (stdout);
|
2018-04-30 17:18:41 +03:00
|
|
|
}
|
|
|
|
|
|
2018-10-23 23:18:00 +03:00
|
|
|
extern FILE* Lfopen (char *f, char *m) {
|
2023-04-26 14:22:14 +02:00
|
|
|
FILE* h;
|
|
|
|
|
|
|
|
|
|
ASSERT_STRING("fopen:1", f);
|
|
|
|
|
ASSERT_STRING("fopen:2", m);
|
2019-12-21 02:34:56 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
h = fopen (f, m);
|
2020-01-04 21:50:14 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
if (h)
|
|
|
|
|
return h;
|
2019-12-21 02:34:56 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
failure ("fopen (\"%s\", \"%s\"): %s, %s, %s\n", f, m, strerror (errno));
|
2018-04-30 17:18:41 +03:00
|
|
|
}
|
|
|
|
|
|
2018-10-23 23:18:00 +03:00
|
|
|
extern void Lfclose (FILE *f) {
|
2023-04-26 14:22:14 +02:00
|
|
|
ASSERT_BOXED("fclose", f);
|
2020-01-04 21:50:14 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
fclose (f);
|
2018-05-16 09:24:40 +03:00
|
|
|
}
|
2019-12-21 02:34:56 +03:00
|
|
|
|
|
|
|
|
extern void* LreadLine () {
|
2023-04-26 14:22:14 +02:00
|
|
|
char *buf;
|
2019-12-21 02:34:56 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
if (scanf ("%m[^\n]", &buf) == 1) {
|
|
|
|
|
void * s = Bstring (buf);
|
2020-02-25 01:19:15 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
getchar ();
|
|
|
|
|
|
|
|
|
|
free (buf);
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (errno != 0)
|
|
|
|
|
failure ("readLine (): %s\n", strerror (errno));
|
2019-12-21 02:34:56 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
return (void*) BOX (0);
|
2019-12-21 02:34:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern void* Lfread (char *fname) {
|
2023-04-26 14:22:14 +02:00
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
|
|
ASSERT_STRING("fread", fname);
|
|
|
|
|
|
|
|
|
|
f = fopen (fname, "r");
|
|
|
|
|
|
|
|
|
|
if (f) {
|
|
|
|
|
if (fseek (f, 0l, SEEK_END) >= 0) {
|
|
|
|
|
long size = ftell (f);
|
|
|
|
|
void *s = LmakeString (BOX(size));
|
|
|
|
|
|
|
|
|
|
rewind (f);
|
|
|
|
|
|
|
|
|
|
if (fread (s, 1, size, f) == size) {
|
|
|
|
|
fclose (f);
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-21 02:34:56 +03:00
|
|
|
}
|
|
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
failure ("fread (\"%s\"): %s\n", fname, strerror (errno));
|
2019-12-21 02:34:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern void Lfwrite (char *fname, char *contents) {
|
2023-04-26 14:22:14 +02:00
|
|
|
FILE *f;
|
2020-01-04 21:50:14 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
ASSERT_STRING("fwrite:1", fname);
|
|
|
|
|
ASSERT_STRING("fwrite:2", contents);
|
2019-12-21 02:34:56 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
f = fopen (fname, "w");
|
|
|
|
|
|
|
|
|
|
if (f) {
|
|
|
|
|
if (fprintf (f, "%s", contents) < 0);
|
|
|
|
|
else {
|
|
|
|
|
fclose (f);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-12-21 02:34:56 +03:00
|
|
|
}
|
|
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
failure ("fwrite (\"%s\"): %s\n", fname, strerror (errno));
|
2019-12-21 02:34:56 +03:00
|
|
|
}
|
|
|
|
|
|
2021-11-19 01:38:22 +03:00
|
|
|
extern void* Lfexists (char *fname) {
|
2023-04-26 14:22:14 +02:00
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
|
|
ASSERT_STRING("fexists", fname);
|
2021-11-19 01:38:22 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
f = fopen (fname, "r");
|
2021-11-19 01:38:22 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
if (f) return (void*) BOX(1); // (void*) cast?
|
2021-11-19 01:38:22 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
return (void*) BOX(0); // (void*) cast?
|
2021-11-19 01:38:22 +03:00
|
|
|
}
|
|
|
|
|
|
2019-12-24 03:59:05 +03:00
|
|
|
extern void* Lfst (void *v) {
|
2023-04-26 14:22:14 +02:00
|
|
|
return Belem (v, BOX(0));
|
2019-12-24 03:59:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern void* Lsnd (void *v) {
|
2023-04-26 14:22:14 +02:00
|
|
|
return Belem (v, BOX(1));
|
2019-12-24 03:59:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern void* Lhd (void *v) {
|
2023-04-26 14:22:14 +02:00
|
|
|
return Belem (v, BOX(0));
|
2019-12-24 03:59:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern void* Ltl (void *v) {
|
2023-04-26 14:22:14 +02:00
|
|
|
return Belem (v, BOX(1));
|
2019-12-24 03:59:05 +03:00
|
|
|
}
|
|
|
|
|
|
2018-03-04 23:13:08 +03:00
|
|
|
/* Lread is an implementation of the "read" construct */
|
|
|
|
|
extern int Lread () {
|
2023-04-26 14:22:14 +02:00
|
|
|
int result = BOX(0);
|
2018-03-04 23:13:08 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
printf ("> ");
|
|
|
|
|
fflush (stdout);
|
|
|
|
|
scanf ("%d", &result);
|
2018-03-04 23:13:08 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
return BOX(result);
|
2018-03-04 23:13:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Lwrite is an implementation of the "write" construct */
|
|
|
|
|
extern int Lwrite (int n) {
|
2023-04-26 14:22:14 +02:00
|
|
|
printf ("%d\n", UNBOX(n));
|
|
|
|
|
fflush (stdout);
|
2018-03-04 23:13:08 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
return 0;
|
2018-03-04 23:13:08 +03:00
|
|
|
}
|
2018-04-30 17:18:41 +03:00
|
|
|
|
2020-08-02 23:56:21 +03:00
|
|
|
extern int Lrandom (int n) {
|
2023-04-26 14:22:14 +02:00
|
|
|
ASSERT_UNBOXED("Lrandom, 0", n);
|
|
|
|
|
|
|
|
|
|
if (UNBOX(n) <= 0) {
|
|
|
|
|
failure ("invalid range in random: %d\n", UNBOX(n));
|
|
|
|
|
}
|
2020-08-02 23:56:21 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
return BOX (random () % UNBOX(n));
|
2020-08-02 23:56:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern int Ltime () {
|
2023-04-26 14:22:14 +02:00
|
|
|
struct timespec t;
|
|
|
|
|
|
|
|
|
|
clock_gettime (CLOCK_MONOTONIC_RAW, &t);
|
|
|
|
|
|
|
|
|
|
return BOX(t.tv_sec * 1000000 + t.tv_nsec / 1000);
|
2020-08-02 23:56:21 +03:00
|
|
|
}
|
|
|
|
|
|
2020-01-28 03:01:54 +03:00
|
|
|
extern void set_args (int argc, char *argv[]) {
|
2023-04-26 14:22:14 +02:00
|
|
|
data *a;
|
|
|
|
|
int n = argc, *p = NULL;
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
__pre_gc ();
|
2020-01-28 03:01:54 +03:00
|
|
|
|
2020-01-28 18:06:09 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2023-04-26 14:22:14 +02:00
|
|
|
indent++; print_indent ();
|
2020-02-07 19:42:24 +03:00
|
|
|
printf ("set_args: call: n=%i &p=%p p=%p: ", n, &p, p); fflush(stdout);
|
|
|
|
|
for (i = 0; i < n; i++)
|
2020-01-29 14:21:17 +03:00
|
|
|
printf("%s ", argv[i]);
|
2020-02-07 19:42:24 +03:00
|
|
|
printf("EE\n");
|
2020-01-28 18:06:09 +03:00
|
|
|
#endif
|
|
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
p = LmakeArray (BOX(n));
|
|
|
|
|
push_extra_root ((void**)&p);
|
|
|
|
|
|
|
|
|
|
for (i=0; i<n; i++) {
|
2020-01-28 18:06:09 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2023-04-26 14:22:14 +02:00
|
|
|
print_indent ();
|
2020-01-28 18:06:09 +03:00
|
|
|
printf ("set_args: iteration %i %p %p ->\n", i, &p, p); fflush(stdout);
|
|
|
|
|
#endif
|
2023-04-26 14:22:14 +02:00
|
|
|
((int*)p) [i] = (int) Bstring (argv[i]);
|
2020-01-28 18:06:09 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2023-04-26 14:22:14 +02:00
|
|
|
print_indent ();
|
2020-01-28 18:06:09 +03:00
|
|
|
printf ("set_args: iteration %i <- %p %p\n", i, &p, p); fflush(stdout);
|
|
|
|
|
#endif
|
2023-04-26 14:22:14 +02:00
|
|
|
}
|
2020-02-07 19:42:24 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
pop_extra_root ((void**)&p);
|
|
|
|
|
__post_gc ();
|
2020-01-28 03:01:54 +03:00
|
|
|
|
2023-04-26 14:22:14 +02:00
|
|
|
global_sysargs = p;
|
|
|
|
|
push_extra_root ((void**)&global_sysargs);
|
2020-02-07 19:42:24 +03:00
|
|
|
#ifdef DEBUG_PRINT
|
2023-04-26 14:22:14 +02:00
|
|
|
print_indent ();
|
2020-02-07 19:42:24 +03:00
|
|
|
printf ("set_args: end\n", n, &p, p); fflush(stdout);
|
|
|
|
|
indent--;
|
|
|
|
|
#endif
|
2020-01-28 03:01:54 +03:00
|
|
|
}
|
|
|
|
|
|
2018-11-06 16:18:09 +03:00
|
|
|
/* GC starts here */
|
|
|
|
|
|
2020-08-02 23:56:21 +03:00
|
|
|
static int enable_GC = 1;
|
|
|
|
|
|
|
|
|
|
extern void LenableGC () {
|
2023-04-26 14:22:14 +02:00
|
|
|
enable_GC = 1;
|
2020-08-02 23:56:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern void LdisableGC () {
|
2023-04-26 14:22:14 +02:00
|
|
|
enable_GC = 0;
|
2018-12-05 18:31:12 +03:00
|
|
|
}
|