Skip to content
Merged
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
3 changes: 2 additions & 1 deletion src/builtins.rs → src/compile/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use std::num::NonZeroUsize;

use simplicity::node::CoreConstructible;

use crate::{named::CoreExt, ProgNode};
use super::ProgNode;
use crate::named::CoreExt;

/// Fold an array of size `size` elements using function `f`.
///
Expand Down
19 changes: 14 additions & 5 deletions src/compile.rs → src/compile/mod.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
//! Compile the parsed ast into a simplicity program

mod builtins;

use std::sync::Arc;

use either::Either;
use simplicity::jet::Elements;
use simplicity::node::{CoreConstructible as _, JetConstructible as _};
use simplicity::{Cmr, FailEntropy};

use self::builtins::array_fold;
use crate::array::{BTreeSlice, Partition};
use crate::ast::{
Call, CallName, Expression, ExpressionInner, Match, Program, SingleExpression,
SingleExpressionInner, Statement,
};
use crate::builtins::array_fold;
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 @@ -258,13 +262,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
42 changes: 15 additions & 27 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
//! Library for parsing and compiling SimplicityHL

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

pub mod array;
pub mod ast;
pub mod builtins;
pub mod compile;
pub mod debug;
pub mod dummy_env;
Expand Down Expand Up @@ -81,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 @@ -95,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 @@ -134,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 @@ -163,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