diff --git a/assembly/src/assembler/mod.rs b/assembly/src/assembler/mod.rs index 284665bcd3..f8a85f47bd 100644 --- a/assembly/src/assembler/mod.rs +++ b/assembly/src/assembler/mod.rs @@ -24,8 +24,15 @@ type ProcedureCache = BTreeMap; // ASSEMBLER // ================================================================================================ - -/// TODO: add comments +/// Miden Assembler which can be used to convert Miden assembly source code into program MAST ( +/// represented by the [Program] struct). The assembler can be instantiated in several ways using +/// a "builder" patter. Specifically: +/// - If `with_kernel()` or `with_kernel_module()` methods are not used, the assembler will be +/// instantiated with a default empty kernel. Programs compiled using such assembler +/// cannot make calls to kernel procedures via `syscall` instruction. +/// - If `with_module_provider()` method is not used, the assembler will be instantiated without +/// access to external libraries. Programs compiled with such assembler must be self-contained +/// (i.e., they cannot invoke procedures from external libraries). pub struct Assembler { kernel: Kernel, module_provider: Box, diff --git a/assembly/src/errors.rs b/assembly/src/errors.rs index e03570d18a..14cb7a8f00 100644 --- a/assembly/src/errors.rs +++ b/assembly/src/errors.rs @@ -1,4 +1,4 @@ -use super::{ProcedureId, String, ToString, Token}; +use super::{ProcedureId, String, ToString, Token, Vec}; use core::fmt; // ASSEMBLY ERROR diff --git a/miden/src/main.rs b/miden/src/main.rs index 50910168d9..3d9dccad14 100644 --- a/miden/src/main.rs +++ b/miden/src/main.rs @@ -1,3 +1,5 @@ +use core::fmt; +use miden::{AssemblyError, ExecutionError}; use structopt::StructOpt; mod cli; @@ -52,3 +54,22 @@ pub fn main() { println!("{}", error); } } + +// PROGRAM ERROR +// ================================================================================================ + +/// This is used to specify the error type returned from analyze. +#[derive(Debug)] +pub enum ProgramError { + AssemblyError(AssemblyError), + ExecutionError(ExecutionError), +} + +impl fmt::Display for ProgramError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + ProgramError::AssemblyError(e) => write!(f, "Assembly Error: {:?}", e), + ProgramError::ExecutionError(e) => write!(f, "Execution Error: {:?}", e), + } + } +} diff --git a/miden/src/repl/mod.rs b/miden/src/repl/mod.rs index 04ad00e9dc..ca28783691 100644 --- a/miden/src/repl/mod.rs +++ b/miden/src/repl/mod.rs @@ -1,7 +1,7 @@ -use air::StarkField; -use processor::{ExecutionError, Process}; +use super::ProgramError; +use processor::Process; use rustyline::{error::ReadlineError, Editor}; -use vm_core::{Felt, ProgramInputs, Word}; +use vm_core::{Felt, ProgramInputs, StarkField, Word}; /// This work is in continuation to the amazing work done by team `Scribe` /// [here](https://github.com/ControlCplusControlV/Scribe/blob/main/transpiler/src/repl.rs#L8) @@ -259,17 +259,17 @@ pub fn start_repl() { /// Compiles and executes a compiled Miden program, returning the stack, memory and any Miden errors. /// The program is passed in as a String, passed to the Miden Assembler, and then passed into the Miden /// Processor to be executed. -fn execute(program: String) -> Result<(Vec<(u64, Word)>, Vec), MidenError> { +fn execute(program: String) -> Result<(Vec<(u64, Word)>, Vec), ProgramError> { let program = assembly::Assembler::new() .compile(&program) - .map_err(MidenError::AssemblyError)?; + .map_err(ProgramError::AssemblyError)?; let pub_inputs = vec![]; let inputs = ProgramInputs::new(&pub_inputs, &[], vec![]).unwrap(); let mut process = Process::new_debug(program.kernel(), inputs); let _program_outputs = process .execute(&program) - .map_err(MidenError::ExecutionError); + .map_err(ProgramError::ExecutionError); let (sys, _, stack, _, chiplets) = process.to_components(); @@ -282,13 +282,6 @@ fn execute(program: String) -> Result<(Vec<(u64, Word)>, Vec), MidenError> Ok((mem, stack_state)) } -/// Errors that are returned from the Miden processor during execution. -#[derive(Debug)] -pub enum MidenError { - AssemblyError(assembly::ParsingError), - ExecutionError(ExecutionError), -} - /// Parses the address in integer form from "!mem[addr]" command, otherwise throws an error. fn read_mem_address(mem_str: &str) -> Result { // the first five characters is "!mem[" and the digit character should start from 6th diff --git a/miden/src/tools/mod.rs b/miden/src/tools/mod.rs index 391907e627..6119b984b2 100644 --- a/miden/src/tools/mod.rs +++ b/miden/src/tools/mod.rs @@ -1,8 +1,7 @@ -use super::cli::InputFile; -use assembly::ParsingError; +use super::{cli::InputFile, ProgramError}; use core::fmt; use miden::Assembler; -use processor::{AsmOpInfo, ExecutionError}; +use processor::AsmOpInfo; use std::path::PathBuf; use stdlib::StdLibrary; use structopt::StructOpt; @@ -163,25 +162,6 @@ pub fn analyze(program: &str, inputs: ProgramInputs) -> Result) -> fmt::Result { - match self { - ProgramError::AssemblyError(e) => write!(f, "Assembly Error: {:?}", e), - ProgramError::ExecutionError(e) => write!(f, "Execution Error: {:?}", e), - } - } -} - // ASMOP STATS // ================================================================================================