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() { for i in 0..parameters.len() {
let parameter = &parameters[i]; let parameter = &parameters[i];
let argument = &self.arguments[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( diagnostics.push(Diagnostic::new(
&format!( &format!(
"Mismatched types; expected {} but found {}", "Mismatched types; expected {} but found {}",

View File

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

View File

@ -5,6 +5,7 @@ use std::rc::Rc;
#[derive(Clone)] #[derive(Clone)]
pub enum TypeInfo { pub enum TypeInfo {
Any,
Integer, Integer,
String, String,
Function(Rc<RefCell<FunctionSymbol>>), Function(Rc<RefCell<FunctionSymbol>>),
@ -12,32 +13,34 @@ pub enum TypeInfo {
impl Display for TypeInfo { impl Display for TypeInfo {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { 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 { match self {
TypeInfo::Integer => match other { TypeInfo::Any => write!(f, "Any"),
TypeInfo::Integer => true, TypeInfo::Integer => write!(f, "Int"),
_ => false, TypeInfo::String => write!(f, "String"),
}, TypeInfo::Function(function_symbol) => {
TypeInfo::String => match other { write!(f, "fn(")?;
TypeInfo::String => true, for parameter in function_symbol.borrow().parameters() {
_ => false, parameter.borrow().type_info().fmt(f)?;
},
TypeInfo::Function(f0) => match other {
TypeInfo::Function(f1) => {
f0.as_ptr() == f1.as_ptr() // todo
} }
_ => 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.")
}
} }
} }
} }