将断言子句与 where 子句组合在一起



我注意到,当我有多个where子句和多个asserting子句时,当我将它们组合在一起时,我经常会遇到语法错误。错误将显示"意外令牌在何处之后"。我应该如何编写它以避免此错误?以下是最新示例:

尝试 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

我还尝试以不同的方式重写它,这也导致了"在何处之后出现意外令牌"语法错误。

尝试 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 */ ]
)

从此示例中可以看出,除非前面有where子句,否则asserting a=b工作正常。

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

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解析的标准是什么。您只需调试解析器即可知道。

这是一个重要的问题,但可能并不重要,必须立即解决,因为主要可以通过包含更多括号的详尽性来管理。

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

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 日添加了错误精确定位。

最新更新