From 2bb6b324fef3f0fe8850bb973e57ec5d3aa13c6d Mon Sep 17 00:00:00 2001
From: Per Lindgren <per.lindgren@ltu.se>
Date: Mon, 21 Sep 2020 22:07:41 +0200
Subject: [PATCH] move to new syntax, wip6

---
 examples/wip.rs     |  2 +-
 src/ast.rs          | 10 ++++------
 src/check.rs        |  2 +-
 src/grammar.lalrpop | 27 ++++++++-------------------
 tests/test_check.rs |  2 +-
 5 files changed, 15 insertions(+), 28 deletions(-)

diff --git a/examples/wip.rs b/examples/wip.rs
index 9f5ffa5..db7ffed 100644
--- a/examples/wip.rs
+++ b/examples/wip.rs
@@ -7,5 +7,5 @@ fn main() {
 }
 
 fn b(x: i32) -> i32 {
-    x;
+    x
 }
diff --git a/src/ast.rs b/src/ast.rs
index 8ee7334..857e272 100644
--- a/src/ast.rs
+++ b/src/ast.rs
@@ -28,13 +28,11 @@ pub struct Stmts {
 
 #[derive(Debug, PartialEq, Clone)]
 pub enum Stmt {
-    // Id mut? (: Type)? (= Expr)?
     Let(Id, bool, Option<Type>, Option<Box<Expr>>),
     Assign(Box<Expr>, Box<Expr>),
     While(Box<Expr>, Stmts),
     If(Box<Expr>, Stmts, Option<Stmts>),
     Expr(Box<Expr>),
-    // perhaps block should be expression, hmm everything is expressions?
     Block(Stmts),
     Semi,
 }
@@ -216,13 +214,13 @@ impl Stmts {
     fn ifmt(&self, fmt: &mut Formatter, indent: usize) -> Result<(), Error> {
         write!(fmt, "{{")?;
         let len = self.stmts.len();
-        for (_i, s) in (&self.stmts).into_iter().enumerate() {
+        for (i, s) in (&self.stmts).into_iter().enumerate() {
             // set tab
             write!(fmt, "\n{}", " ".repeat(indent + 4))?;
             s.ifmt(fmt, indent + 4)?;
-            // if i < len - 1 || self.ret {
-            //     write!(fmt, ";")?;
-            // }
+            if i < len - 1 || self.ret {
+                write!(fmt, ";")?;
+            }
         }
         write!(fmt, "\n{}}}", " ".repeat(indent))
     }
diff --git a/src/check.rs b/src/check.rs
index 0941d75..8870a42 100644
--- a/src/check.rs
+++ b/src/check.rs
@@ -441,7 +441,7 @@ pub fn check(p: &Program) -> Result<(), Error> {
         var_env.push_param_scope(arg_ty);
 
         let stmt_type = check_stmts(&fd.body, &fn_env, &type_env, &mut var_env);
-        trace!("result {}: {:?}", fd.id, &stmt_type);
+        trace!("result {}: {} {:?}", &fd.id, &fd.result, &stmt_type);
 
         let stmt_type = strip_mut(stmt_type?);
 
diff --git a/src/grammar.lalrpop b/src/grammar.lalrpop
index 3efe062..b978e1f 100644
--- a/src/grammar.lalrpop
+++ b/src/grammar.lalrpop
@@ -15,13 +15,9 @@ match {
 
 // A comma separated sequence with optional trailing comma
 Comma<T>: Vec<T> = { 
-    <v:(<T> ",")*> <e:T?> => match e { 
+    <mut v:(<T> ",")*> <e:T?> => match e { 
         None => v,
-        Some(e) => {
-            let mut v = v;
-            v.push(e);
-            v
-        }
+        Some(e) => { v.push(e); v },
     }
 }
 
@@ -32,16 +28,12 @@ CommaNoTrail<T>: Vec<T> = {
 
 // Parenthesized, comma delimeted
 ParCNT<T>: Vec<T> = {
-    "()" => Vec::new(), // seems like a hack?
+    "()" => Vec::new(),
     "(" <CommaNoTrail<T>> ")" => <>,
 }
 
 pub Path: Path = {
-    <h: (<Id> "::")+> <t: Id> => {
-        let mut h = h;
-        h.push(t);
-        Path(h)
-    }
+    <mut h: (<Id> "::")+> <t: Id> => { h.push(t); Path(h) }
 }
 
 // Type and FnDecl declarations may occur in any order.
@@ -113,10 +105,7 @@ Params : Params = {
 Param : Param = {
     <is_mut: "mut"?> <id: Id> ":" <ty: Type> => 
     Param {
-        is_mut: match is_mut {
-            Some(_) => true,
-            _ => false,
-        },
+        is_mut: is_mut.is_some(),
         id, 
         ty,
     },
@@ -134,14 +123,14 @@ Type : Type = {
 pub Block: Stmts = {
     "{" <mut stmts: StmtSeq*> <st: Stmt?> "}" => {
         match &st {
-            Some(st) => stmts.push(st.clone()),
+            Some(st) => stmts.push(st.clone()), 
             _ =>(),
         };
         Stmts { 
             stmts, 
             ret: match st { 
-                Some(Stmt::Semi) => true, 
-                _ => false
+                Some(_) => false, 
+                _ => true,
             } 
         }
     }
diff --git a/tests/test_check.rs b/tests/test_check.rs
index 57daf9c..ce824c1 100644
--- a/tests/test_check.rs
+++ b/tests/test_check.rs
@@ -177,5 +177,5 @@ fn check_let_if() {
 
 #[test]
 fn wip() {
-    let _ = check(&read_file::parse("examples/wip.rs")).is_ok();
+    let _ = check(&read_file::parse("examples/wip.rs")).unwrap();
 }
-- 
GitLab