如何在Lua中无需使用LPeg模块对用户输入进行词法分析?

我查看了其他回答(例如安装和使用LPeg),但没有找到可用的解释。我正在使用Lua制作一个非常基本的编程语言(我知道如何做),但只需要针对某些内容(如变量)进行词法分析器。以下是我其中两个函数,并且我在命令(cmd)函数中经常使用string.match:

function screen (action, actionvar)
if action = "clear" then
os.execute("clear")
elseif action = "cls" then
os.execute("cls")
elseif action = "showText" then
print(actionvar)
end
end

function command (cmd)
if string.match(cmd, "screen") then
    if string.match(cmd, "clear") then
        screen(clear)
    elseif string.match(cmd, "cls") then
        screen(cls)
    end
    -- 必须是 showText 或不当的 cmd。
    if string.match(cmd, "showText") then

    else
    print("Error: Unrecognized screen command.")
    return null
    end
end
end
点赞