]> code.bitgloo.com Git - bitgloo/alee-forth.git/commitdiff
make parser extendable
authorClyne Sullivan <clyne@bitgloo.com>
Sun, 5 Nov 2023 12:00:38 +0000 (07:00 -0500)
committerClyne Sullivan <clyne@bitgloo.com>
Sun, 5 Nov 2023 12:00:38 +0000 (07:00 -0500)
libalee/parser.cpp
libalee/parser.hpp

index 16281d336077a88917598377bb11c1a0083b5662..3236ae223e4b6c56f6917452745961e1ea4a8428 100644 (file)
@@ -23,6 +23,8 @@
 #include <algorithm>
 #include <cstring>
 
+Error (*Parser::customParse)(State&, Word) = nullptr;
+
 Error Parser::parse(State& state, const char *str)
 {
     auto addr = Dictionary::Input;
@@ -60,7 +62,11 @@ Error Parser::parseWord(State& state, Word word)
         auto cw = CoreWords::findi(state, word);
 
         if (cw < 0) {
-            return parseNumber(state, word);
+            auto r = parseNumber(state, word);
+            if (r != Error::none)
+                return customParse ? customParse(state, word) : r;
+            else
+                return r;
         } else {
             ins = cw;
             imm = ins == CoreWords::Semicolon;
@@ -106,9 +112,14 @@ Error Parser::parseNumber(State& state, Word word)
     if (inv)
         result *= -1;
 
-    auto value = static_cast<Cell>(result);
+    processLiteral(state, static_cast<Cell>(result));
+    return Error::none;
+}
+
+void Parser::processLiteral(State& state, Cell value)
+{
     if (state.compiling()) {
-        auto ins = CoreWords::findi("_lit");
+        constexpr auto ins = CoreWords::findi("_lit");
 
         const Cell maxlit = Dictionary::Begin - CoreWords::WordCount;
         if (value >= 0 && value < maxlit)
@@ -120,7 +131,5 @@ Error Parser::parseNumber(State& state, Word word)
     } else {
         state.push(value);
     }
-
-    return Error::none;
 }
 
index 6af3ef9fbe82794d415c4e198acdd40f391cc516..f868afbe838b7982407051f561c333bbd5c7fa8e 100644 (file)
@@ -26,6 +26,8 @@
 class Parser
 {
 public:
+    static Error (*customParse)(State&, Word);
+
     /**
      * Parses (and evaluates) the given string using the given state.
      * The string is stored in the state's input buffer, then parseSource()
@@ -40,6 +42,8 @@ public:
      */
     static Error parseSource(State&);
 
+    static void processLiteral(State&, Cell);
+
 private:
     /**
      * Parses the given word using the given state.