|
|
@ -43,7 +43,7 @@ void CoreWords::run(unsigned int index, State& state)
|
|
|
|
if (auto j = state.dict.find(word); j > 0)
|
|
|
|
if (auto j = state.dict.find(word); j > 0)
|
|
|
|
state.push(state.dict.getexec(j));
|
|
|
|
state.push(state.dict.getexec(j));
|
|
|
|
else if (auto i = CoreWords::findi(state, word); i >= 0)
|
|
|
|
else if (auto i = CoreWords::findi(state, word); i >= 0)
|
|
|
|
state.push(i & ~CoreWords::Compiletime);
|
|
|
|
state.push(i & ~CoreWords::Immediate);
|
|
|
|
else
|
|
|
|
else
|
|
|
|
state.push(0);
|
|
|
|
state.push(0);
|
|
|
|
};
|
|
|
|
};
|
|
|
@ -57,77 +57,77 @@ void CoreWords::run(unsigned int index, State& state)
|
|
|
|
state.pushr(state.ip);
|
|
|
|
state.pushr(state.ip);
|
|
|
|
state.ip = index - sizeof(Cell);
|
|
|
|
state.ip = index - sizeof(Cell);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 0: // drop
|
|
|
|
case 0: // _lit
|
|
|
|
|
|
|
|
state.push(state.beyondip());
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1: // drop
|
|
|
|
state.pop();
|
|
|
|
state.pop();
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 1: // dup
|
|
|
|
case 2: // dup
|
|
|
|
state.push(state.top());
|
|
|
|
state.push(state.top());
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 2: // swap
|
|
|
|
case 3: // swap
|
|
|
|
std::swap(state.top(), state.pick(1));
|
|
|
|
std::swap(state.top(), state.pick(1));
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 3: // pick
|
|
|
|
case 4: // pick
|
|
|
|
state.push(state.pick(state.pop()));
|
|
|
|
state.push(state.pick(state.pop()));
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 4: // sys
|
|
|
|
case 5: // sys
|
|
|
|
user_sys(state);
|
|
|
|
user_sys(state);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 5: // add
|
|
|
|
case 6: // add
|
|
|
|
cell = state.pop();
|
|
|
|
cell = state.pop();
|
|
|
|
state.top() += cell;
|
|
|
|
state.top() += cell;
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 6: // sub
|
|
|
|
case 7: // sub
|
|
|
|
cell = state.pop();
|
|
|
|
cell = state.pop();
|
|
|
|
state.top() -= cell;
|
|
|
|
state.top() -= cell;
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 7: // mul ( n n -- d )
|
|
|
|
case 8: // mul ( n n -- d )
|
|
|
|
cell = state.pop();
|
|
|
|
cell = state.pop();
|
|
|
|
dcell = state.pop() * cell;
|
|
|
|
dcell = state.pop() * cell;
|
|
|
|
state.push(dcell);
|
|
|
|
state.push(dcell);
|
|
|
|
state.push(dcell >> (sizeof(Cell) * 8));
|
|
|
|
state.push(dcell >> (sizeof(Cell) * 8));
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 8: // div ( d n -- n )
|
|
|
|
case 9: // div ( d n -- n )
|
|
|
|
cell = state.pop();
|
|
|
|
cell = state.pop();
|
|
|
|
dcell = state.pop() << (sizeof(Cell) * 8);
|
|
|
|
dcell = state.pop() << (sizeof(Cell) * 8);
|
|
|
|
dcell |= state.pop();
|
|
|
|
dcell |= state.pop();
|
|
|
|
state.push(dcell / cell);
|
|
|
|
state.push(dcell / cell);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 9: // mod ( d n -- n )
|
|
|
|
case 10: // mod ( d n -- n )
|
|
|
|
cell = state.pop();
|
|
|
|
cell = state.pop();
|
|
|
|
dcell = state.pop() << (sizeof(Cell) * 8);
|
|
|
|
dcell = state.pop() << (sizeof(Cell) * 8);
|
|
|
|
dcell |= state.pop();
|
|
|
|
dcell |= state.pop();
|
|
|
|
state.push(dcell % cell);
|
|
|
|
state.push(dcell % cell);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 10: // peek
|
|
|
|
case 11: // peek
|
|
|
|
if (state.pop())
|
|
|
|
if (state.pop())
|
|
|
|
state.push(state.dict.read(state.pop()));
|
|
|
|
state.push(state.dict.read(state.pop()));
|
|
|
|
else
|
|
|
|
else
|
|
|
|
state.push(state.dict.readbyte(state.pop()));
|
|
|
|
state.push(state.dict.readbyte(state.pop()));
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 11: // poke
|
|
|
|
case 12: // poke
|
|
|
|
cell = state.pop();
|
|
|
|
cell = state.pop();
|
|
|
|
if (auto addr = state.pop(); cell)
|
|
|
|
if (auto addr = state.pop(); cell)
|
|
|
|
state.dict.write(addr, state.pop());
|
|
|
|
state.dict.write(addr, state.pop());
|
|
|
|
else
|
|
|
|
else
|
|
|
|
state.dict.writebyte(addr, state.pop());
|
|
|
|
state.dict.writebyte(addr, state.pop());
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 12: // pushr
|
|
|
|
case 13: // pushr
|
|
|
|
state.pushr(state.pop());
|
|
|
|
state.pushr(state.pop());
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 13: // popr
|
|
|
|
case 14: // popr
|
|
|
|
state.push(state.popr());
|
|
|
|
state.push(state.popr());
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 14: // equal
|
|
|
|
case 15: // equal
|
|
|
|
cell = state.pop();
|
|
|
|
cell = state.pop();
|
|
|
|
state.top() = state.top() == cell;
|
|
|
|
state.top() = state.top() == cell;
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 15: // lt
|
|
|
|
case 16: // lt
|
|
|
|
cell = state.pop();
|
|
|
|
cell = state.pop();
|
|
|
|
state.top() = state.top() < cell;
|
|
|
|
state.top() = state.top() < cell;
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 16: // allot
|
|
|
|
|
|
|
|
state.dict.allot(state.pop());
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 17: // and
|
|
|
|
case 17: // and
|
|
|
|
cell = state.pop();
|
|
|
|
cell = state.pop();
|
|
|
|
state.top() &= cell;
|
|
|
|
state.top() &= cell;
|
|
|
@ -165,32 +165,22 @@ void CoreWords::run(unsigned int index, State& state)
|
|
|
|
state.dict.add(findi("exit"));
|
|
|
|
state.dict.add(findi("exit"));
|
|
|
|
state.compiling(false);
|
|
|
|
state.compiling(false);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 27: // here
|
|
|
|
case 27: // _jmp
|
|
|
|
state.push(state.dict.here);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 28: // _lit
|
|
|
|
|
|
|
|
state.push(state.beyondip());
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 29: // literal
|
|
|
|
|
|
|
|
state.dict.add(findi("_lit"));
|
|
|
|
|
|
|
|
state.dict.add(state.pop());
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 30: // _jmp
|
|
|
|
|
|
|
|
state.ip = state.beyondip() - sizeof(Cell);
|
|
|
|
state.ip = state.beyondip() - sizeof(Cell);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 31: // _jmp0
|
|
|
|
case 28: // _jmp0
|
|
|
|
if (state.pop())
|
|
|
|
if (state.pop())
|
|
|
|
state.beyondip();
|
|
|
|
state.beyondip();
|
|
|
|
else
|
|
|
|
else
|
|
|
|
state.ip = state.beyondip() - sizeof(Cell);
|
|
|
|
state.ip = state.beyondip() - sizeof(Cell);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 32: // depth
|
|
|
|
case 29: // depth
|
|
|
|
state.push(state.size());
|
|
|
|
state.push(state.size());
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 33: // _rdepth
|
|
|
|
case 30: // _rdepth
|
|
|
|
state.push(state.rsize());
|
|
|
|
state.push(state.rsize());
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 34: // key
|
|
|
|
case 31: // key
|
|
|
|
cell = state.dict.read(Dictionary::Input);
|
|
|
|
cell = state.dict.read(Dictionary::Input);
|
|
|
|
while (cell <= 0) {
|
|
|
|
while (cell <= 0) {
|
|
|
|
state.input(state);
|
|
|
|
state.input(state);
|
|
|
@ -219,7 +209,7 @@ int CoreWords::findi(const char *word)
|
|
|
|
++end;
|
|
|
|
++end;
|
|
|
|
|
|
|
|
|
|
|
|
if (size == end - i && !std::strncmp(word, wordsarr + i, size))
|
|
|
|
if (size == end - i && !std::strncmp(word, wordsarr + i, size))
|
|
|
|
return wordsarr[end] == '\0' ? wordsi : (wordsi | Compiletime);
|
|
|
|
return wordsarr[end] == '\0' ? wordsi : (wordsi | Immediate);
|
|
|
|
|
|
|
|
|
|
|
|
++wordsi;
|
|
|
|
++wordsi;
|
|
|
|
i = end + 1;
|
|
|
|
i = end + 1;
|
|
|
@ -239,7 +229,7 @@ int CoreWords::findi(State& state, Word word)
|
|
|
|
++end;
|
|
|
|
++end;
|
|
|
|
|
|
|
|
|
|
|
|
if (state.dict.equal(word, wordsarr + i, end - i))
|
|
|
|
if (state.dict.equal(word, wordsarr + i, end - i))
|
|
|
|
return wordsarr[end] == '\0' ? wordsi : (wordsi | Compiletime);
|
|
|
|
return wordsarr[end] == '\0' ? wordsi : (wordsi | Immediate);
|
|
|
|
|
|
|
|
|
|
|
|
++wordsi;
|
|
|
|
++wordsi;
|
|
|
|
i = end + 1;
|
|
|
|
i = end + 1;
|
|
|
|