#include <perms.h>

//          Edinburgh IMP77 Compiler - Version 8.4
//
//     1
//     2  %begin

int main(int argc, char **argv) { /*%begin*/
  _imp_enter(main);
  _imp_initialise(argc, argv);

  //     3
  //     4  %constintegerarray fred(0:2) = ;

  const int FRED[3] = {1, 2, 3};
  _imp_create_constdv(const, int, FRED, 0, 2);

  //     5+           1,2,
  //     6+
  //     7+           3
  //     8
  //     9  %routine jim(%integer p,

  void JIM(int P, int Q) {
    _imp_enter(JIM);

    //    10+              %integer q)
    //    11  %end

    _imp_return;
    _imp_begin_backtrace(JIM);
    _imp_diagf("\n\nArguments:\n\n");
    _imp_diagf("P = %d\n", P);
    _imp_diagf("Q = %d\n", Q);
    _imp_diagf("\nLocal Variables:\n\n");
    _imp_end_backtrace();
  } /* end of JIM */

  // ?Q unused
  // ?P unused
  //    12
  //    13
  //    14
  //    15
  //    16  %constc
  //    17+
  //    18+
  //    19+ %integerarray sheila(0:2) =

  const int SHEILA[3] = {1, 2, 3};
  _imp_create_constdv(const, int, SHEILA, 0, 2);

  //    20+
  //    21+
  //    22+           1,2,
  //    23+
  //    24+           3
  //    25
  //    26
  //    27  %switch lab(1:4)

  int LAB_index;
  int LAB_line;
  char *LAB_file;
  static const void *LAB[4] = {&&case_LAB_default, &&case_LAB_2, &&case_LAB_3, &&case_LAB_default};

  //    28  %integer i

  int I = 0xFEFEFEFE;

  //    29  %constinteger one=1, two=2, three=3
  //    30
  //    31  i = sheila(2)

  I = SHEILA[2];

  //    32
  //    33  lab(':'+one+two-':'):

case_LAB_3:;

//    34                                          ;! %commc
//    35
//    36  print string("test 0"); newline

  _imp_printf("%s", "test 0");
  _imp_NEWLINE();

  //    37  print string("If there is output mentioning test 0 above, test 0 passed"); newline

  _imp_printf("%s", "If there is output mentioning test 0 above, test 0 passed");
  _imp_NEWLINE();

  //    38
  //    39  %comment %c
  //    40  print string("test 1"); newline   ;

  _imp_printf("%s", "test 1");
  _imp_NEWLINE();

  //    41  print string("If there is output mentioning test 1 above, test 1 passed"); newline

  _imp_printf("%s", "If there is output mentioning test 1 above, test 1 passed");
  _imp_NEWLINE();

  //    42
  //    43  %comment %c
  //    44+ print string("test 2a failed - this is part of the comment above") %and newline; print string("test 2b failed - semicolons should not be honoured in %comments"); newline
  //    45  print string("If there is no output mentioning test 2 above, test2 passed"); newline

  _imp_printf("%s", "If there is no output mentioning test 2 above, test2 passed");
  _imp_NEWLINE();

  //    46
  //    47  %comment %c
  //    48  print string("test 3"); newline

  _imp_printf("%s", "test 3");
  _imp_NEWLINE();

  //    49  print string("If there is output mentioning test 3 above, test 3 passed"); newline

  _imp_printf("%s", "If there is output mentioning test 3 above, test 3 passed");
  _imp_NEWLINE();

  //    50
  //    51
  //    52  %comment %c
  //    53  print string("test 4"); newline

  _imp_printf("%s", "test 4");
  _imp_NEWLINE();

  //    54  print string("If there is output mentioning test 4 above, test 4 passed"); newline

  _imp_printf("%s", "If there is output mentioning test 4 above, test 4 passed");
  _imp_NEWLINE();

  //    55
  //    56
  //    57  lab(':'+two+two*':'-':'*3):        i=10

case_LAB_2:;
  I = 10;

  //    58
  //    59                                          %comment ' { again
  //    60                                          %comment   { 'again
  //    61                                          %comment             '
  //    62
  //    63  %comment %c
  //    64+
  //    65+ This this part of the comment in Imp77!
  //    66
  //    67  ! %c
  //    68  print string("Note that %c is not honoured within a '!' comment..."); newline

  _imp_printf("%s", "Note that %c is not honoured within a '!' comment...");
  _imp_NEWLINE();

  //    69
  //    70  !  a comment with an unbalanced ' is a problem
  //    71  print string("test 5 passed"); newline

  _imp_printf("%s", "test 5 passed");
  _imp_NEWLINE();

  //    72
  //    73
  //    74  print string("test 6 passed"); newline

  _imp_printf("%s", "test 6 passed");
  _imp_NEWLINE();

  //    75
  //    76
  //    77  print string("test 7 passed"); newline

  _imp_printf("%s", "test 7 passed");
  _imp_NEWLINE();

  //    78
  //    79  %comment  a comment with an unbalanced ' is a problem
  //    80  print string("test 8 passed"); newline

  _imp_printf("%s", "test 8 passed");
  _imp_NEWLINE();

  //    81
  //    82  lab(*):

case_LAB_default:;

//    83  !}
//    84
//    85  %end{}%of%pro %gram

  _imp_stop(0);

case_LAB_dispatch:
  if (LAB_index < 1 || LAB_index > 4)
    _imp_signalf(6, 3, LAB_index, "->LAB(%d) out of bounds at %s line %d", LAB_index, LAB_file, LAB_line);
  goto *LAB[LAB_index - 1];
end_0:;
  _imp_begin_backtrace(main);
  _imp_diagf("\n\nArguments:\n\n");
  _imp_diagf("ARGC = %d\n", argc);
  _imp_diagf("ARGV @[%p]\n", argv);
  for (int i = 0; i < argc; i++) {
    _imp_diagf("  ARGV(%d) = \"%s\"\n", i, argv[i]);
  }
  if (argv[argc] == 0) {
    _imp_diagf("  ARGV(%d) = NULL\n", argc);
  } else {
    _imp_diagf("  ARGV(%d) = \"%s\"\n", argc, argv[argc]);
  }
  _imp_diagf("\nLocal Variables:\n\n");
  _imp_bt_int(I);
  _imp_end_backtrace();
  _imp_stop(0);
}