Skip to content

Commit

Permalink
refactor: various mundane changes to tidy up
Browse files Browse the repository at this point in the history
Delete AstNodeId struct from ast.rs
Remove ast module from lib.rs
Rename cfg_reducer.rs to cfg_reducer.rs in montyc_driver/src/typeck/
Remove global_context module and rename it to session_context in montyc_driver/src/
Delete pretty_printer module from montyc_driver/src/
Rename host.rs to host.rs in montyc_driver/src/session_context/
Rename mod.rs to mod.rs in montyc_driver/src/session_context/
Rename query.rs to query.rs in montyc_driver/src/session_context/
Create session_request module in montyc_driver/src/ with SessionRequestBuilder and SessionRequest structs
Move block_cfg, typing_machine, and variable_flowgraph modules into typeck/mod.rs
  • Loading branch information
mental32 committed Oct 2, 2023
1 parent 956377e commit ea2110c
Show file tree
Hide file tree
Showing 15 changed files with 151 additions and 109 deletions.
4 changes: 0 additions & 4 deletions montyc_core/src/ast.rs

This file was deleted.

3 changes: 0 additions & 3 deletions montyc_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ pub(crate) mod macros {
}
}

pub mod ast;
pub mod codegen;
pub mod dict;
pub mod error;
Expand All @@ -52,6 +51,4 @@ pub type Qualname = Vec<String>; // TODO: turn this into some cheap u64 ID type.

pub type MapT<K, V> = ahash::AHashMap<K, V>;

pub type Rib = ahash::AHashMap<u32, TypeId>;

pub use {error::*, func::*, module::*, span::*, typing::*, value::*};
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use montyc_core::MontyResult;

use crate::global_context::SessionContext;
use crate::session_context::SessionContext;

pub trait GraphLike<IndexT> {
fn n_nodes(&self) -> usize;
Expand Down
2 changes: 1 addition & 1 deletion montyc_driver/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use montyc_hlirt::{
ObjectId, ObjectSpace, PyException, PyResult, PyResultExt,
};

use crate::global_context::SessionContext;
use crate::session_context::SessionContext;

// -- ModuleSpec

Expand Down
8 changes: 5 additions & 3 deletions montyc_driver/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#![warn(warnings)]

pub(crate) mod import;
pub(crate) mod pretty_printer;
pub(crate) mod typeck;
pub(crate) mod value_store;

mod global_context;
pub use global_context::{SessionContext, SessionMode, SessionOpts, UninitializedSession};
pub mod cfg_reducer;
pub mod session_request;

pub mod session_context;
pub use session_context::{SessionContext, SessionMode, SessionOpts, UninitializedSession};
46 changes: 0 additions & 46 deletions montyc_driver/src/pretty_printer.rs

This file was deleted.

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ impl SessionContext {
UninitializedSession(this)
}

/// Initialize the interpreter runtime, setting .
/// Initialize the interpreter runtime
fn initialize_hlirt(
&mut self,
_monty: &ModuleData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,6 @@ impl Queries for SessionContext {
Ok(modules.get(mref).unwrap().as_ref().clone())
}

fn get_qualname_of(
&self,
_func: TaggedValueId<{ montyc_core::FUNCTION }>,
) -> montyc_core::Qualname {
todo!()
}

fn get_value(&self, _value_id: ValueId) -> Option<Value> {
todo!()
}

fn get_rib_of<'a>(&'a self, _value_id: ValueId) -> Option<&'a montyc_core::Rib> {
todo!()
}

fn get_module_flatcode(&self, mref: ModuleRef) -> MontyResult<montyc_flatcode::FlatCode> {
fn ast_to_flatcode<T: AstObject>(mref: ModuleRef, ast: &T, span: Span) -> FlatCode {
let mut code = montyc_flatcode::FlatCode::new((mref.clone(), span.clone()));
Expand Down Expand Up @@ -263,10 +248,6 @@ impl Queries for SessionContext {
Ok(func)
}

fn spanref_to_value(&self, _sref: SpanRef) -> MontyResult<ValueId> {
todo!()
}

fn spanref_to_str(&self, sref: SpanRef) -> MontyResult<String> {
self.resolve_sref_as_str(sref)
.ok_or_else(|| MontyError::None)
Expand Down
126 changes: 126 additions & 0 deletions montyc_driver/src/session_request.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
//! A request is used to create a session, to parse, type-check, consteval and compile Python code.
//!
use std::path::PathBuf;

/// A builder for creating a `Request` instance with various options.
///
/// A `Request` is used to create a session, to parse, type-check, consteval and compile Python code.
///
/// # Examples
///
/// ```
/// let request = RequestBuilder::default()
/// .entry("__main__:main".to_string())
/// .libstd("libstd/".into())
/// .input("input.mt".into())
/// .output("output".into())
/// .build();
/// ```
#[derive(Debug, Default)]
pub struct SessionRequestBuilder {
entry: Option<String>,
libstd: Option<PathBuf>,
input: Option<PathBuf>,
output: Option<PathBuf>,
show_ir: Option<String>,
cc: Option<PathBuf>,
ld: Option<PathBuf>,
cranelift_settings: Option<Vec<String>>,
}

impl SessionRequestBuilder {
/// Sets the path to the entry function.
///
/// The default value is `__main__:main`.
pub fn entry(mut self, entry: &str) -> Self {
self.entry.replace(entry.to_owned());
self
}

/// Sets the path to a monty compatible stdlib.
///
/// The default value is `libstd/`.
pub fn libstd(mut self, libstd: PathBuf) -> Self {
self.libstd.replace(libstd);
self
}

/// Sets the input file to compile.
pub fn input(mut self, input: PathBuf) -> Self {
self.input.replace(input);
self
}

/// Sets the name of the output binary.
///
/// The default value is the input file's name.
pub fn output(mut self, output: PathBuf) -> Self {
self.output.replace(output);
self
}

/// Shows the Cranelift IR for the specified function.
pub fn show_ir(mut self, show_ir: Option<String>) -> Self {
self.show_ir = show_ir;
self
}

/// Sets the C compiler to use.
pub fn cc(mut self, cc: Option<PathBuf>) -> Self {
self.cc = cc;
self
}

/// Sets the linker to use.
pub fn ld(mut self, ld: Option<PathBuf>) -> Self {
self.ld = ld;
self
}

/// Sets low level codegen settings to pass to Cranelift.
pub fn cranelift_settings(mut self, cranelift_settings: Vec<String>) -> Self {
self.cranelift_settings = Some(cranelift_settings);
self
}

/// Builds a `Request` instance with the configured options.
pub fn build(self) -> SessionRequest {
SessionRequest {
entry: self.entry.unwrap_or("__main__:main".to_string()),
libstd: self.libstd.unwrap_or(PathBuf::from("libstd/")),
input: self.input.unwrap_or(PathBuf::from("main.py")),
output: self.output.unwrap_or(PathBuf::from("main")),
show_ir: self.show_ir,
cc: self.cc,
ld: self.ld,
cranelift_settings: self.cranelift_settings.unwrap_or_default(),
}
}
}

#[derive(Debug)]
pub struct SessionRequest {
/// The path to the entry function, for instance `main` in a `__main__.py` would be `__main__:main` (and is the default.)
pub entry: String,
/// The path to a monty compatible stdlib.
pub libstd: PathBuf,
/// The input file to compile.
pub input: PathBuf,
// The name of the output binary, defaults to the input file's name.
pub output: PathBuf,
/// Show the Cranelift IR for the specified function.
pub show_ir: Option<String>,
/// The C compiler to use.
pub cc: Option<PathBuf>,
/// The linker to use.
pub ld: Option<PathBuf>,
/// Low level codegen settings to pass to Cranelift.
pub cranelift_settings: Vec<String>,
}

impl SessionRequest {
pub fn new() -> SessionRequestBuilder {
SessionRequestBuilder::default()
}
}
5 changes: 2 additions & 3 deletions montyc_driver/src/typeck/block_cfg.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use crate::global_context::SessionContext;
use crate::session_context::SessionContext;

use super::cfg_reducer::GraphLike;
use super::variable_flowgraph::VariableFlowGraph;
use super::{FlatInst, NodeIndex, RawInst};

pub(crate) type BlockCFG = petgraph::graph::Graph<Vec<FlatInst>, BlockCFGEdge>;

impl GraphLike<NodeIndex> for BlockCFG {
impl crate::cfg_reducer::GraphLike<NodeIndex> for BlockCFG {
fn n_nodes(&self) -> usize {
self.raw_nodes().len()
}
Expand Down
7 changes: 3 additions & 4 deletions montyc_driver/src/typeck/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@ use montyc_parser::ast::{AstNode, Constant};
use montyc_query::Queries;
use petgraph::{data::DataMap, graph::NodeIndex, EdgeDirection};

use crate::global_context::SessionContext;
use crate::global_context::SessionMode;
use crate::cfg_reducer::CFGReducer;
use crate::session_context::SessionContext;
use crate::session_context::SessionMode;

mod block_cfg;
mod cfg_reducer;
mod typing_machine;
mod variable_flowgraph;

use block_cfg::BlockCFG;
use cfg_reducer::CFGReducer;
use typing_machine::TypingMachine;

/// run through a sequence of instructions and group linear sequences of instructions into "blocks"
Expand Down
6 changes: 3 additions & 3 deletions montyc_driver/src/typeck/typing_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ use montyc_query::Queries;
use petgraph::graph::NodeIndex;
use petgraph::visit::EdgeRef;

use crate::global_context::SessionContext;
use crate::session_context::SessionContext;

use super::block_cfg::BlockCFGBuilder;
use super::*;
use super::{block_cfg::BlockCFGBuilder, cfg_reducer::CFGReducer};

/// Akin to Pytype's VM this is a virtual machine for abstract interpretation of code.
#[derive(Debug)]
Expand Down Expand Up @@ -595,7 +595,7 @@ impl TypingMachine {
}
}

impl CFGReducer for TypingMachine {
impl crate::cfg_reducer::CFGReducer for TypingMachine {
type InputGraphT = BlockCFG;
type OutputT = montyc_core::codegen::CgBlockCFG<Constant, ()>;
type IndexT = NodeIndex;
Expand Down
10 changes: 6 additions & 4 deletions montyc_driver/src/typeck/variable_flowgraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ use montyc_core::MontyResult;
use montyc_flatcode::raw_inst::RawInst;
use petgraph::{graph::NodeIndex, visit::EdgeRef, EdgeDirection};

use crate::{global_context::SessionContext, typeck::block_cfg::BlockCFGBuilder};
use crate::{
cfg_reducer::CFGReducer, session_context::SessionContext, typeck::block_cfg::BlockCFGBuilder,
};

use super::{block_cfg::BlockCFG, cfg_reducer::CFGReducer};
use super::block_cfg::BlockCFG;

#[derive(Debug, Clone)]
pub enum DefPlace<I, N> {
Expand All @@ -25,7 +27,7 @@ struct VFGBuilder {
inp: BlockCFGBuilder,
}

impl CFGReducer for VFGBuilder {
impl crate::cfg_reducer::CFGReducer for VFGBuilder {
type IndexT = NodeIndex;

type InputGraphT = BlockCFG;
Expand All @@ -50,7 +52,7 @@ impl CFGReducer for VFGBuilder {

fn visit_block(
&mut self,
cx: &crate::global_context::SessionContext,
cx: &crate::session_context::SessionContext,
output: &mut Self::OutputT,
ix: Self::IndexT,
_errors: &mut Vec<montyc_core::error::TypeError>,
Expand Down
20 changes: 3 additions & 17 deletions montyc_query/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@

use std::{alloc::Layout, fmt};

use montyc_core::{
span::SpanRef,
value::{TaggedValueId, ValueId},
Function, ModuleData, ModuleRef, MontyError, Qualname, TypeId, TypingContext, Value, FUNCTION,
};
use montyc_core::span::SpanRef;
use montyc_core::value::{TaggedValueId, ValueId};
use montyc_core::{Function, ModuleData, ModuleRef, MontyError, TypeId, TypingContext, FUNCTION};
use montyc_flatcode::{FlatCode, FlatSeq};
use montyc_parser::ast::Constant;

Expand All @@ -34,15 +32,6 @@ pub trait Queries {
/// Given a `ModuleRef` try and get its associated module data.
fn get_module_data(&self, mref: ModuleRef) -> MontyResult<ModuleData>;

/// get the fully qualified name for this function.
fn get_qualname_of(&self, func: TaggedValueId<FUNCTION>) -> Qualname;

/// get the value from its value_id.
fn get_value(&self, value_id: ValueId) -> Option<Value>;

/// get the associated namespace rib of a value.
fn get_rib_of<'a>(&'a self, value_id: ValueId) -> Option<&'a montyc_core::Rib>;

/// Get specified modules flatcode.
fn get_module_flatcode(&self, mref: ModuleRef) -> MontyResult<FlatCode>;

Expand All @@ -58,9 +47,6 @@ pub trait Queries {
/// Get the associated function for this value.
fn get_function(&self, value_id: ValueId) -> MontyResult<Function>;

/// Get the computed value produced and assigned to a distinct ident spanref.
fn spanref_to_value(&self, sref: SpanRef) -> MontyResult<ValueId>;

/// Given a `SpanRef` try and resolve it to its corresponding string slice.
fn spanref_to_str(&self, sref: SpanRef) -> MontyResult<String>;

Expand Down

0 comments on commit ea2110c

Please sign in to comment.