almost done data constr creation

This commit is contained in:
Kasey White 2022-11-10 02:37:34 -05:00 committed by Kasey White
parent 4a95fc5588
commit a3935c5df7
2 changed files with 102 additions and 22 deletions

View File

@ -214,6 +214,7 @@ impl<'a> CodeGenerator<'a> {
} }
} }
(ValueConstructorVariant::Record { .. }, Type::App { .. }) => {} (ValueConstructorVariant::Record { .. }, Type::App { .. }) => {}
(ValueConstructorVariant::Record { .. }, Type::Fn { .. }) => {}
_ => todo!(), _ => todo!(),
}, },
TypedExpr::Fn { .. } => todo!(), TypedExpr::Fn { .. } => todo!(),
@ -508,7 +509,7 @@ impl<'a> CodeGenerator<'a> {
if name == "True" || name == "False" { if name == "True" || name == "False" {
Term::Constant(Constant::Bool(name == "True")) Term::Constant(Constant::Bool(name == "True"))
} else { } else {
match constructor.variant.clone() { match dbg!(constructor.variant.clone()) {
ValueConstructorVariant::LocalVariable { .. } => Term::Var(Name { ValueConstructorVariant::LocalVariable { .. } => Term::Var(Name {
text: name.to_string(), text: name.to_string(),
unique: 0.into(), unique: 0.into(),
@ -521,20 +522,93 @@ impl<'a> CodeGenerator<'a> {
} }
TypedExpr::Fn { .. } => todo!(), TypedExpr::Fn { .. } => todo!(),
TypedExpr::List { .. } => todo!(), TypedExpr::List { .. } => todo!(),
TypedExpr::Call { fun, args, .. } => { TypedExpr::Call {
let mut term = fun, args, tipo, ..
self.recurse_code_gen(fun, scope_level.scope_increment(args.len() as i32 + 1)); } => {
if let (
Type::App { module, name, .. },
TypedExpr::Var {
name: constr_name, ..
},
) = (&**tipo, &**fun)
{
let mut term: Term<Name> =
Term::Constant(Constant::ProtoList(uplc::ast::Type::Data, vec![]));
for (i, arg) in args.iter().enumerate() { let data_type = self
term = Term::Apply { .data_types
function: term.into(), .get(&(module.to_string(), name.to_string()))
argument: self .unwrap();
.recurse_code_gen(&arg.value, scope_level.scope_increment(i as i32 + 1))
println!("DATATYPES ARE {data_type:#?}");
let constr = data_type
.constructors
.iter()
.find(|x| x.name == constr_name.to_string())
.unwrap();
let arg_to_data: Vec<(bool, Term<Name>)> = constr
.arguments
.iter()
.map(|x| {
if let Type::App { name, .. } = &*x.tipo {
if name == "ByteArray" {
(true, Term::Builtin(DefaultFunction::BData))
} else if name == "Int" {
(true, Term::Builtin(DefaultFunction::IData))
} else {
(false, Term::Constant(Constant::Unit))
}
} else {
unreachable!()
}
})
.collect();
for (i, arg) in args.iter().enumerate().rev() {
let arg_term = self.recurse_code_gen(
&arg.value,
scope_level.scope_increment(i as i32 + 1),
);
term = Term::Apply {
function: Term::Apply {
function: Term::Force(
Term::Builtin(DefaultFunction::MkCons).into(),
)
.into(),
argument: if arg_to_data[i].0 {
Term::Apply {
function: arg_to_data[i].1.clone().into(),
argument: arg_term.into(),
}
.into()
} else {
arg_term.into()
},
}
.into(), .into(),
}; argument: term.into(),
} };
}
term
} else {
let mut term = self
.recurse_code_gen(fun, scope_level.scope_increment(args.len() as i32 + 1));
term for (i, arg) in args.iter().enumerate() {
term = Term::Apply {
function: term.into(),
argument: self
.recurse_code_gen(
&arg.value,
scope_level.scope_increment(i as i32 + 1),
)
.into(),
};
}
term
}
} }
TypedExpr::BinOp { TypedExpr::BinOp {
name, left, right, .. name, left, right, ..

View File

@ -3,8 +3,17 @@ use sample/mint
use sample/spend use sample/spend
pub type Redeemer { pub type Redeemer {
Buy { id: Int } signer: ByteArray,
Sell(Int) amount: Int
}
pub type Reen {
Buy{
signer: ByteArray,
amount: Int
}
Sell
} }
pub fn spend( pub fn spend(
@ -12,12 +21,9 @@ pub fn spend(
rdmr: Redeemer, rdmr: Redeemer,
ctx: spend.ScriptContext, ctx: spend.ScriptContext,
) -> Bool { ) -> Bool {
let y = 2 let x = rdmr.amount
let x = datum.sc.signer let z = Buy(rdmr.signer, 55)
let a = datum.sc.signer.hash
let b = datum.rdmr
when b is { True
sample.Buy -> 1 == 1
sample.Sell -> 5 == 1
}
} }