Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit

Permalink
lints + format
Browse files Browse the repository at this point in the history
  • Loading branch information
doug-q committed Jun 11, 2024
1 parent fc5ed39 commit f450458
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 25 deletions.
30 changes: 22 additions & 8 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
use std::rc::Rc;

use clap::Parser;
use hugr::{HugrView, Hugr};
use inkwell::module::Module;
use thiserror::Error;
use hugr::std_extensions::arithmetic::{
conversions::EXTENSION as CONVERSIONS_EXTENSION, float_ops::EXTENSION as FLOAT_OPS_EXTENSION,
float_types::EXTENSION as FLOAT_TYPES_EXTENSION, int_ops::EXTENSION as INT_OPS_EXTENSION,
int_types::EXTENSION as INT_TYPES_EXTENSION,
};
use hugr::std_extensions::logic::EXTENSION as LOGICS_EXTENSION;
use hugr::Hugr;
use inkwell::module::Module;
use thiserror::Error;

use anyhow::Result;
use hugr::extension::{ExtensionRegistry, PRELUDE};
use anyhow::{anyhow,Result};
use lazy_static::lazy_static;

use crate::custom::CodegenExtsMap;
Expand Down Expand Up @@ -54,7 +54,13 @@ pub enum CliError {
Other(#[from] anyhow::Error),
}

pub fn emit_module<'c>(context: &'c inkwell::context::Context, hugr: &'c Hugr, module_name: impl AsRef<str>, namer: Rc<Namer>, exts: Rc<CodegenExtsMap<'c, Hugr>>) -> Result<Module<'c>> {
pub fn emit_module<'c>(
context: &'c inkwell::context::Context,
hugr: &'c Hugr,
module_name: impl AsRef<str>,
namer: Rc<Namer>,
exts: Rc<CodegenExtsMap<'c, Hugr>>,
) -> Result<Module<'c>> {
let module = context.create_module(module_name.as_ref());
let emit = EmitHugr::new(context, module, namer, exts);
Ok(emit.emit_module(hugr.fat_root().unwrap())?.finish())
Expand All @@ -67,8 +73,16 @@ impl CmdLineArgs {
let hugr = self.base.run(registry)?;

let context = inkwell::context::Context::create();
let module = emit_module(&context, &hugr, &self.module_name, self.namer(), self.codegenexts())?;
module.verify().map_err(|e| anyhow::anyhow!(e.to_string()))?;
let module = emit_module(
&context,
&hugr,
&self.module_name,
self.namer(),
self.codegenexts(),
)?;
module
.verify()
.map_err(|e| anyhow::anyhow!(e.to_string()))?;
println!("{}", module.print_to_string());
Ok(())
}
Expand All @@ -82,7 +96,7 @@ impl CmdLineArgs {
Namer::new(self.mangle_prefix.clone(), self.mangle_node_suffix).into()
}

fn codegenexts<'c>(&self) -> Rc<CodegenExtsMap<'c,Hugr>> {
fn codegenexts<'c>(&self) -> Rc<CodegenExtsMap<'c, Hugr>> {
CodegenExtsMap::new().add_int_extensions().into()
}
}
12 changes: 8 additions & 4 deletions src/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl<OT, H: HugrView> EmitOp<'_, OT, H> for NullEmitLlvm {
#[derive(Clone)]
pub struct Namer {
prefix: String,
node_suffix: bool
node_suffix: bool,
}

impl Namer {
Expand All @@ -127,13 +127,17 @@ impl Namer {
pub fn new(prefix: impl Into<String>, node_suffix: bool) -> Self {
Self {
prefix: prefix.into(),
node_suffix
node_suffix,
}
}

/// Mangle the the name of a [FuncDefn] or [FuncDecl].
pub fn name_func(&self, name: impl AsRef<str>, node: Node) -> String {
let suffix = if self.node_suffix { format!(".{}", node.index()) } else { "".to_string() };
let suffix = if self.node_suffix {
format!(".{}", node.index())
} else {
"".to_string()
};
format!("{}{}{}", &self.prefix, name.as_ref(), suffix)
}
}
Expand All @@ -144,7 +148,7 @@ impl Default for Namer {
fn default() -> Self {
Self {
prefix: NAMER_DEFAULT_PREFIX.into(),
node_suffix: true
node_suffix: true,
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/emit/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use std::{

use anyhow::{anyhow, Result};
use hugr::{
ops::{FuncDecl, FuncDefn}, types::Type, HugrView, NodeIndex, OutgoingPort, PortIndex, Wire
ops::{FuncDecl, FuncDefn},
types::Type,
HugrView, NodeIndex, OutgoingPort, PortIndex, Wire,
};
use inkwell::{
basic_block::BasicBlock,
Expand Down
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@
//! [BasicValueEnum]: [inkwell::values::BasicValueEnum]
//! [BasicValue]: [inkwell::values::BasicValue]
//!
pub mod cli;
pub mod custom;
pub mod emit;
pub mod fat;
pub mod types;
pub mod cli;

#[allow(unreachable_code)]
pub fn llvm_version() -> &'static str {
Expand All @@ -74,6 +74,5 @@ pub fn llvm_version() -> &'static str {
panic!("No recognised llvm feature")
}


#[cfg(test)]
pub mod test;
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use hugr::std_extensions::logic::EXTENSION as LOGICS_EXTENSION;

use hugr::extension::{ExtensionRegistry, PRELUDE};

use hugr_cli::{Parser, Level, CmdLineArgs};
use hugr_cli::{CmdLineArgs, Level, Parser};

fn main() {
let opts = CmdLineArgs::parse();
Expand Down
17 changes: 8 additions & 9 deletions tests/guppy.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{env, path::{Path, PathBuf}};
use std::{env, path::PathBuf};

use rstest::{fixture, rstest};

Expand All @@ -9,14 +9,15 @@ struct TestConfig {

impl TestConfig {
pub fn new() -> TestConfig {
let python_bin = env::var("HUGR_LLVM_PYTHON_BIN").map(Into::into).ok().or_else( ||
pathsearch::find_executable_in_path("python")
).unwrap_or_else(|| panic!("Could not find python in PATH or HUGR_LLVM_PYTHON_BIN")
);
let python_bin = env::var("HUGR_LLVM_PYTHON_BIN")
.map(Into::into)
.ok()
.or_else(|| pathsearch::find_executable_in_path("python"))
.unwrap_or_else(|| panic!("Could not find python in PATH or HUGR_LLVM_PYTHON_BIN"));
let hugr_llvm_bin = env!("CARGO_BIN_EXE_hugr-llvm").into();
TestConfig {
python_bin,
hugr_llvm_bin
hugr_llvm_bin,
}
}
}
Expand All @@ -27,6 +28,4 @@ fn test_config() -> TestConfig {
}

#[rstest]
fn test_even_odd(test_config: TestConfig) {

}
fn test_even_odd(test_config: TestConfig) {}

0 comments on commit f450458

Please sign in to comment.