-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from lambdaclass/syntax-union-enum
Union & enum syntax
- Loading branch information
Showing
10 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
use crate::{ | ||
common::{GenericParam, Ident, Span}, | ||
expressions::Expression, | ||
structs::Field, | ||
}; | ||
|
||
#[derive(Clone, Debug, Eq, PartialEq)] | ||
pub struct UnionDecl { | ||
pub name: Ident, | ||
pub generics: Vec<GenericParam>, | ||
pub variants: Vec<Field>, | ||
pub span: Span, | ||
} | ||
|
||
#[derive(Clone, Debug, Eq, PartialEq)] | ||
pub struct EnumDecl { | ||
pub name: Ident, | ||
pub generics: Vec<GenericParam>, | ||
pub variants: Vec<EnumVariant>, | ||
pub span: Span, | ||
} | ||
|
||
#[derive(Clone, Debug, Eq, PartialEq)] | ||
pub struct EnumVariant { | ||
pub name: Ident, | ||
pub fields: Vec<Field>, | ||
pub discriminant: Option<Expression>, | ||
pub span: Span, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
mod EnumExample { | ||
enum Foo { | ||
Bar, | ||
Baz { n1: i32, n2: i32 }, | ||
Qux = 3, | ||
} | ||
|
||
fn main() -> i32 { | ||
let mut foo: Foo = Foo.Bar; | ||
|
||
match foo { | ||
Foo.Bar -> return 1;, | ||
Foo.Baz -> return 2;, | ||
Foo.Qux -> return 3;, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
mod UnionExample { | ||
union Foo { | ||
bar: i32, | ||
baz: i64, | ||
} | ||
|
||
fn main() -> i32 { | ||
let mut foo: Foo = Foo { bar: 1 }; | ||
|
||
// unsafe! | ||
let bar: i32 = foo.bar; | ||
} | ||
} |