2018-03-04 23:13:08 +03:00
|
|
|
/* Runtime library */
|
|
|
|
|
|
|
|
|
|
# include <stdio.h>
|
2018-04-30 17:18:41 +03:00
|
|
|
# include <stdio.h>
|
|
|
|
|
# include <malloc.h>
|
|
|
|
|
# include <string.h>
|
|
|
|
|
# include <stdarg.h>
|
|
|
|
|
# include <alloca.h>
|
2018-10-31 20:10:50 +03:00
|
|
|
# include <stdlib.h>
|
2018-04-30 17:18:41 +03:00
|
|
|
|
|
|
|
|
# define STRING_TAG 0x00000000
|
2018-05-16 09:24:40 +03:00
|
|
|
# define ARRAY_TAG 0x01000000
|
|
|
|
|
# define SEXP_TAG 0x02000000
|
2018-04-30 17:18:41 +03:00
|
|
|
|
|
|
|
|
# define LEN(x) (x & 0x00FFFFFF)
|
|
|
|
|
# define TAG(x) (x & 0xFF000000)
|
|
|
|
|
|
|
|
|
|
# define TO_DATA(x) ((data*)((char*)(x)-sizeof(int)))
|
2018-05-16 09:24:40 +03:00
|
|
|
# define TO_SEXP(x) ((sexp*)((char*)(x)-2*sizeof(int)))
|
2018-04-30 17:18:41 +03:00
|
|
|
|
2018-10-31 20:10:50 +03:00
|
|
|
# define UNBOXED(x) (((int) (x)) & 0x0001)
|
|
|
|
|
# define UNBOX(x) (((int) (x)) >> 1)
|
|
|
|
|
# define BOX(x) ((((int) (x)) << 1) | 0x0001)
|
2018-10-25 03:15:24 +03:00
|
|
|
|
2018-04-30 17:18:41 +03:00
|
|
|
typedef struct {
|
|
|
|
|
int tag;
|
|
|
|
|
char contents[0];
|
|
|
|
|
} data;
|
|
|
|
|
|
2018-05-16 09:24:40 +03:00
|
|
|
typedef struct {
|
|
|
|
|
int tag;
|
|
|
|
|
data contents;
|
|
|
|
|
} sexp;
|
|
|
|
|
|
2018-04-30 17:18:41 +03:00
|
|
|
extern int Blength (void *p) {
|
|
|
|
|
data *a = TO_DATA(p);
|
2018-10-25 03:15:24 +03:00
|
|
|
return BOX(LEN(a->tag));
|
2018-04-30 17:18:41 +03:00
|
|
|
}
|
|
|
|
|
|
2018-10-31 20:10:50 +03:00
|
|
|
char* de_hash (int n) {
|
|
|
|
|
static char *chars = "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNJPQRSTUVWXYZ";
|
|
|
|
|
static char buf[6];
|
|
|
|
|
char *p = &buf[5];
|
|
|
|
|
|
|
|
|
|
/*printf ("tag: %d\n", n);*/
|
|
|
|
|
|
|
|
|
|
*p-- = 0;
|
|
|
|
|
|
|
|
|
|
while (n != 0) {
|
|
|
|
|
/*printf ("char: %c\n", chars [n & 0x003F]);*/
|
|
|
|
|
*p-- = chars [n & 0x003F];
|
|
|
|
|
n = n >> 6;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ++p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
char *contents;
|
|
|
|
|
int ptr;
|
|
|
|
|
int len;
|
|
|
|
|
} StringBuf;
|
|
|
|
|
|
|
|
|
|
static StringBuf stringBuf;
|
|
|
|
|
|
|
|
|
|
# define STRINGBUF_INIT 128
|
|
|
|
|
|
|
|
|
|
static void createStringBuf () {
|
|
|
|
|
stringBuf.contents = (char*) malloc (STRINGBUF_INIT);
|
|
|
|
|
stringBuf.ptr = 0;
|
|
|
|
|
stringBuf.len = STRINGBUF_INIT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void deleteStringBuf () {
|
|
|
|
|
free (stringBuf.contents);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void extendStringBuf () {
|
|
|
|
|
int len = stringBuf.len << 1;
|
|
|
|
|
|
|
|
|
|
stringBuf.contents = (char*) realloc (stringBuf.contents, len);
|
|
|
|
|
stringBuf.len = len;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void printStringBuf (char *fmt, ...) {
|
|
|
|
|
va_list args;
|
|
|
|
|
int written, rest;
|
2018-11-06 14:25:28 +03:00
|
|
|
char *buf;
|
2018-10-31 20:10:50 +03:00
|
|
|
|
|
|
|
|
again:
|
|
|
|
|
va_start (args, fmt);
|
2018-11-06 14:25:28 +03:00
|
|
|
buf = &stringBuf.contents[stringBuf.ptr];
|
2018-10-31 20:10:50 +03:00
|
|
|
rest = stringBuf.len - stringBuf.ptr;
|
|
|
|
|
written = vsnprintf (buf, rest, fmt, args);
|
|
|
|
|
|
|
|
|
|
if (written >= rest) {
|
|
|
|
|
extendStringBuf ();
|
|
|
|
|
goto again;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stringBuf.ptr += written;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void printValue (void *p) {
|
|
|
|
|
if (UNBOXED(p)) printStringBuf ("%d", UNBOX(p));
|
|
|
|
|
else {
|
|
|
|
|
data *a = TO_DATA(p);
|
|
|
|
|
|
|
|
|
|
switch (TAG(a->tag)) {
|
|
|
|
|
case STRING_TAG:
|
|
|
|
|
printStringBuf ("\"%s\"", a->contents);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ARRAY_TAG:
|
|
|
|
|
printStringBuf ("[");
|
|
|
|
|
for (int i = 0; i < LEN(a->tag); i++) {
|
|
|
|
|
printValue ((void*)((int*) a->contents)[i]);
|
|
|
|
|
if (i != LEN(a->tag) - 1) printStringBuf (", ");
|
|
|
|
|
}
|
|
|
|
|
printStringBuf ("]");
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SEXP_TAG:
|
|
|
|
|
printStringBuf ("`%s", de_hash (TO_SEXP(p)->tag));
|
2018-10-31 21:48:44 +03:00
|
|
|
if (LEN(a->tag)) {
|
|
|
|
|
printStringBuf (" (");
|
|
|
|
|
for (int i = 0; i < LEN(a->tag); i++) {
|
|
|
|
|
printValue ((void*)((int*) a->contents)[i]);
|
|
|
|
|
if (i != LEN(a->tag) - 1) printStringBuf (", ");
|
|
|
|
|
}
|
|
|
|
|
printStringBuf (")");
|
2018-10-31 20:10:50 +03:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
printStringBuf ("*** invalid tag: %x ***", TAG(a->tag));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-30 17:18:41 +03:00
|
|
|
extern void* Belem (void *p, int i) {
|
|
|
|
|
data *a = TO_DATA(p);
|
2018-10-25 03:15:24 +03:00
|
|
|
i = UNBOX(i);
|
|
|
|
|
|
|
|
|
|
/* printf ("elem %d = %p\n", i, (void*) ((int*) a->contents)[i]); */
|
2018-04-30 17:18:41 +03:00
|
|
|
|
2018-10-25 03:15:24 +03:00
|
|
|
if (TAG(a->tag) == STRING_TAG) {
|
|
|
|
|
return (void*) BOX(a->contents[i]);
|
|
|
|
|
}
|
2018-05-16 09:24:40 +03:00
|
|
|
|
2018-04-30 17:18:41 +03:00
|
|
|
return (void*) ((int*) a->contents)[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern void* Bstring (void *p) {
|
|
|
|
|
int n = strlen (p);
|
|
|
|
|
data *r = (data*) malloc (n + 1 + sizeof (int));
|
|
|
|
|
|
|
|
|
|
r->tag = n;
|
|
|
|
|
strncpy (r->contents, p, n + 1);
|
|
|
|
|
|
|
|
|
|
return r->contents;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-31 20:10:50 +03:00
|
|
|
extern void* Bstringval (void *p) {
|
|
|
|
|
void *s;
|
|
|
|
|
|
|
|
|
|
createStringBuf ();
|
|
|
|
|
printValue (p);
|
|
|
|
|
|
|
|
|
|
s = Bstring (stringBuf.contents);
|
|
|
|
|
|
|
|
|
|
deleteStringBuf ();
|
|
|
|
|
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-30 17:18:41 +03:00
|
|
|
extern void* Barray (int n, ...) {
|
|
|
|
|
va_list args;
|
|
|
|
|
int i;
|
|
|
|
|
data *r = (data*) malloc (sizeof(int) * (n+1));
|
|
|
|
|
|
2018-05-16 09:24:40 +03:00
|
|
|
r->tag = ARRAY_TAG | n;
|
2018-04-30 17:18:41 +03:00
|
|
|
|
|
|
|
|
va_start(args, n);
|
|
|
|
|
|
|
|
|
|
for (i=0; i<n; i++) {
|
|
|
|
|
int ai = va_arg(args, int);
|
|
|
|
|
((int*)r->contents)[i] = ai;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
|
|
return r->contents;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-16 09:24:40 +03:00
|
|
|
extern void* Bsexp (int n, ...) {
|
2018-04-30 17:18:41 +03:00
|
|
|
va_list args;
|
2018-05-16 09:24:40 +03:00
|
|
|
int i;
|
|
|
|
|
sexp *r = (sexp*) malloc (sizeof(int) * (n+2));
|
|
|
|
|
data *d = &(r->contents);
|
2018-04-30 17:18:41 +03:00
|
|
|
|
2018-05-16 09:24:40 +03:00
|
|
|
d->tag = SEXP_TAG | (n-1);
|
|
|
|
|
|
|
|
|
|
va_start(args, n);
|
|
|
|
|
|
2018-04-30 17:18:41 +03:00
|
|
|
for (i=0; i<n-1; i++) {
|
2018-05-16 09:24:40 +03:00
|
|
|
int ai = va_arg(args, int);
|
|
|
|
|
//printf ("arg %d = %x\n", i, ai);
|
|
|
|
|
((int*)d->contents)[i] = ai;
|
2018-04-30 17:18:41 +03:00
|
|
|
}
|
|
|
|
|
|
2018-05-16 09:24:40 +03:00
|
|
|
r->tag = va_arg(args, int);
|
|
|
|
|
va_end(args);
|
2018-04-30 17:18:41 +03:00
|
|
|
|
2018-05-16 09:24:40 +03:00
|
|
|
//printf ("tag %d\n", r->tag);
|
|
|
|
|
//printf ("returning %p\n", d->contents);
|
|
|
|
|
|
|
|
|
|
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) {
|
2018-05-16 09:24:40 +03:00
|
|
|
data *r = TO_DATA(d);
|
2018-11-05 20:17:11 +03:00
|
|
|
return BOX(TAG(r->tag) == SEXP_TAG && TO_SEXP(d)->tag == t && LEN(r->tag) == n);
|
2018-04-30 17:18:41 +03:00
|
|
|
}
|
2018-05-16 09:24:40 +03:00
|
|
|
|
|
|
|
|
extern void Bsta (int n, int v, void *s, ...) {
|
2018-04-30 17:18:41 +03:00
|
|
|
va_list args;
|
2018-05-16 09:24:40 +03:00
|
|
|
int i, k;
|
|
|
|
|
data *a;
|
|
|
|
|
|
|
|
|
|
va_start(args, s);
|
2018-04-30 17:18:41 +03:00
|
|
|
|
|
|
|
|
for (i=0; i<n-1; i++) {
|
2018-10-25 03:15:24 +03:00
|
|
|
k = UNBOX(va_arg(args, int));
|
2018-05-16 09:24:40 +03:00
|
|
|
s = ((int**) s) [k];
|
2018-04-30 17:18:41 +03:00
|
|
|
}
|
|
|
|
|
|
2018-10-25 03:15:24 +03:00
|
|
|
k = UNBOX(va_arg(args, int));
|
2018-05-16 09:24:40 +03:00
|
|
|
a = TO_DATA(s);
|
2018-04-30 17:18:41 +03:00
|
|
|
|
2018-10-31 20:10:50 +03:00
|
|
|
if (TAG(a->tag) == STRING_TAG)((char*) s)[k] = (char) UNBOX(v);
|
2018-05-16 09:24:40 +03:00
|
|
|
else ((int*) s)[k] = v;
|
2018-04-30 17:18:41 +03:00
|
|
|
}
|
|
|
|
|
|
2018-10-23 23:18:00 +03:00
|
|
|
extern int Lraw (int x) {
|
2018-10-31 20:10:50 +03:00
|
|
|
return UNBOX(x);
|
2018-10-23 23:18:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern void Lprintf (char *s, ...) {
|
2018-04-30 17:18:41 +03:00
|
|
|
va_list args;
|
|
|
|
|
|
2018-05-16 09:24:40 +03:00
|
|
|
va_start (args, s);
|
2018-10-31 20:10:50 +03:00
|
|
|
vprintf (s, args); // vprintf (char *, va_list) <-> printf (char *, ...)
|
|
|
|
|
va_end (args);
|
2018-04-30 17:18:41 +03:00
|
|
|
}
|
|
|
|
|
|
2018-10-23 23:18:00 +03:00
|
|
|
extern void* Lstrcat (void *a, void *b) {
|
2018-05-25 09:53:10 +03:00
|
|
|
data *da = TO_DATA(a);
|
|
|
|
|
data *db = TO_DATA(b);
|
|
|
|
|
|
|
|
|
|
data *d = (data *) malloc (sizeof(int) + LEN(da->tag) + LEN(db->tag) + 1);
|
|
|
|
|
|
|
|
|
|
d->tag = LEN(da->tag) + LEN(db->tag);
|
|
|
|
|
|
|
|
|
|
strcpy (d->contents, da->contents);
|
|
|
|
|
strcat (d->contents, db->contents);
|
|
|
|
|
|
|
|
|
|
return d->contents;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-23 23:18:00 +03:00
|
|
|
extern void Lfprintf (FILE *f, char *s, ...) {
|
2018-04-30 17:18:41 +03:00
|
|
|
va_list args;
|
|
|
|
|
|
2018-05-16 09:24:40 +03:00
|
|
|
va_start (args, s);
|
|
|
|
|
vfprintf (f, s, args);
|
2018-10-31 20:10:50 +03:00
|
|
|
va_end (args);
|
2018-04-30 17:18:41 +03:00
|
|
|
}
|
|
|
|
|
|
2018-10-23 23:18:00 +03:00
|
|
|
extern FILE* Lfopen (char *f, char *m) {
|
2018-05-16 09:24:40 +03:00
|
|
|
return fopen (f, m);
|
2018-04-30 17:18:41 +03:00
|
|
|
}
|
|
|
|
|
|
2018-10-23 23:18:00 +03:00
|
|
|
extern void Lfclose (FILE *f) {
|
2018-05-16 09:24:40 +03:00
|
|
|
fclose (f);
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-04 23:13:08 +03:00
|
|
|
/* Lread is an implementation of the "read" construct */
|
|
|
|
|
extern int Lread () {
|
|
|
|
|
int result;
|
|
|
|
|
|
|
|
|
|
printf ("> ");
|
|
|
|
|
fflush (stdout);
|
|
|
|
|
scanf ("%d", &result);
|
|
|
|
|
|
2018-10-25 03:15:24 +03: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) {
|
2018-10-25 03:15:24 +03:00
|
|
|
printf ("%d\n", UNBOX(n));
|
2018-03-04 23:13:08 +03:00
|
|
|
fflush (stdout);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2018-04-30 17:18:41 +03:00
|
|
|
|