Mutable types
This commit is contained in:
parent
65c2cd521b
commit
90ecde89c9
@ -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>,
|
||||
|
@ -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,10 +1169,13 @@ 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![] }),
|
||||
@ -1179,12 +1184,17 @@ fn a(x) {
|
||||
Record(rc!(Esperanza), 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
|
||||
}
|
||||
)]));
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
@ -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) => {
|
||||
|
Loading…
Reference in New Issue
Block a user