From 2d601d6115aa5e23ce3ebb8612617d272594634a Mon Sep 17 00:00:00 2001 From: Jesse Brault Date: Mon, 2 Mar 2026 19:45:46 -0600 Subject: [PATCH] Add Any type. --- dmc-lib/src/ast/call.rs | 6 ++++- dmc-lib/src/ast/parameter.rs | 2 +- dmc-lib/src/type_info.rs | 51 +++++++++++++++++++----------------- 3 files changed, 33 insertions(+), 26 deletions(-) diff --git a/dmc-lib/src/ast/call.rs b/dmc-lib/src/ast/call.rs index b578509..ac5244c 100644 --- a/dmc-lib/src/ast/call.rs +++ b/dmc-lib/src/ast/call.rs @@ -97,7 +97,11 @@ impl Call { for i in 0..parameters.len() { let parameter = ¶meters[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 {}", diff --git a/dmc-lib/src/ast/parameter.rs b/dmc-lib/src/ast/parameter.rs index e2e45ae..784cd16 100644 --- a/dmc-lib/src/ast/parameter.rs +++ b/dmc-lib/src/ast/parameter.rs @@ -25,7 +25,7 @@ impl Parameter { ) -> Result>, Vec> { 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), diff --git a/dmc-lib/src/type_info.rs b/dmc-lib/src/type_info.rs index f3a7c0e..dd1bf5b 100644 --- a/dmc-lib/src/type_info.rs +++ b/dmc-lib/src/type_info.rs @@ -5,6 +5,7 @@ use std::rc::Rc; #[derive(Clone)] pub enum TypeInfo { + Any, Integer, String, Function(Rc>), @@ -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.") + } } } }