Skip to content

Commit

Permalink
Move some things around.
Browse files Browse the repository at this point in the history
  • Loading branch information
FranchuFranchu committed Feb 27, 2024
1 parent 92deecb commit 7fdf441
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 79 deletions.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::{
ops::{Deref, DerefMut, RangeFrom},
};

pub mod encode;
mod encode;

/// Stores a bidirectional mapping between names and runtime defs.
#[derive(Default)]
Expand Down
4 changes: 2 additions & 2 deletions src/host/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use crate::{
};

impl Host {
/// Encode a tree `tree` directly into a port or wire `trg`, skipping the
/// intermediate `Def` representation
/// Encode `tree` directly into `trg`, skipping the intermediate `Def`
/// representationw
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);
}
Expand Down
30 changes: 15 additions & 15 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,62 +80,62 @@ struct BareCli {
#[derive(Args, Clone, Debug)]
struct RuntimeOpts {
#[arg(short = 's', long = "stats")]
/// Show performance statistics
/// Show performance statistics.
show_stats: bool,
#[arg(short = '1', long = "single")]
/// Single-core mode (no parallelism)
/// Single-core mode (no parallelism).
single_core: bool,
#[arg(short = 'l', long = "lazy")]
/// Lazy mode
/// Lazy mode.
///
/// Lazy mode only expands references that are reachable
/// by a walk from the root of the net. This leads to a dramatic slowdown,
/// but allows running programs that would expand indefinitely otherwise
/// but allows running programs that would expand indefinitely otherwise.
lazy_mode: bool,
#[arg(short = 'm', long = "memory", default_value = "1G", value_parser = mem_parser)]
/// How much memory to allocate on startup.
///
/// Supports abbreviations such as '4G' or '400M'
/// Supports abbreviations such as '4G' or '400M'.
memory: u64,
}

#[derive(Args, Clone, Debug)]
struct RunArgs {
#[arg(short = 'e', default_value = "main")]
/// Name of the definition that will get reduced
/// Name of the definition that will get reduced.
entry_point: String,
/// List of arguments to pass to the program
/// List of arguments to pass to the program.
///
/// Arguments are passed using the lambda-calculus interpretation
/// of interaction combinators. So, for example, if the arguments are
/// "#1" "#2" "#3", then the expression that will get reduced is
/// `r & @main ~ (#1 (#2 (#3 r)))`
/// `r & @main ~ (#1 (#2 (#3 r)))`.
args: Vec<String>,
}

#[derive(Subcommand, Clone, Debug)]
#[command(author, version)]
enum CliMode {
/// Compile a hvm-core program into a Rust crate
/// Compile a hvm-core program into a Rust crate.
Compile {
/// hvm-core file to compile
/// hvm-core file to compile.
file: String,
},
/// Run a program, optionally passing a list of arguments to it.
Run {
#[command(flatten)]
opts: RuntimeOpts,
/// Name of the file to load
/// Name of the file to load.
file: String,
#[command(flatten)]
args: RunArgs,
},
/// Reduce hvm-core expressions to their normal form
/// Reduce hvm-core expressions to their normal form.
///
/// The expressions are passed as command-line arguments.
/// It is also possible to load files before reducing the expression,
/// which makes it possible to reference definitions from the file
/// in the expression
/// in the expression.
Reduce {
#[command(flatten)]
run_opts: RuntimeOpts,
Expand All @@ -149,7 +149,7 @@ enum CliMode {
///
/// The normal form of each expression will be
/// printed on a new line. This list must be separated from the file list
/// with a double dash ('--')
/// with a double dash ('--').
exprs: Vec<String>,
},
}
Expand All @@ -169,7 +169,7 @@ fn run(host: &Host, opts: RuntimeOpts, args: RunArgs) {
///
/// This return a [`u64`] instead of [`usize`] to ensure that parsing CLI args
/// doesn't fail on 32-bit systems. We want it to fail later on, when attempting
/// to run the program
/// to run the program.
fn mem_parser(arg: &str) -> Result<u64, String> {
let (base, mult) = match arg.to_lowercase().chars().last() {
None => return Err("Mem size argument is empty".to_string()),
Expand Down
59 changes: 0 additions & 59 deletions tests/api.rs

This file was deleted.

55 changes: 54 additions & 1 deletion tests/binary.rs → tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ use std::{
process::{Command, ExitStatus, Stdio},
};

use hvmc::{ast::{Net, Tree}, host::Host};
use insta::assert_display_snapshot;

fn get_arithmetic_program_path() -> String {
return env!("CARGO_MANIFEST_DIR").to_owned() + "/tests/programs/arithmetic.hvmc";
return env!("CARGO_MANIFEST_DIR").to_owned() + "/examples/arithmetic.hvmc";
}

fn execute_hvmc(args: &[&str]) -> Result<(ExitStatus, String), Box<dyn Error>> {
Expand Down Expand Up @@ -128,3 +129,55 @@ fn test_cli_errors() {
"###
);
}


#[test]
fn test_apply_tree() {
use hvmc::run;
fn eval_with_args(fun: &str, args: &[&str]) -> Net {
let area = run::Net::<run::Strict>::init_heap(1 << 10);

let mut fun: Net = fun.parse().unwrap();
for arg in args {
let arg: Tree = arg.parse().unwrap();
fun.apply_tree(arg)
}
// TODO: When feature/sc-472/argument-passing, use encode_net instead.
let host = Host::default();

let mut rnet = run::Net::<run::Strict>::new(&area);
let root_port = run::Trg::port(run::Port::new_var(rnet.root.addr()));
host.encode_net(&mut rnet, root_port, &fun);
rnet.normal();
let got_result = host.readback(&rnet);
got_result
}
assert_display_snapshot!(
eval_with_args("(a a)", &vec!["(a a)"]),
@"(a a)"
);
assert_display_snapshot!(
eval_with_args("b & (a b) ~ a", &vec!["(a a)"]),
@"a"
);
assert_display_snapshot!(
eval_with_args("(z0 z0)", &vec!["(z1 z1)"]),
@"(a a)"
);
assert_display_snapshot!(
eval_with_args("(* #1)", &vec!["(a a)"]),
@"#1"
);
assert_display_snapshot!(
eval_with_args("(<+ a b> (a b))", &vec!["#1", "#2"]),
@"#3"
);
assert_display_snapshot!(
eval_with_args("(<* a b> (a b))", &vec!["#2", "#3"]),
@"#6"
);
assert_display_snapshot!(
eval_with_args("(<* a b> (a b))", &vec!["#2"]),
@"(<2* a> a)"
);
}
2 changes: 1 addition & 1 deletion tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
source: tests/tests.rs
expression: output
input_file: tests/programs/arithmetic.hvmc
input_file: examples/arithmetic.hvmc
---
({3 </ a b> <% c d>} ({5 a c} [b d]))
RWTS : 7
Expand Down

0 comments on commit 7fdf441

Please sign in to comment.