From 1d83f9e6a2c4cc71cbcf664b9f216ea2f6a275bb Mon Sep 17 00:00:00 2001 From: FranchuFranchu Date: Tue, 27 Feb 2024 15:45:25 -0300 Subject: [PATCH] Improve help messages and reorganize `encode_tree` and `encode_net` methods. --- src/host.rs | 2 ++ src/{ => host}/encode.rs | 34 ++++++++++++++++++---------------- src/lib.rs | 1 - src/main.rs | 17 +++++++++++++---- 4 files changed, 33 insertions(+), 21 deletions(-) rename src/{ => host}/encode.rs (90%) diff --git a/src/host.rs b/src/host.rs index 2edcd684..4c4d7f98 100644 --- a/src/host.rs +++ b/src/host.rs @@ -11,6 +11,8 @@ use std::{ ops::{Deref, DerefMut, RangeFrom}, }; +pub mod encode; + /// Stores a bidirectional mapping between names and runtime defs. #[derive(Default)] pub struct Host { diff --git a/src/encode.rs b/src/host/encode.rs similarity index 90% rename from src/encode.rs rename to src/host/encode.rs index 94fd12d5..dae485d6 100644 --- a/src/encode.rs +++ b/src/host/encode.rs @@ -9,6 +9,24 @@ use crate::{ run::{self, Mode, Port, Trg}, }; +impl Host { + /// Encode a tree `tree` directly into a port or wire `trg`, skipping the intermediate `Def` representation + pub fn encode_tree(&self, net: &mut run::Net, trg: Trg, tree: &Tree) { + EncodeState { host: self, net, vars: Default::default() }.encode(trg, tree); + } + /// Encode the root of `ast_net` directly into `trg` and encode its redexes into `net` redex list. + pub fn encode_net(&self, net: &mut run::Net, trg: Trg, ast_net: &Net) { + let mut state = EncodeState { host: self, net, vars: Default::default() }; + for (l, r) in &ast_net.rdex { + let (ap, a, bp, b) = state.net.do_wires(); + state.encode(ap, l); + state.encode(bp, r); + state.net.link_trg(a, b); + } + state.encode(trg, &ast_net.root); + } +} + struct EncodeState<'c, 'n, M: Mode> { host: &'c Host, net: &'c mut run::Net<'n, M>, @@ -49,19 +67,3 @@ impl<'c, 'n, M: Mode> EncodeState<'c, 'n, M> { } } } - -impl Host { - pub fn encode_tree(&self, net: &mut run::Net, trg: Trg, tree: &Tree) { - EncodeState { host: self, net, vars: Default::default() }.encode(trg, tree); - } - pub fn encode_net(&self, net: &mut run::Net, trg: Trg, ast_net: &Net) { - let mut state = EncodeState { host: self, net, vars: Default::default() }; - for (l, r) in &ast_net.rdex { - let (ap, a, bp, b) = state.net.do_wires(); - state.encode(ap, l); - state.encode(bp, r); - state.net.link_trg(a, b); - } - state.encode(trg, &ast_net.root); - } -} diff --git a/src/lib.rs b/src/lib.rs index 4751036f..efa6fd0d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,7 +4,6 @@ pub mod ast; pub mod compile; -pub mod encode; pub mod host; pub mod ops; pub mod run; diff --git a/src/main.rs b/src/main.rs index b12c599e..dfbeb305 100644 --- a/src/main.rs +++ b/src/main.rs @@ -49,8 +49,16 @@ fn main() { } #[derive(Parser, Debug)] -#[command(author, version)] -/// A massively parallel Interaction Combinator evaluator +#[command(author, version, about = "A massively parallel Interaction Combinator evaluator", + long_about = r##" +A massively parallel Interaction Combinator evaluator + +Examples: +$ hvmc run examples/church_encoding/church.hvm +$ hvmc run tests/programs/addition.hvmc "#16" "#3" +$ hvmc compile tests/programs/addition.hvmc +$ hvmc reduce tests/programs/addition.hvmc -- "a & @mul ~ (#3 (#4 a))" +$ hvmc reduce -- "a & #3 ~ <* #4 a>""##)] struct FullCli { #[command(subcommand)] pub mode: CliMode, @@ -90,6 +98,7 @@ struct RuntimeOpts { #[derive(Args, Clone, Debug)] struct RunArgs { #[arg(short = 'e', default_value = "main")] + /// Name of the definition that will get reduced entry_point: String, /// List of arguments to pass to the program /// @@ -108,7 +117,7 @@ enum CliMode { /// hvm-core file to compile file: String, }, - /// Run a program, optionally passing a set of arguments into it. + /// Run a program, optionally passing a list of arguments to it. Run { #[command(flatten)] opts: RuntimeOpts, @@ -250,7 +259,7 @@ fn compile_executable(file_name: &str, host: &host::Host) -> Result<(), io::Erro fs::write(".hvm/Cargo.toml", cargo_toml)?; fs::write(".hvm/src/ast.rs", include_str!("../src/ast.rs"))?; fs::write(".hvm/src/compile.rs", include_str!("../src/compile.rs"))?; - fs::write(".hvm/src/encode.rs", include_str!("../src/encode.rs"))?; + fs::write(".hvm/src/host/encode.rs", include_str!("../src/host/encode.rs"))?; fs::write(".hvm/src/fuzz.rs", include_str!("../src/fuzz.rs"))?; fs::write(".hvm/src/host.rs", include_str!("../src/host.rs"))?; fs::write(".hvm/src/lib.rs", include_str!("../src/lib.rs"))?;