Skip to content

Commit

Permalink
[sc-472] Allow passing arguments to program using a new CLI interface. (
Browse files Browse the repository at this point in the history
#66)

Co-authored-by: tjjfvi <[email protected]>
  • Loading branch information
FranchuFranchu and tjjfvi authored Feb 28, 2024
1 parent cfcbf05 commit 0ba064c
Show file tree
Hide file tree
Showing 10 changed files with 593 additions and 176 deletions.
98 changes: 90 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ panic = "abort"
debug = "full"

[dependencies]
clap = { version = "4.5.1", features = ["derive"] }
nohash-hasher = { version = "0.2.0" }

##--COMPILER-CUTOFF--##
Expand Down
6 changes: 6 additions & 0 deletions examples/arithmetic.hvm2
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
add = λaλb(+ a b)
sub = λaλb(- a b)
mul = λaλb(* a b)
div = λaλb(/ a b)
mod = λaλb(% a b)
main = λaλb((div a b), (mod a b))
9 changes: 9 additions & 0 deletions examples/arithmetic.hvmc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@add = (<+ a b> (a b))
@div = (</ a b> (a b))
@main = ({3 a b} ({5 c d} [e f]))
& @mod ~ (b (d f))
& @div ~ (a (c e))
@mod = (<% a b> (a b))
@mul = (<* a b> (a b))
@sub = (<- a b> (a b))

49 changes: 3 additions & 46 deletions src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
use crate::{
ast::{Book, Net, Tree},
run::{self, Addr, Def, Instruction, InterpretedDef, LabSet, Mode, Port, Tag, Trg, TrgId, Wire},
run::{self, Addr, Def, Instruction, InterpretedDef, LabSet, Mode, Port, Tag, TrgId, Wire},
util::create_var,
};
use std::{
collections::{hash_map::Entry, HashMap},
ops::{Deref, DerefMut, RangeFrom},
};

mod encode;

/// Stores a bidirectional mapping between names and runtime defs.
#[derive(Default)]
pub struct Host {
Expand Down Expand Up @@ -102,51 +104,6 @@ impl Host {

net
}

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);

struct EncodeState<'c, 'n, M: Mode> {
host: &'c Host,
net: &'c mut run::Net<'n, M>,
vars: HashMap<&'c str, Trg>,
}

impl<'c, 'n, M: Mode> EncodeState<'c, 'n, M> {
fn encode(&mut self, trg: Trg, tree: &'c Tree) {
match tree {
Tree::Era => self.net.link_trg_port(trg, Port::ERA),
Tree::Num { val } => self.net.link_trg_port(trg, Port::new_num(*val)),
Tree::Ref { nam } => self.net.link_trg_port(trg, Port::new_ref(&self.host.defs[nam])),
Tree::Ctr { lab, lft, rgt } => {
let (l, r) = self.net.do_ctr(*lab, trg);
self.encode(l, lft);
self.encode(r, rgt);
}
Tree::Op2 { opr, lft, rgt } => {
let (l, r) = self.net.do_op2(*opr, trg);
self.encode(l, lft);
self.encode(r, rgt);
}
Tree::Op1 { opr, lft, rgt } => {
let r = self.net.do_op1(*opr, *lft, trg);
self.encode(r, rgt);
}
Tree::Mat { sel, ret } => {
let (s, r) = self.net.do_mat(trg);
self.encode(s, sel);
self.encode(r, ret);
}
Tree::Var { nam } => match self.vars.entry(nam) {
Entry::Occupied(e) => self.net.link_trg(e.remove(), trg),
Entry::Vacant(e) => {
e.insert(trg);
}
},
}
}
}
}
}

/// See [`Host::readback`].
Expand Down
71 changes: 71 additions & 0 deletions src/host/encode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//! Code for directly encoding a [`hvmc::ast::Net`] or a [`hvmc::ast::Tree`]
//! into a [`hvmc::run::Net`]
use std::collections::{hash_map::Entry, HashMap};

use crate::{
ast::{Net, Tree},
host::Host,
run::{self, Mode, Port, Trg},
};

impl Host {
/// Encode `tree` directly into `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>,
vars: HashMap<&'c str, Trg>,
}

impl<'c, 'n, M: Mode> EncodeState<'c, 'n, M> {
fn encode(&mut self, trg: Trg, tree: &'c Tree) {
match tree {
Tree::Era => self.net.link_trg_port(trg, Port::ERA),
Tree::Num { val } => self.net.link_trg_port(trg, Port::new_num(*val)),
Tree::Ref { nam } => self.net.link_trg_port(trg, Port::new_ref(&self.host.defs[nam])),
Tree::Ctr { lab, lft, rgt } => {
let (l, r) = self.net.do_ctr(*lab, trg);
self.encode(l, lft);
self.encode(r, rgt);
}
Tree::Op2 { opr, lft, rgt } => {
let (l, r) = self.net.do_op2(*opr, trg);
self.encode(l, lft);
self.encode(r, rgt);
}
Tree::Op1 { opr, lft, rgt } => {
let r = self.net.do_op1(*opr, *lft, trg);
self.encode(r, rgt);
}
Tree::Mat { sel, ret } => {
let (s, r) = self.net.do_mat(trg);
self.encode(s, sel);
self.encode(r, ret);
}
Tree::Var { nam } => match self.vars.entry(nam) {
Entry::Occupied(e) => self.net.link_trg(e.remove(), trg),
Entry::Vacant(e) => {
e.insert(trg);
}
},
}
}
}
Loading

0 comments on commit 0ba064c

Please sign in to comment.