use ecow::EcoString; use crate::{ast, type_}; #[derive(Debug, Default)] pub struct Module { functions: Vec, } #[derive(Debug)] pub struct Function { pub name: EcoString, pub parameters: Vec, } #[derive(Debug)] pub struct FunctionParameter { pub type_: Type, pub name: Option, } #[derive(Debug, Default)] pub struct Translator { module: Module, } #[derive(Debug, Clone, Copy)] pub enum Type { Int, Float, } impl Translator { pub fn translate(mut self, module: &ast::TypedModule) -> Module { for definition in &module.definitions { match definition { ast::Definition::Function(function) => { self.module .functions .push(Self::translate_function(function)); } ast::Definition::TypeAlias(_type_alias) => todo!(), ast::Definition::CustomType(_custom_type) => todo!(), ast::Definition::Import(_import) => todo!(), ast::Definition::ModuleConstant(_module_constant) => todo!(), } } self.module } fn translate_function(function: &ast::TypedFunction) -> Function { let (_, name) = function.name.clone().expect("function should have a name"); let parameters = Iterator::collect(function.arguments.iter().map(|argument| { let name = argument.get_variable_name().cloned(); let type_ = Self::translate_type(&argument.type_); FunctionParameter { name, type_ } })); Function { name, parameters } } fn translate_type(type_: &type_::Type) -> Type { match type_ { type_::Type::Named { publicity: _, package: _, module, name, arguments: _, inferred_variant: _, } => match (module.as_str(), name.as_str()) { ("gleam", "Int") => Type::Int, ("gleam", "Float") => Type::Float, (_, _) => todo!(), }, type_::Type::Fn { arguments: _, return_: _, } => todo!(), type_::Type::Var { type_: _ } => todo!(), type_::Type::Tuple { elements: _ } => todo!(), } } }