Words and universal macOS installer
Some checks failed
Deploy Website / deploy (push) Failing after 4m48s
Some checks failed
Deploy Website / deploy (push) Failing after 4m48s
This commit is contained in:
@@ -711,6 +711,61 @@ impl Forth {
|
||||
drain_select_run(count, idx, stack, outputs, cmd)?;
|
||||
}
|
||||
|
||||
Op::Bounce => {
|
||||
let count = stack.pop().ok_or("stack underflow")?.as_int()? as usize;
|
||||
if count == 0 {
|
||||
return Err("bounce count must be > 0".into());
|
||||
}
|
||||
let idx = if count == 1 {
|
||||
0
|
||||
} else {
|
||||
let period = 2 * (count - 1);
|
||||
let raw = ctx.runs % period;
|
||||
if raw < count { raw } else { period - raw }
|
||||
};
|
||||
drain_select_run(count, idx, stack, outputs, cmd)?;
|
||||
}
|
||||
|
||||
Op::WChoose => {
|
||||
let count = stack.pop().ok_or("stack underflow")?.as_int()? as usize;
|
||||
if count == 0 {
|
||||
return Err("wchoose count must be > 0".into());
|
||||
}
|
||||
let pairs_needed = count * 2;
|
||||
if stack.len() < pairs_needed {
|
||||
return Err("stack underflow".into());
|
||||
}
|
||||
let start = stack.len() - pairs_needed;
|
||||
let mut values = Vec::with_capacity(count);
|
||||
let mut weights = Vec::with_capacity(count);
|
||||
for i in 0..count {
|
||||
let val = stack[start + i * 2].clone();
|
||||
let w = stack[start + i * 2 + 1].as_float()?;
|
||||
if w < 0.0 {
|
||||
return Err("wchoose: negative weight".into());
|
||||
}
|
||||
values.push(val);
|
||||
weights.push(w);
|
||||
}
|
||||
stack.truncate(start);
|
||||
let total: f64 = weights.iter().sum();
|
||||
if total <= 0.0 {
|
||||
return Err("wchoose: total weight must be > 0".into());
|
||||
}
|
||||
let threshold: f64 = self.rng.lock().gen::<f64>() * total;
|
||||
let mut cumulative = 0.0;
|
||||
let mut selected_idx = count - 1;
|
||||
for (i, &w) in weights.iter().enumerate() {
|
||||
cumulative += w;
|
||||
if threshold < cumulative {
|
||||
selected_idx = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
let selected = values.swap_remove(selected_idx);
|
||||
select_and_run(selected, stack, outputs, cmd)?;
|
||||
}
|
||||
|
||||
Op::ChanceExec | Op::ProbExec => {
|
||||
let threshold = stack.pop().ok_or("stack underflow")?.as_float()?;
|
||||
let quot = stack.pop().ok_or("stack underflow")?;
|
||||
|
||||
Reference in New Issue
Block a user