From 41df5f90d0eb97df49586571a80a78a3487ff928 Mon Sep 17 00:00:00 2001 From: Oleksandr Kozachuk Date: Wed, 8 Apr 2026 11:03:14 +0200 Subject: [PATCH] =?UTF-8?q?Fix=20DU<,=20register=202VARIABLE/2CONSTANT=20c?= =?UTF-8?q?allable=20=E2=80=94=20double=2027=E2=86=923?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - DU< had same comparison order bug as D< (comparing d2-hi < d1-hi instead of d1-hi < d2-hi). Fixed with SWAP U<. - 2VARIABLE and 2CONSTANT were handled as special tokens but not registered in the dictionary, so they couldn't be called from compiled code (e.g., : CD4 2VARIABLE ;). Added pending codes 9/10. --- crates/core/boot.fth | 3 ++- crates/core/src/outer.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/crates/core/boot.fth b/crates/core/boot.fth index 34a9f3f..6e10977 100644 --- a/crates/core/boot.fth +++ b/crates/core/boot.fth @@ -118,7 +118,8 @@ : M+ S>D D+ ; \ DU< ( ud1 ud2 -- flag ) unsigned double-cell less-than -: DU< ROT 2DUP = IF 2DROP U< ELSE U< NIP NIP THEN ; +\ Compare high cells (unsigned); if equal, compare low cells unsigned. +: DU< ROT 2DUP = IF 2DROP U< ELSE SWAP U< NIP NIP THEN ; \ --------------------------------------------------------------- \ Phase 3: Mixed arithmetic (built on M* and UM/MOD host primitives) diff --git a/crates/core/src/outer.rs b/crates/core/src/outer.rs index 33eb3be..4128808 100644 --- a/crates/core/src/outer.rs +++ b/crates/core/src/outer.rs @@ -4264,6 +4264,34 @@ impl ForthVM { self.register_host_primitive("CREATE", false, func)?; } + // 2CONSTANT: sets pending_define to 9 + { + let pending = Arc::clone(&self.pending_define); + let func = Func::new( + &mut self.store, + FuncType::new(&self.engine, [], []), + move |_caller, _params, _results| { + *pending.lock().unwrap() = 9; + Ok(()) + }, + ); + self.register_host_primitive("2CONSTANT", false, func)?; + } + + // 2VARIABLE: sets pending_define to 10 + { + let pending = Arc::clone(&self.pending_define); + let func = Func::new( + &mut self.store, + FuncType::new(&self.engine, [], []), + move |_caller, _params, _results| { + *pending.lock().unwrap() = 10; + Ok(()) + }, + ); + self.register_host_primitive("2VARIABLE", false, func)?; + } + Ok(()) } @@ -4422,6 +4450,8 @@ impl ForthVM { 6 => self.interpret_find(), 7 => self.interpret_parse(), 8 => self.interpret_parse_name(), + 9 => self.define_2constant(), + 10 => self.define_2variable(), _ => Ok(()), } }