From 90ecde89c9cb14408513129354700c2b14fa82d1 Mon Sep 17 00:00:00 2001 From: greg Date: Thu, 12 Jul 2018 02:07:52 -0700 Subject: [PATCH] Mutable types --- schala-lang/src/ast.rs | 6 +++++- schala-lang/src/parsing.rs | 34 +++++++++++++++++++++------------ schala-lang/src/reduced_ast.rs | 2 +- schala-lang/src/symbol_table.rs | 2 +- 4 files changed, 29 insertions(+), 15 deletions(-) diff --git a/schala-lang/src/ast.rs b/schala-lang/src/ast.rs index 315a216..191997c 100644 --- a/schala-lang/src/ast.rs +++ b/schala-lang/src/ast.rs @@ -21,7 +21,11 @@ pub type FormalParam = (ParamName, Option); pub enum Declaration { FuncSig(Signature), FuncDecl(Signature, Block), - TypeDecl(TypeSingletonName, TypeBody), //should have TypeSingletonName in it + TypeDecl { + name: TypeSingletonName, + body: TypeBody, + mutable: bool + }, TypeAlias(Rc, Rc), //should have TypeSingletonName in it, or maybe just String, not sure Binding { name: Rc, diff --git a/schala-lang/src/parsing.rs b/schala-lang/src/parsing.rs index bdb3762..6a2546f 100644 --- a/schala-lang/src/parsing.rs +++ b/schala-lang/src/parsing.rs @@ -316,14 +316,16 @@ impl Parser { if let Keyword(Alias) = self.peek() { self.type_alias() } else { - if let Keyword(Mut) = self.peek() { + let mutable = if let Keyword(Mut) = self.peek() { self.next(); - //TODO make this part of the type - } + true + } else { + false + }; let name = self.type_singleton_name()?; expect!(self, Operator(ref c) if **c == "="); let body = self.type_body()?; - Ok(Declaration::TypeDecl(name, body)) + Ok(Declaration::TypeDecl { name, body, mutable}) } }); @@ -1167,24 +1169,32 @@ fn a(x) { #[test] fn parsing_types() { - parse_test!("type Yolo = Yolo", AST(vec![Declaration(TypeDecl(tys!("Yolo"), TypeBody(vec![UnitStruct(rc!(Yolo))])))])); + parse_test!("type Yolo = Yolo", AST(vec![Declaration(TypeDecl { name: tys!("Yolo"), body: TypeBody(vec![UnitStruct(rc!(Yolo))]), mutable: false} )])); + parse_test!("type mut Yolo = Yolo", AST(vec![Declaration(TypeDecl { name: tys!("Yolo"), body: TypeBody(vec![UnitStruct(rc!(Yolo))]), mutable: true} )])); parse_test!("type alias Sex = Drugs", AST(vec![Declaration(TypeAlias(rc!(Sex), rc!(Drugs)))])); parse_test!("type Sanchez = Miguel | Alejandro(Int, Option) | Esperanza { a: Int, b: String }", - AST(vec![Declaration(TypeDecl(tys!("Sanchez"), TypeBody(vec![ + AST(vec![Declaration(TypeDecl{ + name: tys!("Sanchez"), + body: TypeBody(vec![ UnitStruct(rc!(Miguel)), TupleStruct(rc!(Alejandro), vec![ Singleton(TypeSingletonName { name: rc!(Int), params: vec![] }), Singleton(TypeSingletonName { name: rc!(Option), params: vec![Singleton(TypeSingletonName { name: rc!(a), params: vec![] })] }), ]), Record(rc!(Esperanza), vec![ - (rc!(a), Singleton(TypeSingletonName { name: rc!(Int), params: vec![] })), - (rc!(b), Singleton(TypeSingletonName { name: rc!(String), params: vec![] })), - ])])))])); + (rc!(a), Singleton(TypeSingletonName { name: rc!(Int), params: vec![] })), + (rc!(b), Singleton(TypeSingletonName { name: rc!(String), params: vec![] })), + ]) + ]), + mutable: false + })])); parse_test!("type Jorge = Diego | Kike(a)", AST(vec![ - Declaration(TypeDecl( - TypeSingletonName { name: rc!(Jorge), params: vec![Singleton(TypeSingletonName { name: rc!(a), params: vec![] })] }, - TypeBody(vec![UnitStruct(rc!(Diego)), TupleStruct(rc!(Kike), vec![Singleton(TypeSingletonName { name: rc!(a), params: vec![] })])])) + Declaration(TypeDecl{ + name: TypeSingletonName { name: rc!(Jorge), params: vec![Singleton(TypeSingletonName { name: rc!(a), params: vec![] })] }, + body: TypeBody(vec![UnitStruct(rc!(Diego)), TupleStruct(rc!(Kike), vec![Singleton(TypeSingletonName { name: rc!(a), params: vec![] })])]), + mutable: false + } )])); } diff --git a/schala-lang/src/reduced_ast.rs b/schala-lang/src/reduced_ast.rs index 854fb36..5e970b8 100644 --- a/schala-lang/src/reduced_ast.rs +++ b/schala-lang/src/reduced_ast.rs @@ -156,7 +156,7 @@ impl Declaration { body: statements.iter().map(|stmt| stmt.reduce(symbol_table)).collect(), } }, - TypeDecl(_,_) => Stmt::Noop, + TypeDecl { .. } => Stmt::Noop, _ => Stmt::Expr(Expr::UnimplementedSigilValue) } } diff --git a/schala-lang/src/symbol_table.rs b/schala-lang/src/symbol_table.rs index 431f351..d9ebd56 100644 --- a/schala-lang/src/symbol_table.rs +++ b/schala-lang/src/symbol_table.rs @@ -79,7 +79,7 @@ impl SymbolTable { Symbol { name: signature.name.clone(), spec } ); }, - TypeDecl(TypeSingletonName { name, params}, TypeBody(variants)) => { + TypeDecl { name: TypeSingletonName { name, params}, body: TypeBody(variants), mutable } => { for var in variants { match var { Variant::UnitStruct(variant_name) => {