Add fetch multiple optimization

This commit is contained in:
Dominic Grimm 2022-09-10 12:49:53 +02:00
parent f997fdaf64
commit 655a67fe38
No known key found for this signature in database
GPG key ID: A6C051C716D2CE65
2 changed files with 86 additions and 67 deletions

View file

@ -1,3 +1,2 @@
." test-string" cr 0 @ @ debug
r" bliblablub" debug

View file

@ -73,71 +73,6 @@ pub enum Instruction {
}, },
} }
impl Instruction {
pub fn from_word(word: &str) -> Option<Self> {
match word.to_lowercase().as_str() {
"nop" => Some(Instruction::Nop),
"debug" => Some(Instruction::Debug),
"quit" => Some(Instruction::Quit),
"drop" => Some(Instruction::Drop),
"depth" => Some(Instruction::Depth),
"pick" => Some(Instruction::Pick),
"dup" => Some(Instruction::Dup),
"swap" => Some(Instruction::Swap),
"over" => Some(Instruction::Over),
"rot" => Some(Instruction::Rot),
"nip" => Some(Instruction::Nip),
"i" => Some(Instruction::I),
"j" => Some(Instruction::J),
"tuck" => Some(Instruction::Tuck),
"@" => Some(Instruction::Fetch),
"?" => Some(Instruction::FetchPrint),
"!" => Some(Instruction::Store),
"+!" => Some(Instruction::PlusStore),
"-!" => Some(Instruction::MinusStore),
"cells" => Some(Instruction::Cells),
"allot" => Some(Instruction::Allot),
"." => Some(Instruction::Dot),
"emit" => Some(Instruction::Emit),
"space" => Some(Instruction::Space),
"spaces" => Some(Instruction::Spaces),
"cr" => Some(Instruction::Cr),
"count" => Some(Instruction::Count),
"not" => Some(Instruction::Not),
"and" => Some(Instruction::And),
"nand" => Some(Instruction::Nand),
"or" => Some(Instruction::Or),
"nor" => Some(Instruction::Nor),
"xor" => Some(Instruction::Xor),
"xnor" => Some(Instruction::Xnor),
"lshift" => Some(Instruction::Lsh),
"rshift" => Some(Instruction::Rsh),
"compare" => Some(Instruction::Compare),
"=" => Some(Instruction::Eq),
"!=" => Some(Instruction::Neq),
"<" => Some(Instruction::Lt),
"<=" => Some(Instruction::Leq),
">" => Some(Instruction::Gt),
">=" => Some(Instruction::Geq),
"min" => Some(Instruction::Min),
"max" => Some(Instruction::Max),
"boolean" => Some(Instruction::Boolean),
"invert" => Some(Instruction::Invert),
"random" => Some(Instruction::Random),
"+" => Some(Instruction::Plus),
"1+" => Some(Instruction::OnePlus),
"2+" => Some(Instruction::TwoPlus),
"-" => Some(Instruction::Minus),
"1-" => Some(Instruction::OneMinus),
"2-" => Some(Instruction::TwoMinus),
"*" => Some(Instruction::Times),
"/" => Some(Instruction::Divide),
"mod" => Some(Instruction::Mod),
_ => None,
}
}
}
impl compiler::Compilable<compiler::Data, hence::parser::ast::Body> for Instruction { impl compiler::Compilable<compiler::Data, hence::parser::ast::Body> for Instruction {
fn compile(&self, data: &compiler::Data) -> Result<hence::parser::ast::Body> { fn compile(&self, data: &compiler::Data) -> Result<hence::parser::ast::Body> {
match self { match self {
@ -1376,6 +1311,25 @@ impl compiler::Compilable<compiler::Data, hence::parser::ast::Body> for Instruct
.collect(), .collect(),
] ]
.concat()), .concat()),
Instruction::Fetch => Ok([
vec![hence::parser::ast::Node::MacroCall {
name: "std_ld".to_string(),
args: vec![],
}],
[hence::parser::ast::Node::Call {
name: "get".to_string(),
arg: None,
}]
.into_iter()
.cycle()
.take(*count)
.collect(),
vec![hence::parser::ast::Node::Call {
name: "tls".to_string(),
arg: None,
}],
]
.concat()),
Instruction::Space | Instruction::Cr => Ok([ Instruction::Space | Instruction::Cr => Ok([
instruction.compile(data)?, instruction.compile(data)?,
[hence::parser::ast::Node::Call { [hence::parser::ast::Node::Call {
@ -1399,3 +1353,69 @@ impl compiler::Compilable<compiler::Data, hence::parser::ast::Body> for Instruct
} }
} }
} }
impl Instruction {
pub fn from_word(word: &str) -> Option<Self> {
match word.to_lowercase().as_str() {
"nop" => Some(Instruction::Nop),
"debug" => Some(Instruction::Debug),
"quit" => Some(Instruction::Quit),
"drop" => Some(Instruction::Drop),
"depth" => Some(Instruction::Depth),
"pick" => Some(Instruction::Pick),
"dup" => Some(Instruction::Dup),
"swap" => Some(Instruction::Swap),
"over" => Some(Instruction::Over),
"rot" => Some(Instruction::Rot),
"nip" => Some(Instruction::Nip),
"i" => Some(Instruction::I),
"j" => Some(Instruction::J),
"tuck" => Some(Instruction::Tuck),
"@" => Some(Instruction::Fetch),
"?" => Some(Instruction::FetchPrint),
"!" => Some(Instruction::Store),
"+!" => Some(Instruction::PlusStore),
"-!" => Some(Instruction::MinusStore),
"cells" => Some(Instruction::Cells),
"allot" => Some(Instruction::Allot),
"." => Some(Instruction::Dot),
"emit" => Some(Instruction::Emit),
"space" => Some(Instruction::Space),
"spaces" => Some(Instruction::Spaces),
"cr" => Some(Instruction::Cr),
"count" => Some(Instruction::Count),
"not" => Some(Instruction::Not),
"and" => Some(Instruction::And),
"nand" => Some(Instruction::Nand),
"or" => Some(Instruction::Or),
"nor" => Some(Instruction::Nor),
"xor" => Some(Instruction::Xor),
"xnor" => Some(Instruction::Xnor),
"lshift" => Some(Instruction::Lsh),
"rshift" => Some(Instruction::Rsh),
"compare" => Some(Instruction::Compare),
"=" => Some(Instruction::Eq),
"!=" => Some(Instruction::Neq),
"<" => Some(Instruction::Lt),
"<=" => Some(Instruction::Leq),
">" => Some(Instruction::Gt),
">=" => Some(Instruction::Geq),
"min" => Some(Instruction::Min),
"max" => Some(Instruction::Max),
"boolean" => Some(Instruction::Boolean),
"invert" => Some(Instruction::Invert),
"random" => Some(Instruction::Random),
"+" => Some(Instruction::Plus),
"1+" => Some(Instruction::OnePlus),
"2+" => Some(Instruction::TwoPlus),
"-" => Some(Instruction::Minus),
"1-" => Some(Instruction::OneMinus),
"2-" => Some(Instruction::TwoMinus),
"*" => Some(Instruction::Times),
"/" => Some(Instruction::Divide),
"mod" => Some(Instruction::Mod),
_ => None,
}
}
}