From e107b53f576e49eae98c0b3c65d74115325fde2c Mon Sep 17 00:00:00 2001
From: Per Lindgren <per.lindgren@ltu.se>
Date: Thu, 3 Sep 2020 16:46:21 +0200
Subject: [PATCH] display for ast

---
 CHANGELOG.md           |  3 +++
 src/ast/ast.rs         | 17 ++++++++++++++++-
 src/ast/main.rs        |  7 +++++--
 src/ast/parser.lalrpop |  2 +-
 4 files changed, 25 insertions(+), 4 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 10e01a6..425d840 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 e79e70d..5041731 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 2a0f662..998eed7 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 d749efa..87f0b32 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(<>),
 }
-- 
GitLab