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>,
},
ErrorTerm {
scope: Vec<u64>,
tipo: Arc<Type>,
},
Trace {
scope: Vec<u64>,
text: Option<String>,
@ -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, .. }

View File

@ -148,6 +148,11 @@ pub enum TypedExpr {
tipo: Arc<Type>,
},
ErrorTerm {
location: Span,
tipo: Arc<Type>,
},
RecordUpdate {
location: Span,
tipo: Arc<Type>,
@ -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<Annotation>,
},
Trace {
location: Span,
then: Box<Self>,
text: Option<String>,
},
When {
location: Span,
subjects: Vec<Self>,
@ -412,6 +423,10 @@ pub enum UntypedExpr {
label: Option<String>,
},
ErrorTerm {
location: Span,
},
RecordUpdate {
location: Span,
constructor: Box<Self>,
@ -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, .. }

View File

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

View File

@ -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,

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() {
"trace" => Token::Trace,
"error" => Token::ErrorTerm,
"as" => Token::As,
"assert" => Token::Assert,
"check" => Token::Assert,

View File

@ -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)
}

View File

@ -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,

View File

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

View File

@ -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),
}
}
}