This is the mail archive of the
kawa@sourceware.org
mailing list for the Kawa project.
Re: Google Summer of Code
- From: Andrea Bernardini <andrebask at gmail dot com>
- To: kawa at sourceware dot org
- Date: Thu, 20 Mar 2014 20:48:56 +0100
- Subject: Re: Google Summer of Code
- Authentication-results: sourceware.org; auth=none
- References: <20140305094242 dot 349492d0 at debian-andrebask> <5316F956 dot 8040303 at gmail dot com> <20140305132208 dot 3081f8dc at debian-andrebask> <m2lhwo48vi dot fsf at gmail dot com> <5319088C dot 5060401 at bothner dot com> <7B118191-4144-4937-B679-89392D5463EE at theptrgroup dot com> <20140316012026 dot 4e0ebfef at debian-andrebask> <53269580 dot 8040205 at bothner dot com>
Il giorno Sun, 16 Mar 2014 23:26:08 -0700
Per Bothner <per@bothner.com> ha scritto:
> > I created a Case class in kawa.lang. It is an abstract class, so we
> > can generate a specialized matchKey method for each type of key
> > (String, Enums, etc.):
>
> That doesn't sound right. How would you decide which kind of key you
> have until you expand it? In general you may not know until you do
> type-inference/-propagation, which happens in the (awkwardly-named)
> InlineCalls phase.
Ok, so now I'm wondering if it is possible to use the same concepts
described in "Rapid case dispatch in Scheme" in the compile method of
CaseExp. That is, to use dispatch on the type and index/clause mapping
in Java to generate optimized bytecode.
>
> > Then we need a CaseExp in gnu.expr to perform the actual compilation
> > using gnu.bytecode.SwitchState:
>
> Right - though an alternative may be to do similar to what we do
> for TypeSwitch: Represent each case branch as a LambdaExp - which
> then gets inlined. This avoids creating a new Expression type, but
> it is more complicated and slightly harder to understand. However,
> the TypeSwitch approach may mesh better with the pattern-matching
> framework I'm working on.
>
> Perhaps best to focus on implementing a CaseExpr class; we can look
> at the TypeSwitch-like LambdaExp approach later.
I agree.
>
> > An other thing I didn't understand yet is where and how this two
> > classes should be used in the rest of the code.
>
> You don't actually need a Case Syntax class. You can create the
> CaseExp object directly using Scheme code. Look at how try-finally
> (in syntax,scm) creates a TryExp, or how if (in prim_syntax.scm)
> creates an IfExp.
I came to this:
>public class CaseExp extends Expression {
>
> public CaseExp(Expression key, Expression clauses) {
> System.out.print("Case Expression\n");
> }
>
> public CaseExp(Expression key, Expression clauses,
> Expression elseClause , Boolean isLambdaForm) {
> System.out.print("Case Expression with else
> clause"+...));
> }
>(define-rewrite-syntax case
> (lambda (x)
> (syntax-case x (else =>)
> ((_ case-key case-clause ... (else => case-else-lambda))
> (make <it.polimi.kawacase.CaseExp>
> (syntax->expression (syntax case-key))
> (syntax->expression (syntax (case-clause ...)))
> (syntax->expression (syntax case-else-lambda))
> #t))
> ((_ case-key case-clause ... (else case-else))
> (make <it.polimi.kawacase.CaseExp>
> (syntax->expression (syntax case-key))
> (syntax->expression (syntax (case-clause ...)))
> (syntax->expression (syntax case-else))
> #f))
> ((_ case-key case-clause ...)
> (make <it.polimi.kawacase.CaseExp>
> (syntax->expression (syntax case-key))
> (syntax->expression (syntax (case-clause ...))))))))
So if i compile this sample code:
>(case 'sym
> ((sym) (display 1))
> ((boo) (display 2))
> (else => (lambda (x)
> (display 3))))
Kawa prints:
> Case Expression with else clause in lambda form
Am I on the right track? Now I have to study how to generate the
bytecode, obviously at the current state the generated code does
nothing.
Thank you very much for all your answers.
Andrea