Lua <eof> 预计在 'print' 附近

Lua <eof> expected near 'print'

我在 lua 中遇到问题。

问题一: 每次我 运行 以下程序时,控制台都会告诉我:

'end' expected (to close 'function' at line 1) near 'local'

请注意我在所有大写字母中标记错误详细信息的地方

function m11()
    local inst = mc.mcGetInstance() -- controller instance
    local gcodeLineNbr = mc.mcCntlGetGcodeLineNbr(inst) -- get current Gcode line number
    local gcodeLineStr = mc.mcCntlGetGcodeLine(inst, gcodeLineNbr)  -- get current Gcode line


    function valFromLine(string, axis)
        local startPoint = string.find(string, axis) + 1
        local outputVal = ""
        local isNum = true
        while isNum do
            local num = string.sub(string, startPoint, startPoint)
            startPoint = startPoint + 1
            if num ~= " " then
                outputVal = outputVal .. num
            else
                isNum = false
            end
        end
        outputVal = tonumber(outputVal)
    end
    return outputVal

    --COMPILER HIGHLIGHTS FOLLOWING LINE AS LOCATION OF ERROR
    local gcodeLocX = valFromLine(gcodeLineStr, "X")
    local curLocX = mc.mcAxisGetPos(inst, 0)        -- get current X axis value
    local curLocY = mc.mcAxisGetPos(inst, 1)        -- get current Y axis value
    local curLocZ = mc.mcAxisGetPos(inst, 2)        -- get current Z axis value
    local curAngB = mc.mcAxisGetPos(inst, 4)        -- get current C axis value
    local curAngC = mc.mcAxisGetPos(inst, 5)        -- get current C axis value
    local toolOffset = mc.mcCntlGetToolOffset(inst, 2)  -- get tool offset for axis Z

    function rotateToolB()
        local comHypot = toolOffset * math.sin(angle)       -- get XY planar dist from C pivot to tool centre point
        local compDestinX = comHypot * math.sin(math.rad(90) - curAxisC + curLocX
    end
end
--END OF M11() FUNCTION SHOULD BE HERE

if (mc.mcInEditor() == 1) then
    m11()
end

我不明白为什么它期望 m11() 这么早关闭。

问题二: 我在一个完全独立的文件中重写了 valFromLine(),我得到:

'eof' expected near 'print'

function valFromLine(string, axis)
    local startPoint = string.find(string, axis) + 1
    local outputVal = ""
    local isNum = true
    while isNum do
        local num = string.sub(string, startPoint, startPoint)
        startPoint = startPoint + 1
        if num ~= " " then
            outputVal = outputVal .. num
        else
            isNum = false
        end
    end
  outputVal = tonumber(outputVal)
end
return outputVal

print(valFromLine("GO1 X254.348 Y1039.456 Z150.13 B90.23 C13 M11", "X"))

我统计了我的 'end' 条语句,我在这两个代码示例中都找不到它们有什么问题。在这一点上我完全失去了想法,请帮忙。谢谢。

return outputVal行位置错误。将其移动到函数 valFromLine 中。 您不能 return 在函数之外。

正确:

function someFunction()
  -- do something
  local something = "something"
  return something
end

错误:

function someFunction()
  -- do something
  local something = "something"
end
return something

用函数定义全局函数也不是很干净,请使用局部变量。