feat: error keyword

This commit is contained in:
rvcas 2022-12-23 15:13:46 -05:00 committed by Lucas
parent 22103739c3
commit 37196a29ee
9 changed files with 50 additions and 0 deletions

View File

@ -202,6 +202,11 @@ pub enum Air {
tipo: Arc<Type>, tipo: Arc<Type>,
}, },
ErrorTerm {
scope: Vec<u64>,
tipo: Arc<Type>,
},
Trace { Trace {
scope: Vec<u64>, scope: Vec<u64>,
text: Option<String>, text: Option<String>,
@ -263,6 +268,7 @@ impl Air {
| Air::FieldsExpose { scope, .. } | Air::FieldsExpose { scope, .. }
| Air::Tuple { scope, .. } | Air::Tuple { scope, .. }
| Air::Todo { scope, .. } | Air::Todo { scope, .. }
| Air::ErrorTerm { scope, .. }
| Air::Record { scope, .. } | Air::Record { scope, .. }
| Air::RecordUpdate { scope, .. } | Air::RecordUpdate { scope, .. }
| Air::Negate { scope, .. } | Air::Negate { scope, .. }

View File

@ -148,6 +148,11 @@ pub enum TypedExpr {
tipo: Arc<Type>, tipo: Arc<Type>,
}, },
ErrorTerm {
location: Span,
tipo: Arc<Type>,
},
RecordUpdate { RecordUpdate {
location: Span, location: Span,
tipo: Arc<Type>, tipo: Arc<Type>,
@ -170,6 +175,7 @@ impl TypedExpr {
Self::Fn { tipo, .. } Self::Fn { tipo, .. }
| Self::Int { tipo, .. } | Self::Int { tipo, .. }
| Self::Todo { tipo, .. } | Self::Todo { tipo, .. }
| Self::ErrorTerm { tipo, .. }
| Self::When { tipo, .. } | Self::When { tipo, .. }
| Self::List { tipo, .. } | Self::List { tipo, .. }
| Self::Call { tipo, .. } | Self::Call { tipo, .. }
@ -214,6 +220,7 @@ impl TypedExpr {
| TypedExpr::Call { .. } | TypedExpr::Call { .. }
| TypedExpr::When { .. } | TypedExpr::When { .. }
| TypedExpr::Todo { .. } | TypedExpr::Todo { .. }
| TypedExpr::ErrorTerm { .. }
| TypedExpr::BinOp { .. } | TypedExpr::BinOp { .. }
| TypedExpr::Tuple { .. } | TypedExpr::Tuple { .. }
| TypedExpr::Negate { .. } | TypedExpr::Negate { .. }
@ -252,6 +259,7 @@ impl TypedExpr {
| Self::Var { location, .. } | Self::Var { location, .. }
| Self::Trace { location, .. } | Self::Trace { location, .. }
| Self::Todo { location, .. } | Self::Todo { location, .. }
| Self::ErrorTerm { location, .. }
| Self::When { location, .. } | Self::When { location, .. }
| Self::Call { location, .. } | Self::Call { location, .. }
| Self::List { location, .. } | Self::List { location, .. }
@ -287,6 +295,7 @@ impl TypedExpr {
| Self::Trace { location, .. } | Self::Trace { location, .. }
| Self::Var { location, .. } | Self::Var { location, .. }
| Self::Todo { location, .. } | Self::Todo { location, .. }
| Self::ErrorTerm { location, .. }
| Self::When { location, .. } | Self::When { location, .. }
| Self::Call { location, .. } | Self::Call { location, .. }
| Self::If { location, .. } | Self::If { location, .. }
@ -372,11 +381,13 @@ pub enum UntypedExpr {
kind: AssignmentKind, kind: AssignmentKind,
annotation: Option<Annotation>, annotation: Option<Annotation>,
}, },
Trace { Trace {
location: Span, location: Span,
then: Box<Self>, then: Box<Self>,
text: Option<String>, text: Option<String>,
}, },
When { When {
location: Span, location: Span,
subjects: Vec<Self>, subjects: Vec<Self>,
@ -412,6 +423,10 @@ pub enum UntypedExpr {
label: Option<String>, label: Option<String>,
}, },
ErrorTerm {
location: Span,
},
RecordUpdate { RecordUpdate {
location: Span, location: Span,
constructor: Box<Self>, constructor: Box<Self>,
@ -482,6 +497,7 @@ impl UntypedExpr {
| Self::Var { location, .. } | Self::Var { location, .. }
| Self::Int { location, .. } | Self::Int { location, .. }
| Self::Todo { location, .. } | Self::Todo { location, .. }
| Self::ErrorTerm { location, .. }
| Self::When { location, .. } | Self::When { location, .. }
| Self::Call { location, .. } | Self::Call { location, .. }
| Self::List { location, .. } | Self::List { location, .. }

View File

@ -783,7 +783,9 @@ impl<'comments> Formatter<'comments> {
.append((index + 1).to_doc()) .append((index + 1).to_doc())
.append(suffix) .append(suffix)
} }
UntypedExpr::ErrorTerm { .. } => "error".to_doc(),
}; };
commented(document, comments) commented(document, comments)
} }

View File

@ -851,6 +851,10 @@ pub fn expr_parser(
label, label,
}); });
let error_parser = just(Token::ErrorTerm)
.ignored()
.map_with_span(|_, span| expr::UntypedExpr::ErrorTerm { location: span });
let tuple = just(Token::Hash) let tuple = just(Token::Hash)
.ignore_then( .ignore_then(
r.clone() r.clone()
@ -1065,6 +1069,7 @@ pub fn expr_parser(
field_access_constructor, field_access_constructor,
var_parser, var_parser,
todo_parser, todo_parser,
error_parser,
tuple, tuple,
bytearray, bytearray,
list_parser, list_parser,

View File

@ -95,6 +95,7 @@ pub fn lexer() -> impl Parser<char, Vec<(Token, Span)>, Error = ParseError> {
let keyword = text::ident().map(|s: String| match s.as_str() { let keyword = text::ident().map(|s: String| match s.as_str() {
"trace" => Token::Trace, "trace" => Token::Trace,
"error" => Token::ErrorTerm,
"as" => Token::As, "as" => Token::As,
"assert" => Token::Assert, "assert" => Token::Assert,
"check" => Token::Assert, "check" => Token::Assert,

View File

@ -75,6 +75,7 @@ pub enum Token {
Type, Type,
When, When,
Trace, Trace,
ErrorTerm,
} }
impl fmt::Display for Token { impl fmt::Display for Token {
@ -154,6 +155,7 @@ impl fmt::Display for Token {
Token::Trace => "trace", Token::Trace => "trace",
Token::Type => "type", Token::Type => "type",
Token::Test => "test", Token::Test => "test",
Token::ErrorTerm => "error",
}; };
write!(f, "\"{}\"", s) write!(f, "\"{}\"", s)
} }

View File

@ -236,6 +236,8 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
.. ..
} => Ok(self.infer_todo(location, kind, label)), } => 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::Var { location, name, .. } => self.infer_var(name, location),
UntypedExpr::Int { 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( fn infer_trace(
&mut self, &mut self,
then: UntypedExpr, then: UntypedExpr,

View File

@ -476,6 +476,8 @@ fn str_to_keyword(word: &str) -> Option<Token> {
"todo" => Some(Token::Todo), "todo" => Some(Token::Todo),
"type" => Some(Token::Type), "type" => Some(Token::Type),
"trace" => Some(Token::Trace), "trace" => Some(Token::Trace),
"test" => Some(Token::Test),
"error" => Some(Token::ErrorTerm),
_ => None, _ => None,
} }
} }

View File

@ -536,6 +536,13 @@ impl<'a> CodeGenerator<'a> {
TypedExpr::TupleIndex { .. } => { TypedExpr::TupleIndex { .. } => {
todo!("Tuple indexing not implementing yet"); 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); arg_stack.push(term);
} }
Air::ErrorTerm { .. } => arg_stack.push(Term::Error),
} }
} }
} }