Feat: begin slight refactoring
Some checks failed
Deploy Website / deploy (push) Failing after 4m46s

This commit is contained in:
2026-02-01 12:38:48 +01:00
parent a0585b0814
commit dd853b8e1b
39 changed files with 4699 additions and 3168 deletions

View File

@@ -1,3 +1,6 @@
use std::collections::HashMap;
use std::sync::LazyLock;
use super::ops::Op;
use super::theory;
use super::types::{Dictionary, SourceSpan};
@@ -2446,6 +2449,21 @@ pub const WORDS: &[Word] = &[
},
];
static WORD_MAP: LazyLock<HashMap<&'static str, &'static Word>> = LazyLock::new(|| {
let mut map = HashMap::with_capacity(WORDS.len() * 2);
for word in WORDS {
map.insert(word.name, word);
for alias in word.aliases {
map.insert(alias, word);
}
}
map
});
fn lookup_word(name: &str) -> Option<&'static Word> {
WORD_MAP.get(name).copied()
}
pub(super) fn simple_op(name: &str) -> Option<Op> {
Some(match name {
"dup" => Op::Dup,
@@ -2636,23 +2654,21 @@ pub(super) fn compile_word(
return true;
}
for word in WORDS {
if word.name == name || word.aliases.contains(&name) {
match &word.compile {
Simple => {
if let Some(op) = simple_op(word.name) {
ops.push(op);
}
}
Context(ctx) => ops.push(Op::GetContext((*ctx).into())),
Param => ops.push(Op::SetParam(word.name.into())),
Probability(p) => {
ops.push(Op::PushFloat(*p, None));
ops.push(Op::ChanceExec);
if let Some(word) = lookup_word(name) {
match &word.compile {
Simple => {
if let Some(op) = simple_op(word.name) {
ops.push(op);
}
}
return true;
Context(ctx) => ops.push(Op::GetContext((*ctx).into())),
Param => ops.push(Op::SetParam(word.name.into())),
Probability(p) => {
ops.push(Op::PushFloat(*p, None));
ops.push(Op::ChanceExec);
}
}
return true;
}
// @varname - fetch variable