]> code.bitgloo.com Git - bitgloo/alee-forth.git/commitdiff
concise parser; >body, >in, source
authorClyne Sullivan <clyne@bitgloo.com>
Tue, 21 Feb 2023 00:00:30 +0000 (19:00 -0500)
committerClyne Sullivan <clyne@bitgloo.com>
Tue, 21 Feb 2023 00:00:30 +0000 (19:00 -0500)
compat.txt
core.fth
corewords.cpp
corewords.hpp
parser.cpp
parser.hpp

index bab23a79786e71c1428cc1f20c457774ffa0c2b9..ae1b2dbf120b234bb5a74395d84f706c37150ef2 100644 (file)
@@ -36,8 +36,8 @@ yes 6.1.0480 <
     6.1.0490 <#
 yes 6.1.0530 =
 yes 6.1.0540 >
-    6.1.0550 >BODY
-    6.1.0560 >IN
+yes 6.1.0550 >BODY
+yes 6.1.0560 >IN
     6.1.0570 >NUMBER
 yes 6.1.0580 >R
 yes 6.1.0630 ?DUP
@@ -112,7 +112,7 @@ yes 6.1.2162 RSHIFT
     6.1.2170 S>D
     6.1.2210 SIGN
     6.1.2214 SM/REM
-    6.1.2216 SOURCE
+yes 6.1.2216 SOURCE
 yes 6.1.2220 SPACE
 yes 6.1.2230 SPACES
 yes 6.1.2250 STATE
index 8c57e6d6741917fa5c8c2531e146c6294759df9a..71645974ca53b4699403fb584ac90e4b83ed0823 100644 (file)
--- a/core.fth
+++ b/core.fth
 
 : base     0 ;
 : _latest  1 cells ;
+: imm      _latest @ dup @ 1 5 << | swap ! ;
 : state    2 cells ;
-: decimal  1 1+ base ! 1010 base ! ;
+: postpone 1 3 cells ! ; imm
+: _input   4 cells ;
 
-: imm      _latest @ dup @ 1 5 << | swap ! ;
+: decimal  1 1+ base ! 1010 base ! ;
 
-: postpone 1 3 cells ! ; imm
 : [']      ' postpone literal ; imm
 : [        0 state ! ; imm
 : ]        1 state ! ;
            2 cells +
            ['] _jmp over ! cell+
            here swap ! ] ;
+: >body    cell+ @ ;
 
 : variable create 1 cells allot ;
 : constant create , does> ['] @ , postpone ; ;
 ( TODO fix compile-time does>... above should simply be "does> @ ;" )
+
+: >in      _input 80 chars + cell+ _input @ - 4 chars - ;
+: source   _input @ 6 chars + >in 3 chars - swap ;
index fff6d9f58c9fbd89e8b1ae8d0eaf677035544617..de71f3e454abdfd0279f5a789f17a12180a4180b 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_jump
+        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
     };
 
     return index >= 0 && index < WordCount ? ops[index] : nullptr;
@@ -162,20 +161,19 @@ void CoreWords::op_comment(State& state) {
 }
 
 void CoreWords::op_colon(State& state) {
-    if (state.compiling()) {
-        Word word = state.dict.input();
-        while (word.size() == 0) {
-            state.input(state);
-            word = state.dict.input();
-        }
-
-        const auto start = state.dict.alignhere();
-        state.dict.addDefinition(word);
-        state.dict.write(start,
-            (state.dict.read(start) & 0x1F) |
-            ((start - state.dict.latest()) << 6));
-        state.dict.latest(start);
+    Word word = state.dict.input();
+    while (word.size() == 0) {
+        state.input(state);
+        word = state.dict.input();
     }
+
+    const auto start = state.dict.alignhere();
+    state.dict.addDefinition(word);
+    state.dict.write(start,
+        (state.dict.read(start) & 0x1F) |
+        ((start - state.dict.latest()) << 6));
+    state.dict.latest(start);
+    state.compiling(true);
 }
 
 void CoreWords::op_tick(State& state) {
@@ -243,12 +241,6 @@ void CoreWords::op_literal(State& state)
     }
 }
 
-void CoreWords::op_jump(State& state)
-{
-    state.pushr(state.ip + sizeof(Cell));
-    op_jmp(state);
-}
-
 void CoreWords::op_jmp(State& state)
 {
     state.ip = state.beyondip() - sizeof(Cell);
index af719b719ed1e14adeb85ef1899f95ca17c3e5a9..0c0010010062be5b75791022858b6534eabe3327 100644 (file)
@@ -29,9 +29,7 @@ void user_sys(State&);
 class CoreWords
 {
 public:
-    constexpr static std::size_t VisibleWordCount = 37;             // size
-    constexpr static auto HiddenWordJump    = VisibleWordCount;     // index
-    constexpr static auto WordCount         = VisibleWordCount + 1; // size
+    constexpr static std::size_t WordCount = 37;
 
     constexpr static Cell Immediate   = (1 << 5);
     constexpr static Cell Compiletime = (1 << 6);
@@ -85,7 +83,6 @@ private:
     static void op_here(State&);
     static void op_const(State&);
     static void op_lit(State&);
-    static void op_jump(State&);
     static void op_depth(State&);
     static void op_key(State&);
     static void op_exit(State&);
index b78390c0d5e99e9f0207e82c9749fcf84e39cbd9..3c54adc9d66496c4d16bcb0fbbd452127617c479 100644 (file)
@@ -52,69 +52,58 @@ ParseStatus Parser::parseSource(State& state)
 
 ParseStatus Parser::parseWord(State& state, Word word)
 {
-    // TODO unify core-word and defined-word parsing/execution.
+    int ins, imm;
 
-    if (auto i = CoreWords::findi(state, word); i >= 0) {
-        auto p = state.dict.read(Dictionary::Postpone);
-        auto imm = (i & CoreWords::Compiletime);
+    ins = CoreWords::findi(state, word);
+    if (ins < 0) {
+        ins = state.dict.find(word);
 
-        if (state.compiling() || p) {
-            if (p || !imm) {
-                state.dict.add(i & ~CoreWords::Compiletime);
-
-                if (p)
-                    state.dict.write(Dictionary::Postpone, 0);
-            } else if (imm) {
-                CoreWords::run(i & ~CoreWords::Compiletime, state);
-            }
+        if (ins <= 0) {
+            return parseNumber(state, word);
         } else {
-            if (state.dict.equal(word, ":"))
-                state.compiling(true);
-
-            CoreWords::run(i & ~CoreWords::Compiletime, state);
-        }
-    } else if (auto j = state.dict.find(word); j > 0) {
-        auto e = state.dict.getexec(j);
-        auto p = state.dict.read(Dictionary::Postpone);
-
-        if (state.compiling() || p) {
-            auto imm = state.dict.read(j) & CoreWords::Immediate;
-
-            if (p || !imm) {
-                state.dict.add(CoreWords::HiddenWordJump);
-                state.dict.add(e);
-
-                if (p)
-                    state.dict.write(Dictionary::Postpone, 0);
-            } else if (imm) {
-                state.execute(e);
-            }
-        } else {
-            state.execute(e);
+            imm = state.dict.read(ins) & CoreWords::Immediate;
+            ins = state.dict.getexec(ins);
         }
     } else {
-        char buf[word.size() + 1];
-        for (unsigned i = 0; i < word.size(); ++i)
-            buf[i] = state.dict.readbyte(word.start + i);
-        buf[word.size()] = '\0';
-
-        char *p;
-        const auto base = state.dict.read(0);
-        const Cell l = std::strtol(buf, &p, base);
-
-        if (std::distance(buf, p) == word.size()) {
-            if (state.compiling()) {
-                state.dict.add(CoreWords::findi("_lit"));
-                state.dict.add(l);
-            } else {
-                state.push(l);
-            }
-        } else {
-            std::cout << "word not found: " << buf << std::endl;
-            return ParseStatus::NotAWord;
-        }
+        imm = ins & CoreWords::Compiletime;
+        ins &= ~CoreWords::Compiletime;
+    }
+
+    if (state.dict.read(Dictionary::Postpone)) {
+        state.dict.add(ins);
+        state.dict.write(Dictionary::Postpone, 0);
+    } else if (state.compiling() && !imm) {
+        state.dict.add(ins);
+    } else {
+        state.execute(ins);
     }
 
     return ParseStatus::Finished;
 }
 
+ParseStatus Parser::parseNumber(State& state, Word word)
+{
+    char buf[word.size() + 1];
+    for (unsigned i = 0; i < word.size(); ++i)
+        buf[i] = state.dict.readbyte(word.start + i);
+    buf[word.size()] = '\0';
+
+    char *p;
+    const auto base = state.dict.read(0);
+    const Cell l = std::strtol(buf, &p, base);
+
+    if (std::distance(buf, p) == word.size()) {
+        if (state.compiling()) {
+            state.dict.add(CoreWords::findi("_lit"));
+            state.dict.add(l);
+        } else {
+            state.push(l);
+        }
+
+        return ParseStatus::Finished;
+    } else {
+        std::cout << "word not found: " << buf << std::endl;
+        return ParseStatus::NotAWord;
+    }
+}
+
index 49223f052e7446498514bbcb1096897858ebdf59..ccd5647dc6faff8349f0633659cf692e9105ff79 100644 (file)
@@ -31,6 +31,7 @@ public:
 private:
     ParseStatus parseSource(State&);
     ParseStatus parseWord(State&, Word);
+    ParseStatus parseNumber(State&, Word);
 };
 
 #endif // ALEEFORTH_PARSER_HPP