在 Rebol3 中评估代码块
Evaluating code blocks in Rebol3
我正在尝试通过使起始位置随机化来改进 the Sliding Tile Puzzle example。
有更好的方法来做到这一点--"It is considered bad practice to convert values to strings and join them together to pass to do for evaluation."--但我采用的方法是尝试生成 Rebol3 源代码,然后对其求值。我认为它可以正确生成:
random/seed now
arr: random collect [ repeat tilenum 9 [ keep tilenum ] ]
hgroup-data: copy {}
repeat pos 9 [
curtile: (pick arr pos)
append hgroup-data either curtile = 9
[ reduce "x: box tilesize gameback " ]
[ rejoin [ { p "} curtile {" } ] ]
if all [(pos // 3) = 0 pos != 9] [ append hgroup-data " return^/" ]
]
print hgroup-data
...输出类似:
p "4" x: box tilesize gameback p "5" return
p "3" p "7" p "1" return
p "2" p "8" p "6"
...如果我随后将其复制并粘贴到此部分,则可以正常工作:
view/options [
hgroup [
PASTE-HERE
]
] [bg-color: gameback]
但是,如果我尝试动态执行:
view/options [
hgroup [
hgroup-data
]
] [bg-color: gameback]
...(还有 print hgroup-data
、do hgroup-data
和 load hgroup-data
),我收到此错误:
** GUI ERROR: Cannot parse the GUI dialect at: hgroup-data
...(或 at: print hgroup-data
,等等,具体取决于我尝试的变体。)
如果我尝试 load [ hgroup-data ]
我得到:
** Script error: extend-face does not allow none! for its face argument
** Where: either if forever -apply- apply init-layout make-layout actor all foreach do-actor unless -apply- apply all build-face -apply- apply init-layout make-layout actor all foreach do-actor if build-face -apply- apply init-layout make-layout actor all foreach do-actor unless make-face -apply- apply case view do either either either -apply-
** Near: either all [
word? act: dial/1
block? body: get dial...
但是,如果我使用语法 hgroup do [ hgroup-data ]
,程序会运行,但没有按钮:它似乎在某种程度上被高估了,因此函数的 return 值 p
和 box
等直接放入 hgroup
作为代码。
我肯定在这里漏掉了一个简单的语法错误。这是什么?
首先,我认为最好直接构造一个块,而不是构造一个字符串并将其转换为块。但如果你真的想这样做,这应该可以解决问题:
view/options compose/only [
hgroup (load hgroup-data)
] [bg-color: gameback]
我正在尝试通过使起始位置随机化来改进 the Sliding Tile Puzzle example。
有更好的方法来做到这一点--"It is considered bad practice to convert values to strings and join them together to pass to do for evaluation."--但我采用的方法是尝试生成 Rebol3 源代码,然后对其求值。我认为它可以正确生成:
random/seed now
arr: random collect [ repeat tilenum 9 [ keep tilenum ] ]
hgroup-data: copy {}
repeat pos 9 [
curtile: (pick arr pos)
append hgroup-data either curtile = 9
[ reduce "x: box tilesize gameback " ]
[ rejoin [ { p "} curtile {" } ] ]
if all [(pos // 3) = 0 pos != 9] [ append hgroup-data " return^/" ]
]
print hgroup-data
...输出类似:
p "4" x: box tilesize gameback p "5" return
p "3" p "7" p "1" return
p "2" p "8" p "6"
...如果我随后将其复制并粘贴到此部分,则可以正常工作:
view/options [
hgroup [
PASTE-HERE
]
] [bg-color: gameback]
但是,如果我尝试动态执行:
view/options [
hgroup [
hgroup-data
]
] [bg-color: gameback]
...(还有 print hgroup-data
、do hgroup-data
和 load hgroup-data
),我收到此错误:
** GUI ERROR: Cannot parse the GUI dialect at: hgroup-data
...(或 at: print hgroup-data
,等等,具体取决于我尝试的变体。)
如果我尝试 load [ hgroup-data ]
我得到:
** Script error: extend-face does not allow none! for its face argument
** Where: either if forever -apply- apply init-layout make-layout actor all foreach do-actor unless -apply- apply all build-face -apply- apply init-layout make-layout actor all foreach do-actor if build-face -apply- apply init-layout make-layout actor all foreach do-actor unless make-face -apply- apply case view do either either either -apply-
** Near: either all [
word? act: dial/1
block? body: get dial...
但是,如果我使用语法 hgroup do [ hgroup-data ]
,程序会运行,但没有按钮:它似乎在某种程度上被高估了,因此函数的 return 值 p
和 box
等直接放入 hgroup
作为代码。
我肯定在这里漏掉了一个简单的语法错误。这是什么?
首先,我认为最好直接构造一个块,而不是构造一个字符串并将其转换为块。但如果你真的想这样做,这应该可以解决问题:
view/options compose/only [
hgroup (load hgroup-data)
] [bg-color: gameback]