]> code.bitgloo.com Git - bitgloo/alee-forth.git/commitdiff
move some corewords to core.fth; fix word reading
authorClyne Sullivan <clyne@bitgloo.com>
Wed, 22 Feb 2023 16:43:21 +0000 (11:43 -0500)
committerClyne Sullivan <clyne@bitgloo.com>
Wed, 22 Feb 2023 16:43:21 +0000 (11:43 -0500)
alee.cpp
core.fth
corewords.cpp
corewords.hpp
dictionary.cpp
parser.cpp

index e7f2e13a6d5b5de6e9d780e8a031f98387e565f7..38b16e42c9166df8b45c5179ef738c4c46c3f8bd 100644 (file)
--- a/alee.cpp
+++ b/alee.cpp
@@ -37,7 +37,8 @@ static void readchar(State& state)
     for (Addr i = 0; i < len; ++i, ++addr)
         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);
 }
 
index f460bef8e00b153edc096780e76acb6ec591cbf5..a49530230a408ad3e9654c6b58a81cf51012de24 100644 (file)
--- a/core.fth
+++ b/core.fth
@@ -1,6 +1,3 @@
-( : variable create 0 , ; )
-( : create here const ; )
-
 : .        0 sys ;
 : emit     1 sys ;
 
@@ -14,6 +11,7 @@
 : cells    2 * ;
 
 : over     1 pick ;
+: rot      >r swap r> swap ;
 : -rot     rot rot ;
 : nip      swap drop ;
 : tuck     swap over ;
 : char     bl word cell+ c@ ;
 : [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 ;
 : s"       state @ if ['] _jmp , here 0 , then
            [char] " word count
 : constant create , does> ['] @ , postpone ; ;
 ( TODO fix compile-time does>... above should simply be "does> @ ;" )
 
--1 constant true 
-0  constant false 
+-1 constant true
+0  constant false
 
 : >in      _input 80 chars + cell+ _input @ - 4 chars - ;
 : source   _input @ 6 chars + >in 3 chars - swap ;
index c6b3d7d6432cc3975ce3bc1455da659bc2625dcf..9c52c8d9caa27e9cb550a960625cdab73ca488f8 100644 (file)
 Func CoreWords::get(int index)
 {
     static const Func ops[WordCount] = {
-        op_drop,  op_dup,     op_swap,    op_pick,    op_sys,
-        op_add,   op_sub,     op_mul,     op_div,     op_mod,
- /*10*/ op_peek,  op_poke,    op_rot,     op_pushr,   op_popr,
-        op_eq,    op_lt,      op_allot,   op_and,     op_or,
- /*20*/ op_xor,   op_shl,     op_shr,     op_comment, op_colon,
-        op_semic, op_here,    op_const,   op_depth,   op_key,
- /*30*/ op_exit,  op_tick,    op_execute, op_jmp,     op_jmp0,
-        op_lit,   op_literal, op_rdepth
+        op_drop,  op_dup,   op_swap,  op_pick,    op_sys,
+        op_add,   op_sub,   op_mul,   op_div,     op_mod,
+ /*10*/ op_peek,  op_poke,  op_pushr, op_popr,    op_eq,
+        op_lt,    op_allot, op_and,   op_or,      op_xor,
+ /*20*/ op_shl,   op_shr,   op_colon, op_semic,   op_here,
+        op_depth, op_key,   op_exit,  op_tick,    op_execute,
+ /*30*/ op_jmp,   op_jmp0,  op_lit,   op_literal, op_rdepth
     };
 
     return index >= 0 && index < WordCount ? ops[index] : nullptr;
@@ -102,11 +101,6 @@ void CoreWords::op_poke(State& state) {
         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) {
     state.pushr(state.pop());
 }
@@ -154,12 +148,6 @@ void CoreWords::op_shr(State& state) {
     state.top() >>= a;
 }
 
-void CoreWords::op_comment(State& state) {
-    do {
-        op_key(state);
-    } while (state.pop() != ')');
-}
-
 void CoreWords::op_colon(State& state) {
     Word word = state.dict.input();
     while (word.size() == 0) {
@@ -212,21 +200,6 @@ void CoreWords::op_here(State& state) {
     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)
 {
     state.push(state.beyondip());
index 9862bebe7cb96b55ad5c89014b1aa792dbb76062..ce5bba0dc2a9fb31f326950b0a789d55e347a5dc 100644 (file)
@@ -29,7 +29,7 @@ void user_sys(State&);
 class CoreWords
 {
 public:
-    constexpr static std::size_t WordCount = 38;
+    constexpr static std::size_t WordCount = 35;
 
     constexpr static Cell Immediate   = (1 << 5);
     constexpr static Cell Compiletime = (1 << 6);
@@ -45,10 +45,10 @@ private:
     constexpr static char wordsarr[] =
         "drop\0dup\0swap\0pick\0sys\0"
         "+\0-\0*\0/\0%\0"
-        "_@\0_!\0rot\0>r\0r>\0"
+        "_@\0_!\0>r\0r>\0"
         "=\0<\0allot\0&\0|\0"
-        "^\0<<\0>>\0(\1:\1"
-        ";\1here\0const\0depth\0"
+        "^\0<<\0>>\0:\1"
+        ";\1here\0depth\0"
         "key\0exit\0'\0execute\0_jmp\0"
         "_jmp0\0_lit\0literal\1_rdepth\0";
 
@@ -66,7 +66,6 @@ private:
     static void op_mod(State&);
     static void op_peek(State&);
     static void op_poke(State&);
-    static void op_rot(State&);   // : rot >r swap r> swap ;
     static void op_pushr(State&);
     static void op_popr(State&);
     static void op_eq(State&);
@@ -77,11 +76,9 @@ private:
     static void op_xor(State&);
     static void op_shl(State&);
     static void op_shr(State&);
-    static void op_comment(State&);
     static void op_colon(State&);
     static void op_semic(State&);
     static void op_here(State&);
-    static void op_const(State&);
     static void op_lit(State&);
     static void op_depth(State&);
     static void op_rdepth(State&);
@@ -91,7 +88,7 @@ private:
     static void op_execute(State&);
     static void op_jmp(State&);
     static void op_jmp0(State&);
-    static void op_literal(State&); // : literal ['] _lit , , ; imm
+    static void op_literal(State&);
 };
 
 #endif // ALEEFORTH_COREWORDS_HPP
index 9fe361a0d9a7c532daae8e16c8f6bcdb1e4e9e29..2ba6f889288ca29197a356f62788cf709a9e57cf 100644 (file)
@@ -95,7 +95,7 @@ Word Dictionary::input()
     auto cnt = len;
     while (cnt) {
         auto b = readbyte(wordend);
-        if (isspace(b) || b == '\0') {
+        if (isspace(b)) {
             if (wordstart != wordend) {
                 Word word {wordstart, wordend};
                 writebyte(Dictionary::Input, cnt - 1);
index 3c54adc9d66496c4d16bcb0fbbd452127617c479..8a77df5e371f1e61c898b03d5c261c812a6068b5 100644 (file)
@@ -32,7 +32,7 @@ ParseStatus Parser::parse(State& state, std::string_view& str)
     addr += sizeof(Cell) + Dictionary::InputCells - str.size() - 1;
     for (char c : str)
         state.dict.writebyte(addr++, c);
-    state.dict.writebyte(addr, '\0');
+    state.dict.writebyte(addr, ' ');
 
     return parseSource(state);
 }