gpt4 book ai didi

lua - 为什么 Lua REPL 要求你在前面加上一个等号才能得到一个值?

转载 作者:行者123 更新时间:2023-12-04 03:27:47 24 4
gpt4 key购买 nike

如果可能,我需要帮助从交互模式关闭此功能,否则我会发疯。如果你想要这个值,REPL 会在每个表达式之前坚持一个等号。我觉得这非常令人恼火和不直观。更糟糕的是,如果你错误地忘记了等号,它会将你带到这个辅助提示,只能通过
键入会导致错误的表达式。

*** str="This is some string"
*** str
>>
>>
>> =
>>
>> =str
stdin:6: unexpected symbol near '='
*** =str
This is some string
*** #str
stdin:1: unexpected symbol near '#'
*** =#str
19
***
*** 545+8
stdin:1: unexpected symbol near '545'
*** =545+8
553
***

我需要一个使用 REPL 的类(class):
有没有办法去掉等号,使其表现得像其他 REPL?
您如何在不执行我所做的操作的情况下退出辅助提示?

最佳答案

您输入的所有内容 standalone Lua被视为语句,而不是表达式。对语句进行评估,并将其结果(如果有)打印到终端。这就是为什么您需要预先添加 = (实际上是 return 的简写)到您作为示例给出的表达式,以使它们正确显示而不会出错。

您看到的“辅助提示”是您输入不完整语句时发生的情况。

In interactive mode, if you write an incomplete statement, the interpreter waits for its completion by issuing a different prompt.



您通过完成该语句退出它。

但是,制作自己想要的 REPL 并不太难。当然,您无法以这种方式从不完整的块中逐步构建语句,但也许您不需要那样做。
local function print_results(...)
-- This function takes care of nils at the end of results and such.
if select('#', ...) > 1 then
print(select(2, ...))
end
end

repeat -- REPL
io.write'> '
io.stdout:flush()
local s = io.read()
if s == 'exit' then break end

local f, err = load(s, 'stdin')
if err then -- Maybe it's an expression.
-- This is a bad hack, but it might work well enough.
f = load('return (' .. s .. ')', 'stdin')
end

if f then
print_results(pcall(f))
else
print(err)
end
until false

关于lua - 为什么 Lua REPL 要求你在前面加上一个等号才能得到一个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20410082/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com