Skip to content

Commit

Permalink
Remove clause guards.
Browse files Browse the repository at this point in the history
  Closes #886.
  • Loading branch information
KtorZ committed Aug 1, 2024
1 parent b28d4a6 commit 3442f31
Show file tree
Hide file tree
Showing 20 changed files with 113 additions and 1,367 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

### Removed

- **aiken-lang**: clause guards are no longer part of the language. See [#886](https://github.com/aiken-lang/aiken/issues/886). @KtorZ.

## v1.0.30-alpha - 2024-07-25

Expand Down
122 changes: 2 additions & 120 deletions crates/aiken-lang/src/ast.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
builtins::{self, bool, g1_element, g2_element},
builtins::{self, g1_element, g2_element},
expr::{TypedExpr, UntypedExpr},
line_numbers::LineNumbers,
parser::token::{Base, Token},
Expand Down Expand Up @@ -1752,15 +1752,13 @@ pub type TypedMultiPattern = MultiPattern<PatternConstructor, Rc<Type>>;
pub struct UntypedClause {
pub location: Span,
pub patterns: Vec1<Pattern<(), ()>>,
pub guard: Option<ClauseGuard<()>>,
pub then: UntypedExpr,
}

#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct TypedClause {
pub location: Span,
pub pattern: Pattern<PatternConstructor, Rc<Type>>,
pub guard: Option<ClauseGuard<Rc<Type>>>,
pub then: TypedExpr,
}

Expand All @@ -1779,123 +1777,7 @@ impl TypedClause {
}
}

pub type UntypedClauseGuard = ClauseGuard<()>;
pub type TypedClauseGuard = ClauseGuard<Rc<Type>>;

#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum ClauseGuard<Type> {
Not {
location: Span,
value: Box<Self>,
},

Equals {
location: Span,
left: Box<Self>,
right: Box<Self>,
},

NotEquals {
location: Span,
left: Box<Self>,
right: Box<Self>,
},

GtInt {
location: Span,
left: Box<Self>,
right: Box<Self>,
},

GtEqInt {
location: Span,
left: Box<Self>,
right: Box<Self>,
},

LtInt {
location: Span,
left: Box<Self>,
right: Box<Self>,
},

LtEqInt {
location: Span,
left: Box<Self>,
right: Box<Self>,
},

Or {
location: Span,
left: Box<Self>,
right: Box<Self>,
},

And {
location: Span,
left: Box<Self>,
right: Box<Self>,
},

Var {
location: Span,
tipo: Type,
name: String,
},

Constant(Constant),
}

impl<A> ClauseGuard<A> {
pub fn location(&self) -> Span {
match self {
ClauseGuard::Constant(constant) => constant.location(),
ClauseGuard::Not { location, .. }
| ClauseGuard::Or { location, .. }
| ClauseGuard::And { location, .. }
| ClauseGuard::Var { location, .. }
| ClauseGuard::Equals { location, .. }
| ClauseGuard::NotEquals { location, .. }
| ClauseGuard::GtInt { location, .. }
| ClauseGuard::GtEqInt { location, .. }
| ClauseGuard::LtInt { location, .. }
| ClauseGuard::LtEqInt { location, .. } => *location,
}
}

pub fn precedence(&self) -> u8 {
// Ensure that this matches the other precedence function for guards
match self {
ClauseGuard::Not { .. } => 1,
ClauseGuard::Or { .. } => 2,
ClauseGuard::And { .. } => 3,
ClauseGuard::Equals { .. } | ClauseGuard::NotEquals { .. } => 4,
ClauseGuard::GtInt { .. }
| ClauseGuard::GtEqInt { .. }
| ClauseGuard::LtInt { .. }
| ClauseGuard::LtEqInt { .. } => 5,
ClauseGuard::Constant(_) | ClauseGuard::Var { .. } => 6,
}
}
}

impl TypedClauseGuard {
pub fn tipo(&self) -> Rc<Type> {
match self {
ClauseGuard::Var { tipo, .. } => tipo.clone(),
ClauseGuard::Constant(constant) => constant.tipo(),
ClauseGuard::Not { .. }
| ClauseGuard::Or { .. }
| ClauseGuard::And { .. }
| ClauseGuard::Equals { .. }
| ClauseGuard::NotEquals { .. }
| ClauseGuard::GtInt { .. }
| ClauseGuard::GtEqInt { .. }
| ClauseGuard::LtInt { .. }
| ClauseGuard::LtEqInt { .. } => bool(),
}
}
}
pub struct UntypedClauseGuard {}

pub type TypedIfBranch = IfBranch<TypedExpr, TypedPattern>;
pub type UntypedIfBranch = IfBranch<UntypedExpr, AssignmentPattern>;
Expand Down
73 changes: 5 additions & 68 deletions crates/aiken-lang/src/format.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::{
ast::{
Annotation, ArgBy, ArgName, ArgVia, AssignmentKind, AssignmentPattern, BinOp,
ByteArrayFormatPreference, CallArg, ClauseGuard, Constant, CurveType, DataType, Definition,
Function, LogicalOpChainKind, ModuleConstant, OnTestFailure, Pattern, RecordConstructor,
ByteArrayFormatPreference, CallArg, Constant, CurveType, DataType, Definition, Function,
LogicalOpChainKind, ModuleConstant, OnTestFailure, Pattern, RecordConstructor,
RecordConstructorArg, RecordUpdateSpread, Span, TraceKind, TypeAlias, TypedArg, UnOp,
UnqualifiedImport, UntypedArg, UntypedArgVia, UntypedAssignmentKind, UntypedClause,
UntypedClauseGuard, UntypedDefinition, UntypedFunction, UntypedIfBranch, UntypedModule,
UntypedPattern, UntypedRecordUpdateArg, Use, Validator, CAPTURE_VARIABLE,
UntypedDefinition, UntypedFunction, UntypedIfBranch, UntypedModule, UntypedPattern,
UntypedRecordUpdateArg, Use, Validator, CAPTURE_VARIABLE,
},
docvec,
expr::{FnStyle, UntypedExpr, DEFAULT_ERROR_STR, DEFAULT_TODO_STR},
Expand Down Expand Up @@ -1844,10 +1844,6 @@ impl<'comments> Formatter<'comments> {
clause.patterns.iter().map(|p| self.pattern(p)),
" | ".to_doc(),
);
let clause_doc = match &clause.guard {
None => clause_doc,
Some(guard) => clause_doc.append(" if ").append(self.clause_guard(guard)),
};

if index == 0 {
clause_doc
Expand Down Expand Up @@ -1946,61 +1942,6 @@ impl<'comments> Formatter<'comments> {
commented(doc, comments)
}

pub fn clause_guard_bin_op<'a>(
&mut self,
name: &'a str,
name_precedence: u8,
left: &'a UntypedClauseGuard,
right: &'a UntypedClauseGuard,
) -> Document<'a> {
let left_precedence = left.precedence();
let right_precedence = right.precedence();
let left = self.clause_guard(left);
let right = self.clause_guard(right);
self.operator_side(left, name_precedence, left_precedence)
.append(name)
.append(self.operator_side(right, name_precedence, right_precedence.saturating_sub(1)))
}

fn clause_guard<'a>(&mut self, clause_guard: &'a UntypedClauseGuard) -> Document<'a> {
match clause_guard {
ClauseGuard::Not { value, .. } => {
docvec!["!", self.clause_guard(value)]
}
ClauseGuard::And { left, right, .. } => {
self.clause_guard_bin_op(" && ", clause_guard.precedence(), left, right)
}
ClauseGuard::Or { left, right, .. } => {
self.clause_guard_bin_op(" || ", clause_guard.precedence(), left, right)
}
ClauseGuard::Equals { left, right, .. } => {
self.clause_guard_bin_op(" == ", clause_guard.precedence(), left, right)
}

ClauseGuard::NotEquals { left, right, .. } => {
self.clause_guard_bin_op(" != ", clause_guard.precedence(), left, right)
}
ClauseGuard::GtInt { left, right, .. } => {
self.clause_guard_bin_op(" > ", clause_guard.precedence(), left, right)
}

ClauseGuard::GtEqInt { left, right, .. } => {
self.clause_guard_bin_op(" >= ", clause_guard.precedence(), left, right)
}
ClauseGuard::LtInt { left, right, .. } => {
self.clause_guard_bin_op(" < ", clause_guard.precedence(), left, right)
}

ClauseGuard::LtEqInt { left, right, .. } => {
self.clause_guard_bin_op(" <= ", clause_guard.precedence(), left, right)
}

ClauseGuard::Var { name, .. } => name.to_doc(),

ClauseGuard::Constant(constant) => self.const_expr(constant),
}
}

fn un_op<'a>(&mut self, value: &'a UntypedExpr, op: &'a UnOp) -> Document<'a> {
match op {
UnOp::Not => docvec!["!", self.wrap_unary_op(value)],
Expand Down Expand Up @@ -2031,11 +1972,7 @@ impl<'a> Documentable<'a> for &'a ArgName {
}

fn pub_(public: bool) -> Document<'static> {
if public {
"pub ".to_doc()
} else {
nil()
}
if public { "pub ".to_doc() } else { nil() }
}

impl<'a> Documentable<'a> for &'a UnqualifiedImport {
Expand Down
27 changes: 2 additions & 25 deletions crates/aiken-lang/src/gen_uplc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1970,28 +1970,7 @@ impl<'a> CodeGenerator<'a> {
props.complex_clause = false;

if let Some((clause, rest_clauses)) = clauses.split_first() {
let mut clause_then = self.build(&clause.then, module_name, &[]);

// handles clause guard if it exists
if let Some(guard) = &clause.guard {
props.complex_clause = true;

let clause_guard_name = format!(
"__clause_guard_span_{}_{}",
clause.location.start, clause.location.end
);

clause_then = AirTree::let_assignment(
&clause_guard_name,
builder::handle_clause_guard(guard),
AirTree::clause_guard(
&clause_guard_name,
AirTree::bool(true),
bool(),
clause_then,
),
);
}
let clause_then = self.build(&clause.then, module_name, &[]);

match &mut props.specific_clause {
// TODO: Implement PairClause and PairClauseGuard
Expand Down Expand Up @@ -2170,7 +2149,7 @@ impl<'a> CodeGenerator<'a> {
}
};

let mut is_wild_card_elems_clause = clause.guard.is_none();
let mut is_wild_card_elems_clause = true;
for element in elements.iter() {
is_wild_card_elems_clause = is_wild_card_elems_clause
&& !pattern_has_conditions(element, &self.data_types);
Expand Down Expand Up @@ -2320,8 +2299,6 @@ impl<'a> CodeGenerator<'a> {
// handle final_clause
props.final_clause = true;

assert!(final_clause.guard.is_none());

let clause_then = self.build(&final_clause.then, module_name, &[]);

let (condition, assignments) =
Expand Down
Loading

0 comments on commit 3442f31

Please sign in to comment.