(self, line_toks)
return b''.join(tok.code for tok in line_toks) + b'\n'
Transforms a line of PICO-8 Lua to pure Lua.

        This relies heavily on the fact that PICO-8 Lua shortcut syntax
        operates on a single line. It uses the token stream to handle strings
        and multiline comments correctly.

        This assumes that the code parses correctly. If the input doesn't
        parse, the output will be worse.

        Args:
            An iterable of lexer.Token instances representing a single line up
            to and including an optional TokNewLine.

        Returns:
            A bytestring of Lua code.
Transforms a line of PICO-8 Lua to pure Lua.
Transforms a line of PICO - 8 Lua to pure Lua .
def line_to_pure_lua(self, line_toks):
        """Transforms a line of PICO-8 Lua to pure Lua.

        This relies heavily on the fact that PICO-8 Lua shortcut syntax
        operates on a single line. It uses the token stream to handle strings
        and multiline comments correctly.

        This assumes that the code parses correctly. If the input doesn't
        parse, the output will be worse.

        Args:
            An iterable of lexer.Token instances representing a single line up
            to and including an optional TokNewLine.

        Returns:
            A bytestring of Lua code.
        """
        if not line_toks:
            return b''

        # Drop newline
        if line_toks[-1].matches(lexer.TokNewline):
            line_toks = line_toks[:-1]

        if not line_toks:
            return b'\n'

        # Comment beginning with //
        if line_toks[-1].matches(lexer.TokComment):
            if line_toks[-1].code.startswith(b'//'):
                line_toks[-1].code = line_toks[-1].code.replace(
                    b'//', b'--', 1)

        # ?...[EOL] -> print(...)
        short_print_i = self._find_tok(line_toks, lexer.TokName(b'?'))
        if short_print_i != -1:
            line_toks[short_print_i].code = b'print'
            line_toks[short_print_i + 1:] = (
                [lexer.TokSymbol(b'(')] +
                line_toks[short_print_i + 1:] +
                [lexer.TokSymbol(b')')])

        # if (cond) [not-THEN] ... [EOL]
        try:
            short_if_i = self._find_tok(
                line_toks, lexer.TokKeyword(b'if'), check=True)
            short_if_lparen_i = self._find_tok(
                line_toks,
                lexer.TokSymbol(b'('), start=short_if_i + 1, check=True)
            short_if_rparen_i = self._find_tok(
                line_toks, lexer.TokSymbol(b')'), start=short_if_lparen_i + 1,
                check=True)
            short_if_then_i = self._next_nonspace_tok(
                line_toks, start=short_if_rparen_i + 1)
            next_tok = line_toks[short_if_then_i]
            if (not next_tok.matches(lexer.TokKeyword(b'then'))
                    and not next_tok.matches(lexer.TokKeyword(b'and'))
                    and not next_tok.matches(lexer.TokKeyword(b'or'))):
                line_toks[short_if_rparen_i + 1:] = (
                    [lexer.TokSpace(b' '), lexer.TokKeyword(b'then'),
                     lexer.TokSpace(b' ')] +
                    line_toks[short_if_rparen_i + 1:] +
                    [lexer.TokSpace(b' '), lexer.TokKeyword(b'end')])
        except ValueError:
            pass

        # lhs += ... [EOL]
        first_nonspace_i = self._next_nonspace_tok(line_toks)
        if (first_nonspace_i != -1 and
                line_toks[first_nonspace_i].matches(lexer.TokName)):
            assign_i = self._next_nonspace_tok(
                line_toks, start=first_nonspace_i + 1)
            if (assign_i != -1 and
                    (line_toks[assign_i].matches(lexer.TokSymbol(b'+=')) or
                     line_toks[assign_i].matches(lexer.TokSymbol(b'-=')) or
                     line_toks[assign_i].matches(lexer.TokSymbol(b'*=')) or
                     line_toks[assign_i].matches(lexer.TokSymbol(b'/=')) or
                     line_toks[assign_i].matches(lexer.TokSymbol(b'%=')))):
                op = line_toks[assign_i].code[0:1]
                replacement = [
                    lexer.TokSymbol(b'='),
                    lexer.TokSpace(b' '),
                    lexer.TokName(line_toks[first_nonspace_i].code),
                    lexer.TokSpace(b' '),
                    lexer.TokSymbol(op),
                    lexer.TokSpace(b' ')] + line_toks[assign_i + 1:]
                line_toks[assign_i:] = replacement

        # !=
        not_equal_i = self._find_tok(line_toks, lexer.TokSymbol(b'!='))
        if not_equal_i != -1:
            line_toks[not_equal_i] = lexer.TokSymbol(b'~=')

        return b''.join(tok.code for tok in line_toks) + b'\n'
def line_to_pure_lua ( self , line_toks ) : if not line_toks : return b'' # Drop newline if line_toks [ - 1 ] . matches ( lexer . TokNewline ) : line_toks = line_toks [ : - 1 ] if not line_toks : return b'\n' # Comment beginning with // if line_toks [ - 1 ] . matches ( lexer . TokComment ) : if line_toks [ - 1 ] . code . startswith ( b'//' ) : line_toks [ - 1 ] . code = line_toks [ - 1 ] . code . replace ( b'//' , b'--' , 1 ) # ?...[EOL] -> print(...) short_print_i = self . _find_tok ( line_toks , lexer . TokName ( b'?' ) ) if short_print_i != - 1 : line_toks [ short_print_i ] . code = b'print' line_toks [ short_print_i + 1 : ] = ( [ lexer . TokSymbol ( b'(' ) ] + line_toks [ short_print_i + 1 : ] + [ lexer . TokSymbol ( b')' ) ] ) # if (cond) [not-THEN] ... [EOL] try : short_if_i = self . _find_tok ( line_toks , lexer . TokKeyword ( b'if' ) , check = True ) short_if_lparen_i = self . _find_tok ( line_toks , lexer . TokSymbol ( b'(' ) , start = short_if_i + 1 , check = True ) short_if_rparen_i = self . _find_tok ( line_toks , lexer . TokSymbol ( b')' ) , start = short_if_lparen_i + 1 , check = True ) short_if_then_i = self . _next_nonspace_tok ( line_toks , start = short_if_rparen_i + 1 ) next_tok = line_toks [ short_if_then_i ] if ( not next_tok . matches ( lexer . TokKeyword ( b'then' ) ) and not next_tok . matches ( lexer . TokKeyword ( b'and' ) ) and not next_tok . matches ( lexer . TokKeyword ( b'or' ) ) ) : line_toks [ short_if_rparen_i + 1 : ] = ( [ lexer . TokSpace ( b' ' ) , lexer . TokKeyword ( b'then' ) , lexer . TokSpace ( b' ' ) ] + line_toks [ short_if_rparen_i + 1 : ] + [ lexer . TokSpace ( b' ' ) , lexer . TokKeyword ( b'end' ) ] ) except ValueError : pass # lhs += ... [EOL] first_nonspace_i = self . _next_nonspace_tok ( line_toks ) if ( first_nonspace_i != - 1 and line_toks [ first_nonspace_i ] . matches ( lexer . TokName ) ) : assign_i = self . _next_nonspace_tok ( line_toks , start = first_nonspace_i + 1 ) if ( assign_i != - 1 and ( line_toks [ assign_i ] . matches ( lexer . TokSymbol ( b'+=' ) ) or line_toks [ assign_i ] . matches ( lexer . TokSymbol ( b'-=' ) ) or line_toks [ assign_i ] . matches ( lexer . TokSymbol ( b'*=' ) ) or line_toks [ assign_i ] . matches ( lexer . TokSymbol ( b'/=' ) ) or line_toks [ assign_i ] . matches ( lexer . TokSymbol ( b'%=' ) ) ) ) : op = line_toks [ assign_i ] . code [ 0 : 1 ] replacement = [ lexer . TokSymbol ( b'=' ) , lexer . TokSpace ( b' ' ) , lexer . TokName ( line_toks [ first_nonspace_i ] . code ) , lexer . TokSpace ( b' ' ) , lexer . TokSymbol ( op ) , lexer . TokSpace ( b' ' ) ] + line_toks [ assign_i + 1 : ] line_toks [ assign_i : ] = replacement # != not_equal_i = self . _find_tok ( line_toks , lexer . TokSymbol ( b'!=' ) ) if not_equal_i != - 1 : line_toks [ not_equal_i ] = lexer . TokSymbol ( b'~=' ) return b'' . join ( tok . code for tok in line_toks ) + b'\n'