]> code.bitgloo.com Git - bitgloo/alee-forth.git/commitdiff
depth and more core.fth words
authorClyne Sullivan <clyne@bitgloo.com>
Fri, 10 Feb 2023 02:54:58 +0000 (21:54 -0500)
committerClyne Sullivan <clyne@bitgloo.com>
Fri, 10 Feb 2023 02:54:58 +0000 (21:54 -0500)
core.fth
corewords.cpp
corewords.hpp
dictionary.hpp
parser.cpp
state.cpp
state.hpp

index 06aad0a9f96b1103a8603112b0f8995c76a1bfd3..4d1393c0cb44ea5c0e1b361a2bfd087be27a1bfc 100644 (file)
--- a/core.fth
+++ b/core.fth
@@ -4,47 +4,64 @@
 ( set decimal numbers )
 10 0 !
 
-: .      0 sys ;
-: emit   1 sys ;
-
-: over   1 pick ;
-: -rot   rot rot ;
-: nip    swap drop ;
-: tuck   swap over ;
-
-: +!     swap over @ + swap ! ;
-
-: 1+ 1 + ;
-: 1- 1 - ;
-
-: 0= 0 = ;
-: >= < 0= ;
-
-: 2drop  drop drop ;
-: 2dup   over over ;
-: 2over  3 pick 3 pick ;
-: 2swap  rot >r rot r> ;
-
-: and    & ;
-: or     | ;
-: xor    ^ ;
-: lshift << ;
-: rshift >> ;
-: 2*     2 * ;
-: 2/     2 / ;
-
-: ,      here ! 1 allot ;
-: c!     ! ;
-: c,     , ;
-: c@     @ ;
-: cell+  1+ ;
-: cells  ;
-: char+  1+ ;
-: chars  ;
-
-: cr 9 emit ;
-: bl 32 ;
+: .       0 sys ;
+: emit    1 sys ;
 
+: over    1 pick ;
+: -rot    rot rot ;
+: nip     swap drop ;
+: tuck    swap over ;
+
+: and     & ;
+: or      | ;
+: xor     ^ ;
+: lshift  << ;
+: rshift  >> ;
+: mod     % ;
+: 2*      2 * ;
+: 2/      2 / ;
+
+: 0=      0 = ;
+: 0<      0 < ;
+: <=      2dup < -rot = and ;
+: >       <= 0= ;
+
+: 1+      1 + ;
+: 1-      1 - ;
+
+: 2drop   drop drop ;
+: 2dup    over over ;
+: 2over   3 pick 3 pick ;
+: 2swap   rot >r rot r> ;
+
+: r@      r> dup >r ;
+: ,       here ! 1 allot ;
+: +!      swap over @ + swap ! ;
+: c!      ! ;
+: c,      , ;
+: c@      @ ;
+: cell+   1+ ;
+: cells   ;
+: char+   1+ ;
+: chars   ;
+: 2!      swap over ! cell+ ! ;
+: 2@      dup cell+ @ swap @ ;
+
+: cr      9 emit ;
+: bl      32 ;
+: space   bl emit ;
+
+: state   1 ;
 : base    0 ;
 : decimal 1 2* base ! 1010 base ! ;
 
+: ?dup    dup if dup then ;
+
+: negate  -1 * ;
+: abs     dup 0< if negate then ;
+: min     2dup <= if drop else nip then ;
+: max     2dup <= if nip else drop then ;
+
+: align ;
+: aligned ;
+
index d016ba1a6a8a5ae929cd5771140b3a7371e6254e..f0f8d5d63c6f2a918281ad160f49602a3ad76eac 100644 (file)
@@ -53,8 +53,9 @@ Func CoreWords::get(int index)
     case 29: return op_if;
     case 30: return op_then;
     case 31: return op_else;
-    case 32: return op_literal;
-    case 33: return op_jump;
+    case 32: return op_depth;
+    case 33: return op_literal;
+    case 34: return op_jump;
     default: return nullptr;
     }
 }
@@ -206,7 +207,7 @@ int CoreWords::op_colon(State& state) {
 }
 
 int CoreWords::op_semic(State& state) {
-    if (!state.compiling) {
+    if (!state.compiling()) {
         state.ip = state.popr();
     } else {
         auto begin = state.popr();
@@ -216,7 +217,7 @@ int CoreWords::op_semic(State& state) {
             ((begin - state.dict.latest) << 6));
 
         state.dict.latest = begin;
-        state.compiling = false;
+        state.compiling(false);
     }
 
     return 0;
@@ -257,7 +258,7 @@ int CoreWords::op_jump(State& state)
 
 int CoreWords::op_if(State& state)
 {
-    if (state.compiling) {
+    if (state.compiling()) {
         state.push(state.dict.here);
         state.dict.add(0);
     } else {
@@ -272,7 +273,7 @@ int CoreWords::op_if(State& state)
 
 int CoreWords::op_then(State& state)
 {
-    if (state.compiling) {
+    if (state.compiling()) {
         const auto ifaddr = state.pop();
         if (state.dict.read(ifaddr) == 0)
             state.dict.write(ifaddr, state.dict.here);
@@ -283,7 +284,7 @@ int CoreWords::op_then(State& state)
 
 int CoreWords::op_else(State& state)
 {
-    if (state.compiling) {
+    if (state.compiling()) {
         const auto ifaddr = state.pop();
         state.push(state.dict.here);
         state.dict.add(0);
@@ -295,6 +296,12 @@ int CoreWords::op_else(State& state)
     return 0;
 }
 
+int CoreWords::op_depth(State& state)
+{
+    state.push(state.size());
+    return 0;
+}
+
 int CoreWords::findi(std::string_view str)
 {
     std::size_t i;
index 0b84b0e9495d7d741316cbcc7ea416598b937ff6..2d894a10226cd1a47f8662372f5cb5978a0ae3dc 100644 (file)
@@ -27,7 +27,7 @@ int user_sys(State&);
 class CoreWords
 {
 public:
-    constexpr static std::size_t VisibleWordCount = 32;             // size
+    constexpr static std::size_t VisibleWordCount = 33;             // size
     constexpr static auto HiddenWordLiteral = VisibleWordCount;     // index
     constexpr static auto HiddenWordJump    = VisibleWordCount + 1; // index
     constexpr static auto WordCount         = HiddenWordJump + 1;   // size
@@ -47,7 +47,7 @@ private:
         "=\0<\0allot\0&\0|\0"
         "^\0<<\0>>\0(\0:\0"
         ";\1here\0imm\0const\0"
-       "if\1then\1else\1";
+       "if\1then\1else\1depth\0";
 
     static Func get(int);
 
@@ -85,6 +85,7 @@ private:
     static int op_if(State&);
     static int op_then(State&);
     static int op_else(State&);
+    static int op_depth(State&);
 };
 
 #endif // ALEEFORTH_COREWORDS_HPP
index 2a54a5d3b6431c24279873ca191ec7a275c59468..5c6471acab0db8420c8afd58533330826d5fed02 100644 (file)
 class Dictionary
 {
 public:
-    Addr here = 1; // address zero will be used for BASE.
+    constexpr static Addr Base = 0;
+    constexpr static Addr Compiling = 1;
+
+    Addr here = 2;
     Addr latest = 0;
 
     virtual Cell read(Addr) const = 0;
index 19ef34a267e9f02b716888a7bd7b5fa465d01a43..d58b4ed598e4d646e73ec84fb39cb8c541204631 100644 (file)
@@ -39,12 +39,12 @@ ParseStatus Parser::parse(State& state, std::string_view& str)
             break;
         case Pass::Colon:
             state.pass = Pass::None;
-            state.compiling = true;
+            state.compiling(true);
             state.dict.addDefinition(sub);
             break;
         case Pass::Constant:
             state.pass = Pass::None;
-            state.compiling = true;
+            state.compiling(true);
             state.dict.addDefinition(sub);
             state.dict.add(CoreWords::HiddenWordLiteral);
             state.dict.add(state.pop());
@@ -56,18 +56,18 @@ ParseStatus Parser::parse(State& state, std::string_view& str)
         }
     } else {
         if (auto i = CoreWords::findi(sub); i >= 0) {
-            if (state.compiling)
+            if (state.compiling())
                 state.dict.add(i & ~CoreWords::CoreImmediate);
-            if (!state.compiling || (i & CoreWords::CoreImmediate))
+            if (!state.compiling() || (i & CoreWords::CoreImmediate))
                 CoreWords::run(i & ~CoreWords::CoreImmediate, state);
         } else if (auto j = state.dict.find(sub); j > 0) {
             auto e = state.dict.getexec(j);
 
-            if (state.compiling) {
+            if (state.compiling()) {
                 if (state.dict.read(j) & CoreWords::Immediate) {
-                    state.compiling = false;
+                    state.compiling(false);
                     Executor::fullexec(state, e);
-                    state.compiling = true;
+                    state.compiling(true);
                 } else {
                     state.dict.add(CoreWords::HiddenWordJump);
                     state.dict.add(e);
@@ -81,7 +81,7 @@ ParseStatus Parser::parse(State& state, std::string_view& str)
             const auto l = static_cast<Cell>(std::strtol(sub.data(), &p, base));
 
             if (p != sub.data()) {
-                if (state.compiling) {
+                if (state.compiling()) {
                     state.dict.add(CoreWords::HiddenWordLiteral);
                     state.dict.add(l);
                 } else {
index 0b5c8fe01e1f5f3c59b2f6364737fc76a45c0d10..6165ce4efc910fc832c19792e465ec0b79d7970b 100644 (file)
--- a/state.cpp
+++ b/state.cpp
 
 #include <iterator>
 
+bool State::compiling() const
+{
+    return dict.read(Dictionary::Compiling);
+}
+
+void State::compiling(bool yes)
+{
+    dict.write(Dictionary::Compiling, yes);
+}
+
 Cell State::beyondip() const
 {
     return dict.read(ip + 1);
index 63119493d78b017181a019a18a31514058f7f98e..e17d324288ed680bfdf80980b864aea79a911947 100644 (file)
--- a/state.hpp
+++ b/state.hpp
@@ -35,13 +35,15 @@ class State
     Cell *rsp = rstack - 1;
 
 public:
-    bool compiling = false;
     Addr ip = 0;
     Pass pass = Pass::None;
     Dictionary& dict;
 
     constexpr State(Dictionary& d): dict(d) {}
 
+    bool compiling() const;
+    void compiling(bool);
+
     Cell beyondip() const;
 
     void pushr(Cell);