Last active
December 20, 2015 14:19
-
-
Save huonw/6145412 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
match 'foo { | |
'foo => { | |
if some_thing { | |
match 'bar; | |
} else if other_thing { | |
match 'baz; | |
} | |
} | |
'bar => { blah(); match 'baz; } | |
'baz => { blahblah(); match 'foo; } | |
} |
This file contains hidden or 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
match 'foo { | |
'foo => { | |
match if some_thing { | |
'bar | |
} else if other_thing { | |
'baz | |
} else { | |
'end | |
} | |
} | |
'bar => { blah(); match 'baz; } | |
'baz => { blahblah(); match 'foo; } | |
'end => {} | |
} |
This file contains hidden or 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
enum Matcher { Foo, Bar(int), Baz(float) } | |
match xyz { | |
Foo => { | |
if some_thing { | |
match Bar(10); | |
} else if other_thing { | |
match Baz(10000.0); | |
} | |
} | |
Bar(n) => { blah(n); match Baz(1.2); } | |
Baz(f) => { blahblah(f); match Foo; } | |
} |
@erickt, oh, sorry, I though I replied.
I think that something like
'outer: match xyz {
Foo => match abc {
Bar => match 'outer: Foo;
}
}
might work?
In any case, I feel that pattern_matching.rs
is the most flexible, e.g. it automatically gives "computed goto", and means that the state machine can be easily breakable/resumable.
There are probably concerns about lifetimes and so on that need to be considered carefully. (I guess that it could be restricted to only types without lifetimes in match JumpTo(x);
if it gets too hard to be correct otherwise.)
A basic sketch of what this might look like when used for a yield state machine compiler. (Probably unworkable... it somehow has to handle the loop
properly.)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is great.
One limitation I see with
pattern_matching.rs
though is I'm not sure how we can represent jumping out of a multilevel state machine. For example:Also, how do you think this interacts with the std::pipes / proto! macro? It's also building a state machine, although it's not general purpose.