move some corewords to core.fth; fix word reading

llvm
Clyne 2 years ago
parent 90c27fd4c9
commit bf9f8a902b
Signed by: clyne
GPG Key ID: 3267C8EBF3F9AFC7

@ -37,7 +37,8 @@ static void readchar(State& state)
for (Addr i = 0; i < len; ++i, ++addr) for (Addr i = 0; i < len; ++i, ++addr)
state.dict.writebyte(addr, state.dict.readbyte(addr + 1)); state.dict.writebyte(addr, state.dict.readbyte(addr + 1));
state.dict.writebyte(addr, std::cin.get()); auto c = std::cin.get();
state.dict.writebyte(addr, c ? c : ' ');
state.dict.write(Dictionary::Input, len + 1); state.dict.write(Dictionary::Input, len + 1);
} }

@ -1,6 +1,3 @@
( : variable create 0 , ; )
( : create here const ; )
: . 0 sys ; : . 0 sys ;
: emit 1 sys ; : emit 1 sys ;
@ -14,6 +11,7 @@
: cells 2 * ; : cells 2 * ;
: over 1 pick ; : over 1 pick ;
: rot >r swap r> swap ;
: -rot rot rot ; : -rot rot rot ;
: nip swap drop ; : nip swap drop ;
: tuck swap over ; : tuck swap over ;
@ -105,6 +103,8 @@
: char bl word cell+ c@ ; : char bl word cell+ c@ ;
: [char] char postpone literal ; imm : [char] char postpone literal ; imm
: ( begin [char] ) key <> while repeat ; imm
: type begin dup 0 > while swap dup c@ emit char+ swap 1- repeat 2drop ; : type begin dup 0 > while swap dup c@ emit char+ swap 1- repeat 2drop ;
: s" state @ if ['] _jmp , here 0 , then : s" state @ if ['] _jmp , here 0 , then
[char] " word count [char] " word count
@ -128,8 +128,8 @@
: constant create , does> ['] @ , postpone ; ; : constant create , does> ['] @ , postpone ; ;
( TODO fix compile-time does>... above should simply be "does> @ ;" ) ( TODO fix compile-time does>... above should simply be "does> @ ;" )
-1 constant true -1 constant true
0 constant false 0 constant false
: >in _input 80 chars + cell+ _input @ - 4 chars - ; : >in _input 80 chars + cell+ _input @ - 4 chars - ;
: source _input @ 6 chars + >in 3 chars - swap ; : source _input @ 6 chars + >in 3 chars - swap ;

@ -21,14 +21,13 @@
Func CoreWords::get(int index) Func CoreWords::get(int index)
{ {
static const Func ops[WordCount] = { static const Func ops[WordCount] = {
op_drop, op_dup, op_swap, op_pick, op_sys, op_drop, op_dup, op_swap, op_pick, op_sys,
op_add, op_sub, op_mul, op_div, op_mod, op_add, op_sub, op_mul, op_div, op_mod,
/*10*/ op_peek, op_poke, op_rot, op_pushr, op_popr, /*10*/ op_peek, op_poke, op_pushr, op_popr, op_eq,
op_eq, op_lt, op_allot, op_and, op_or, op_lt, op_allot, op_and, op_or, op_xor,
/*20*/ op_xor, op_shl, op_shr, op_comment, op_colon, /*20*/ op_shl, op_shr, op_colon, op_semic, op_here,
op_semic, op_here, op_const, op_depth, op_key, op_depth, op_key, op_exit, op_tick, op_execute,
/*30*/ op_exit, op_tick, op_execute, op_jmp, op_jmp0, /*30*/ op_jmp, op_jmp0, op_lit, op_literal, op_rdepth
op_lit, op_literal, op_rdepth
}; };
return index >= 0 && index < WordCount ? ops[index] : nullptr; return index >= 0 && index < WordCount ? ops[index] : nullptr;
@ -102,11 +101,6 @@ void CoreWords::op_poke(State& state) {
state.dict.write(addr, state.pop()); state.dict.write(addr, state.pop());
} }
void CoreWords::op_rot(State& state) {
std::swap(state.pick(2), state.pick(1));
std::swap(state.pick(1), state.pick(0));
}
void CoreWords::op_pushr(State& state) { void CoreWords::op_pushr(State& state) {
state.pushr(state.pop()); state.pushr(state.pop());
} }
@ -154,12 +148,6 @@ void CoreWords::op_shr(State& state) {
state.top() >>= a; state.top() >>= a;
} }
void CoreWords::op_comment(State& state) {
do {
op_key(state);
} while (state.pop() != ')');
}
void CoreWords::op_colon(State& state) { void CoreWords::op_colon(State& state) {
Word word = state.dict.input(); Word word = state.dict.input();
while (word.size() == 0) { while (word.size() == 0) {
@ -212,21 +200,6 @@ void CoreWords::op_here(State& state) {
state.push(state.dict.here); state.push(state.dict.here);
} }
void CoreWords::op_const(State& state)
{
Word word = state.dict.input();
while (word.size() == 0) {
state.input(state);
word = state.dict.input();
}
state.pushr(state.dict.alignhere());
state.dict.addDefinition(word);
state.dict.add(findi("_lit"));
state.dict.add(state.pop());
op_semic(state);
}
void CoreWords::op_lit(State& state) void CoreWords::op_lit(State& state)
{ {
state.push(state.beyondip()); state.push(state.beyondip());

@ -29,7 +29,7 @@ void user_sys(State&);
class CoreWords class CoreWords
{ {
public: public:
constexpr static std::size_t WordCount = 38; constexpr static std::size_t WordCount = 35;
constexpr static Cell Immediate = (1 << 5); constexpr static Cell Immediate = (1 << 5);
constexpr static Cell Compiletime = (1 << 6); constexpr static Cell Compiletime = (1 << 6);
@ -45,10 +45,10 @@ private:
constexpr static char wordsarr[] = constexpr static char wordsarr[] =
"drop\0dup\0swap\0pick\0sys\0" "drop\0dup\0swap\0pick\0sys\0"
"+\0-\0*\0/\0%\0" "+\0-\0*\0/\0%\0"
"_@\0_!\0rot\0>r\0r>\0" "_@\0_!\0>r\0r>\0"
"=\0<\0allot\0&\0|\0" "=\0<\0allot\0&\0|\0"
"^\0<<\0>>\0(\1:\1" "^\0<<\0>>\0:\1"
";\1here\0const\0depth\0" ";\1here\0depth\0"
"key\0exit\0'\0execute\0_jmp\0" "key\0exit\0'\0execute\0_jmp\0"
"_jmp0\0_lit\0literal\1_rdepth\0"; "_jmp0\0_lit\0literal\1_rdepth\0";
@ -66,7 +66,6 @@ private:
static void op_mod(State&); static void op_mod(State&);
static void op_peek(State&); static void op_peek(State&);
static void op_poke(State&); static void op_poke(State&);
static void op_rot(State&); // : rot >r swap r> swap ;
static void op_pushr(State&); static void op_pushr(State&);
static void op_popr(State&); static void op_popr(State&);
static void op_eq(State&); static void op_eq(State&);
@ -77,11 +76,9 @@ private:
static void op_xor(State&); static void op_xor(State&);
static void op_shl(State&); static void op_shl(State&);
static void op_shr(State&); static void op_shr(State&);
static void op_comment(State&);
static void op_colon(State&); static void op_colon(State&);
static void op_semic(State&); static void op_semic(State&);
static void op_here(State&); static void op_here(State&);
static void op_const(State&);
static void op_lit(State&); static void op_lit(State&);
static void op_depth(State&); static void op_depth(State&);
static void op_rdepth(State&); static void op_rdepth(State&);
@ -91,7 +88,7 @@ private:
static void op_execute(State&); static void op_execute(State&);
static void op_jmp(State&); static void op_jmp(State&);
static void op_jmp0(State&); static void op_jmp0(State&);
static void op_literal(State&); // : literal ['] _lit , , ; imm static void op_literal(State&);
}; };
#endif // ALEEFORTH_COREWORDS_HPP #endif // ALEEFORTH_COREWORDS_HPP

@ -95,7 +95,7 @@ Word Dictionary::input()
auto cnt = len; auto cnt = len;
while (cnt) { while (cnt) {
auto b = readbyte(wordend); auto b = readbyte(wordend);
if (isspace(b) || b == '\0') { if (isspace(b)) {
if (wordstart != wordend) { if (wordstart != wordend) {
Word word {wordstart, wordend}; Word word {wordstart, wordend};
writebyte(Dictionary::Input, cnt - 1); writebyte(Dictionary::Input, cnt - 1);

@ -32,7 +32,7 @@ ParseStatus Parser::parse(State& state, std::string_view& str)
addr += sizeof(Cell) + Dictionary::InputCells - str.size() - 1; addr += sizeof(Cell) + Dictionary::InputCells - str.size() - 1;
for (char c : str) for (char c : str)
state.dict.writebyte(addr++, c); state.dict.writebyte(addr++, c);
state.dict.writebyte(addr, '\0'); state.dict.writebyte(addr, ' ');
return parseSource(state); return parseSource(state);
} }

Loading…
Cancel
Save