#include <perms.h>

//          Edinburgh IMP77 Compiler - Version 8.4
//
//     1
//     2  !Original grotty faulty version
//     3  !%systemrealfn stor(%string(255)input)
//     4  !  !reads a real from the string, assuming string starts with the real
//     5  !  !(or blank spaces followed by a real)
//     6  !  %integer sign=0,sym,pos=0
//     7  !  %real value,exp
//     8  !  !
//     9  !  input=input."!";         !check that there is a finish character
//    10  !  %cycle
//    11  !    sym = charno(input,pos+1)
//    12  !    %exit %if sym > ' '
//    13  !    pos=pos+1
//    14  !  %repeat
//    15  !  %if sym = '-' %start
//    16  !    sign = 1
//    17  !    pos=pos+1;  sym = charno(input,pos+1)
//    18  !  %finish
//    19  !  value = 0
//    20  !  %if sym # '.' %start
//    21  !    %signal 6,5,pos %unless '0' <= sym <= '9';  !charno out of range
//    22  !    %cycle
//    23  !      value = value*10.0+(sym-'0')
//    24  !      pos=pos+1;  sym = charno(input,pos+1)
//    25  !    %repeat %until %not '0' <= sym <= '9'
//    26  !  %finish
//    27  !  %if sym = '.' %start
//    28  !    exp = 10.0
//    29  !    %cycle
//    30  !      pos=pos+1;  sym = charno(input,pos+1)
//    31  !      %exit %unless '0' <= sym <= '9'
//    32  !      value = value+(sym-'0')/exp
//    33  !      exp = exp*10.0
//    34  !    %repeat
//    35  !  %finish
//    36  !  %if sym = '@' %start
//    37  !    pos = pos+1
//    38  !    sym = charno(input,pos+1); pos = pos+1
//    39  !    value = value*10.0\(sym-'0')
//    40  !  %finish
//    41  !  value = -value %if sign # 0
//    42  !  %result = value
//    43  !%end;  !stor
//    44
//    45  !Working version, courtesy of RMM (as if it wasn't obvious
//    46  !from the grotesque 'aesthetic' source layout)
//    47
//    48  %system %real %function S To R (%string (255) Input)

extern float STOR(_imp_string255 INPUT) {
  _imp_enter(STOR);

  //    49    %integer Sign = 0,

  int SIGN = 0xFEFEFEFE;
  SIGN = 0;
  int SYM = 0xFEFEFEFE;
  int POS = 0xFEFEFEFE;
  POS = 1;

  //    50+            Sym,
  //    51+            Pos = 1
  //    52    %long %real Value,

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

  //    53+               Exp
  //    54
  //    55     %routine Next

  void NEXT(void) {
    _imp_enter(NEXT);

    //    56        Pos = Pos + 1

    POS = POS + 1;

    //    57        %if Pos > Length (Input) %start

    if ((POS) <= (*_imp_LENGTH((_imp_string255 *)&INPUT))) goto L_0001;

      //    58           Sym = 0

    SYM = 0;

    //    59        %else

    goto L_0002;
  L_0001:;

  //    60           Sym = Char No (Input, Pos)

    SYM = *_imp_CHARNO((_imp_string255 *)&INPUT, POS);

    //    61        %finish

  L_0002:;

  //    62     %end

    _imp_return;
    _imp_begin_backtrace(NEXT);
    _imp_diagf("\nLocal Variables:\n\n");
    _imp_end_backtrace();
  } /* end of NEXT */

  //    63
  //    64     Sym = Char No (Input, Pos)

  SYM = *_imp_CHARNO((_imp_string255 *)&INPUT, POS);

  //    65     %if Sym = '-' %start

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

    //    66        Sign = 1

  SIGN = 1;

  //    67        Next

  NEXT();

  //    68     %finish

L_0003:;

//    69     Value = 0

  VALUE = 0;

  //    70     %if Sym # '.' %start

  if ((SYM) == (46)) goto L_0004;

    //    71        %signal 6, 5, Pos %unless '0' <= Sym <= '9'           {Char No out of range}

  {
    int tmp;
    if ((48) > (tmp = SYM)) goto L_0005;
    // Double-sided test on SYM (Part II)
    if (tmp <= (57)) goto L_0006;
  }
L_0005:;
  _imp_signal(6, 5, POS, "");
L_0006:;

//    72        %cycle

L_0007:;

//    73           Value = Value*10.0 + (Sym - '0')

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

  //    74           Next

  NEXT();

  //    75        %repeat %until %not '0' <= Sym <= '9'

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

//    76     %finish

L_0004:;

//    77     %if Sym = '.' %start

  if ((SYM) != (46)) goto L_0009;

    //    78        Exp = 10.0

  EXP = 10.0;

  //    79        %cycle

L_000A:;

//    80           Next

  NEXT();

  //    81           %exit %unless '0' <= Sym <= '9'

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

//    82           Value = Value + (Sym - '0')/Exp

  VALUE = VALUE + (double)((SYM - 48) / EXP);

  //    83           Exp = Exp * 10.0

  EXP = EXP * 10.0;

  //    84        %repeat

  goto L_000A;
L_000D:;

//    85     %finish

L_0009:;

//    86     %if Sym = '@' %start

  if ((SYM) != (64)) goto L_000E;

    //    87        Sym = S To I (Sub String (Input, Pos + 1, Length (Input)))

  SYM = _imp_STOI(
      /* clang-format off */
*({  _imp_string255 tmp = _imp_SUBSTRING(*({  _imp_string255 tmp = INPUT;  (_imp_string255 *)&tmp;  }), POS + 1, *_imp_LENGTH((_imp_string255 *)&INPUT));  (_imp_string255 *)&tmp;  })
      /* clang-format on */
  );

  //    88        Value = Value * 10.0\Sym

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

  //    89     %finish

L_000E:;

//    90     Value = -Value %if Sign # 0

  if ((SIGN) == (0)) goto L_000F;
  VALUE = -VALUE;
L_000F:;

//    91     %result = Value

  _imp_result(VALUE);

  //    92  %end {S To R}

  _imp_begin_backtrace(STOR);
  _imp_diagf("\n\nArguments:\n\n");
  _imp_diagf("INPUT = \"%.*s\"\n", INPUT.cstr.len, INPUT.cstr.s);
  _imp_diagf("\nLocal Variables:\n\n");
  _imp_bt_int(SIGN);
  _imp_bt_int(SYM);
  _imp_bt_int(POS);
  _imp_bt_real(VALUE);
  _imp_bt_real(EXP);
  _imp_end_backtrace();
} /* end of STOR */

//    93
//    94  %systemrealfn ftor(%string(255)s)

extern float FTOR(_imp_string255 S) {
  _imp_enter(FTOR);

  //    95    %result = stor(s)

  _imp_result(STOR(
      /* clang-format off */
*({  _imp_string255 tmp = S;  (_imp_string255 *)&tmp;  })
      /* clang-format on */
      ));

  //    96  %end

  _imp_begin_backtrace(FTOR);
  _imp_diagf("\n\nArguments:\n\n");
  _imp_diagf("S = \"%.*s\"\n", S.cstr.len, S.cstr.s);
  _imp_diagf("\nLocal Variables:\n\n");
  _imp_end_backtrace();
} /* end of FTOR */

//    97
//    98  %endoffile