]> code.bitgloo.com Git - bitgloo/alee-forth.git/commitdiff
double-width mul/div; error strings
authorClyne Sullivan <clyne@bitgloo.com>
Sat, 25 Feb 2023 13:25:31 +0000 (08:25 -0500)
committerClyne Sullivan <clyne@bitgloo.com>
Sat, 25 Feb 2023 13:25:31 +0000 (08:25 -0500)
alee.cpp
compat.txt
core.fth
corewords.cpp
corewords.hpp
state.hpp
types.hpp

index 0f270a83efdc892e41066571eb2c8b1645584df1..f65b700f7b0e6094ba4732d0af552062a6eb3c36 100644 (file)
--- a/alee.cpp
+++ b/alee.cpp
@@ -115,6 +115,20 @@ void parseLine(Parser& parser, State& state, const std::string& line)
         case Parser::UnknownWord:
             std::cout << "word not found in: " << line << std::endl;
             break;
+        case static_cast<int>(State::Error::push):
+            std::cout << "stack overflow" << std::endl;
+            break;
+        case static_cast<int>(State::Error::pushr):
+            std::cout << "return stack overflow" << std::endl;
+            break;
+        case static_cast<int>(State::Error::popr):
+            std::cout << "return stack underflow" << std::endl;
+            break;
+        case static_cast<int>(State::Error::pop):
+        case static_cast<int>(State::Error::top):
+        case static_cast<int>(State::Error::pick):
+            std::cout << "stack underflow" << std::endl;
+            break;
         default:
             std::cout << "error: " << r << std::endl;
             break;
index 6307bfc990e83438d2f06247973e1a47c4ac6ad0..7685f8ca44c936c452e1dafbc552c1da5d8e8700 100644 (file)
@@ -7,8 +7,8 @@ yes 6.1.0010 !
 yes 6.1.0070 '
 yes 6.1.0080 (
 yes 6.1.0090 *
-    6.1.0100 */
-    6.1.0110 */MOD
+yes 6.1.0100 */
+yes 6.1.0110 */MOD
 yes 6.1.0120
 yes 6.1.0130 +!
 yes 6.1.0140 +LOOP
@@ -17,7 +17,7 @@ yes 6.1.0160 -
 yes 6.1.0180 .
 yes 6.1.0190 ."
 yes 6.1.0230 /
-    6.1.0240 /MOD
+yes 6.1.0240 /MOD
 yes 6.1.0250 0<
 yes 6.1.0270 0=
 yes 6.1.0290 1+
@@ -79,7 +79,7 @@ yes 6.1.1370 EXECUTE
 yes 6.1.1380 EXIT
 yes 6.1.1540 FILL
     6.1.1550 FIND
-    6.1.1561 FM/MOD
+yes 6.1.1561 FM/MOD
 yes 6.1.1650 HERE
     6.1.1670 HOLD
 yes 6.1.1680 I
@@ -92,7 +92,7 @@ yes 6.1.1750 KEY
 yes 6.1.1780 LITERAL
 yes 6.1.1800 LOOP
 yes 6.1.1805 LSHIFT
-    6.1.1810 M*
+yes 6.1.1810 M*
 yes 6.1.1870 MAX
 yes 6.1.1880 MIN
 yes 6.1.1890 MOD
@@ -109,9 +109,9 @@ yes 6.1.2140 REPEAT
 yes 6.1.2160 ROT
 yes 6.1.2162 RSHIFT
 yes 6.1.2165 S"
-    6.1.2170 S>D
+yes 6.1.2170 S>D
     6.1.2210 SIGN
-    6.1.2214 SM/REM
+yes 6.1.2214 SM/REM
 yes 6.1.2216 SOURCE
 yes 6.1.2220 SPACE
 yes 6.1.2230 SPACES
index 85744a74c9500f04c9c89b95af0be3a625e2e0a7..0f00ba8fa9e5b10c2bddada457246233c6b33b48 100644 (file)
--- a/core.fth
+++ b/core.fth
@@ -1,7 +1,10 @@
+: *        m* drop ;
+: /        0 swap _/ ;
+: %        0 swap _% ;
+
 : cell+    2 + ;
 : cells    2 * ;
 
-
 : .        0 sys ;
 : emit     1 sys ;
 
 : 2*       2 * ;
 : 2/       2 / ;
 
+: /mod     2dup % -rot / ;
+: s>d      1 m* ;
+: */       >r m* r> _/ ;
+: */mod    >r m* 2dup r@ _% r> _/ ;
+: sm/rem   >r 2dup r@ _% -rot r> _/ ;
+: fm/mod   2dup dup >r ^ >r sm/rem swap dup
+           if r> 0< if r> + swap 1- else swap r> drop then
+           else swap 2r> 2drop then ;
+
 : cr       10 emit ;
 : bl       32 ;
 : space    bl emit ;
index c0cec485bdde43afab1b357681d229cc4832b9af..63ccc7d7b05dc9a3a843d60438310110c874d29a 100644 (file)
@@ -49,6 +49,7 @@ void CoreWords::run(unsigned int index, State& state)
     };
 
     Cell cell;
+    DoubleCell dcell;
 
     switch (index) {
     default:
@@ -79,17 +80,23 @@ void CoreWords::run(unsigned int index, State& state)
         cell = state.pop();
         state.top() -= cell;
         break;
-    case 7: // mul
+    case 7: // mul ( n n -- d )
         cell = state.pop();
-        state.top() *= cell;
+        dcell = state.pop() * cell;
+        state.push(dcell);
+        state.push(dcell >> (sizeof(Cell) * 8));
         break;
-    case 8: // div
+    case 8: // div ( d n -- n )
         cell = state.pop();
-        state.top() /= cell;
+        dcell = state.pop() << (sizeof(Cell) * 8);
+        dcell |= state.pop();
+        state.push(dcell / cell);
         break;
-    case 9: // mod
+    case 9: // mod ( d n -- n )
         cell = state.pop();
-        state.top() %= cell;
+        dcell = state.pop() << (sizeof(Cell) * 8);
+        dcell |= state.pop();
+        state.push(dcell % cell);
         break;
     case 10: // peek
         if (state.pop())
index ab34c78d126b15b966ceec264c20ed1656159351..4eb9d4202be74796a36e91fd78527ca9549b32b3 100644 (file)
@@ -41,7 +41,7 @@ private:
     // Ends with '\1': compile-only word
     constexpr static char wordsarr[] =
         "drop\0dup\0swap\0pick\0sys\0"
-        "+\0-\0*\0/\0%\0"
+        "+\0-\0m*\0_/\0_%\0"
         "_@\0_!\0>r\0r>\0=\0"
         "<\0allot\0&\0|\0^\0"
         "<<\0>>\0:\0'\0execute\0"
index 22f0d74da1a5ea3ec6fa312dbde3352701e47ce0..69db2f62d560c8406d2ee9ed1886f1ae59846ca4 100644 (file)
--- a/state.hpp
+++ b/state.hpp
@@ -25,8 +25,8 @@
 #include <csetjmp>
 #include <cstddef>
 
-constexpr unsigned DataStackSize = 8;
-constexpr unsigned ReturnStackSize = 8;
+constexpr unsigned DataStackSize = 16;
+constexpr unsigned ReturnStackSize = 16;
 
 struct State
 {
index b6391baa887d5729bb930acc22b93ebbc6699015..530ff4f60525530ecd708dd3c04b4ee5f4f164dd 100644 (file)
--- a/types.hpp
+++ b/types.hpp
@@ -25,6 +25,7 @@ struct State;
 
 using Addr = uint16_t;
 using Cell = int16_t;
+using DoubleCell = int32_t;
 using Func = void (*)(State&);
 
 constexpr unsigned int MaxCellNumberChars = 6; // -32768