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