Skip to content

Commit

Permalink
[wip]: tyck
Browse files Browse the repository at this point in the history
  • Loading branch information
CohenArthur committed Apr 27, 2024
1 parent e654ffd commit 2db0ef3
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions typecheck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,38 @@ impl TypeSet {

/// This is the base structure that our typechecker - a type "interpreter" - will play with.
/// In `jinko`, the type of a variable is a set of elements of kind `type`. So this structure can
/// be thought of as a simple set of actual, monomorphized types.
// TODO: for now, let's not think about optimizations - let's box and clone and blurt bytes everywhere
/// be thought of as a simple set of actual, monomorphized types. There is one complication in that
/// the language recognizes a couple of magic types: `int`, `string` and `char` should be treated as
/// sets of all possible literals of that type. So we can imagine that `char` should actually be defined
/// as such:
///
/// ```rust,ignore
/// type char = '0' | '1' | '2' ... 'a' | 'b' | 'c' ... | <last_unicode_char_ever>;
/// ```
///
/// This is of course not a realistic definition to put in our standard library (and it gets worse for `string`)
/// so these types have to be handled separately.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Type(OriginIdx, TypeSet);
pub enum Type {
PrimitiveUnion(OriginIdx),
Set(OriginIdx, TypeSet),
}

impl Type {
pub fn builtin(set: HashSet<RefIdx>) -> Type {
Type(OriginIdx(u64::MAX), TypeSet(set))
Type::Set(OriginIdx(u64::MAX), TypeSet(set))
}

pub fn record(origin: OriginIdx) -> Type {
let mut set = HashSet::new();
// FIXME: Switch to keeping HashSet<OriginIdx> instead
set.insert(RefIdx::Resolved(origin));

Type(origin, TypeSet(set))
Type::Set(origin, TypeSet(set))
}

pub fn union(origin: OriginIdx, variants: impl Iterator<Item = RefIdx>) -> Type {
Type(origin, TypeSet(variants.collect()))
Type::Set(origin, TypeSet(variants.collect()))
}

pub fn set(&self) -> &TypeSet {
Expand Down

0 comments on commit 2db0ef3

Please sign in to comment.