簡體   English   中英

a <eof> 預期接近“打印”

[英]Lua <eof> expected near 'print'

我在lua中遇到麻煩。

問題一:每次運行以下程序,控制台都會告訴我:

“ local”附近的“ end”(關閉第1行的“功能”)

請注意我在所有大寫字母注釋中標記的位置有關該錯誤的詳細信息

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”預計接近“印刷”

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"))

我已經計算了“結束”語句,在兩個代碼示例中都找不到它們出了什么問題。 目前,我對創意完全不知所措,請提供幫助。 謝謝。

return outputVal在錯誤的位置。 將其移到函數valFromLine中。 您不能在函數之外返回。

正確:

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

錯誤:

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

用一個函數定義全局函數也不是很干凈,使用局部變量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM