From 37196a29eecfe320e313d180d09a73d2196d90b2 Mon Sep 17 00:00:00 2001 From: rvcas Date: Fri, 23 Dec 2022 15:13:46 -0500 Subject: [PATCH] feat: error keyword --- crates/aiken-lang/src/air.rs | 6 ++++++ crates/aiken-lang/src/expr.rs | 16 ++++++++++++++++ crates/aiken-lang/src/format.rs | 2 ++ crates/aiken-lang/src/parser.rs | 5 +++++ crates/aiken-lang/src/parser/lexer.rs | 1 + crates/aiken-lang/src/parser/token.rs | 2 ++ crates/aiken-lang/src/tipo/expr.rs | 8 ++++++++ crates/aiken-lang/src/tipo/infer.rs | 2 ++ crates/aiken-lang/src/uplc.rs | 8 ++++++++ 9 files changed, 50 insertions(+) diff --git a/crates/aiken-lang/src/air.rs b/crates/aiken-lang/src/air.rs index b41118b0..4ad55444 100644 --- a/crates/aiken-lang/src/air.rs +++ b/crates/aiken-lang/src/air.rs @@ -202,6 +202,11 @@ pub enum Air { tipo: Arc, }, + ErrorTerm { + scope: Vec, + tipo: Arc, + }, + Trace { scope: Vec, text: Option, @@ -263,6 +268,7 @@ impl Air { | Air::FieldsExpose { scope, .. } | Air::Tuple { scope, .. } | Air::Todo { scope, .. } + | Air::ErrorTerm { scope, .. } | Air::Record { scope, .. } | Air::RecordUpdate { scope, .. } | Air::Negate { scope, .. } diff --git a/crates/aiken-lang/src/expr.rs b/crates/aiken-lang/src/expr.rs index d99b7ef3..530ec438 100644 --- a/crates/aiken-lang/src/expr.rs +++ b/crates/aiken-lang/src/expr.rs @@ -148,6 +148,11 @@ pub enum TypedExpr { tipo: Arc, }, + ErrorTerm { + location: Span, + tipo: Arc, + }, + RecordUpdate { location: Span, tipo: Arc, @@ -170,6 +175,7 @@ impl TypedExpr { Self::Fn { tipo, .. } | Self::Int { tipo, .. } | Self::Todo { tipo, .. } + | Self::ErrorTerm { tipo, .. } | Self::When { tipo, .. } | Self::List { tipo, .. } | Self::Call { tipo, .. } @@ -214,6 +220,7 @@ impl TypedExpr { | TypedExpr::Call { .. } | TypedExpr::When { .. } | TypedExpr::Todo { .. } + | TypedExpr::ErrorTerm { .. } | TypedExpr::BinOp { .. } | TypedExpr::Tuple { .. } | TypedExpr::Negate { .. } @@ -252,6 +259,7 @@ impl TypedExpr { | Self::Var { location, .. } | Self::Trace { location, .. } | Self::Todo { location, .. } + | Self::ErrorTerm { location, .. } | Self::When { location, .. } | Self::Call { location, .. } | Self::List { location, .. } @@ -287,6 +295,7 @@ impl TypedExpr { | Self::Trace { location, .. } | Self::Var { location, .. } | Self::Todo { location, .. } + | Self::ErrorTerm { location, .. } | Self::When { location, .. } | Self::Call { location, .. } | Self::If { location, .. } @@ -372,11 +381,13 @@ pub enum UntypedExpr { kind: AssignmentKind, annotation: Option, }, + Trace { location: Span, then: Box, text: Option, }, + When { location: Span, subjects: Vec, @@ -412,6 +423,10 @@ pub enum UntypedExpr { label: Option, }, + ErrorTerm { + location: Span, + }, + RecordUpdate { location: Span, constructor: Box, @@ -482,6 +497,7 @@ impl UntypedExpr { | Self::Var { location, .. } | Self::Int { location, .. } | Self::Todo { location, .. } + | Self::ErrorTerm { location, .. } | Self::When { location, .. } | Self::Call { location, .. } | Self::List { location, .. } diff --git a/crates/aiken-lang/src/format.rs b/crates/aiken-lang/src/format.rs index 39d1009a..ee6ba4cb 100644 --- a/crates/aiken-lang/src/format.rs +++ b/crates/aiken-lang/src/format.rs @@ -783,7 +783,9 @@ impl<'comments> Formatter<'comments> { .append((index + 1).to_doc()) .append(suffix) } + UntypedExpr::ErrorTerm { .. } => "error".to_doc(), }; + commented(document, comments) } diff --git a/crates/aiken-lang/src/parser.rs b/crates/aiken-lang/src/parser.rs index d2e96e14..68806090 100644 --- a/crates/aiken-lang/src/parser.rs +++ b/crates/aiken-lang/src/parser.rs @@ -851,6 +851,10 @@ pub fn expr_parser( label, }); + let error_parser = just(Token::ErrorTerm) + .ignored() + .map_with_span(|_, span| expr::UntypedExpr::ErrorTerm { location: span }); + let tuple = just(Token::Hash) .ignore_then( r.clone() @@ -1065,6 +1069,7 @@ pub fn expr_parser( field_access_constructor, var_parser, todo_parser, + error_parser, tuple, bytearray, list_parser, diff --git a/crates/aiken-lang/src/parser/lexer.rs b/crates/aiken-lang/src/parser/lexer.rs index d218c2ea..3c05fd0d 100644 --- a/crates/aiken-lang/src/parser/lexer.rs +++ b/crates/aiken-lang/src/parser/lexer.rs @@ -95,6 +95,7 @@ pub fn lexer() -> impl Parser, Error = ParseError> { let keyword = text::ident().map(|s: String| match s.as_str() { "trace" => Token::Trace, + "error" => Token::ErrorTerm, "as" => Token::As, "assert" => Token::Assert, "check" => Token::Assert, diff --git a/crates/aiken-lang/src/parser/token.rs b/crates/aiken-lang/src/parser/token.rs index 5c681a06..09da20bf 100644 --- a/crates/aiken-lang/src/parser/token.rs +++ b/crates/aiken-lang/src/parser/token.rs @@ -75,6 +75,7 @@ pub enum Token { Type, When, Trace, + ErrorTerm, } impl fmt::Display for Token { @@ -154,6 +155,7 @@ impl fmt::Display for Token { Token::Trace => "trace", Token::Type => "type", Token::Test => "test", + Token::ErrorTerm => "error", }; write!(f, "\"{}\"", s) } diff --git a/crates/aiken-lang/src/tipo/expr.rs b/crates/aiken-lang/src/tipo/expr.rs index 31b74aa7..95c47feb 100644 --- a/crates/aiken-lang/src/tipo/expr.rs +++ b/crates/aiken-lang/src/tipo/expr.rs @@ -236,6 +236,8 @@ impl<'a, 'b> ExprTyper<'a, 'b> { .. } => Ok(self.infer_todo(location, kind, label)), + UntypedExpr::ErrorTerm { location } => Ok(self.infer_error_term(location)), + UntypedExpr::Var { location, name, .. } => self.infer_var(name, location), UntypedExpr::Int { @@ -1759,6 +1761,12 @@ impl<'a, 'b> ExprTyper<'a, 'b> { } } + fn infer_error_term(&mut self, location: Span) -> TypedExpr { + let tipo = self.new_unbound_var(); + + TypedExpr::ErrorTerm { location, tipo } + } + fn infer_trace( &mut self, then: UntypedExpr, diff --git a/crates/aiken-lang/src/tipo/infer.rs b/crates/aiken-lang/src/tipo/infer.rs index 83f8f470..d164ce3b 100644 --- a/crates/aiken-lang/src/tipo/infer.rs +++ b/crates/aiken-lang/src/tipo/infer.rs @@ -476,6 +476,8 @@ fn str_to_keyword(word: &str) -> Option { "todo" => Some(Token::Todo), "type" => Some(Token::Type), "trace" => Some(Token::Trace), + "test" => Some(Token::Test), + "error" => Some(Token::ErrorTerm), _ => None, } } diff --git a/crates/aiken-lang/src/uplc.rs b/crates/aiken-lang/src/uplc.rs index 7b436c42..77ec8941 100644 --- a/crates/aiken-lang/src/uplc.rs +++ b/crates/aiken-lang/src/uplc.rs @@ -536,6 +536,13 @@ impl<'a> CodeGenerator<'a> { TypedExpr::TupleIndex { .. } => { todo!("Tuple indexing not implementing yet"); } + + TypedExpr::ErrorTerm { tipo, .. } => { + ir_stack.push(Air::ErrorTerm { + scope, + tipo: tipo.clone(), + }); + } } } @@ -3732,6 +3739,7 @@ impl<'a> CodeGenerator<'a> { arg_stack.push(term); } + Air::ErrorTerm { .. } => arg_stack.push(Term::Error), } } }