Rewrite c-version so that we can add multi-character words

This commit is contained in:
Koichi Nakamura 2021-01-06 01:51:01 +09:00
parent 218112f13c
commit 76a01a127a

View file

@ -64,15 +64,32 @@ static void label()
#include "planck.c" #include "planck.c"
#undef defcode #undef defcode
static void align() {
here = (cell*)((((cell)here) + CELL - 1) & ~(CELL - 1));
}
static void comma(cell v) { *here++ = v; }
static void comma_byte(char c) {
*(char*)here = c;
here = (cell*)(((char*)here) + 1);
}
static void comma_string(char *s) {
while (*s) comma_byte(*s++);
comma_byte(0);
}
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
saved_argc = argc; saved_argc = argc;
saved_argv = argv; saved_argv = argv;
#define defcode(c, label) \ #define defcode(name, label) \
memcpy(here, &(builtin){ latest, 1, c, {0}, label }, 3*CELL); \ comma((cell) latest); \
latest = (builtin*)here; \ latest = (void*)here - CELL; \
here += 3; \ comma_byte(strlen(name)); \
comma_string(name); \
align(); \
comma((cell) label); \
if (0) // skip function body if (0) // skip function body
#include "planck.c" #include "planck.c"
cfa start = (cfa) here; cfa start = (cfa) here;
@ -86,54 +103,55 @@ int main(int argc, char *argv[]) {
return 0; return 0;
} }
#else #else
defcode('Q', quit) { exit(0); } defcode("Q", quit) { exit(0); }
defcode('C', cell_) { push(CELL); next(); } defcode("C", cell_) { push(CELL); next(); }
defcode('h', here_) { push((cell)&here); next(); } defcode("h", here_) { push((cell)&here); next(); }
defcode('l', latest_) { push((cell)&latest); next(); } defcode("l", latest_) { push((cell)&latest); next(); }
defcode('i', docol_) { push((cell)docol); next(); } defcode("i", docol_) { push((cell)docol); next(); }
defcode('e', exit_) { pc = (cell*)rpop(); next(); } defcode("e", exit_) { pc = (cell*)rpop(); next(); }
defcode('@', fetch) { cell *p = (cell*)pop(); push(*p); next(); } defcode("@", fetch) { cell *p = (cell*)pop(); push(*p); next(); }
defcode('!', store) { cell *p = (cell*)pop(); *p = pop(); next(); } defcode("!", store) { cell *p = (cell*)pop(); *p = pop(); next(); }
defcode('?', cfetch) { char *p = (char*)pop(); push(*p); next(); } defcode("?", cfetch) { char *p = (char*)pop(); push(*p); next(); }
defcode('$', cstore) { char *p = (char*)pop(); *p = pop(); next(); } defcode("$", cstore) { char *p = (char*)pop(); *p = pop(); next(); }
defcode('d', dfetch) { push((cell)dsp); next(); } defcode("d", dfetch) { push((cell)dsp); next(); }
defcode('D', dstore) { dsp = (cell*) pop(); next(); } defcode("D", dstore) { dsp = (cell*) pop(); next(); }
defcode('r', rfetch) { push((cell)rsp); next(); } defcode("r", rfetch) { push((cell)rsp); next(); }
defcode('R', rstore) { rsp = (cell*) pop(); next(); } defcode("R", rstore) { rsp = (cell*) pop(); next(); }
defcode('j', jump) { pc += (int)*pc/CELL; next(); } defcode("j", jump) { pc += (int)*pc/CELL; next(); }
defcode('J', jump0) { pc += (int)(pop()?1:*pc/CELL); next(); } defcode("J", jump0) { pc += (int)(pop()?1:*pc/CELL); next(); }
defcode('L', lit) { push(*pc++); next(); } defcode("L", lit) { push(*pc++); next(); }
defcode('S', litstring) { defcode("S", litstring) {
int len = *pc++; int n = *pc++;
push((cell) pc); push((cell)pc);
pc += (len + CELL - 1)/CELL; pc += (n + CELL - 1)/CELL;
next();
} }
defcode('k', key) { defcode("k", key) {
int c = getchar(); int c = getchar();
if (c <= 0) if (c <= 0)
exit(0); exit(0);
push(c); push(c);
next(); next();
} }
defcode('t', type) { putchar(pop()); next(); } defcode("t", type) { putchar(pop()); next(); }
defcode('x', exec) { (*(ip = (cfa) pop()))(); } defcode("x", exec) { (*(ip = (cfa) pop()))(); }
defcode('f', find_) { push((cell) find(pop())); next(); } defcode("f", find_) { push((cell) find(pop())); next(); }
defcode('v', argv_) { push((cell) saved_argv); push(saved_argc); next(); } defcode("v", argv_) { push((cell) saved_argv); push(saved_argc); next(); }
#define defbinary(c, label, op, ty) \ #define defbinary(name, label, op, ty) \
defcode(c, label) { \ defcode(name, label) { \
ty b = (ty) pop(); \ ty b = (ty) pop(); \
*dsp = (cell)((ty) *dsp op b); \ *dsp = (cell)((ty) *dsp op b); \
next(); \ next(); \
} }
defbinary('+', add, +, intptr_t) defbinary("+", add, +, intptr_t)
defbinary('-', sub, -, intptr_t) defbinary("-", sub, -, intptr_t)
defbinary('*', mul, *, intptr_t) defbinary("*", mul, *, intptr_t)
defbinary('/', div_, /, intptr_t) defbinary("/", div_, /, uintptr_t)
defbinary('%', mod, %, intptr_t) defbinary("%", mod, %, uintptr_t)
defbinary('&', and, &, uintptr_t) defbinary("&", and, &, uintptr_t)
defbinary('|', or, |, uintptr_t) defbinary("|", or, |, uintptr_t)
defbinary('^', xor, ^, uintptr_t) defbinary("^", xor, ^, uintptr_t)
defbinary('<', lt, <, intptr_t) defbinary("<", lt, <, intptr_t)
defbinary('=', eq, ==, intptr_t) defbinary("=", eq, ==, intptr_t)
#endif #endif