将断言子句与 where 子句组合

Combine asserting clauses with where clauses

我注意到,当我有多个 where 子句和多个 asserting 子句时,将它们组合在一起时经常会出现语法错误。错误将显示 "unexpected token after where"。我应该怎么写才能避免这个错误?这是最新的例子:

尝试 1:

def(class game_state game) ->commands [
        set(attack, cost),
        set(life, cost),
        set(abilities, []),
    ] where cost=(card.cost + (card.loyalty_cost * size(card.school)))
      where card=crypt_spells[choices[0]]
//      asserting size(choices) = 1 //FIXME: syntax error uncommented
        asserting choices != null
      where crypt_spells=filter(game.crypt.cards_of(player), value.type='spell')
      where player=game.player_obj

我也尝试用不同的方式重写它,这也导致了 "unexpected token after where" 语法错误。

尝试 2:

def(class game_state game) ->commands [
        set(attack, cost),
        set(life, cost),
        set(abilities, []),
    ] where cost=(card.cost + (card.loyalty_cost * size(card.school)))
      where card=crypt_spells[choices[0]]
      asserting choices != null, size(choices)=1 | choices //FIXME: syntax error
      where crypt_spells=filter(game.crypt.cards_of(player), value.type='spell')
      where player=game.player_obj

示例 3:

这是我认为突出问题的另一个例子:

def(class game_state game, class message.play_card info) ->commands
  if(info.choices,
    ([ /* do stuff */ ]
      where entry=game.crypt.get_entry(info.choices[0])
      asserting size(info.targets)=1 | info.targets    /* PARSE ERROR! */
    ) asserting size(info.choices)=1 | info.choices,   /* WORKS */
    [ /* else */ ]
  )

从这个例子可以看出,asserting a=b 工作正常,除非它前面有一个 where 子句。

让我尝试将您的代码转换为通用调试控制台代码:

f()
where f = def
        () -> commands [
                debug(['something']),
                debug(['more stuff']),
                debug(['yet more stuff']),
        ]
        where aaa = (0 + (1 * 2))
        where bbb = 4
//        asserting 2 + 2 = 4 // FIXME: syntax error uncommented
        asserting '000' != null
        where ccc = [0, 1, 2, 3]
        where ddd = {'0': 0, '1': 1, }

调试控制台可以执行:

(debug console):0: ['something']
(debug console):0: ['more stuff']
(debug console):0: ['yet more stuff']
[(Command Object: N10game_logic12_GLOBAL__N_113debug_commandE),
 (Command Object: N10game_logic12_GLOBAL__N_113debug_commandE),
 (Command Object: N10game_logic12_GLOBAL__N_113debug_commandE)]

让我们检查您的语法错误是否仍然存在,或者至少在取消注释断言时存在一些语法错误:

f()
where f = def
        () -> commands [
                debug(['something']),
                debug(['more stuff']),
                debug(['yet more stuff']),
        ]
        where aaa = (0 + (1 * 2))
        where bbb = 4
        asserting 2 + 2 = 4 // FIXME: syntax error uncommented
        asserting '000' != null
        where ccc = [0, 1, 2, 3]
        where ddd = {'0': 0, '1': 1, }

调试控制台无法执行:

error parsing formula: formula.cpp:3942 ASSERTION FAILED: Unexpected tokens after
where
At (debug console) 0:
... where bbb = 4       asserting 2 + 2 = 4 // FIXME syntax error uncommen...
                                        ^

引擎使用简单的解析,目前whereasserting=(甚至,?)可以以直观的方式组合,但不是很好由解析器管理。

在这里,我认为它在到达 , 之前为 where 找到了辅助 =,但我不知道。现在我不知道完成 where 解析的标准是什么。您只需调试解析器就会知道。

这是一个重要问题,但可能并不严重,现在必须解决,因为大多数情况下可以通过包括更多的 parens 详尽性来解决。

让我们只放置一对括号,让我们从失败的断言开始:

f()
where f = def
        () -> commands [
                debug(['something']),
                debug(['more stuff']),
                debug(['yet more stuff']),
        ]
        where aaa = (0 + (1 * 2))
        where bbb = 4
        asserting (2 + 2 = 4) // FIXME: syntax error uncommented
        asserting '000' != null
        where ccc = [0, 1, 2, 3]
        where ddd = {'0': 0, '1': 1, }

调试控制台可以再次执行:

(debug console):0: ['something']
(debug console):0: ['more stuff']
(debug console):0: ['yet more stuff']
[(Command Object: N10game_logic12_GLOBAL__N_113debug_commandE),
 (Command Object: N10game_logic12_GLOBAL__N_113debug_commandE),
 (Command Object: N10game_logic12_GLOBAL__N_113debug_commandE)]

2018年5月27日添加了错误定位