From 655a67fe38b6fa6fd1a26ea7debe918f2b8d2d5b Mon Sep 17 00:00:00 2001 From: Dominic Grimm Date: Sat, 10 Sep 2022 12:49:53 +0200 Subject: [PATCH] Add fetch multiple optimization --- henceforth/examples/test.fth | 3 +- henceforth/src/lib/compiler/instruction.rs | 150 ++++++++++++--------- 2 files changed, 86 insertions(+), 67 deletions(-) diff --git a/henceforth/examples/test.fth b/henceforth/examples/test.fth index 5e95ade..4651478 100644 --- a/henceforth/examples/test.fth +++ b/henceforth/examples/test.fth @@ -1,3 +1,2 @@ -." test-string" cr -r" bliblablub" debug +0 @ @ debug diff --git a/henceforth/src/lib/compiler/instruction.rs b/henceforth/src/lib/compiler/instruction.rs index 08fd3f9..4b1d847 100644 --- a/henceforth/src/lib/compiler/instruction.rs +++ b/henceforth/src/lib/compiler/instruction.rs @@ -73,71 +73,6 @@ pub enum Instruction { }, } -impl Instruction { - pub fn from_word(word: &str) -> Option { - 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 for Instruction { fn compile(&self, data: &compiler::Data) -> Result { match self { @@ -1376,6 +1311,25 @@ impl compiler::Compilable for Instruct .collect(), ] .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.compile(data)?, [hence::parser::ast::Node::Call { @@ -1399,3 +1353,69 @@ impl compiler::Compilable for Instruct } } } + +impl Instruction { + pub fn from_word(word: &str) -> Option { + 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, + } + } +} +