Mutable types

This commit is contained in:
greg 2018-07-12 02:07:52 -07:00
parent 65c2cd521b
commit 90ecde89c9
4 changed files with 29 additions and 15 deletions

View File

@ -21,7 +21,11 @@ pub type FormalParam = (ParamName, Option<TypeName>);
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<String>, Rc<String>), //should have TypeSingletonName in it, or maybe just String, not sure
Binding {
name: Rc<String>,

View File

@ -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<a>) | 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<a> = 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
}
)]));
}

View File

@ -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)
}
}

View File

@ -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) => {