]> code.bitgloo.com Git - bitgloo/alee-forth.git/commitdiff
wip: evaluate
authorClyne Sullivan <clyne@bitgloo.com>
Fri, 3 Mar 2023 23:48:56 +0000 (18:48 -0500)
committerClyne Sullivan <clyne@bitgloo.com>
Fri, 3 Mar 2023 23:48:56 +0000 (18:48 -0500)
alee.cpp
core.fth
dictionary.cpp
dictionary.hpp
parser.hpp

index b2d8fd0452d74061bba4cc4603ad458ea8fed2c8..633b8d7cf6e95330f58870176bc0b67572f3841d 100644 (file)
--- a/alee.cpp
+++ b/alee.cpp
@@ -30,11 +30,12 @@ static void readchar(State& state);
 static void parseLine(Parser&, State&, const std::string&);
 static void parseFile(Parser&, State&, std::istream&);
 
+static Parser parser;
+
 int main(int argc, char *argv[])
 {
     MemDict dict;
     State state (dict, readchar);
-    Parser parser;
 
     dict.initialize();
 
@@ -80,6 +81,7 @@ static void load(State& state)
         state.dict.writebyte(i++, file.get());
 }
 
+#include <cstring>
 void user_sys(State& state)
 {
     char buf[32] = {0};
@@ -107,6 +109,17 @@ void user_sys(State& state)
         std::cout << buf << ' ';
         }
         break;
+    case 5: // eval
+        {
+        auto oldip = state.ip;
+        std::jmp_buf oldjb;
+        memcpy(oldjb, state.jmpbuf, sizeof(std::jmp_buf));
+        state.ip = 0;
+        parser.parseSource(state);
+        memcpy(state.jmpbuf, oldjb, sizeof(std::jmp_buf));
+        state.ip = oldip;
+        }
+        break;
     }
 }
 
index 1d9a10a5aa13b62afd956762956a923db61798d0..2d769c22de80642536c70b0846fe1326b3ee627c 100644 (file)
--- a/core.fth
+++ b/core.fth
@@ -25,7 +25,8 @@
 : imm      _latest @ dup @ 1 5 << | swap ! ;
 : immediate imm ;
 : state    3 cells ;
-: >in      4 cells ;
+: _source  4 cells ;
+: >in      5 cells ;
 
 : ,        here ! 1 cells allot ;
 
 : min      2dup <= if drop else nip then ;
 : max      2dup <= if nip else drop then ;
 
-: key      >in @ 5 cells +
+: source   _source @ 0 begin 2dup + c@ while char+ repeat ;
+: key      _source @ >in @ +
            begin dup c@ 0 = while _in repeat
            c@ 1 >in +! ;
-: key?     >in @ 5 cells + c@ 0 <> ;
+: key?     _source @ >in @ + c@ 0 <> ;
 : word     here dup >r char+ >r
            begin key? if key 2dup <> else 0 0 then while
            r> tuck c! char+ >r repeat
 : [char]   char postpone literal ; imm
 
 : (        begin [char] ) key <> while repeat ; imm
-: \        >in @ 5 cells +
+: \        _source @ >in @ +
            begin dup c@ while 0 over c! char+ repeat drop ; imm
 
 : type     begin dup 0 > while swap dup c@ emit char+ swap 1- repeat 2drop ;
 -1 constant true
 0 constant false
 
-: source   >in cell+ 0 begin 2dup + c@ while char+ repeat ;
-
 : quit     begin _rdepth 1 > while r> drop repeat postpone [ ;
 : abort    begin depth 0 > while drop repeat quit ;
 : abort"   postpone s" ['] rot ,
 : marker   create _latest @ , here , does>
            dup @ _latest ! cell+ @ here swap - allot ;
 : :noname  0 , here ] ;
+
+: evaluate _source @ >r >in @ >r
+           0 >in ! _source ! 5 sys
+           r> >in ! r> _source ! ;
index ff80957e7a50b1242af70e3a328590c0404b2f3e..643260774e31b8ee11f44a53c9a848b7f573b5b3 100644 (file)
@@ -27,6 +27,7 @@ void Dictionary::initialize()
     write(Here, Begin);
     write(Latest, Begin);
     write(Compiling, 0);
+    write(Source, Input + sizeof(Cell));
 }
 
 Addr Dictionary::allot(Cell amount) noexcept
@@ -95,10 +96,11 @@ Addr Dictionary::getexec(Addr addr) noexcept
 Word Dictionary::input() noexcept
 {
     auto idx = read(Dictionary::Input);
-    auto ch = readbyte(Dictionary::Input + sizeof(Cell) + idx);
+    auto src = read(Dictionary::Source);
+    auto ch = readbyte(src + idx);
 
     if (ch) {
-        Addr wordstart = Dictionary::Input + sizeof(Cell) + idx;
+        Addr wordstart = src + idx;
         Addr wordend = wordstart;
 
         do {
index dbf1dcc68ba506f93e5d24b958616bd0bbe05af5..20ac7e4ba989f2d9f5a765bc77807701a1a23643 100644 (file)
@@ -31,9 +31,10 @@ public:
     constexpr static Addr Here       = sizeof(Cell);
     constexpr static Addr Latest     = sizeof(Cell) * 2;
     constexpr static Addr Compiling  = sizeof(Cell) * 3;
-    constexpr static Addr Input      = sizeof(Cell) * 4; // len data...
+    constexpr static Addr Source     = sizeof(Cell) * 4;
+    constexpr static Addr Input      = sizeof(Cell) * 5; // len data...
     constexpr static Addr InputCells = 82; // bytes!
-    constexpr static Addr Begin      = sizeof(Cell) * 5 + InputCells;
+    constexpr static Addr Begin      = sizeof(Cell) * 6 + InputCells;
 
     void initialize();
 
index 5bcf3f95bc607058396d92a37620b70457e516e1..4b14d6429b4e79dc71909a8cccec5b11a6dd793d 100644 (file)
@@ -29,9 +29,9 @@ public:
     constexpr static int UnknownWord = -1;
 
     int parse(State&, const char *);
+    int parseSource(State&);
 
 private:
-    int parseSource(State&);
     int parseWord(State&, Word);
     int parseNumber(State&, Word);
 };