mirror of
https://github.com/forth-ev/VolksForth.git
synced 2024-10-31 19:04:43 +00:00
Initial idea sketch for a C-based VolksForth core.
This commit is contained in:
parent
0037f287c4
commit
f5b9500946
5
portable/README.md
Normal file
5
portable/README.md
Normal 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
62
portable/core.c
Normal 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 won’t 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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user