From 32d34d5fd36fd648a69aeb4b188bcf0a4f6cd913 Mon Sep 17 00:00:00 2001 From: Kasey White Date: Tue, 21 Mar 2023 16:02:52 -0400 Subject: [PATCH] create struct Air Env refactor out some to_strings --- crates/aiken-lang/src/air.rs | 103 ++++++++++-------- crates/aiken-lang/src/builder.rs | 20 ++-- crates/aiken-lang/src/uplc.rs | 57 ++++------ examples/acceptance_tests/048/lib/tests.ak | 33 ++++++ examples/acceptance_tests/048/plutus.json | 32 ------ .../acceptance_tests/048/validators/foo.ak | 12 -- 6 files changed, 118 insertions(+), 139 deletions(-) delete mode 100644 examples/acceptance_tests/048/plutus.json delete mode 100644 examples/acceptance_tests/048/validators/foo.ak diff --git a/crates/aiken-lang/src/air.rs b/crates/aiken-lang/src/air.rs index ca5c1519..a641e101 100644 --- a/crates/aiken-lang/src/air.rs +++ b/crates/aiken-lang/src/air.rs @@ -1,13 +1,11 @@ -use std::sync::Arc; - -use indexmap::IndexSet; -use uplc::builtins::DefaultFunction; - use crate::{ ast::{BinOp, UnOp}, tipo::{Type, ValueConstructor}, + IdGenerator, }; - +use indexmap::IndexSet; +use std::sync::Arc; +use uplc::builtins::DefaultFunction; #[derive(Debug, Clone)] pub enum Air { // Primitives @@ -15,53 +13,44 @@ pub enum Air { scope: Vec, value: String, }, - String { scope: Vec, value: String, }, - ByteArray { scope: Vec, bytes: Vec, }, - Bool { scope: Vec, value: bool, }, - List { scope: Vec, count: usize, tipo: Arc, tail: bool, }, - Tuple { scope: Vec, tipo: Arc, count: usize, }, - Void { scope: Vec, }, - Var { scope: Vec, constructor: ValueConstructor, name: String, variant_name: String, }, - // Functions Call { scope: Vec, count: usize, tipo: Arc, }, - DefineFunc { scope: Vec, func_name: String, @@ -70,19 +59,16 @@ pub enum Air { recursive: bool, variant_name: String, }, - Fn { scope: Vec, params: Vec, }, - Builtin { scope: Vec, count: usize, func: DefaultFunction, tipo: Arc, }, - // Operators BinOp { scope: Vec, @@ -90,52 +76,43 @@ pub enum Air { count: usize, tipo: Arc, }, - UnOp { scope: Vec, op: UnOp, }, - // Assignment Let { scope: Vec, name: String, }, - UnWrapData { scope: Vec, tipo: Arc, }, - WrapData { scope: Vec, tipo: Arc, }, - AssertConstr { scope: Vec, constr_index: usize, }, - AssertBool { scope: Vec, is_true: bool, }, - // When When { scope: Vec, tipo: Arc, subject_name: String, }, - Clause { scope: Vec, tipo: Arc, subject_name: String, complex_clause: bool, }, - ListClause { scope: Vec, tipo: Arc, @@ -143,11 +120,9 @@ pub enum Air { next_tail_name: Option, complex_clause: bool, }, - WrapClause { scope: Vec, }, - TupleClause { scope: Vec, tipo: Arc, @@ -157,13 +132,11 @@ pub enum Air { count: usize, complex_clause: bool, }, - ClauseGuard { scope: Vec, subject_name: String, tipo: Arc, }, - ListClauseGuard { scope: Vec, tipo: Arc, @@ -171,17 +144,14 @@ pub enum Air { next_tail_name: Option, inverse: bool, }, - Finally { scope: Vec, }, - // If If { scope: Vec, tipo: Arc, }, - // Record Creation Record { scope: Vec, @@ -189,27 +159,23 @@ pub enum Air { tipo: Arc, count: usize, }, - RecordUpdate { scope: Vec, highest_index: usize, indices: Vec<(usize, Arc)>, tipo: Arc, }, - // Field Access RecordAccess { scope: Vec, record_index: u64, tipo: Arc, }, - FieldsExpose { scope: Vec, indices: Vec<(usize, String, Arc)>, check_last_item: bool, }, - // ListAccess ListAccessor { scope: Vec, @@ -218,14 +184,12 @@ pub enum Air { tail: bool, check_last_item: bool, }, - ListExpose { scope: Vec, tipo: Arc, tail_head_names: Vec<(String, String)>, tail: Option<(String, String)>, }, - // Tuple Access TupleAccessor { scope: Vec, @@ -233,19 +197,16 @@ pub enum Air { tipo: Arc, check_last_item: bool, }, - TupleIndex { scope: Vec, tipo: Arc, tuple_index: usize, }, - // Misc. ErrorTerm { scope: Vec, tipo: Arc, }, - Trace { scope: Vec, tipo: Arc, @@ -295,7 +256,6 @@ impl Air { | Air::Trace { scope, .. } => scope.clone(), } } - pub fn scope_mut(&mut self) -> &mut Vec { match self { Air::Int { scope, .. } @@ -338,7 +298,6 @@ impl Air { | Air::Trace { scope, .. } => scope, } } - pub fn tipo(&self) -> Option> { match self { Air::Int { .. } => Some( @@ -418,7 +377,6 @@ impl Air { | Air::TupleIndex { tipo, .. } | Air::ErrorTerm { tipo, .. } | Air::Trace { tipo, .. } => Some(tipo.clone()), - Air::DefineFunc { .. } | Air::Fn { .. } | Air::Let { .. } @@ -427,7 +385,6 @@ impl Air { | Air::AssertBool { .. } | Air::Finally { .. } | Air::FieldsExpose { .. } => None, - Air::UnOp { op, .. } => match op { UnOp::Not => Some( Type::App { @@ -451,3 +408,55 @@ impl Air { } } } + +pub struct AirEnv<'a> { + pub id_gen: &'a mut IdGenerator, + pub scope: Vec, + pub air: Vec, +} + +impl<'a> AirEnv<'a> { + pub fn new(id_gen: &'a mut IdGenerator) -> Self { + AirEnv { + id_gen, + scope: vec![0], + air: vec![], + } + } + + pub fn new_with_scope(id_gen: &'a mut IdGenerator, scope: Vec) -> Self { + AirEnv { + id_gen, + scope, + air: vec![], + } + } + + pub fn int(mut self, value: String) -> Self { + self.air.push(Air::Int { + scope: self.scope.clone(), + value, + }); + self + } + + pub fn string(mut self, value: String) -> Self { + self.air.push(Air::String { + scope: self.scope.clone(), + value, + }); + self + } + + pub fn byte_array(mut self, bytes: Vec) -> Self { + self.air.push(Air::ByteArray { + scope: self.scope.clone(), + bytes, + }); + self + } + + // pub fn sequence(mut self, expressions: AirEnv) -> Self{ + + // } +} diff --git a/crates/aiken-lang/src/builder.rs b/crates/aiken-lang/src/builder.rs index 39d7d263..da75e9b4 100644 --- a/crates/aiken-lang/src/builder.rs +++ b/crates/aiken-lang/src/builder.rs @@ -208,14 +208,14 @@ pub fn convert_type_to_data(term: Term, field_type: &Arc) -> Term, field_type: &Arc) -> Term list_access_inner, - _ => list_access_inner.lambda("_".to_string()), + _ => list_access_inner.lambda("_"), } } else { let mut list_access_inner = list_access_to_uplc( @@ -978,14 +978,14 @@ pub fn wrap_validator_args(term: Term, arguments: &[TypedArg]) -> Term CodeGenerator<'a> { term = Term::equals_integer() .apply(Term::integer(constr_index.into())) - .apply(Term::var(CONSTR_INDEX_EXPOSER.to_string()).apply(constr)) + .apply(Term::var(CONSTR_INDEX_EXPOSER).apply(constr)) .delayed_if_else(term, error_term); arg_stack.push(term); @@ -4675,7 +4675,7 @@ impl<'a> CodeGenerator<'a> { if tipo.is_bool() { let other_clauses = if complex_clause { - Term::var("__other_clauses_delayed".to_string()) + Term::var("__other_clauses_delayed") } else { term.clone() }; @@ -4692,9 +4692,7 @@ impl<'a> CodeGenerator<'a> { } if complex_clause { - term = body - .lambda("__other_clauses_delayed".to_string()) - .apply(term.delay()); + term = body.lambda("__other_clauses_delayed").apply(term.delay()); } } else { let condition = if tipo.is_int() { @@ -4719,12 +4717,9 @@ impl<'a> CodeGenerator<'a> { if complex_clause { term = condition - .if_else( - body.delay(), - Term::var("__other_clauses_delayed".to_string()), - ) + .if_else(body.delay(), Term::var("__other_clauses_delayed")) .force() - .lambda("__other_clauses_delayed".to_string()) + .lambda("__other_clauses_delayed") .apply(term.delay()); } else { term = condition.delayed_if_else(body, term); @@ -4754,12 +4749,9 @@ impl<'a> CodeGenerator<'a> { if complex_clause { term = Term::var(tail_name) - .choose_list( - body.delay(), - Term::var("__other_clauses_delayed".to_string()), - ) + .choose_list(body.delay(), Term::var("__other_clauses_delayed")) .force() - .lambda("__other_clauses_delayed".to_string()) + .lambda("__other_clauses_delayed") .apply(arg.delay()); } else { term = Term::var(tail_name).delayed_choose_list(body, arg); @@ -4773,9 +4765,7 @@ impl<'a> CodeGenerator<'a> { let mut term = arg_stack.pop().unwrap(); let arg = arg_stack.pop().unwrap(); - term = term - .lambda("__other_clauses_delayed".to_string()) - .apply(arg.delay()); + term = term.lambda("__other_clauses_delayed").apply(arg.delay()); arg_stack.push(term); } @@ -4787,7 +4777,7 @@ impl<'a> CodeGenerator<'a> { let then = arg_stack.pop().unwrap(); if tipo.is_bool() { - let mut term = Term::var("__other_clauses_delayed".to_string()); + let mut term = Term::var("__other_clauses_delayed"); if matches!(checker, Term::Constant(boolean) if matches!(boolean.as_ref(), UplcConstant::Bool(true))) { term = Term::var(subject_name).if_else(then.delay(), term).force(); @@ -4817,10 +4807,7 @@ impl<'a> CodeGenerator<'a> { }; let term = condition - .if_else( - then.delay(), - Term::var("__other_clauses_delayed".to_string()), - ) + .if_else(then.delay(), Term::var("__other_clauses_delayed")) .force(); arg_stack.push(term); } @@ -4847,17 +4834,11 @@ impl<'a> CodeGenerator<'a> { if !inverse { term = Term::var(tail_name) - .choose_list( - term.delay(), - Term::var("__other_clauses_delayed".to_string()), - ) + .choose_list(term.delay(), Term::var("__other_clauses_delayed")) .force(); } else { term = Term::var(tail_name) - .choose_list( - Term::var("__other_clauses_delayed".to_string()), - term.delay(), - ) + .choose_list(Term::var("__other_clauses_delayed"), term.delay()) .force(); } @@ -4925,8 +4906,8 @@ impl<'a> CodeGenerator<'a> { self.needs_field_access = true; let constr = arg_stack.pop().unwrap(); - let mut term = Term::var(CONSTR_GET_FIELD.to_string()) - .apply(Term::var(CONSTR_FIELDS_EXPOSER.to_string()).apply(constr)) + let mut term = Term::var(CONSTR_GET_FIELD) + .apply(Term::var(CONSTR_FIELDS_EXPOSER).apply(constr)) .apply(Term::integer(record_index.into())); term = convert_data_to_type(term, &tipo); @@ -4970,7 +4951,7 @@ impl<'a> CodeGenerator<'a> { term }; - term = term.apply(Term::var(CONSTR_FIELDS_EXPOSER.to_string()).apply(value)); + term = term.apply(Term::var(CONSTR_FIELDS_EXPOSER).apply(value)); arg_stack.push(term); } @@ -5032,7 +5013,7 @@ impl<'a> CodeGenerator<'a> { .. } => { self.needs_field_access = true; - let tail_name_prefix = "__tail_index".to_string(); + let tail_name_prefix = "__tail_index"; let record = arg_stack.pop().unwrap(); @@ -5108,7 +5089,7 @@ impl<'a> CodeGenerator<'a> { term = term .lambda(prev_tail_name) - .apply(Term::var(CONSTR_FIELDS_EXPOSER.to_string()).apply(record)); + .apply(Term::var(CONSTR_FIELDS_EXPOSER).apply(record)); arg_stack.push(term); } @@ -5144,7 +5125,7 @@ impl<'a> CodeGenerator<'a> { } else { self.needs_field_access = true; term = convert_data_to_type( - Term::var(CONSTR_GET_FIELD.to_string()) + Term::var(CONSTR_GET_FIELD) .apply(term) .apply(Term::integer(tuple_index.into())), &tipo.get_inner_types()[tuple_index], @@ -5226,7 +5207,7 @@ impl<'a> CodeGenerator<'a> { let next_clause = arg_stack.pop().unwrap(); term = term - .lambda("__other_clauses_delayed".to_string()) + .lambda("__other_clauses_delayed") .apply(next_clause.delay()); } diff --git a/examples/acceptance_tests/048/lib/tests.ak b/examples/acceptance_tests/048/lib/tests.ak index 73450339..2f441bb3 100644 --- a/examples/acceptance_tests/048/lib/tests.ak +++ b/examples/acceptance_tests/048/lib/tests.ak @@ -45,3 +45,36 @@ test foo_4() { False } } + +// type Seasons { +// Winter +// Spring +// Summer +// Fall +// } + +// fn is_cold(season, hour) { +// when season is { +// Winter | Fall -> +// True +// _ if hour >= 18 -> +// True +// _ -> +// False +// } +// } + +// test foo_5() { +// !is_cold(Spring, 15) && is_cold(Summer, 22) +// } + +fn when_tuple(a: (Int, Int)) -> Int { + when a is { + (a, _b) -> + a + } +} + +test spend() { + when_tuple((4, 1)) == 4 +} diff --git a/examples/acceptance_tests/048/plutus.json b/examples/acceptance_tests/048/plutus.json deleted file mode 100644 index afd7d127..00000000 --- a/examples/acceptance_tests/048/plutus.json +++ /dev/null @@ -1,32 +0,0 @@ -{ - "preamble": { - "title": "aiken-lang/acceptance_test_048", - "version": "0.0.0", - "plutusVersion": "v2" - }, - "validators": [ - { - "title": "foo.spend", - "datum": { - "title": "a", - "schema": { - "$ref": "#/definitions/Data" - } - }, - "redeemer": { - "title": "b", - "schema": { - "$ref": "#/definitions/Data" - } - }, - "compiledCode": "586001000032323232323232323222253330063370e6464640046eb4c02c008dd69804800a5ef6c6010104000101010048020526163001001222533300800214984cc014c004c024008ccc00c00cc0280080055cd2b9b5573aaae7955cfaba157441", - "hash": "7ecbfc3ae91c4d5ba3799b4d283e385d457c860cd22034d825379ae2" - } - ], - "definitions": { - "Data": { - "title": "Data", - "description": "Any Plutus data." - } - } -} \ No newline at end of file diff --git a/examples/acceptance_tests/048/validators/foo.ak b/examples/acceptance_tests/048/validators/foo.ak deleted file mode 100644 index e41c9c8b..00000000 --- a/examples/acceptance_tests/048/validators/foo.ak +++ /dev/null @@ -1,12 +0,0 @@ -fn when_tuple(a: (Int, Int)) -> Int { - when a is { - (a, b) -> - a - } -} - -validator { - fn spend(a: Data, b: Data, c) -> Bool { - when_tuple((4, 1)) == 4 - } -}