[fine] Giving up on generics for now

This commit is contained in:
John Doty 2024-01-17 09:12:00 -08:00
parent b92287ec1f
commit 106f2eb30f
2 changed files with 14 additions and 19 deletions

View file

@ -79,8 +79,6 @@ pub enum Type {
String, String,
Bool, Bool,
TypeArgument(TreeRef, Box<str>), // An un-bound type argument
Function(Vec<Box<Type>>, Box<Type>), Function(Vec<Box<Type>>, Box<Type>),
} }
@ -105,19 +103,9 @@ impl Type {
(Type::Error, _) => true, (Type::Error, _) => true,
(_, Type::Error) => true, (_, Type::Error) => true,
(Type::TypeArgument(a_id, ..), Type::TypeArgument(b_id, ..)) => a_id == b_id,
(_, _) => false, (_, _) => false,
} }
} }
pub fn is_generic(&self) -> bool {
match self {
Type::TypeArgument(..) => true,
Type::Function(a, b) => a.iter().any(|t| t.is_generic()) || b.is_generic(),
_ => false,
}
}
} }
impl fmt::Debug for Type { impl fmt::Debug for Type {
@ -137,7 +125,6 @@ impl fmt::Display for Type {
String => write!(f, "string"), String => write!(f, "string"),
Bool => write!(f, "bool"), Bool => write!(f, "bool"),
MagicPrintGarbage => write!(f, "MagicPrintGarbage"), MagicPrintGarbage => write!(f, "MagicPrintGarbage"),
TypeArgument(_, id) => write!(f, "{id}"),
Function(args, ret) => { Function(args, ret) => {
write!(f, "fun (")?; write!(f, "fun (")?;
let mut first = true; let mut first = true;
@ -666,7 +653,7 @@ impl<'a> Semantics<'a> {
TreeKind::FunctionDecl => self.type_of_function_decl(tree), TreeKind::FunctionDecl => self.type_of_function_decl(tree),
TreeKind::ReturnType => self.type_of_return_type(tree), TreeKind::ReturnType => self.type_of_return_type(tree),
TreeKind::Parameter => self.type_of_parameter(t, tree), TreeKind::Parameter => self.type_of_parameter(tree),
_ => self.internal_compiler_error(Some(t), "asking for a nonsense type"), _ => self.internal_compiler_error(Some(t), "asking for a nonsense type"),
}; };
@ -1027,14 +1014,13 @@ impl<'a> Semantics<'a> {
Some(Type::Function(parameter_types, return_type)) Some(Type::Function(parameter_types, return_type))
} }
fn type_of_parameter(&self, t: TreeRef, tree: &Tree) -> Option<Type> { fn type_of_parameter(&self, tree: &Tree) -> Option<Type> {
assert_eq!(tree.kind, TreeKind::Parameter); assert_eq!(tree.kind, TreeKind::Parameter);
match tree.child_of_kind(self.syntax_tree, TreeKind::TypeExpression) { match tree.child_of_kind(self.syntax_tree, TreeKind::TypeExpression) {
Some(t) => Some(self.type_of(t)), Some(t) => Some(self.type_of(t)),
None => { None => {
// It must be a generic parameter! We'll name this one after the tree where it exists. self.report_error_tree(tree, format!("the parameter is missing a type"));
let name = tree.nth_token(0)?; Some(Type::Error)
Some(Type::TypeArgument(t, format!("${}", name.as_str()).into()))
} }
} }
} }
@ -1126,7 +1112,7 @@ pub fn check(s: &Semantics) {
TreeKind::File => {} TreeKind::File => {}
TreeKind::FunctionDecl => check_function_decl(s, t, tree), TreeKind::FunctionDecl => check_function_decl(s, t, tree),
TreeKind::ParamList => {} TreeKind::ParamList => {}
TreeKind::Parameter => {} TreeKind::Parameter => check_parameter(s, t),
TreeKind::TypeExpression => { TreeKind::TypeExpression => {
let _ = s.type_of_type_expr(tree); let _ = s.type_of_type_expr(tree);
} }
@ -1234,6 +1220,10 @@ fn check_return_statement(s: &Semantics, tree: &Tree) {
// OK this one is a little bit messed up because it reaches *up*, sorry. // OK this one is a little bit messed up because it reaches *up*, sorry.
} }
fn check_parameter(s: &Semantics, t: TreeRef) {
let _ = s.type_of(t);
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;

View file

@ -0,0 +1,5 @@
fun this_is_missing_a_type(x) -> f64 {
23
}
// @check-error: the parameter is missing a type