]> code.bitgloo.com Git - bitgloo/alee-forth.git/commitdiff
create, does>, variables
authorClyne Sullivan <clyne@bitgloo.com>
Mon, 20 Feb 2023 22:32:35 +0000 (17:32 -0500)
committerClyne Sullivan <clyne@bitgloo.com>
Mon, 20 Feb 2023 22:32:35 +0000 (17:32 -0500)
alee.cpp
compat.txt
core.fth
corewords.cpp
corewords.hpp
dictionary.cpp
dictionary.hpp

index 0fcda26e346af840b2ea0f47c4b9222973a037ea..e7f2e13a6d5b5de6e9d780e8a031f98387e565f7 100644 (file)
--- a/alee.cpp
+++ b/alee.cpp
@@ -48,6 +48,7 @@ int main(int argc, char *argv[])
     Parser parser;
 
     dict.write(Dictionary::Base, 10);
+    dict.write(Dictionary::Latest, Dictionary::Begin);
     dict.write(Dictionary::Compiling, 0);
     dict.write(Dictionary::Postpone, 0);
 
@@ -81,7 +82,7 @@ void parseLine(Parser& parser, State& state, std::string_view line)
 
     if (r == ParseStatus::Finished) {
         if (okay)
-            std::cout << "ok" << std::endl;
+            std::cout << (state.compiling() ? "compiled" : "ok") << std::endl;
     } else {
         std::cout << to_string(r) << ": " << line << std::endl;
     }
index c5d2b56ddaca4a7d03bf7bf7645b7bef0e230517..bab23a79786e71c1428cc1f20c457774ffa0c2b9 100644 (file)
@@ -9,7 +9,7 @@ yes 6.1.0080 (
 yes 6.1.0090 *
     6.1.0100 */
     6.1.0110 */MOD
-yes 6.1.0120 +
+yes 6.1.0120
 yes 6.1.0130 +!
 yes 6.1.0140 +LOOP
 yes 6.1.0150 ,
@@ -61,14 +61,14 @@ yes 6.1.0890 CELLS
 yes 6.1.0895 CHAR
 yes 6.1.0897 CHAR+
 yes 6.1.0898 CHARS
-    6.1.0950 CONSTANT
+yes 6.1.0950 CONSTANT
 yes 6.1.0980 COUNT
 yes 6.1.0990 CR
-    6.1.1000 CREATE
+yes 6.1.1000 CREATE
 yes 6.1.1170 DECIMAL
 yes 6.1.1200 DEPTH
 yes 6.1.1240 DO
-    6.1.1250 DOES>
+yes 6.1.1250 DOES>
 yes 6.1.1260 DROP
 yes 6.1.1290 DUP
 yes 6.1.1310 ELSE
@@ -86,7 +86,7 @@ yes 6.1.1680 I
 yes 6.1.1700 IF
 yes 6.1.1710 IMMEDIATE (as "imm")
     6.1.1720 INVERT
-    6.1.1730 J
+    6.1.1730 J 
 yes 6.1.1750 KEY
     6.1.1760 LEAVE
 yes 6.1.1780 LITERAL
@@ -125,7 +125,7 @@ yes 6.1.2310 TYPE
     6.1.2370 UM/MOD
     6.1.2380 UNLOOP
 yes 6.1.2390 UNTIL
-    6.1.2410 VARIABLE
+yes 6.1.2410 VARIABLE
 yes 6.1.2430 WHILE
 yes 6.1.2450 WORD
 yes 6.1.2490 XOR
index eb41948f6fa32835c207b59ec5f73c06894a66c0..8c57e6d6741917fa5c8c2531e146c6294759df9a 100644 (file)
--- a/core.fth
+++ b/core.fth
 : chars    ;
 
 : base     0 ;
-: state    2 ;
+: _latest  1 cells ;
+: state    2 cells ;
 : decimal  1 1+ base ! 1010 base ! ;
 
-: postpone 1 4 ! ; imm
+: imm      _latest @ dup @ 1 5 << | swap ! ;
+
+: postpone 1 3 cells ! ; imm
 : [']      ' postpone literal ; imm
 : [        0 state ! ; imm
 : ]        1 state ! ;
 
 : type     begin dup 0 > while swap dup c@ emit char+ swap 1- repeat ;
 : ."       [char] " word count type ;
+
+: create   align here bl word count nip cell+ allot align
+           ['] _lit , here 3 cells + , ['] exit , 0 ,
+           dup @ 31 & over _latest @ - 6 << or over ! _latest ! ;
+: does>    _latest @
+           dup @ 31 & + cell+ aligned
+           2 cells +
+           ['] _jmp over ! cell+
+           here swap ! ] ;
+
+: variable create 1 cells allot ;
+: constant create , does> ['] @ , postpone ; ;
+( TODO fix compile-time does>... above should simply be "does> @ ;" )
index 2f81eb58bf124401daa12ab5222a3af1e1a4d40e..fff6d9f58c9fbd89e8b1ae8d0eaf677035544617 100644 (file)
@@ -23,12 +23,12 @@ 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,
       op_peek,  op_poke, op_rot,     op_pushr,   op_popr,
/*10*/ op_peek,  op_poke, op_rot,     op_pushr,   op_popr,
         op_eq,    op_lt,   op_allot,   op_and,     op_or,
       op_xor,   op_shl,  op_shr,     op_comment, op_colon,
-        op_semic, op_here, op_imm,     op_const,   op_depth,
       op_key,   op_exit, op_tick,    op_execute, op_jmp,
-        op_jmp0,  op_lit,  op_literal,
/*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
     };
 
@@ -169,8 +169,12 @@ void CoreWords::op_colon(State& state) {
             word = state.dict.input();
         }
 
-        state.pushr(state.dict.alignhere());
+        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);
     }
 }
 
@@ -202,14 +206,6 @@ void CoreWords::op_exit(State& state) {
 void CoreWords::op_semic(State& state) {
     if (state.compiling()) {
         state.dict.add(findi("exit"));
-
-        auto begin = state.popr();
-
-        state.dict.write(begin,
-            (state.dict.read(begin) & 0x1F) |
-            ((begin - state.dict.latest) << 6));
-
-        state.dict.latest = begin;
         state.compiling(false);
     }
 }
@@ -218,12 +214,6 @@ void CoreWords::op_here(State& state) {
     state.push(state.dict.here);
 }
 
-void CoreWords::op_imm(State& state)
-{
-    state.dict.write(state.dict.latest,
-        state.dict.read(state.dict.latest) | Immediate);
-}
-
 void CoreWords::op_const(State& state)
 {
     Word word = state.dict.input();
@@ -339,8 +329,6 @@ Func CoreWords::find(State& state, Word word)
     return i >= 0 ? get(i & ~Compiletime) : nullptr;
 }
 
-struct corewords_run {};
-
 bool CoreWords::run(int i, State& state)
 {
     i &= ~Compiletime;
index 842544b4facf5b0191c5f5af57ba45678cbc943c..af719b719ed1e14adeb85ef1899f95ca17c3e5a9 100644 (file)
@@ -29,7 +29,7 @@ void user_sys(State&);
 class CoreWords
 {
 public:
-    constexpr static std::size_t VisibleWordCount = 38;             // size
+    constexpr static std::size_t VisibleWordCount = 37;             // size
     constexpr static auto HiddenWordJump    = VisibleWordCount;     // index
     constexpr static auto WordCount         = VisibleWordCount + 1; // size
 
@@ -50,7 +50,7 @@ private:
         "_@\0_!\0rot\0>r\0r>\0"
         "=\0<\0allot\0&\0|\0"
         "^\0<<\0>>\0(\0:\1"
-        ";\1here\0imm\0const\0depth\0"
+        ";\1here\0const\0depth\0"
         "key\0exit\0'\0execute\0_jmp\0"
         "_jmp0\0_lit\0literal\1";
 
@@ -83,7 +83,6 @@ private:
     static void op_colon(State&);
     static void op_semic(State&);
     static void op_here(State&);
-    static void op_imm(State&);
     static void op_const(State&);
     static void op_lit(State&);
     static void op_jump(State&);
index dd1465677ff6297d95587abcc212b5a29c741a94..9fe361a0d9a7c532daae8e16c8f6bcdb1e4e9e29 100644 (file)
@@ -57,7 +57,7 @@ void Dictionary::addDefinition(Word word)
 
 Addr Dictionary::find(Word word)
 {
-    Addr lt = latest, oldlt;
+    Addr lt = latest(), oldlt;
     do {
         oldlt = lt;
         const Cell l = read(lt);
index b7d318f37c15cbb61d07c87476acc63901fdf669..fa081cb2ff5d11c63300686b897c9a61261676ea 100644 (file)
@@ -29,14 +29,17 @@ class Dictionary
 {
 public:
     constexpr static Addr Base       = 0;
-    constexpr static Addr Compiling  = sizeof(Cell);
-    constexpr static Addr Postpone   = sizeof(Cell) * 2;
-    constexpr static Addr Input      = sizeof(Cell) * 3; // len data...
+    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 InputCells = 80; // bytes!
-    constexpr static Addr Begin      = sizeof(Cell) * 4 + InputCells;
+    constexpr static Addr Begin      = sizeof(Cell) * 5 + InputCells;
 
     Addr here = Begin;
-    Addr latest = Begin;
+
+    Addr latest() { return read(Latest); }
+    void latest(Addr l) { write(Latest, l); }
 
     virtual Cell read(Addr) const = 0;
     virtual void write(Addr, Cell) = 0;