abaplint / transpiler

ABAP to JS transpiler & runtime
https://transpiler.abaplint.org
MIT License
82 stars 23 forks source link

exercism minesweeper something wrong somewhere #685

Open sandraros opened 2 years ago

sandraros commented 2 years ago

Trying that code at https://exercism.org/tracks/abap/exercises/minesweeper and getting errors for tests 1 to 5, and 10 to 12. Running the test classes in an actual ABAP system is fine. I don't know how to provide better information, analyze where the transpiler goes wrong.

CLASS zcl_minesweeper DEFINITION FINAL CREATE PUBLIC.

  PUBLIC SECTION.
    TYPES: ty_char1 TYPE c LENGTH 1.

    METHODS annotate
      IMPORTING
        !input        TYPE string_table
      RETURNING
        VALUE(result) TYPE string_table.

    METHODS one_if_cell_is_a_star
      IMPORTING
        !row          TYPE i
        !column       TYPE i
      RETURNING
        VALUE(result) TYPE i.

    METHODS read_cell
      IMPORTING
        !row          TYPE i
        !column       TYPE i
      RETURNING
        VALUE(result) TYPE string.

    METHODS change_cell
      IMPORTING
        !row    TYPE i
        !column TYPE i
        !value  TYPE ty_char1.

    DATA: input TYPE string_table.

ENDCLASS.

CLASS zcl_minesweeper IMPLEMENTATION.

  METHOD annotate.
    TYPES: ty_integers TYPE STANDARD TABLE OF i WITH EMPTY KEY.

    me->input = input.

    TRY.

        DATA(rows) = VALUE ty_integers( FOR i = 1 WHILE i <= lines( input ) ( i ) ).
        DATA(count_columns) = strlen( input[ 1 ] ).
        DATA(columns) = VALUE ty_integers( FOR j = 1 WHILE j <= count_columns ( j ) ).

        LOOP AT rows INTO DATA(row).
          LOOP AT columns INTO DATA(column).
            IF read_cell( row = row column = column ) = ` `.
              DATA(count_adjacent_mines) = one_if_cell_is_a_star( row = row - 1 column = column - 1 )
                                         + one_if_cell_is_a_star( row = row - 1 column = column )
                                         + one_if_cell_is_a_star( row = row - 1 column = column + 1 )
                                         + one_if_cell_is_a_star( row = row     column = column - 1 )
                                         + one_if_cell_is_a_star( row = row     column = column + 1 )
                                         + one_if_cell_is_a_star( row = row + 1 column = column - 1 )
                                         + one_if_cell_is_a_star( row = row + 1 column = column )
                                         + one_if_cell_is_a_star( row = row + 1 column = column + 1 ).
              IF count_adjacent_mines > 0.
                change_cell( row = row column = column value = |{ count_adjacent_mines }| ).
              ENDIF.
            ENDIF.
          ENDLOOP.
        ENDLOOP.

      CATCH cx_root INTO DATA(error).
        " invalid grid
        ASSERT 1 = 1. " debug helper
    ENDTRY.

    result = me->input.

  ENDMETHOD.

  METHOD one_if_cell_is_a_star.

    IF read_cell( row = row column = column ) = '*'.
      result = 1.
    ELSE.
      result = 0.
    ENDIF.

  ENDMETHOD.

  METHOD read_cell.

    IF row < 1 OR row > lines( input )
        OR column < 1 OR column > strlen( input[ row ] ).
      result = ` `.
    ELSE.
      result = substring( val = input[ row ] off = column - 1 len = 1 ).
    ENDIF.

  ENDMETHOD.

  METHOD change_cell.

    ASSIGN input[ row ] TO FIELD-SYMBOL(<line>).
    <line> = substring( val = <line> len = column - 1 )
          && value
          && substring( val = <line> off = column len = strlen( <line> ) - column ).

  ENDMETHOD.

ENDCLASS.
larshp commented 2 years ago

still not working

larshp commented 2 years ago

hmm, lazy evaluation of

IF row < 1 OR row > lines( input ) OR column < 1 OR column > strlen( input[ row ] ).

is downported to

DATA temp5 LIKE LINE OF input.
DATA temp6 LIKE LINE OF input.
READ TABLE input INDEX row INTO temp5.
IF sy-subrc <> 0.
  RAISE EXCEPTION TYPE cx_sy_itab_line_not_found.
ENDIF.
IF row < 1 OR row > lines( input ) OR column < 1 OR column > strlen( temp5 ).

which is not correct, hmm

larshp commented 2 years ago

need to split up the IF condition to make it work -> to be done in abaplint downport rule