From ff4ddfbe1b2ae482bb881f6e3600abbce3ed92aa Mon Sep 17 00:00:00 2001 From: Micah Kendall Date: Mon, 1 Apr 2024 23:17:50 +1100 Subject: [PATCH] Simplifying PR per reviewers request --- crates/aiken-lang/src/ast.rs | 1 + crates/aiken-lang/src/expr.rs | 11 +---- crates/aiken-lang/src/format.rs | 23 ++-------- crates/aiken-lang/src/gen_uplc/tree.rs | 44 ++----------------- .../src/parser/expr/fail_todo_trace.rs | 3 +- crates/aiken-lang/src/tipo/expr.rs | 20 +-------- 6 files changed, 11 insertions(+), 91 deletions(-) diff --git a/crates/aiken-lang/src/ast.rs b/crates/aiken-lang/src/ast.rs index de7d5c97..0beff3a2 100644 --- a/crates/aiken-lang/src/ast.rs +++ b/crates/aiken-lang/src/ast.rs @@ -1814,6 +1814,7 @@ pub enum TraceKind { Trace, Todo, Error, + Emit, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] diff --git a/crates/aiken-lang/src/expr.rs b/crates/aiken-lang/src/expr.rs index 864c9a78..7655c9ff 100644 --- a/crates/aiken-lang/src/expr.rs +++ b/crates/aiken-lang/src/expr.rs @@ -550,12 +550,6 @@ pub enum UntypedExpr { text: Box, }, - Emit { - location: Span, - then: Box, - text: Box, - }, - TraceIfFalse { location: Span, value: Box, @@ -1267,7 +1261,6 @@ impl UntypedExpr { match self { Self::PipeLine { expressions, .. } => expressions.last().location(), Self::Trace { then, .. } => then.location(), - Self::Emit { then, .. } => then.location(), Self::TraceIfFalse { location, .. } | Self::Fn { location, .. } | Self::Var { location, .. } @@ -1307,9 +1300,7 @@ impl UntypedExpr { .map(|e| e.start_byte_index()) .unwrap_or(location.start), Self::PipeLine { expressions, .. } => expressions.first().start_byte_index(), - Self::Trace { location, .. } - | Self::Emit { location, .. } - | Self::Assignment { location, .. } => location.start, + Self::Trace { location, .. } | Self::Assignment { location, .. } => location.start, _ => self.location().start, } } diff --git a/crates/aiken-lang/src/format.rs b/crates/aiken-lang/src/format.rs index b149fef1..3114b435 100644 --- a/crates/aiken-lang/src/format.rs +++ b/crates/aiken-lang/src/format.rs @@ -639,7 +639,7 @@ impl<'comments> Formatter<'comments> { ) -> Document<'a> { let args = wrap_args(args.iter().map(|e| (self.fn_arg(e), false))).group(); let body = match body { - UntypedExpr::Trace { .. } | UntypedExpr::Emit { .. } | UntypedExpr::When { .. } => { + UntypedExpr::Trace { .. } | UntypedExpr::When { .. } => { self.expr(body, true).force_break() } _ => self.expr(body, true), @@ -957,8 +957,6 @@ impl<'comments> Formatter<'comments> { kind, text, then, .. } => self.trace(kind, text, then), - UntypedExpr::Emit { text, then, .. } => self.emit(text, then), - UntypedExpr::When { subject, clauses, .. } => self.when(subject, clauses), @@ -1022,6 +1020,7 @@ impl<'comments> Formatter<'comments> { TraceKind::Trace => ("trace", None), TraceKind::Error => ("fail", Some(DEFAULT_ERROR_STR.to_string())), TraceKind::Todo => ("todo", Some(DEFAULT_TODO_STR.to_string())), + TraceKind::Emit => ("emit", None), }; let body = match text { @@ -1037,7 +1036,7 @@ impl<'comments> Formatter<'comments> { match kind { TraceKind::Error | TraceKind::Todo => body, - TraceKind::Trace => body + TraceKind::Trace | TraceKind::Emit => body .append(if self.pop_empty_lines(then.start_byte_index()) { lines(2) } else { @@ -1047,20 +1046,6 @@ impl<'comments> Formatter<'comments> { } } - pub fn emit<'a>(&mut self, text: &'a UntypedExpr, then: &'a UntypedExpr) -> Document<'a> { - let body = "emit" - .to_doc() - .append(" ") - .append(self.wrap_expr(text)) - .group(); - body.append(if self.pop_empty_lines(then.start_byte_index()) { - lines(2) - } else { - line() - }) - .append(self.expr(then, true)) - } - pub fn pattern_constructor<'a>( &mut self, name: &'a str, @@ -1675,7 +1660,6 @@ impl<'comments> Formatter<'comments> { kind: TraceKind::Trace, .. } - | UntypedExpr::Emit { .. } | UntypedExpr::Sequence { .. } | UntypedExpr::Assignment { .. } => "{" .to_doc() @@ -1718,7 +1702,6 @@ impl<'comments> Formatter<'comments> { kind: TraceKind::Trace, .. } - | UntypedExpr::Emit { .. } | UntypedExpr::Sequence { .. } | UntypedExpr::Assignment { .. } => " {" .to_doc() diff --git a/crates/aiken-lang/src/gen_uplc/tree.rs b/crates/aiken-lang/src/gen_uplc/tree.rs index 5863a1ae..9666a63a 100644 --- a/crates/aiken-lang/src/gen_uplc/tree.rs +++ b/crates/aiken-lang/src/gen_uplc/tree.rs @@ -354,11 +354,6 @@ pub enum AirTree { msg: Box, then: Box, }, - Emit { - tipo: Rc, - msg: Box, - then: Box, - }, // End Expressions } @@ -836,7 +831,7 @@ impl AirTree { } } pub fn emit(msg: AirTree, tipo: Rc, then: AirTree) -> AirTree { - AirTree::Emit { + AirTree::Trace { tipo, msg: msg.into(), then: then.into(), @@ -1391,11 +1386,6 @@ impl AirTree { msg.create_air_vec(air_vec); then.create_air_vec(air_vec); } - AirTree::Emit { tipo, msg, then } => { - air_vec.push(Air::Emit { tipo: tipo.clone() }); - msg.create_air_vec(air_vec); - then.create_air_vec(air_vec); - } } } @@ -1417,8 +1407,7 @@ impl AirTree { | AirTree::Constr { tipo, .. } | AirTree::RecordUpdate { tipo, .. } | AirTree::ErrorTerm { tipo, .. } - | AirTree::Trace { tipo, .. } - | AirTree::Emit { tipo, .. } => tipo.clone(), + | AirTree::Trace { tipo, .. } => tipo.clone(), AirTree::Void => void(), AirTree::Var { constructor, .. } => constructor.tipo.clone(), AirTree::Fn { func_body, .. } => func_body.return_type(), @@ -1474,8 +1463,7 @@ impl AirTree { | AirTree::If { tipo, .. } | AirTree::Constr { tipo, .. } | AirTree::ErrorTerm { tipo, .. } - | AirTree::Trace { tipo, .. } - | AirTree::Emit { tipo, .. } => vec![tipo], + | AirTree::Trace { tipo, .. } => vec![tipo], AirTree::Var { constructor, .. } => { vec![constructor.tipo.borrow_mut()] } @@ -1959,23 +1947,6 @@ impl AirTree { apply_with_func_last, ); } - AirTree::Emit { msg, then, .. } => { - msg.do_traverse_tree_with( - tree_path, - current_depth + 1, - index_count.next_number(), - with, - apply_with_func_last, - ); - - then.do_traverse_tree_with( - tree_path, - current_depth + 1, - index_count.next_number(), - with, - apply_with_func_last, - ); - } AirTree::DefineFunc { func_body, then, .. } => { @@ -2320,15 +2291,6 @@ impl AirTree { panic!("Tree Path index outside tree children nodes") } } - AirTree::Emit { msg, then, .. } => { - if *index == 0 { - msg.as_mut().do_find_air_tree_node(tree_path_iter) - } else if *index == 1 { - then.as_mut().do_find_air_tree_node(tree_path_iter) - } else { - panic!("Tree Path index outside tree children nodes") - } - } _ => { panic!("A tree node with no children was encountered with a longer tree path.") } diff --git a/crates/aiken-lang/src/parser/expr/fail_todo_trace.rs b/crates/aiken-lang/src/parser/expr/fail_todo_trace.rs index 1e30893a..96d23b7e 100644 --- a/crates/aiken-lang/src/parser/expr/fail_todo_trace.rs +++ b/crates/aiken-lang/src/parser/expr/fail_todo_trace.rs @@ -39,7 +39,8 @@ pub fn parser<'a>( just(Token::Emit) .ignore_then(choice((string::hybrid(), expression.clone()))) .then(sequence.clone().or_not()) - .map_with_span(|(text, then_), span| UntypedExpr::Emit { + .map_with_span(|(text, then_), span| UntypedExpr::Trace { + kind: TraceKind::Emit, location: span, then: Box::new(then_.unwrap_or_else(|| UntypedExpr::todo(None, span))), text: Box::new(text), diff --git a/crates/aiken-lang/src/tipo/expr.rs b/crates/aiken-lang/src/tipo/expr.rs index a1b7f173..7aa932f9 100644 --- a/crates/aiken-lang/src/tipo/expr.rs +++ b/crates/aiken-lang/src/tipo/expr.rs @@ -281,22 +281,6 @@ impl<'a, 'b> ExprTyper<'a, 'b> { kind, } => self.infer_trace(kind, *then, location, *text), - UntypedExpr::Emit { - location, - then, - text, - } => { - let text = self.infer(*text)?; - self.unify(string(), text.tipo(), text.location(), false)?; - let then = self.infer(*then)?; - Ok(TypedExpr::Emit { - location, - tipo: then.tipo(), - then: Box::new(then), - text: Box::new(text), - }) - } - UntypedExpr::When { location, subject, @@ -2340,9 +2324,7 @@ fn assert_no_assignment(expr: &UntypedExpr) -> Result<(), Error> { location: expr.location(), expr: *value.clone(), }), - UntypedExpr::Trace { then, .. } | UntypedExpr::Emit { then, .. } => { - assert_no_assignment(then) - } + UntypedExpr::Trace { then, .. } => assert_no_assignment(then), UntypedExpr::Fn { .. } | UntypedExpr::BinOp { .. } | UntypedExpr::ByteArray { .. }