Inertia
Inertia

Reputation: 3

Lua <eof> expected near 'print'

I'm having trouble with in lua.

Problem one: Every time I run the following program the console tells me:

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

Please notice where I've marked in all caps comments details about the error

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

I can't see why it's expecting m11() to close so early.

Problem two: I rewrote valFromLine() in a completely separate file and I get:

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

I've counted my 'end' statements, I can't find what's wrong with them in either examples of code. I'm at a complete loss for ideas at this point, please help. Thanks.

Upvotes: 0

Views: 2681

Answers (1)

Piglet
Piglet

Reputation: 28940

The line return outputVal is at the wrong position. Move it into the function valFromLine. You cannot return outside the function.

correct:

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

wrong:

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

Defining global functions withn a function is also not very clean, use locals.

Upvotes: 2

Related Questions