Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ use crate::ast::{
};
use crate::debug::CallTracker;
use crate::error::{Error, RichError, Span, WithSpan};
use crate::named::{CoreExt, PairBuilder};
use crate::named::{self, CoreExt, PairBuilder};
use crate::num::{NonZeroPow2Usize, Pow2Usize};
use crate::pattern::{BasePattern, Pattern};
use crate::str::WitnessName;
use crate::types::{StructuralType, TypeDeconstructible};
use crate::value::StructuralValue;
use crate::witness::Arguments;
use crate::{ProgNode, Value};
use crate::Value;

type ProgNode = Arc<named::ConstructNode<Elements>>;

/// Each SimplicityHL expression expects an _input value_.
/// A SimplicityHL expression is translated into a Simplicity expression
Expand Down Expand Up @@ -257,13 +259,18 @@ impl Program {
&self,
arguments: Arguments,
include_debug_symbols: bool,
) -> Result<ProgNode, RichError> {
) -> Result<Arc<named::CommitNode<Elements>>, RichError> {
let mut scope = Scope::new(
Arc::clone(self.call_tracker()),
arguments,
include_debug_symbols,
);
self.main().compile(&mut scope).map(PairBuilder::build)

let main = self.main();
let construct = main.compile(&mut scope).map(PairBuilder::build)?;
// SimplicityHL types should be correct by construction. If not, assign the
// whole main function as the span for them, which is as sensible as anything.
named::finalize_types(&construct).with_span(main)
}
}

Expand Down
41 changes: 15 additions & 26 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! Library for parsing and compiling SimplicityHL

pub type ProgNode = Arc<named::ConstructNode>;

pub mod array;
pub mod ast;
pub mod compile;
Expand Down Expand Up @@ -80,12 +78,15 @@ impl TemplateProgram {
arguments
.is_consistent(self.simfony.parameters())
.map_err(|error| error.to_string())?;

let commit = self
.simfony
.compile(arguments, include_debug_symbols)
.with_file(Arc::clone(&self.file))?;

Ok(CompiledProgram {
debug_symbols: self.simfony.debug_symbols(self.file.as_ref()),
simplicity: self
.simfony
.compile(arguments, include_debug_symbols)
.with_file(Arc::clone(&self.file))?,
simplicity: commit,
witness_types: self.simfony.witness_types().shallow_clone(),
})
}
Expand All @@ -94,22 +95,11 @@ impl TemplateProgram {
/// A SimplicityHL program, compiled to Simplicity.
#[derive(Clone, Debug)]
pub struct CompiledProgram {
simplicity: ProgNode,
simplicity: Arc<named::CommitNode<Elements>>,
witness_types: WitnessTypes,
debug_symbols: DebugSymbols,
}

impl Default for CompiledProgram {
fn default() -> Self {
use simplicity::node::CoreConstructible;
Self {
simplicity: ProgNode::unit(&simplicity::types::Context::new()),
witness_types: WitnessTypes::default(),
debug_symbols: DebugSymbols::default(),
}
}
}

impl CompiledProgram {
/// Parse and compile a SimplicityHL program from the given string.
///
Expand All @@ -133,8 +123,7 @@ impl CompiledProgram {

/// Access the Simplicity target code, without witness data.
pub fn commit(&self) -> Arc<CommitNode<Elements>> {
named::to_commit_node(&self.simplicity)
.expect("Compiled SimplicityHL program has type 1 -> 1")
named::forget_names(&self.simplicity)
}

/// Satisfy the SimplicityHL program with the given `witness_values`.
Expand Down Expand Up @@ -162,13 +151,13 @@ impl CompiledProgram {
witness_values
.is_consistent(&self.witness_types)
.map_err(|e| e.to_string())?;
let simplicity_witness = named::to_witness_node(&self.simplicity, witness_values);
let simplicity_redeem = match env {
Some(env) => simplicity_witness.finalize_pruned(env),
None => simplicity_witness.finalize_unpruned(),
};

let mut simplicity_redeem = named::populate_witnesses(&self.simplicity, witness_values)?;
if let Some(env) = env {
simplicity_redeem = simplicity_redeem.prune(env).map_err(|e| e.to_string())?;
}
Ok(SatisfiedProgram {
simplicity: simplicity_redeem.map_err(|e| e.to_string())?,
simplicity: simplicity_redeem,
debug_symbols: self.debug_symbols.clone(),
})
}
Expand Down
Loading