简体   繁体   English

困难的boo语法宏

[英]Difficult boo syntactic macro

I'm creating a DSL for an extensible card game engine I'm working on, with boo. 我正在使用boo为正在使用的可扩展纸牌游戏引擎创建DSL。

I have a card macro that creates a class for a new type of card, and initializes some properties in the constructor. 我有一个card宏,它为一种新型的card创建一个类,并在构造函数中初始化一些属性。 That macro has a few submacros for setting other things. 该宏具有一些用于设置其他内容的子宏。 Basically I want it to turn something like this: 基本上我希望它变成这样的东西:

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

into this: 到这个:

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

    class EffectA (Effect):
        // effectdefintion

The effect definitely needs to be a class, because it will be passed around (it's a Strategy pattern). 效果肯定需要是一个类,因为它会被传递(这是一种策略模式)。

So far, I have this simple skeleton: 到目前为止,我有这个简单的框架:

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

Now, I don't know what should I do with card.Body to make the ability macro add code to the constructor while also generating a nested class. 现在,我不知道该如何使用card.Body来使能力宏将代码添加到构造函数中,同时还要生成一个嵌套类。 Any thoughts? 有什么想法吗? Can this be done with current language capabilities? 可以使用当前的语言功能吗?

It can be done. 可以办到。 Here's how: 这是如何做:

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)()) |] 

Credit goes to Cedric Vivier for helping me out in the boo Google group . 感谢Cedric Vivier协助我加入Google boo小组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM