diff --git a/CHANGELOG.md b/CHANGELOG.md index 10e01a6dcbc6b62b04fff3ca7909b07fcbb2d2af..425d8406dfcd36aa40a16f133652ad192c194393 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog for the repository +## 2020-09-03 +- Updated ast with Display + ## 2020-09-03 - Added src/comment - single and multiline comments diff --git a/src/ast/ast.rs b/src/ast/ast.rs index e79e70de9cab432b6a84b5ac4e10179b50c3db3e..50417318082330ea12b8623d3d2fb7ee57daa764 100644 --- a/src/ast/ast.rs +++ b/src/ast/ast.rs @@ -1,7 +1,22 @@ +use std::fmt; + // ast -#[derive(Debug)] +// println!("{:?}", ..) +#[derive(Debug)] pub enum NumOrId { Num(usize), Id(String), +} + +// println!("{}", ..) +impl fmt::Display for NumOrId { + + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + NumOrId::Num(i) => write!(f, "{}", i)?, + NumOrId::Id(s) => write!(f, "{}", s)?, + }; + Ok(()) + } } \ No newline at end of file diff --git a/src/ast/main.rs b/src/ast/main.rs index 2a0f6624d6b3f24a0f215bee20eee2f062967803..998eed7d08991ba6c56c49d8c78aa26c190709c8 100644 --- a/src/ast/main.rs +++ b/src/ast/main.rs @@ -10,10 +10,13 @@ fn main() { println!("minimal"); println!("{:?}", NumOrIdParser::new().parse("123")); println!("{:?}", NumOrIdParser::new().parse("a1_a")); + + println!("{}", NumOrIdParser::new().parse("123").unwrap()); + println!("{}", NumOrIdParser::new().parse("a1_a").unwrap()); } #[test] fn parse_num_or_id() { - // println!("{:?}", NumOrIdParser::new().parse("123")); - // println!("{:?}", NumOrIdParser::new().parse("a1_a")); + assert_eq!(format!("{}", NumOrIdParser::new().parse("123").unwrap()), "123"); + assert_eq!(format!("{}", NumOrIdParser::new().parse("a1_a").unwrap()), "a1_a"); } diff --git a/src/ast/parser.lalrpop b/src/ast/parser.lalrpop index d749efaba2644b25cd0beced5b5b05d3a66e21e5..87f0b32f26126cf26e612b5fc40cdda48d17a881 100644 --- a/src/ast/parser.lalrpop +++ b/src/ast/parser.lalrpop @@ -4,7 +4,7 @@ use crate::ast::*; grammar; -pub NumOrId : NumOrId = { +pub NumOrId: NumOrId = { Num => NumOrId::Num(<>), Id => NumOrId::Id(<>), }