Re: Re: pattern matching on fields of the argument
Daniel Bonniot <[email protected]> Thu, 05 Aug 2004 11:27:12 +0200
| Newsgroups | gmane.comp.lang.nice.devel |
|---|---|
| Message-ID | <[email protected]> |
> class Expr { ... }
> class Apply { ?Expr e1; ?Expr e2; }
> class Lambda { String id; ... }
>
> foo(?Expr)
> foo(Apply(e1:Lambda(id:"foo"), e2:null))
>
> This would require two null-checks, two instanceof checks, an ==
> check, and an .equals() check.
Not that many, I think you could get by with:
foo(Apply a) = if (a.e1 is Lambda && a.e1.id.equals("foo") && a.e2 == null)
It's still a one liner (introducing 'is' for 'instanceof'), but I agree your
syntax is much more compact.
> Ideally I'd like most of these
> functions to be one-liners so they appear like a table when viewed in
> a text editor...
>
> What would you say is the most difficult challenge in implementing
> something like this?
Probably getting acquainted with the internals of the compiler :-)
Here is a rough plan:
1) the pattern() production in src/bossa/parser/Parser.jj needs to be extended
to accept deep patterns. Luckily I think your syntax matches exactly arguments
for a method call, so you can just call the arguments() production at the
right place. Store that expression representing the additional condition in
the pattern.
Then most of your job should be in the src/bossa/syntax/Pattern.java class. It
might be a good idea to create a subclass of Pattern for the case of deep
patterns.
2) during resolution, you can process the content of the arguments, and relate
them to fields being tested.
3) you need to provide an ordering with the leq method.
4) during code generation (method matchTest), you need to generate the code
that actually tests if the condition is satisfied.
That should be it. Does that help you get started? I encourage you to take the
steps one by one, and to write test cases along the way to mesure and
consilodate your progress. You should create a new file for those tests:
testsuite/compiler/methods/deepDispatch.testsuite
You should really not hesitate to ask any question, that should save a lot of
time. If you wish, we can set up a virtual meeting, to avoid one-day lags in
communication :-)
Cheers,
Daniel
-------------------------------------------------------
This SF.Net email is sponsored by OSTG. Have you noticed the changes on
Linux.com, ITManagersJournal and NewsForge in the past few weeks? Now,
one more big change to announce. We are now OSTG- Open Source Technology
Group. Come see the changes on the new OSTG site. www.ostg.com