#include <perms.h>

//          Edinburgh IMP77 Compiler - Version 8.4
//
//     1  ! Copyright (C) 3L Ltd. 1987-1992
//     2
//     3  %routine Read3L ( %name X )

static void READ3L(_imp_name X) {
  _imp_enter(READ3L);

  //     4     %integer sign, sym, limit, digit

  int SIGN = 0xFEFEFEFE;
  int SYM = 0xFEFEFEFE;
  int LIMIT = 0xFEFEFEFE;
  int DIGIT = 0xFEFEFEFE;

  //     5     %long %real value, exp

  double VALUE = __builtin_nan("");
  double EXP = __builtin_nan("");

  //     6     %string(255) s

  _imp_string255 S;

  //     7
  //     8     %long %real Base = 10.0

  double BASE = __builtin_nan("");
  BASE = 10.0;

  //     9
  //    10     %integer Type    = Type Of(X),

  int TYPE = 0xFEFEFEFE;
  TYPE = X.typeinfo;
  int SIZE = 0xFEFEFEFE;
  SIZE = X.sizeinfo;
  int ADDRESS = 0xFEFEFEFE;
  ADDRESS = ((_imp_address)(X.address));
  int FLOAT = 0xFEFEFEFE;
  FLOAT = 0;

  //    11+             Size    = Size Of(X),
  //    12+             Address = Addr(X),
  //    13+             Float   = 0
  //    14
  //    15     %if Type < 1 %or Type > 8 %or Type = 4 %or Type = 7 %start

  if ((TYPE) < (1)) goto L_0001;
  if ((TYPE) > (8)) goto L_0001;
  if ((TYPE) == (4)) goto L_0001;
  if ((TYPE) != (7)) goto L_0002;
L_0001:;

//    16        %signal 5, 5, Type

  _imp_signal(5, 5, TYPE, "");

  //    17     %finish

L_0002:;

//    18
//    19     %if Type = 3 %start; ! string

  if ((TYPE) != (3)) goto L_0003;

    //    20        limit = Size-1;   ! max length

  LIMIT = SIZE - 1;

  //    21        s="";  digit = 0

  _imp_strcpy(&S, _imp_strp(_imp_str_literal("")), /*max:*/ 255);
  DIGIT = 0;

  //    22
  //    23        Read Symbol(Sym) %until Sym > ' '

L_0004:;
  _imp_READSYMBOL(&SYM);
  if ((SYM) > (32)) goto L_0005;
  goto L_0004;
L_0005:;

//    24
//    25        %cycle

L_0006:;

//    26           s = s.tostring(sym)

  _imp_strassign(&S, 255, "%S%S", _imp_strp(S), _imp_strp(_imp_TOSTRING(SYM)));

  //    27           digit=digit+1

  DIGIT = DIGIT + 1;

  //    28           sym = nextsymbol

  SYM = _imp_NEXTSYMBOL();

  //    29           %exit %if sym <= ' '

  if ((SYM) > (32)) goto L_0007;
  goto L_0008;
L_0007:;

//    30           skipsymbol

  _imp_SKIPSYMBOL();

  //    31        %repeat

  goto L_0006;
L_0008:;

//    32        %signal 1,3 %if digit > limit

  if ((DIGIT) <= (LIMIT)) goto L_0009;
  _imp_signal(1, 3, 0, "");
L_0009:;

//    33        string(address) = s

  *_imp_STRING(ADDRESS) = S;  // UNHANDLED STR ASSVAL DEST

  //    34        %return

  _imp_return;

  //    35     %finish

L_0003:;

//    36
//    37     ! Else numeric
//    38
//    39     Float = 1 %if Type = 2 %or Type = 8

  if ((TYPE) == (2)) goto L_000A;
  if ((TYPE) != (8)) goto L_000B;
L_000A:;
  FLOAT = 1;
L_000B:;

//    40
//    41     sign = 1

  SIGN = 1;

  //    42     %cycle

L_000C:;

//    43        sym = nextsymbol

  SYM = _imp_NEXTSYMBOL();

  //    44        %if sym = '-' %start

  if ((SYM) != (45)) goto L_000D;

    //    45           %signal 4,1,sym %if sign < 0

  if ((SIGN) >= (0)) goto L_000E;
  _imp_signal(4, 1, SYM, "");
L_000E:;

//    46           sign = -1

  SIGN = -1;

  //    47           skipsymbol;  sym = nextsymbol

  _imp_SKIPSYMBOL();
  SYM = _imp_NEXTSYMBOL();

  //    48           %exit

  goto L_000F;

  //    49        %finish

L_000D:;

//    50        %exit %if sym > ' '

  if ((SYM) <= (32)) goto L_0010;
  goto L_000F;
L_0010:;

//    51        skipsymbol

  _imp_SKIPSYMBOL();

  //    52     %repeat

  goto L_000C;
L_000F:;

//    53  more:

U_1000:;

//    54     value = 0

  VALUE = 0;

  //    55     limit = int(base)

  LIMIT = _imp_INT(BASE);

  //    56     digit = -1; sym = sym - 32 %if 'a'<= sym <= 'z'

  DIGIT = -1;
  {
    int tmp;
    if ((97) > (tmp = SYM)) goto L_0011;
    // Double-sided test on SYM (Part II)
    if (tmp > (122)) goto L_0011;
  }
  SYM = SYM - 32;
L_0011:;

//    57     digit = sym-'0' %if '0' <= sym <= '9'

  {
    int tmp;
    if ((48) > (tmp = SYM)) goto L_0012;
    // Double-sided test on SYM (Part II)
    if (tmp > (57)) goto L_0012;
  }
  DIGIT = SYM - 48;
L_0012:;

//    58     digit = sym -'A'+10 %if 'A'<=sym<='Z'

  {
    int tmp;
    if ((65) > (tmp = SYM)) goto L_0013;
    // Double-sided test on SYM (Part II)
    if (tmp > (90)) goto L_0013;
  }
  DIGIT = SYM - 65 + 10;
L_0013:;

//    59     %signal 4,1,sym %unless 0 <= digit < limit %or

  {
    int tmp;
    if ((0) > (tmp = DIGIT)) goto L_0014;
    // Double-sided test on DIGIT (Part II)
    if (tmp < (LIMIT)) goto L_0015;
  }
L_0014:;
  if ((SYM) != (46)) goto L_0016;
  if ((FLOAT) != (0)) goto L_0015;
L_0016:;
  _imp_signal(4, 1, SYM, "");
L_0015:;

//    60+                               (sym = '.' %and Float # 0)
//    61     %while 0 <= digit < limit %cycle

L_0017:;
  {
    int tmp;
    if ((0) > (tmp = DIGIT)) goto L_0018;
    // Double-sided test on DIGIT (Part II)
    if (tmp >= (LIMIT)) goto L_0018;
  }

  //    62        skipsymbol

  _imp_SKIPSYMBOL();

  //    63        value = value*base+digit

  VALUE = VALUE * BASE + DIGIT;

  //    64        sym = nextsymbol

  SYM = _imp_NEXTSYMBOL();

  //    65        digit = -1; sym=sym-32 %if 'a'<=sym<='z'

  DIGIT = -1;
  {
    int tmp;
    if ((97) > (tmp = SYM)) goto L_0019;
    // Double-sided test on SYM (Part II)
    if (tmp > (122)) goto L_0019;
  }
  SYM = SYM - 32;
L_0019:;

//    66        digit = sym-'0' %if '0'<=sym<='9'

  {
    int tmp;
    if ((48) > (tmp = SYM)) goto L_001A;
    // Double-sided test on SYM (Part II)
    if (tmp > (57)) goto L_001A;
  }
  DIGIT = SYM - 48;
L_001A:;

//    67        digit = sym-'A'+10 %if 'A'<=sym<='Z'

  {
    int tmp;
    if ((65) > (tmp = SYM)) goto L_001B;
    // Double-sided test on SYM (Part II)
    if (tmp > (90)) goto L_001B;
  }
  DIGIT = SYM - 65 + 10;
L_001B:;

//    68     %repeat

  goto L_0017;
L_0018:;

//    69     %if sym = '_' %start

  if ((SYM) != (95)) goto L_001C;

    //    70        base = value

  BASE = VALUE;

  //    71        skipsymbol

  _imp_SKIPSYMBOL();

  //    72        sym = nextsymbol

  SYM = _imp_NEXTSYMBOL();

  //    73        ->more

  goto U_1000;

  //    74     %finish

L_001C:;

//    75     %if sym = '.' %and limit = 10 %and Float # 0 %start

  if ((SYM) != (46)) goto L_001D;
  if ((LIMIT) != (10)) goto L_001D;
  if ((FLOAT) == (0)) goto L_001D;

    //    76        skipsymbol

  _imp_SKIPSYMBOL();

  //    77        exp = 0.1

  EXP = 0.1;

  //    78        %cycle

L_001E:;

//    79           sym = nextsymbol

  SYM = _imp_NEXTSYMBOL();

  //    80           %exit %unless '0' <= sym <= '9'

  {
    int tmp;
    if ((48) > (tmp = SYM)) goto L_001F;
    // Double-sided test on SYM (Part II)
    if (tmp <= (57)) goto L_0020;
  }
L_001F:;
  goto L_0021;
L_0020:;

//    81           value = value+(sym-'0')*exp

  VALUE = VALUE + (SYM - 48) * EXP;

  //    82           exp = exp*0.1

  EXP = EXP * 0.1;

  //    83           skipsymbol

  _imp_SKIPSYMBOL();

  //    84        %repeat

  goto L_001E;
L_0021:;

//    85     %finish

L_001D:;

//    86     %if sym = '@' %and limit = 10 %and Float # 0 %start

  if ((SYM) != (64)) goto L_0022;
  if ((LIMIT) != (10)) goto L_0022;
  if ((FLOAT) == (0)) goto L_0022;

    //    87        skipsymbol

  _imp_SKIPSYMBOL();

  //    88        read(sym)

  _imp_READ(((_imp_name){.address = &SYM, .typeinfo = 1, .sizeinfo = sizeof(SYM)}));

  //    89        value = value*10.0^sym

  VALUE = VALUE * (_imp_REXP(10.0, SYM));

  //    90     %finish

L_0022:;

//    91     value = -value %if sign < 0

  if ((SIGN) >= (0)) goto L_0023;
  VALUE = -VALUE;
L_0023:;

//    92     %if Float = 0 %start;       !integral

  if ((FLOAT) != (0)) goto L_0024;

    //    93        Sym = Int(Value)

  SYM = _imp_INT(VALUE);

  //    94        %if type = 1 %start

  if ((TYPE) != (1)) goto L_0025;

    //    95           integer(address) = sym

  *_imp_INTEGER(ADDRESS) = SYM;

  //    96        %else %if Type = 5

  goto L_0026;
L_0025:;
  if ((TYPE) != (5)) goto L_0027;

    //    97           byteinteger(address) = sym

  *_imp_BYTEINTEGER(ADDRESS) = SYM;

  //    98        %else

  goto L_0026;
L_0027:;

//    99           shortinteger(address) = sym

  *_imp_SHORTINTEGER(ADDRESS) = SYM;

  //   100        %finish

L_0026:;

//   101     %else %if type = 2

  goto L_0028;
L_0024:;
  if ((TYPE) != (2)) goto L_0029;

    //   102        real(address) = value

  *_imp_REAL(ADDRESS) = VALUE;

  //   103     %else

  goto L_0028;
L_0029:;

//   104        longreal(address) = value

  *_imp_LONGREAL(ADDRESS) = VALUE;

  //   105     %finish

L_0028:;

//   106  %end

  _imp_return;
  _imp_begin_backtrace(READ3L);
  _imp_diagf("\n\nArguments:\n\n");
  _imp_diagf("X = ?\n");
  _imp_diagf("\nLocal Variables:\n\n");
  _imp_bt_int(SIGN);
  _imp_bt_int(SYM);
  _imp_bt_int(LIMIT);
  _imp_bt_int(DIGIT);
  _imp_bt_real(VALUE);
  _imp_bt_real(EXP);
  _imp_diagf("S = \"%.*s\"\n", S.cstr.len, S.cstr.s);
  _imp_bt_real(BASE);
  _imp_bt_int(TYPE);
  _imp_bt_int(SIZE);
  _imp_bt_int(ADDRESS);
  _imp_bt_int(FLOAT);
  _imp_end_backtrace();
} /* end of READ3L */

//   107
//   108  %routine diagnostic indirection test(%real uninitialised scalar parameter, %realname pointer to uninitialised)

static void DIAGNOSTICINDIRECTIONTEST(float UNINITIALISEDSCALARPARAMETER, float *POINTERTOUNINITIALISED) {
  _imp_enter(DIAGNOSTICINDIRECTIONTEST);

  //   109    %byteinteger byteinteger

  unsigned char BYTEINTEGER;

  //   110    %shortinteger shortinteger

  short int SHORTINTEGER;

  //   111    %integer integer

  int INTEGER = 0xFEFEFEFE;

  //   112    %real real

  float REAL = __builtin_nanf("");

  //   113    %longreal longreal

  double LONGREAL = __builtin_nan("");

  //   114
  //   115    printstring("Executing!"); newline

  _imp_printf("%s", "Executing!");
  _imp_NEWLINE();

  //   116
  //   117    prompt("byte: ")

  _imp_PROMPT(_imp_str_literal_size(31, "byte: "));

  //   118    read3l(byteinteger)

  READ3L(((_imp_name){.address = &BYTEINTEGER, .typeinfo = 5, .sizeinfo = sizeof(BYTEINTEGER)}));

  //   119    printstring("Byte = "); write(byteinteger, -1); newline

  _imp_printf("%s", "Byte = ");
  _imp_WRITE(BYTEINTEGER, -1);
  _imp_NEWLINE();

  //   120    prompt("short: ")

  _imp_PROMPT(_imp_str_literal_size(31, "short: "));

  //   121    read3l(shortinteger)

  READ3L(((_imp_name){.address = &SHORTINTEGER, .typeinfo = 6, .sizeinfo = sizeof(SHORTINTEGER)}));

  //   122    printstring("Short = "); write(shortinteger, -1); newline

  _imp_printf("%s", "Short = ");
  _imp_WRITE(SHORTINTEGER, -1);
  _imp_NEWLINE();

  //   123    prompt("integer: ")

  _imp_PROMPT(_imp_str_literal_size(31, "integer: "));

  //   124    read3l(integer)

  READ3L(((_imp_name){.address = &INTEGER, .typeinfo = 1, .sizeinfo = sizeof(INTEGER)}));

  //   125    printstring("Integer = "); write(integer, -1); newline

  _imp_printf("%s", "Integer = ");
  _imp_WRITE(INTEGER, -1);
  _imp_NEWLINE();

  //   126
  //   127    prompt("real: ")

  _imp_PROMPT(_imp_str_literal_size(31, "real: "));

  //   128    read3l(real)

  READ3L(((_imp_name){.address = &REAL, .typeinfo = 2, .sizeinfo = sizeof(REAL)}));

  //   129    printstring("Real = "); printfl(real, 12); newline

  _imp_printf("%s", "Real = ");
  _imp_PRINTFL(REAL, 12);
  _imp_NEWLINE();

  //   130    prompt("longreal: ")

  _imp_PROMPT(_imp_str_literal_size(31, "longreal: "));

  //   131    read3l(longreal)

  READ3L(((_imp_name){.address = &LONGREAL, .typeinfo = 8, .sizeinfo = sizeof(LONGREAL)}));

  //   132    printstring("Longreal = "); printfl(longreal, 12); newline

  _imp_printf("%s", "Longreal = ");
  _imp_PRINTFL(LONGREAL, 12);
  _imp_NEWLINE();

  //   133  %end

  _imp_return;
  _imp_begin_backtrace(DIAGNOSTICINDIRECTIONTEST);
  _imp_diagf("\n\nArguments:\n\n");
  if (__builtin_isnan(UNINITIALISEDSCALARPARAMETER))
    _imp_diagf("UNINITIALISEDSCALARPARAMETER = <UNASSIGNED REAL>\n");
  else
    _imp_diagf("UNINITIALISEDSCALARPARAMETER = %LE\n", (long double)UNINITIALISEDSCALARPARAMETER);
  _imp_diagf("POINTERTOUNINITIALISED = %LE\n", (long double)*POINTERTOUNINITIALISED);
  _imp_diagf("\nLocal Variables:\n\n");
  _imp_bt_int(BYTEINTEGER);
  _imp_bt_int(SHORTINTEGER);
  _imp_bt_int(INTEGER);
  _imp_bt_real(REAL);
  _imp_bt_real(LONGREAL);
  _imp_end_backtrace();
} /* end of DIAGNOSTICINDIRECTIONTEST */

// ?POINTERTOUNINITIALISED unused
// ?UNINITIALISEDSCALARPARAMETER unused
//   134
//   135  %begin

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

  //   136    %real uninitialised scalar

  float UNINITIALISEDSCALAR = __builtin_nanf("");

  //   137    %realname initialised pointer == uninitialised scalar

  float *INITIALISEDPOINTER = (void *)0xFEFEFEFEFEFEFEFELL;
  INITIALISEDPOINTER = (float *)&UNINITIALISEDSCALAR;
  //   138    printstring("Starting!"); newline

  _imp_printf("%s", "Starting!");
  _imp_NEWLINE();

  //   139    diagnostic indirection test(uninitialised scalar, initialised pointer)   ;! later implementation should catch this at the point of call, but at least we show it correctly in the backtrace!

  DIAGNOSTICINDIRECTIONTEST(UNINITIALISEDSCALAR, INITIALISEDPOINTER);

  //   140    printstring("Done!"); newline

  _imp_printf("%s", "Done!");
  _imp_NEWLINE();

  //   141  %end

  _imp_stop(0);
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_real(UNINITIALISEDSCALAR);
  _imp_diagf("INITIALISEDPOINTER @[%p] = %LE\n", INITIALISEDPOINTER, INITIALISEDPOINTER,
             *(long double *)INITIALISEDPOINTER);
  _imp_end_backtrace();
  _imp_stop(0);
}

//   142
//   143  %endoffile