Skip to content

Commit

Permalink
Improve help messages and reorganize encode_tree and encode_net m…
Browse files Browse the repository at this point in the history
…ethods.
  • Loading branch information
FranchuFranchu committed Feb 27, 2024
1 parent 9864b50 commit 1d83f9e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 21 deletions.
2 changes: 2 additions & 0 deletions src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
34 changes: 18 additions & 16 deletions src/encode.rs → src/host/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<M: Mode>(&self, net: &mut run::Net<M>, 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<M: Mode>(&self, net: &mut run::Net<M>, 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>,
Expand Down Expand Up @@ -49,19 +67,3 @@ impl<'c, 'n, M: Mode> EncodeState<'c, 'n, M> {
}
}
}

impl Host {
pub fn encode_tree<M: Mode>(&self, net: &mut run::Net<M>, trg: Trg, tree: &Tree) {
EncodeState { host: self, net, vars: Default::default() }.encode(trg, tree);
}
pub fn encode_net<M: Mode>(&self, net: &mut run::Net<M>, 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);
}
}
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

pub mod ast;
pub mod compile;
pub mod encode;
pub mod host;
pub mod ops;
pub mod run;
Expand Down
17 changes: 13 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
///
Expand All @@ -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,
Expand Down Expand Up @@ -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"))?;
Expand Down

0 comments on commit 1d83f9e

Please sign in to comment.