Difficult syntax macro

I am creating a DSL for an extensible card game engine I'm working on using boo.

I have a card macro that creates a class for a new card type and initializes some properties in the constructor. This macro has several sub-macros to configure other things. Basically, I want this to work out like this:

card 'A new card':
    type TypeA
    ability EffectA:
        // effect definition

in it:

class ANewCard (Card):
    def constructor():
        Name = "A new card"
        Type = Types.TypeA
        AddEffect(EffectA())

    class EffectA (Effect):
        // effectdefintion

An effect should definitely be a class because it will be passed (this is a strategy template).

So far I have had this simple skeleton:

macro card:
    yield [|
        class $(ReferenceExpression(card.Arguments[0])) (Card):
            def constructor():
                Name = $(card.Arguments[0])
    |]

Now I don’t know what to do with card.Body to make the macro able to add code to the constructor and also create a nested class. Any suggestions? Can this be done using current language features?

+5
1

. :

import Boo.Lang.Compiler.Ast 
import Boo.Lang.PatternMatching 

macro card(name as string): 
    klass = [| 
        class $(ReferenceExpression(name)): 
            def constructor(): 
                Name = $name
    |] 
    klass.Members.Add(card["effect"]) 
    klass.GetConstructor(0).Body.Add(card["effect-ctor"] as Expression) 
    yield klass 

macro effect(eff as ReferenceExpression): 
    card["effect"] = [| 
        class $eff (Effect): 
            pass 
    |] 
    card["effect-ctor"] = [| Effects.Add($(eff)()) |] 

Cedric Vivier , boo Google.

+6

All Articles