Add Any type.

This commit is contained in:
Jesse Brault 2026-03-02 19:45:46 -06:00
parent ec18b5d5ba
commit 2d601d6115
3 changed files with 33 additions and 26 deletions

View File

@ -97,7 +97,11 @@ impl Call {
for i in 0..parameters.len() {
let parameter = &parameters[i];
let argument = &self.arguments[i];
if parameter.borrow().type_info() != &argument.type_info() {
if !parameter
.borrow()
.type_info()
.is_assignable_from(&argument.type_info())
{
diagnostics.push(Diagnostic::new(
&format!(
"Mismatched types; expected {} but found {}",

View File

@ -25,7 +25,7 @@ impl Parameter {
) -> Result<Rc<RefCell<ParameterSymbol>>, Vec<Diagnostic>> {
let insert_result = symbol_table.insert_parameter_symbol(ParameterSymbol::new(
&self.declared_name,
TypeInfo::String, // todo
TypeInfo::Any, // todo
));
match insert_result {
Ok(parameter_symbol) => Ok(parameter_symbol),

View File

@ -5,6 +5,7 @@ use std::rc::Rc;
#[derive(Clone)]
pub enum TypeInfo {
Any,
Integer,
String,
Function(Rc<RefCell<FunctionSymbol>>),
@ -12,32 +13,34 @@ pub enum TypeInfo {
impl Display for TypeInfo {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let s = match self {
TypeInfo::Integer => "Int",
TypeInfo::String => "String",
TypeInfo::Function(_) => "fn", // todo
};
write!(f, "{}", s)
}
}
impl PartialEq for &TypeInfo {
fn eq(&self, other: &Self) -> bool {
match self {
TypeInfo::Integer => match other {
TypeInfo::Integer => true,
_ => false,
},
TypeInfo::String => match other {
TypeInfo::String => true,
_ => false,
},
TypeInfo::Function(f0) => match other {
TypeInfo::Function(f1) => {
f0.as_ptr() == f1.as_ptr() // todo
TypeInfo::Any => write!(f, "Any"),
TypeInfo::Integer => write!(f, "Int"),
TypeInfo::String => write!(f, "String"),
TypeInfo::Function(function_symbol) => {
write!(f, "fn(")?;
for parameter in function_symbol.borrow().parameters() {
parameter.borrow().type_info().fmt(f)?;
}
_ => false,
},
write!(f, ")")
}
}
}
}
impl TypeInfo {
pub fn is_assignable_from(&self, other: &TypeInfo) -> bool {
match self {
TypeInfo::Any => true,
TypeInfo::Integer => {
matches!(other, TypeInfo::Integer)
}
TypeInfo::String => {
matches!(other, TypeInfo::String)
}
TypeInfo::Function(_) => {
unimplemented!("Type matching on Functions not yet supported.")
}
}
}
}