Add double-stack words (2dup, 2drop, 2swap, 2over) and forget

This commit is contained in:
2026-02-02 07:46:39 +01:00
parent 3e8076e416
commit 2af0b67714
6 changed files with 186 additions and 0 deletions

View File

@@ -1,4 +1,5 @@
use super::harness::*;
use cagire::forth::Value;
#[test]
fn define_and_use_word() {
@@ -113,3 +114,44 @@ fn define_word_with_conditional() {
f.evaluate("10 maybe-double", &ctx).unwrap();
assert_eq!(stack_int(&f), 20);
}
#[test]
fn forget_removes_word() {
let f = forth();
let ctx = default_ctx();
f.evaluate(": double 2 * ;", &ctx).unwrap();
f.evaluate("5 double", &ctx).unwrap();
assert_eq!(stack_int(&f), 10);
f.clear_stack();
f.evaluate("\"double\" forget", &ctx).unwrap();
f.evaluate("double", &ctx).unwrap();
let stack = f.stack();
assert_eq!(stack.len(), 1);
match &stack[0] {
Value::Str(s, _) => assert_eq!(s, "double"),
other => panic!("expected Str, got {:?}", other),
}
}
#[test]
fn forget_nonexistent_is_noop() {
let f = forth();
let ctx = default_ctx();
f.evaluate("\"nosuchword\" forget", &ctx).unwrap();
f.evaluate("42", &ctx).unwrap();
assert_eq!(stack_int(&f), 42);
}
#[test]
fn forget_and_redefine() {
let f = forth();
let ctx = default_ctx();
f.evaluate(": foo 10 ;", &ctx).unwrap();
f.evaluate("foo", &ctx).unwrap();
assert_eq!(stack_int(&f), 10);
f.clear_stack();
f.evaluate("\"foo\" forget", &ctx).unwrap();
f.evaluate(": foo 20 ;", &ctx).unwrap();
f.evaluate("foo", &ctx).unwrap();
assert_eq!(stack_int(&f), 20);
}