feat: start using AirStack methods

Co-authored-by: Kasey White <kwhitemsg@gmail.com>
This commit is contained in:
rvcas
2023-03-21 23:17:51 -04:00
committed by Lucas
parent 3e6f688e2d
commit ca0d896b8d
4 changed files with 313 additions and 326 deletions

View File

@@ -1,4 +1,11 @@
use crate::IdGenerator;
use std::sync::Arc;
use uplc::builtins::DefaultFunction;
use crate::{
tipo::{Type, ValueConstructor},
IdGenerator,
};
use super::air::Air;
@@ -12,7 +19,7 @@ impl<'a> AirStack<'a> {
pub fn new(id_gen: &'a mut IdGenerator) -> Self {
AirStack {
id_gen,
scope: vec![0],
scope: vec![id_gen.next()],
air: vec![],
}
}
@@ -25,44 +32,158 @@ impl<'a> AirStack<'a> {
}
}
pub fn merge(mut self, other: AirStack) -> Self {
self.air.extend(other.air.into_iter());
pub fn in_new_scope(&mut self) -> Self {
let mut new_stack = AirStack::with_scope(&mut self.id_gen, self.scope.clone());
self
new_stack.new_scope();
new_stack
}
pub fn int(self, value: String) -> Self {
let mut air = self.air.clone();
pub fn new_scope(&mut self) {
self.scope.push(self.id_gen.next());
}
air.push(Air::Int {
scope: self.scope.clone(),
value,
});
pub fn merge(&mut self, mut other: AirStack) {
self.air.append(&mut other.air);
}
AirStack {
id_gen: self.id_gen,
scope: self.scope,
air,
pub fn sequence(&mut self, stacks: Vec<AirStack>) {
for stack in stacks {
self.merge(stack)
}
}
pub fn string(mut self, value: String) -> Self {
pub fn integer(&mut self, value: String) {
self.air.push(Air::Int {
scope: self.scope.clone(),
value,
});
}
pub fn string(&mut self, value: String) {
self.air.push(Air::String {
scope: self.scope.clone(),
value,
});
self
}
pub fn byte_array(mut self, bytes: Vec<u8>) -> Self {
pub fn byte_array(&mut self, bytes: Vec<u8>) {
self.air.push(Air::ByteArray {
scope: self.scope.clone(),
bytes,
});
self
}
// pub fn sequence(mut self, expressions: AirEnv) -> Self {}
pub fn builtin(&mut self, func: DefaultFunction, tipo: Arc<Type>, args: Vec<AirStack>) {
self.air.push(Air::Builtin {
scope: self.scope.clone(),
count: args.len(),
func,
tipo,
});
self.sequence(args);
}
pub fn var(
&mut self,
constructor: ValueConstructor,
name: impl ToString,
variant_name: impl ToString,
) {
self.air.push(Air::Var {
scope: self.scope.clone(),
constructor,
name: name.to_string(),
variant_name: variant_name.to_string(),
});
}
pub fn anonymous_function(&mut self, params: Vec<String>, body: AirStack) {
self.air.push(Air::Fn {
scope: self.scope.clone(),
params,
});
self.merge(body);
}
pub fn list(&mut self, tipo: Arc<Type>, elements: Vec<AirStack>, tail: Option<AirStack>) {
self.air.push(Air::List {
scope: self.scope.clone(),
count: elements.len(),
tipo,
tail: tail.is_some(),
});
self.sequence(elements);
if let Some(tail) = tail {
self.merge(tail);
}
}
pub fn record(&mut self, tipo: Arc<Type>, tag: usize, fields: Vec<AirStack>) {
self.air.push(Air::Record {
scope: self.scope.clone(),
tag,
tipo,
count: fields.len(),
});
self.sequence(fields);
}
pub fn call(&mut self, tipo: Arc<Type>, fun: AirStack, args: Vec<AirStack>) {
self.air.push(Air::Call {
scope: self.scope.clone(),
count: args.len(),
tipo,
});
self.merge(fun);
self.sequence(args);
}
pub fn binop(
&mut self,
name: crate::ast::BinOp,
tipo: Arc<Type>,
left: AirStack,
right: AirStack,
) {
self.air.push(Air::BinOp {
scope: self.scope.clone(),
name,
tipo,
});
self.merge(left);
self.merge(right);
}
pub fn let_assignment(&mut self, name: impl ToString, value: AirStack) {
self.air.push(Air::Let {
scope: self.scope.clone(),
name: name.to_string(),
});
self.merge(value);
}
pub fn wrap_data(&mut self, tipo: Arc<Type>) {
self.air.push(Air::WrapData {
scope: self.scope.clone(),
tipo,
})
}
pub fn un_wrap_data(&mut self, tipo: Arc<Type>) {
self.air.push(Air::UnWrapData {
scope: self.scope.clone(),
tipo,
})
}
}