Skip to content

Instantly share code, notes, and snippets.

@huonw
Last active December 20, 2015 14:19
Show Gist options
  • Select an option

  • Save huonw/6145412 to your computer and use it in GitHub Desktop.

Select an option

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

erickt commented Aug 5, 2013

Copy link
Copy Markdown

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:

match 'foo {
    'foo => {
        match 'bar {
            'bar => match 'baz,
            'baz => match 'end,
        }
    'end => {}
}

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.

@erickt

erickt commented Aug 5, 2013

Copy link
Copy Markdown

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

@huonw

huonw commented Aug 6, 2013

Copy link
Copy Markdown
Author

@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

huonw commented Aug 6, 2013

Copy link
Copy Markdown
Author

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