Skip to content

Instantly share code, notes, and snippets.

@huonw
Last active December 20, 2015 14:19
Show Gist options
  • Save huonw/6145412 to your computer and use it in GitHub Desktop.
Save huonw/6145412 to your computer and use it in GitHub Desktop.
match 'foo {
'foo => {
if some_thing {
match 'bar;
} else if other_thing {
match 'baz;
}
}
'bar => { blah(); match 'baz; }
'baz => { blahblah(); match 'foo; }
}
match 'foo {
'foo => {
match if some_thing {
'bar
} else if other_thing {
'baz
} else {
'end
}
}
'bar => { blah(); match 'baz; }
'baz => { blahblah(); match 'foo; }
'end => {}
}
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
Copy link

erickt commented Aug 5, 2013

@huonw: Well we don't need to worry about the proto! macro anymore, it just got removed.

@huonw
Copy link
Author

huonw commented Aug 6, 2013

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

@huonw
Copy link
Author

huonw commented Aug 6, 2013

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