Skip to content

Commit

Permalink
make progress on list deconstruction with IR
Browse files Browse the repository at this point in the history
  • Loading branch information
MicroProofs committed Nov 29, 2022
1 parent 1a068ec commit 4e00862
Show file tree
Hide file tree
Showing 7 changed files with 1,118 additions and 208 deletions.
10 changes: 8 additions & 2 deletions crates/lang/src/builtins.rs
Expand Up @@ -7,7 +7,7 @@ use uplc::builtins::DefaultFunction;
use crate::{
ast::{ModuleKind, Span},
tipo::{
fields::FieldMap, Type, TypeConstructor, TypeInfo, TypeVar, ValueConstructor,
self, fields::FieldMap, Type, TypeConstructor, TypeInfo, TypeVar, ValueConstructor,
ValueConstructorVariant,
},
IdGenerator,
Expand Down Expand Up @@ -336,7 +336,13 @@ pub fn from_default_function(

Some((tipo, 1))
}
DefaultFunction::IfThenElse => None,
DefaultFunction::IfThenElse => {
let ret = generic_var(id_gen.next());

let tipo = function(vec![bool(), ret.clone(), ret.clone()], ret);

Some((tipo, 3))
}
DefaultFunction::ChooseUnit => None,
DefaultFunction::Trace => {
let ret = generic_var(id_gen.next());
Expand Down
16 changes: 14 additions & 2 deletions crates/lang/src/ir.rs
@@ -1,6 +1,6 @@
use std::sync::Arc;

use vec1::Vec1;
use uplc::builtins::DefaultFunction;

use crate::{
ast::{
Expand Down Expand Up @@ -40,12 +40,19 @@ pub enum IR {
// },
List {
count: usize,
tipo: Arc<Type>,
tail: bool,
},

Tail {
count: usize,
},

ListAccessor {
names: Vec<String>,
tail: bool,
},

// func(x, other(y))
//[ Define(3) x definition *lam_body* -> [ (x [ (func [ func x [ (y [ other y ]) *definition* ] ]) *definition* ]) *definition* ], Define func -> [ (func [ func x [ (y [ other y ]) *definition* ] ]) *definition* ] , Call func -> [ func x [ (y [ other y ]) *definition* ] ], Var x -> x, Define y -> [ (y [ other y ]) *definition* ], Call other -> [ other y ], Var y -> y]

Expand All @@ -54,13 +61,18 @@ pub enum IR {
count: usize,
},

Builtin {
func: DefaultFunction,
},

BinOp {
name: BinOp,
count: usize,
tipo: Arc<Type>,
},

Assignment {
count: usize,
name: String,
kind: AssignmentKind,
},

Expand Down
46 changes: 45 additions & 1 deletion crates/lang/src/tipo.rs
@@ -1,6 +1,6 @@
use std::{cell::RefCell, collections::HashMap, ops::Deref, sync::Arc};

use uplc::builtins::DefaultFunction;
use uplc::{ast::Type as UplcType, builtins::DefaultFunction};

use crate::{
ast::{Constant, DefinitionLocation, ModuleKind, Span, TypedConstant},
Expand Down Expand Up @@ -127,6 +127,43 @@ impl Type {
}
}

pub fn is_list(&self) -> bool {
match self {
Self::App { module, name, .. } if "List" == name && module.is_empty() => true,
Self::Var { tipo } => tipo.borrow().is_list(),
_ => false,
}
}

pub fn get_uplc_type(&self) -> UplcType {
if self.is_int() {
UplcType::Integer
} else if self.is_bytearray() {
UplcType::ByteString
} else if self.is_string() {
UplcType::String
} else if self.is_bool() {
UplcType::Bool
} else if self.is_list() {
let args_type = match self {
Self::App {
module, name, args, ..
} if "List" == name && module.is_empty() => args[0].clone(),
Self::Var { tipo } => {
if let TypeVar::Link { tipo } = tipo.borrow().clone() {
tipo
} else {
todo!()
}
}
_ => todo!(),
};
UplcType::List(Box::new(args_type.get_uplc_type()))
} else {
UplcType::Data
}
}

/// Get the args for the type if the type is a specific `Type::App`.
/// Returns None if the type is not a `Type::App` or is an incorrect `Type:App`
///
Expand Down Expand Up @@ -290,6 +327,13 @@ impl TypeVar {
_ => false,
}
}

pub fn is_list(&self) -> bool {
match self {
Self::Link { tipo } => tipo.is_list(),
_ => false,
}
}
}

#[derive(Debug, Clone, PartialEq)]
Expand Down

0 comments on commit 4e00862

Please sign in to comment.