~jakintosh/co

78e77770caa010d5ac93290f68f2f47dfc16f7b4 — Jak Tiano 7 months ago ce12859
added better error output to context.rs, fixed some compiler warnings
3 files changed, 18 insertions(+), 28 deletions(-)

M Cargo.lock
M Cargo.toml
M src/assembler/context.rs
M Cargo.lock => Cargo.lock +1 -1
@@ 57,7 57,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"

[[package]]
name = "co"
version = "0.2.2"
version = "0.2.3"
dependencies = [
 "base64",
 "blake3",

M Cargo.toml => Cargo.toml +1 -1
@@ 1,6 1,6 @@
[package]
name = "co"
version = "0.2.2"
version = "0.2.3"
edition = "2021"

[profile.release]

M src/assembler/context.rs => src/assembler/context.rs +16 -26
@@ 111,7 111,7 @@ impl Context {
            Ok(())
        }
        for (name, path) in &self.imports {
            let hash = resolver.resolve(&path).ok_or(resolver_err(name, path))?;
            let hash = resolver.resolve(path).ok_or(resolver_err(name, path))?;
            let byteco = library.get_byteco(&hash).ok_or(library_err(path, &hash))?;
            resolve_inner(&byteco, path, library, &mut self.symbols)?;
            self.hashes.insert(name.clone(), hash);


@@ 168,7 168,7 @@ impl Context {
            None => match self.sources.get(&label) {
                Some(source) => self
                    .export_routine_symbol(label.clone(), source.clone(), render_stack)
                    .map_err(|e| format!("couldn't export '{label}': {e}")),
                    .map_err(|e| format!("{e}\nfailed to get routine hash for '{label}'")),
                None => Err(format!("couldn't resolve '{label}' to hash")),
            },
        }


@@ 195,7 195,7 @@ impl Context {
                Some(source) => {
                    let hash = self
                        .export_macro_symbol(label.clone(), source.clone(), render_stack)
                        .map_err(|e| format!("couldn't export macro '{label}: {e}"))?;
                        .map_err(|e| format!("{e}\ncouldn't export macro '{label}"))?;
                    Ok(hash)
                }
                None => Err(format!("couldn't resolve '{label}' to hash")),


@@ 237,9 237,9 @@ impl Context {
                Instruction::ParameterDef => return Err(scope_err(instruction, "assembly")),

                // ingest definitions
                Instruction::ImportDef => self.def_import(&stream)?,
                Instruction::RoutineDef => self.def_routine(slice, &stream)?,
                Instruction::MacroDef => self.def_macro(slice, &stream)?,
                Instruction::ImportDef => self.def_import(stream)?,
                Instruction::RoutineDef => self.def_routine(slice, stream)?,
                Instruction::MacroDef => self.def_macro(slice, stream)?,

                // accumulate
                _ => self.append_byteco(slice),


@@ 292,7 292,7 @@ impl Context {
    fn def_routine(&mut self, slice: &[u8], stream: &ByteCoStream) -> Result<(), String> {
        let name = slice_stream_from(slice)
            .read_string()
            .map_err(|e| format!("def routine: couldn't read name: {}", e))?;
            .map_err(|e| format!("{e}\ndef routine: couldn't read name"))?;

        let mut byteco = Vec::new();
        loop {


@@ 313,7 313,7 @@ impl Context {
    fn def_macro(&mut self, slice: &[u8], stream: &ByteCoStream) -> Result<(), String> {
        let name = slice_stream_from(slice)
            .read_string()
            .map_err(|e| format!("def macro: couldn't read name: {}", e))?;
            .map_err(|e| format!("{e}\ndef macro: couldn't read name"))?;

        let mut byteco = Vec::new();
        loop {


@@ 360,7 360,7 @@ impl Context {
        // wrap the conversion with render stack guards for infinite recursion
        render_stack.guard_routine(&name)?;
        let render = byteco::render_routine_symbol(self, &byteco, render_stack)
            .map_err(|e| format!("symbol export '{}' failure: {}", name, e))?;
            .map_err(|e| format!("{e}\nsymbol export '{name}' failure"))?;
        render_stack.pop();

        // hash, register, return


@@ 384,7 384,7 @@ impl Context {
        // wrap the conversion with render stack guards for infinite recursion
        render_stack.guard_macro(&name)?;
        let render = byteco::render_macro_symbol(self, &byteco, render_stack)
            .map_err(|e| format!("symbol export '{}' failure: {}", name, e))?;
            .map_err(|e| format!("{e}\nsymbol export '{name}' failure"))?;
        render_stack.pop();

        // hash, register, return


@@ 425,30 425,20 @@ fn slice_stream_from(slice: &[u8]) -> ByteCoStream {
}

fn scope_err(instruction: Instruction, block: &str) -> String {
    format!(
        "'{}' found in illegal block '{}'",
        instruction.to_string(),
        block
    )
    format!("'{instruction}' found in illegal block '{block}'",)
}
fn extract_err(context: &str, name: Option<&str>, err: String) -> String {
    match name {
        Some(name) => format!("failed to extract {} '{}': {}", context, name, err),
        None => format!("failed to extract {}: {}", context, err),
        Some(name) => format!("{err}\nfailed to extract {context} '{name}'"),
        None => format!("{err}\nfailed to extract {context}"),
    }
}
fn resolver_err(name: &str, path: &Path) -> String {
    format!(
        "no resolver for '{}' at '.{}'\n\ndo you need to `co resolve` this file?",
        name, path
    )
    format!("no resolver for '{name}' at '.{path}'\n\ndo you need to `co resolve` this file?")
}
fn library_err(path: &Path, hash: &Hash) -> String {
    format!(
        "no symbol for '.{}': {:#?}\n\ndo you need to `co import` a source file?",
        path, hash
    )
    format!("no symbol for '.{path}': {hash:#?}\n\ndo you need to `co import` a source file?",)
}
fn multi_def_error(name: &str) -> String {
    format!("multiple symbols defined for '{}'", name)
    format!("multiple symbols defined for '{name}'")
}