Initial idea sketch for a C-based VolksForth core.

This commit is contained in:
Philip Zembrod 2020-06-23 21:53:51 +02:00
parent 0037f287c4
commit f5b9500946
2 changed files with 67 additions and 0 deletions

5
portable/README.md Normal file
View File

@ -0,0 +1,5 @@
# Idea for a "C-VolksForth"
So far anything in this directory is just an early idea for a C-based
core of a VolksForth variant that could be compiled and run on any
platform with a C compiler, e.g. Linux.

62
portable/core.c Normal file
View File

@ -0,0 +1,62 @@
// This is only a sketch of an idea yet.
byte core[0x10000];
uint16 ip;
uint16 w;
void run() {
while (true) {
next();
}
}
void next() {
w = *((uint16*) core + ip);
ip += 2;
byte opcode = core[w];
// on second thought, the above looks more like direct threaded,
// which is maybe not what we want in a C core for VolksForth.
// Or is it?
process(opcode);
}
void process(byte opcode) {
if (opcode & 0xc0) {
// handle special opcode
// This is assuming we wont have more than 64 C primitives,
// and might have use for a few
// opcodes with inlined parameters. Not sure about this, though.
} else {
jumptable[opcode]();
}
}
void jumptable[0x40]() = {
nest, unnest, dup, drop, plus, emit
};
void nest() {
rs[rp++] = ip;
ip = w + 2;
}
void unnest() {
ip = rs[--rp];
}
void dup() {
uint16 = s[sp];
s[++sp] = uint16;
}
void emit() {
putc(s[sp--]);
}
void plus() {
a = s[sp--];
s[sp] += a;
}