Skip to content

continue in while loop with break as last statement #190

@anthonyjb

Description

@anthonyjb

I have a while loop as follows which is used to read data from a socket:

class GameClient

   ...

    receive: () =>
        -- Receive data from the game server
        data = ''
        err = ''

        -- Receive any data from the client
        while true
            block, err, partial = @_socket\receive(1024)

            if block
                data = data .. block
                continue

            if partial
                data = data .. partial

            break

        -- Check for data received and add it to the data map
        if data\len() > 0
            return data

        return nil

The code (receive method) compiles to the following lua:

    ...
    receive = function(self)
      local data = ''
      local err = ''
      while true do
        local _continue_0 = false
        repeat
          local block, partial
          block, err, partial = self._socket:receive(1024)
          if block then
            data = data .. block
            _continue_0 = true
            break
          end
          if partial then
            data = data .. partial
          end
          break
          _continue_0 = true
        until true
        if not _continue_0 then
          break
        end
      end
      if data:len() > 0 then
        return data
      end
      return nil
    end

Which in lua 5.2 raises a syntax error based on the following series of lines:

      break
      _continue_0 = true
    until true
Error: Syntax error: ... 'until' expected (to close 'repeat' at line 19) near '_continue_0'

The workaround I'm currently using is to change my while loop (in moonscript) to:

        while true
            block, err, partial = @_socket\receive(1024)

            if block
                data = data .. block
                continue
            else
                if partial
                    data = data .. partial

                break

which compiles to to the following lua:

    receive = function(self)
      local data = ''
      local err = ''
      while true do
        local _continue_0 = false
        repeat
          local block, partial
          block, err, partial = self._socket:receive(1024)
          if block then
            data = data .. block
            _continue_0 = true
            break
          else
            if partial then
              data = data .. partial
            end
            break
          end
          _continue_0 = true
        until true
        if not _continue_0 then
          break
        end
      end
      if data:len() > 0 then
        return data
      end
      return nil
    end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions