Skip to content

Commit

Permalink
Clean up (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
samestep authored Aug 29, 2023
1 parent 2c23c1d commit 478f903
Show file tree
Hide file tree
Showing 10 changed files with 281 additions and 352 deletions.
6 changes: 0 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 0 additions & 9 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,3 @@ include = ["src/lib.rs"]

[dependencies]
enumset = "1"
serde = { version = "1", features = ["derive"], optional = true }
thiserror = "1"

[dev-dependencies]
ts-rs = "6"

[features]
default = ["serde"]
serde = ["dep:serde", "enumset/serde"]
48 changes: 5 additions & 43 deletions crates/core/src/id.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,4 @@
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

// remember to `serde(rename)` everything here to avoid ts-rs name conflicts with non-ID types

#[cfg(test)]
use ts_rs::TS;

/// Index of a member in a tuple.
#[cfg_attr(test, derive(TS), ts(export))]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(rename = "MemberId")
)]
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Member(usize);

Expand All @@ -27,32 +13,20 @@ impl Member {
}

/// Index of an uninstantiated function reference in a definition context.
#[cfg_attr(test, derive(TS), ts(export))]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(rename = "FunctionId")
)]
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Function(usize);
pub struct Func(usize);

pub fn function(id: usize) -> Function {
Function(id)
pub fn func(id: usize) -> Func {
Func(id)
}

impl Function {
pub fn function(self) -> usize {
impl Func {
pub fn func(self) -> usize {
self.0
}
}

/// Index of a generic type parameter in a definition context.
#[cfg_attr(test, derive(TS), ts(export))]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(rename = "GenericId")
)]
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Generic(usize);

Expand All @@ -67,12 +41,6 @@ impl Generic {
}

/// Index of a type in a definition context.
#[cfg_attr(test, derive(TS), ts(export))]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(rename = "TyId")
)]
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Ty(usize);

Expand All @@ -87,12 +55,6 @@ impl Ty {
}

/// Index of a local variable in a function definition context.
#[cfg_attr(test, derive(TS), ts(export))]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(rename = "VarId")
)]
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Var(usize);

Expand Down
28 changes: 6 additions & 22 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,7 @@ pub mod id;

use enumset::{EnumSet, EnumSetType};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

#[cfg(test)]
use ts_rs::TS;

/// A type constraint.
#[cfg_attr(test, derive(TS), ts(export))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[allow(clippy::derived_hash_with_manual_eq)] // `PartialEq` impl comes from enumset; should be fine
#[derive(Debug, EnumSetType, Hash)]
pub enum Constraint {
Expand All @@ -25,8 +17,6 @@ pub enum Constraint {
}

/// A type.
#[cfg_attr(test, derive(TS), ts(export))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum Ty {
Unit,
Expand Down Expand Up @@ -55,13 +45,13 @@ pub enum Ty {
elem: id::Ty,
},
Tuple {
members: Vec<id::Ty>, // TODO: change to `Box<[id::Ty]`
members: Box<[id::Ty]>,
},
}

/// A function definition.
#[derive(Debug)]
pub struct Function {
pub struct Func {
/// Generic type parameters.
pub generics: Box<[EnumSet<Constraint>]>,
/// Types used in this function definition.
Expand All @@ -76,13 +66,13 @@ pub struct Function {
pub body: Box<[Instr]>,
}

/// Resolves `id::Function`s.
/// Resolves `id::Func`s.
pub trait Refs<'a> {
/// See `Node`.
type Opaque;

/// Resolve `id` to a function node.
fn get(&self, id: id::Function) -> Option<Node<'a, Self::Opaque, Self>>
fn get(&self, id: id::Func) -> Option<Node<'a, Self::Opaque, Self>>
where
Self: Sized;
}
Expand All @@ -95,7 +85,7 @@ pub enum Node<'a, O, T: Refs<'a, Opaque = O>> {
/// To traverse the graph by resolving functions called by this one.
refs: T,
/// The signature and definition of this function.
def: &'a Function,
def: &'a Func,
},
/// A function with an opaque body.
Opaque {
Expand All @@ -112,14 +102,12 @@ pub enum Node<'a, O, T: Refs<'a, Opaque = O>> {
},
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug)]
pub struct Instr {
pub var: id::Var,
pub expr: Expr,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug)]
pub enum Expr {
Unit,
Expand Down Expand Up @@ -177,7 +165,7 @@ pub enum Expr {
},

Call {
id: id::Function,
id: id::Func,
generics: Box<[id::Ty]>,
args: Box<[id::Var]>,
},
Expand Down Expand Up @@ -223,8 +211,6 @@ pub enum Expr {
},
}

#[cfg_attr(test, derive(TS), ts(export))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum Unop {
// `Bool` -> `Bool`
Expand All @@ -236,8 +222,6 @@ pub enum Unop {
Sqrt,
}

#[cfg_attr(test, derive(TS), ts(export))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum Binop {
// `Bool` -> `Bool` -> `Bool`
Expand Down
14 changes: 7 additions & 7 deletions crates/frontend/src/translate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,17 @@ pub struct Typedef<'input> {
#[derive(Debug)]
pub struct Module<'input> {
types: IndexMap<&'input str, Typedef<'input>>,
funcs: IndexMap<&'input str, rose::Function>,
funcs: IndexMap<&'input str, rose::Func>,
}

type Opaque = Infallible;

impl<'input, 'a> rose::Refs<'a> for &'a Module<'input> {
type Opaque = Opaque;

fn get(&self, id: id::Function) -> Option<ir::Node<'a, Opaque, Self>> {
fn get(&self, id: id::Func) -> Option<ir::Node<'a, Opaque, Self>> {
self.funcs
.get_index(id.function())
.get_index(id.func())
.map(|(_, def)| ir::Node::Transparent { refs: *self, def })
}
}
Expand All @@ -132,7 +132,7 @@ impl Module<'_> {

pub fn get_func(&self, name: &str) -> Option<ir::Node<Opaque, &Module>> {
let i = self.funcs.get_index_of(name)?;
ir::Refs::get(&self, id::function(i))
ir::Refs::get(&self, id::func(i))
}
}

Expand Down Expand Up @@ -183,7 +183,7 @@ impl<'input, 'a> BlockCtx<'input, 'a> {

fn unify(
&mut self,
_f: &ir::Function,
_f: &ir::Func,
_args: &[id::Ty],
) -> Result<(Vec<id::Ty>, id::Ty), TypeError> {
todo!()
Expand Down Expand Up @@ -271,7 +271,7 @@ impl<'input, 'a> BlockCtx<'input, 'a> {
Ok(self.instr(
ret,
ir::Expr::Call {
id: id::function(i),
id: id::func(i),
generics: generics.into(),
args: vars,
},
Expand Down Expand Up @@ -425,7 +425,7 @@ impl<'input> Module<'input> {
ctx.bind(bind, id::var(i));
}
let retvar = ctx.typecheck(body)?; // TODO: ensure this matches `ret`
let f = ir::Function {
let f = ir::Func {
generics,
types: ctx.t.into_iter().collect(),
vars: ctx.v.into(),
Expand Down
Loading

0 comments on commit 478f903

Please sign in to comment.