diff --git a/mist-test/src/logger.mist b/mist-test/src/logger.mist index a1c5631..fbfc688 100644 --- a/mist-test/src/logger.mist +++ b/mist-test/src/logger.mist @@ -1,6 +1,6 @@ use ; -public trait TestLogging: Display {} +public trait TestLogging: fmt::Display {} public enum LogLevel { Info, diff --git a/parser/src/ast.rs b/parser/src/ast.rs index 18509dd..ed75ba1 100644 --- a/parser/src/ast.rs +++ b/parser/src/ast.rs @@ -80,7 +80,7 @@ pub enum TopLevelKind { name: Identifier, generics: Generics, requirements: Vec, - items: Vec, + items: Vec, }, EnumDecl { visibility: Visibility, @@ -105,12 +105,6 @@ pub enum TopLevelKind { }, } -#[derive(Debug, Clone, Serialize)] -pub enum TraitItem { - WithBody(FunctionDecl), - NoBody(FunctionDecl), -} - #[derive(Debug, Clone, Serialize)] pub enum ClassItem { Method(FunctionDecl), @@ -158,7 +152,7 @@ pub struct FunctionDecl { pub generics: Generics, pub params: ParamList, pub return_type: TypeExpr, - pub body: Block, + pub body: Option, } #[derive(Debug, Clone, Serialize)] diff --git a/parser/src/lib.rs b/parser/src/lib.rs index fb0343e..ba4193b 100644 --- a/parser/src/lib.rs +++ b/parser/src/lib.rs @@ -314,7 +314,7 @@ impl From> for TopLevelKind { requirements: consume_rule(&mut inner, Rule::trait_requirements) .map(|pair| pair.into_inner().map(TypeExpr::from).collect()) .unwrap_or_default(), - items: inner.map(TraitItem::from).collect(), + items: inner.map(FunctionDecl::from).collect(), }, _ => unimplemented!("{rule:#?}"), @@ -322,19 +322,6 @@ impl From> for TopLevelKind { } } -impl From> for TraitItem { - fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { - let rule = pair.as_rule(); - - match rule { - Rule::method => TraitItem::WithBody(FunctionDecl::from(pair)), - Rule::method_no_body => TraitItem::NoBody(FunctionDecl::from(pair)), - - _ => unimplemented!("{rule:#?}"), - } - } -} - impl From> for ClassItem { fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { let rule = pair.as_rule(); @@ -789,7 +776,7 @@ impl From> for FunctionDecl { }) .unwrap_or_else(|| ParamList(self_param.into_iter().collect())); - let body = Block::from(inner.next().unwrap()); + let body = inner.next().map(Block::from); Self { visibility, diff --git a/src/codegen.rs b/src/codegen.rs index 3df7404..0330b60 100644 --- a/src/codegen.rs +++ b/src/codegen.rs @@ -358,6 +358,38 @@ impl ToRust for TopLevelKind { cg.indent -= 1; cg.addln("}\n"); } + Self::TraitDecl { + visibility, + name, + generics, + requirements, + items, + } => { + cg.addln(&format!( + "{}trait {}{}{} {{", + visibility.get_rust(), + name.get_rust(), + generics.get_rust(), + if requirements.len() != 0 { + String::from(": ") + + &requirements + .iter() + .map(TypeExpr::get_rust) + .collect::>() + .join("+") + } else { + String::new() + }, + )); + cg.indent += 1; + + for item in items { + item.to_rust(cg); + } + + cg.indent -= 1; + cg.addln("}\n"); + } Self::ClassDecl { visibility, name, @@ -627,9 +659,13 @@ impl ToRust for FunctionDecl { params_str, self.return_type.get_rust() )); - cg.indent += 1; - self.body.to_rust(cg); - cg.indent -= 1; + if let Some(body) = &self.body { + cg.indent += 1; + body.to_rust(cg); + cg.indent -= 1; + } else { + cg.add(";"); + } cg.add_indentedln("}\n"); } }