首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Io语言解释器和文件执行之间的不同行为

Io语言解释器和文件执行之间的不同行为
EN

Stack Overflow用户
提问于 2017-10-15 20:36:12
回答 1查看 82关注 0票数 1

我正在尝试用Io语言创建一个新的操作符,但是当我用解释器检查我的工作与文件执行时,我注意到了不同的行为:

代码语言:javascript
运行
复制
## Executing nand.io
##########################
OperatorTable addOperator("nand", 10)
true nand := method(bool, if(bool, false, true))
false nand := method(bool, true)

true nand false println        # => false
true nand true println         # => true
false nand false println       # => false
false nand true println        # => true

# Why does this print 'true' (it should be 'false')?
(true nand true) println      # => true

# I noticed that the argument to nand is always printed
true nand "not" println        # => not
true nand "what" println       # => what
false nand "I" println         # => I
false nand "expected" println  # => expected

# Another sanity check. I thought this might have to do with println behaving
# oddly, but this conditional really does return true
if(true nand true, "This should not print" println, "This should print" println)
# => This should not print

# More unexpected behavior
false nand := method(arg, "foo")
false nand "bar" println       # => "bar"

true nand := method(arg, "foo")
true nand "bar" println        # => "bar"

false nand := "Why is this not returned?"
false nand "aaaaahhhh" println # => "aaaaahhhh"



## Ran in the io interpreter
###############################
OperatorTable addOperator("nand", 10)
true nand := method(bool, if(bool, false, true))
false nand := method(bool, true)

# Same as file execution when sent to println
true nand false println   # => false
true nand true println    # => true
false nand false println  # => false
false nand true println   # => true

# It acually does what's expected here
true nand false           # => true
true nand true            # => false    -- It works here!!!
false nand false          # => true
false nand true           # => true

if(true nand true, "This should not print" println, "This should print" println)
# => This should print

也许这需要编译还是解释?这实际上是预期的行为吗?也许我忽略了语言的一些基本内容(我今天才开始学习Io )?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-15 23:51:31

好的,这与VM解析操作符的方式有关。该文件的第一次传递将遍历并重写它已经知道的所有运算符,根据所提供的优先数重写消息。然后,第二次传递将执行修改后的消息树。因此,当您在稍后通过VM传递的文件中向运算符表中添加一个新操作符时,您的新操作符将仅适用于在此之后加载的任何文件或模块。对于从该文件生成的消息树的生命周期,您的操作符不存在。

基本上,这里的经验法则是维护一个单独的operators.io文件,并且您的主io文件是分开的;然后确保在main.io之前加载operators.io,您就可以设置好了。

CLI没有出现此问题的原因是它逐个对消息进行评估。也就是说,您输入的下一条消息将有新的运算符st。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46759732

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档