reduce built-in word count

llvm
Clyne 2 years ago
parent fc0d3ed9cf
commit 4af14b8c3e

@ -36,6 +36,7 @@ int main(int argc, char *argv[])
Parser parser;
dict.write(Dictionary::Base, 10);
dict.write(Dictionary::Here, Dictionary::Begin);
dict.write(Dictionary::Latest, Dictionary::Begin);
dict.write(Dictionary::Compiling, 0);
dict.write(Dictionary::Postpone, 0);
@ -71,7 +72,7 @@ static void save(State& state)
std::ofstream file ("alee.dat", std::ios::binary);
if (file.good()) {
for (Addr i = 0; i < state.dict.here; ++i)
for (Addr i = 0; i < state.dict.here(); ++i)
file.put(state.dict.readbyte(i));
}
}
@ -83,8 +84,6 @@ static void load(State& state)
Addr i = 0;
while (file.good())
state.dict.writebyte(i++, file.get());
state.dict.here = i - 1;
}
void user_sys(State& state)

@ -13,6 +13,17 @@
: ! 1 _! ;
: @ 1 _@ ;
: +! dup >r swap r> @ + swap ! ;
: base 0 ;
: here 1 cells @ ;
: allot 1 cells +! ;
: _latest 2 cells ;
: imm _latest @ dup @ 1 5 << | swap ! ;
: state 3 cells ;
: postpone 1 4 cells ! ; imm
: _input 5 cells ;
: , here ! 1 cells allot ;
: over 1 pick ;
@ -32,16 +43,10 @@
: char+ 1+ ;
: chars ;
: base 0 ;
: _latest 1 cells ;
: imm _latest @ dup @ 1 5 << | swap ! ;
: state 2 cells ;
: postpone 1 3 cells ! ; imm
: _input 4 cells ;
: decimal 10 base ! ;
: hex 16 base ! ;
: literal 0 , , ; imm
: ['] ' postpone literal ; imm
: [ 0 state ! ; imm
: ] 1 state ! ;
@ -53,7 +58,6 @@
: 2! swap over ! cell+ ! ;
: 2@ dup cell+ @ swap @ ;
: +! swap over @ + swap ! ;
: 0= 0 = ;
: 0< 0 < ;

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

@ -27,10 +27,9 @@ void user_sys(State&);
class CoreWords
{
public:
constexpr static std::size_t WordCount = 35;
constexpr static std::size_t WordCount = 32;
constexpr static Cell Immediate = (1 << 5);
constexpr static Cell Compiletime = (1 << 6);
static int findi(const char *);
static int findi(State&, Word);
@ -40,13 +39,13 @@ private:
// Ends with '\0': regular word
// Ends with '\1': compile-only word
constexpr static char wordsarr[] =
"drop\0dup\0swap\0pick\0sys\0"
"_lit\0drop\0dup\0swap\0pick\0sys\0"
"+\0-\0m*\0_/\0_%\0"
"_@\0_!\0>r\0r>\0=\0"
"<\0allot\0&\0|\0^\0"
"<\0&\0|\0^\0"
"<<\0>>\0:\0'\0execute\0"
"exit\0;\1here\0_lit\0literal\1"
"_jmp\0_jmp0\0depth\0_rdepth\0key\0";
"exit\0;\1_jmp\0"
"_jmp0\0depth\0_rdepth\0key\0";
};
#endif // ALEEFORTH_COREWORDS_HPP

@ -23,8 +23,8 @@
Addr Dictionary::allot(Cell amount) noexcept
{
Addr old = here;
here += amount;
Addr old = here();
here(old + amount);
return old;
}
@ -44,8 +44,8 @@ Addr Dictionary::aligned(Addr addr) const noexcept
Addr Dictionary::alignhere() noexcept
{
here = aligned(here);
return here;
here(aligned(here()));
return here();
}
void Dictionary::addDefinition(Word word) noexcept

@ -28,14 +28,16 @@ class Dictionary
{
public:
constexpr static Addr Base = 0;
constexpr static Addr Latest = sizeof(Cell);
constexpr static Addr Compiling = sizeof(Cell) * 2;
constexpr static Addr Postpone = sizeof(Cell) * 3;
constexpr static Addr Input = sizeof(Cell) * 4; // len data...
constexpr static Addr Here = sizeof(Cell);
constexpr static Addr Latest = sizeof(Cell) * 2;
constexpr static Addr Compiling = sizeof(Cell) * 3;
constexpr static Addr Postpone = sizeof(Cell) * 4;
constexpr static Addr Input = sizeof(Cell) * 5; // len data...
constexpr static Addr InputCells = 80; // bytes!
constexpr static Addr Begin = sizeof(Cell) * 5 + InputCells;
constexpr static Addr Begin = sizeof(Cell) * 6 + InputCells;
Addr here = Begin;
Addr here() const noexcept { return read(Here); }
void here(Addr l) noexcept { write(Here, l); }
Addr latest() const noexcept { return read(Latest); }
void latest(Addr l) noexcept { write(Latest, l); }

@ -62,8 +62,8 @@ int Parser::parseWord(State& state, Word word)
if (ins < 0) {
return parseNumber(state, word);
} else {
imm = ins & CoreWords::Compiletime;
ins &= ~CoreWords::Compiletime;
imm = ins & CoreWords::Immediate;
ins &= ~CoreWords::Immediate;
}
} else {
imm = state.dict.read(ins) & CoreWords::Immediate;

Loading…
Cancel
Save