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

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

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

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

@ -28,14 +28,16 @@ class Dictionary
{ {
public: public:
constexpr static Addr Base = 0; constexpr static Addr Base = 0;
constexpr static Addr Latest = sizeof(Cell); constexpr static Addr Here = sizeof(Cell);
constexpr static Addr Compiling = sizeof(Cell) * 2; constexpr static Addr Latest = sizeof(Cell) * 2;
constexpr static Addr Postpone = sizeof(Cell) * 3; constexpr static Addr Compiling = sizeof(Cell) * 3;
constexpr static Addr Input = sizeof(Cell) * 4; // len data... 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 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); } Addr latest() const noexcept { return read(Latest); }
void latest(Addr l) noexcept { write(Latest, l); } void latest(Addr l) noexcept { write(Latest, l); }

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

Loading…
Cancel
Save