#include <perms.h>

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

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

  //     2  %realfn eval    {evaluate sub-expression}

  float EVAL(void) {
    _imp_enter(EVAL);

    auto void PUSHOPD(float VAL);

    auto void PUSHOP(int OP);

    auto float POPOPD(void);

    auto int POPOP(void);

    auto float NEXTOPD(void);

    auto int NEXTOP(void);

    auto void OPERATE(void);

    //     3  %routinespec pushopd(%real val)
    //     4  %routinespec pushop(%integer op)
    //     5  %realfnspec popopd
    //     6  %integerfnspec popop
    //     7  %realfnspec nextopd
    //     8  %integerfnspec nextop
    //     9  %routinespec operate
    //    10  %constinteger plus=1,minus=2,times=3,divide=4,exp=5,assign=6,end=7,space=' '
    //    11  %constintegerarray prec(plus:assign)=1,1,2,2,3,0

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

    //    12  %realarray opds(0:4)    {operand stack}

    float OPDS[5];
    _imp_create_constdv(, float, OPDS, 0, 4);

    //    13  %integerarray ops(0:3)    {operator stack}

    int OPS[4];
    _imp_create_constdv(, int, OPS, 0, 3);

    //    14  %ownrealarray vars('a':'z')=0(*)

    static float VARS[26] = {[0 ... 25] = 0};
    _imp_create_constdv(static, float, VARS, 97, 122);

    //    15  %ownintegerarray vara('a':'z')=0(*)

    static int VARA[26] = {[0 ... 25] = 0};
    _imp_create_constdv(static, int, VARA, 97, 122);

    //    16  %integer op,opdsp,opsp,var

    int OP = 0xFEFEFEFE;
    int OPDSP = 0xFEFEFEFE;
    int OPSP = 0xFEFEFEFE;
    int VAR = 0xFEFEFEFE;

    //    17    opdsp=0 ; opsp=0    {init stack pointers}

    OPDSP = 0;
    OPSP = 0;

    //    18    pushopd(nextopd)    {first operand}

    PUSHOPD(NEXTOPD());

    //    19    %cycle    {for each operator}

  L_0001:;

  //    20      op=nextop

    OP = NEXTOP();

    //    21      %if op=end %then %exit

    if ((OP) != (7)) goto L_0002;
    goto L_0003;
  L_0002:;

  //    22      operate %while opsp>0 %and prec(ops(opsp))>=prec(op)

  L_0004:;
    if ((OPSP) <= (0)) goto L_0005;
    if ((PREC[OPS[OPSP] - 1]) < (PREC[OP - 1])) goto L_0005;
    OPERATE();
    goto L_0004;
  L_0005:;

  //    23      %if op=assign %then %start

    if ((OP) != (6)) goto L_0006;

      //    24        read symbol(var)

    _imp_READSYMBOL(&VAR);

    //    25        %unless 'a'<=var<='z' %or 'A'<=var<='Z' %then print symbol(var) %c

    {
      int tmp;
      if ((97) > (tmp = VAR)) goto L_0007;
      // Double-sided test on VAR (Part II)
      if (tmp <= (122)) goto L_0008;
    }
  L_0007:;
    {
      int tmp;
      if ((65) > (tmp = VAR)) goto L_0009;
      // Double-sided test on VAR (Part II)
      if (tmp <= (90)) goto L_0008;
    }
  L_0009:;
    _imp_PRINTSYMBOL(VAR);
    _imp_printf("%s", " variable");
    _imp_signal(15, 0, 0, "");
  L_0008:;

  //    26+         %and print string(" variable") %and %signal 15
  //    27        %if 'A'<=var<='Z' %then var=var-'A'+'a'

    {
      int tmp;
      if ((65) > (tmp = VAR)) goto L_000A;
      // Double-sided test on VAR (Part II)
      if (tmp > (90)) goto L_000A;
    }
    VAR = VAR - 65 + 97;
  L_000A:;

  //    28        vars(var)=opds(opdsp)

    VARS[VAR - 97] = OPDS[OPDSP];

    //    29        vara(var)=1    {assigned}

    VARA[VAR - 97] = 1;

    //    30        %continue

    goto L_000B;

    //    31      %finish

  L_0006:;

  //    32      pushop(op)

    PUSHOP(OP);

    //    33      pushopd(nextopd)

    PUSHOPD(NEXTOPD());

    //    34    %repeat

  L_000B:;
    goto L_0001;
  L_0003:;

  //    35    operate %while opsp>0

  L_000C:;
    if ((OPSP) <= (0)) goto L_000D;
    OPERATE();
    goto L_000C;
  L_000D:;

  //    36    %result=popopd

    _imp_result(POPOPD());

    //    37  !
    //    38  %routine pushopd(%real val)

    void PUSHOPD(float VAL) {
      _imp_enter(PUSHOPD);

      //    39    opdsp=opdsp+1

      OPDSP = OPDSP + 1;

      //    40    opds(opdsp)=val

      OPDS[OPDSP] = VAL;

      //    41  %end

      _imp_return;
      _imp_begin_backtrace(PUSHOPD);
      _imp_diagf("\n\nArguments:\n\n");
      if (__builtin_isnan(VAL))
        _imp_diagf("VAL = <UNASSIGNED REAL>\n");
      else
        _imp_diagf("VAL = %LE\n", (long double)VAL);
      _imp_diagf("\nLocal Variables:\n\n");
      _imp_end_backtrace();
    } /* end of PUSHOPD */

    //    42  !
    //    43  %routine pushop(%integer op)

    void PUSHOP(int OP) {
      _imp_enter(PUSHOP);

      //    44    opsp=opsp+1

      OPSP = OPSP + 1;

      //    45    ops(opsp)=op

      OPS[OPSP] = OP;

      //    46  %end

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

    //    47  !
    //    48  %realfn popopd

    float POPOPD(void) {
      _imp_enter(POPOPD);

      //    49  %real val

      float VAL = __builtin_nanf("");

      //    50    val=opds(opdsp)

      VAL = OPDS[OPDSP];

      //    51    opdsp=opdsp-1

      OPDSP = OPDSP - 1;

      //    52    %result=val

      _imp_result(VAL);

      //    53  %end

      _imp_begin_backtrace(POPOPD);
      _imp_diagf("\nLocal Variables:\n\n");
      _imp_bt_real(VAL);
      _imp_end_backtrace();
    } /* end of POPOPD */

    //    54  !
    //    55  %integerfn popop

    int POPOP(void) {
      _imp_enter(POPOP);

      //    56  %integer op

      int OP = 0xFEFEFEFE;

      //    57    op=ops(opsp)

      OP = OPS[OPSP];

      //    58    opsp=opsp-1

      OPSP = OPSP - 1;

      //    59    %result=op

      _imp_result(OP);

      //    60  %end

      _imp_begin_backtrace(POPOP);
      _imp_diagf("\nLocal Variables:\n\n");
      _imp_bt_int(OP);
      _imp_end_backtrace();
    } /* end of POPOP */

    //    61  !
    //    62  %realfn nextopd

    float NEXTOPD(void) {
      _imp_enter(NEXTOPD);

      //    63  %integer var

      int VAR = 0xFEFEFEFE;

      //    64  %real opd

      float OPD = __builtin_nanf("");

      //    65    skip symbol %while next symbol=space %or next symbol=nl

    L_000E:;
      if ((_imp_NEXTSYMBOL()) == (32)) goto L_000F;
      if ((_imp_NEXTSYMBOL()) != (10)) goto L_0010;
    L_000F:;
      _imp_SKIPSYMBOL();
      goto L_000E;
    L_0010:;

    //    66    %if '0'<=next symbol<='9' %or next symbol='-' %or next symbol='.' %c

      {
        int tmp;
        if ((48) > (tmp = _imp_NEXTSYMBOL())) goto L_0011;
        // Double-sided test on _imp_NEXTSYMBOL() (Part II)
        if (tmp <= (57)) goto L_0012;
      }
    L_0011:;
      if ((_imp_NEXTSYMBOL()) == (45)) goto L_0012;
      if ((_imp_NEXTSYMBOL()) != (46)) goto L_0013;
    L_0012:;
      _imp_READ(((_imp_name){.address = &OPD, .typeinfo = 2, .sizeinfo = sizeof(OPD)}));
      _imp_result(OPD);
    L_0013:;

    //    67+      %then read(opd) %and %result=opd
    //    68    %if next symbol='(' %then skip symbol %and %result=eval

      if ((_imp_NEXTSYMBOL()) != (40)) goto L_0014;
      _imp_SKIPSYMBOL();
      _imp_result(EVAL());
    L_0014:;

    //    69    %if 'a'<=next symbol<='z' %or 'A'<=next symbol<='Z' %then %start

      {
        int tmp;
        if ((97) > (tmp = _imp_NEXTSYMBOL())) goto L_0015;
        // Double-sided test on _imp_NEXTSYMBOL() (Part II)
        if (tmp <= (122)) goto L_0016;
      }
    L_0015:;
      {
        int tmp;
        if ((65) > (tmp = _imp_NEXTSYMBOL())) goto L_0017;
        // Double-sided test on _imp_NEXTSYMBOL() (Part II)
        if (tmp > (90)) goto L_0017;
      }
    L_0016:;

    //    70      read symbol(var)

      _imp_READSYMBOL(&VAR);

      //    71      %if 'A'<=var<='Z' %then var=var-'A'+'a'

      {
        int tmp;
        if ((65) > (tmp = VAR)) goto L_0018;
        // Double-sided test on VAR (Part II)
        if (tmp > (90)) goto L_0018;
      }
      VAR = VAR - 65 + 97;
    L_0018:;

    //    72      %if vara(var)=0 %then print symbol(var) %and %c

      if ((VARA[VAR - 97]) != (0)) goto L_0019;
      _imp_PRINTSYMBOL(VAR);
      _imp_printf("%s", " unassigned");
      _imp_signal(15, 0, 0, "");
    L_0019:;

    //    73+       print string(" unassigned") %and %signal 15
    //    74      %result=vars(var)

      _imp_result(VARS[VAR - 97]);

      //    75    %finish

    L_0017:;

    //    76    read symbol(var)

      _imp_READSYMBOL(&VAR);

      //    77    print symbol(var)

      _imp_PRINTSYMBOL(VAR);

      //    78    print string(" operand")

      _imp_printf("%s", " operand");

      //    79    %signal 15

      _imp_signal(15, 0, 0, "");

      //    80  %end

      _imp_signal(8, 4, 0, "result missing");
      _imp_begin_backtrace(NEXTOPD);
      _imp_diagf("\nLocal Variables:\n\n");
      _imp_bt_int(VAR);
      _imp_bt_real(OPD);
      _imp_end_backtrace();
    } /* end of NEXTOPD */

    //    81  !
    //    82  %integerfn nextop

    int NEXTOP(void) {
      _imp_enter(NEXTOP);

      //    83  %integer sym

      int SYM = 0xFEFEFEFE;

      //    84    skip symbol %while next symbol=space

    L_001A:;
      if ((_imp_NEXTSYMBOL()) != (32)) goto L_001B;
      _imp_SKIPSYMBOL();
      goto L_001A;
    L_001B:;

    //    85    read symbol(sym)

      _imp_READSYMBOL(&SYM);

      //    86    %if sym='+' %then %result=plus

      if ((SYM) != (43)) goto L_001C;
      _imp_result(1);
    L_001C:;

    //    87    %if sym='-' %then %result=minus

      if ((SYM) != (45)) goto L_001D;
      _imp_result(2);
    L_001D:;

    //    88    %if sym='*' %then %result=times

      if ((SYM) != (42)) goto L_001E;
      _imp_result(3);
    L_001E:;

    //    89    %if sym='/' %then %result=divide

      if ((SYM) != (47)) goto L_001F;
      _imp_result(4);
    L_001F:;

    //    90    %if sym='^' %then %result=exp

      if ((SYM) != (94)) goto L_0020;
      _imp_result(5);
    L_0020:;

    //    91    %if sym=':' %then %result=assign

      if ((SYM) != (58)) goto L_0021;
      _imp_result(6);
    L_0021:;

    //    92    %if sym=')' %or sym=nl %then %result=end

      if ((SYM) == (41)) goto L_0022;
      if ((SYM) != (10)) goto L_0023;
    L_0022:;
      _imp_result(7);
    L_0023:;

    //    93    print symbol(sym)

      _imp_PRINTSYMBOL(SYM);

      //    94    print string(" operator")

      _imp_printf("%s", " operator");

      //    95    %signal 15

      _imp_signal(15, 0, 0, "");

      //    96  %end

      _imp_signal(8, 4, 0, "result missing");
      _imp_begin_backtrace(NEXTOP);
      _imp_diagf("\nLocal Variables:\n\n");
      _imp_bt_int(SYM);
      _imp_end_backtrace();
    } /* end of NEXTOP */

    //    97  !
    //    98  %routine operate

    void OPERATE(void) {
      _imp_enter(OPERATE);

      //    99  %real opd1,opd2

      float OPD1 = __builtin_nanf("");
      float OPD2 = __builtin_nanf("");

      //   100  %switch opn(plus:exp)

      int OPN_index;
      int OPN_line;
      char *OPN_file;
      static const void *OPN[5] = {&&case_OPN_1, &&case_OPN_2, &&case_OPN_3, &&case_OPN_4, &&case_OPN_5};

      //   101    opd2=popopd

      OPD2 = POPOPD();

      //   102    opd1=popopd

      OPD1 = POPOPD();

      //   103    ->opn(popop)

      OPN_line = __LINE__;
      OPN_file = __FILE__;
      OPN_index = POPOP();
      goto case_OPN_dispatch;

      //   104  opn(plus):pushopd(opd1+opd2) ; %return

    case_OPN_1:;
      PUSHOPD(OPD1 + OPD2);
      _imp_return;

      //   105  opn(minus):pushopd(opd1-opd2) ; %return

    case_OPN_2:;
      PUSHOPD(OPD1 - OPD2);
      _imp_return;

      //   106  opn(times):pushopd(opd1*opd2) ; %return

    case_OPN_3:;
      PUSHOPD(OPD1 * OPD2);
      _imp_return;

      //   107  opn(divide):%if opd2=0 %then print string("divide by zero") %and %signal 15

    case_OPN_4:;
      if ((OPD2) != (0)) goto L_0025;
      _imp_printf("%s", "divide by zero");
      _imp_signal(15, 0, 0, "");
    L_0025:;

    //   108              pushopd(opd1/opd2) ; %return

      PUSHOPD((double)(OPD1 / OPD2));
      _imp_return;

      //   109  opn(exp):pushopd(opd1^int(opd2))

    case_OPN_5:;
      PUSHOPD(_imp_REXP(OPD1, _imp_INT(OPD2)));

      //   110  %end

      _imp_return;

    case_OPN_dispatch:
      if (OPN_index < 1 || OPN_index > 5)
        _imp_signalf(6, 3, OPN_index, "->OPN(%d) out of bounds at %s line %d", OPN_index, OPN_file, OPN_line);
      goto *OPN[OPN_index - 1];
      _imp_begin_backtrace(OPERATE);
      _imp_diagf("\nLocal Variables:\n\n");
      _imp_bt_real(OPD1);
      _imp_bt_real(OPD2);
      _imp_end_backtrace();
    } /* end of OPERATE */

    //   111  !
    //   112  %end  {of eval}

    _imp_signal(8, 4, 0, "result missing");
    _imp_begin_backtrace(EVAL);
    _imp_diagf("\nLocal Variables:\n\n");
    _imp_bt_int(OP);
    _imp_bt_int(OPDSP);
    _imp_bt_int(OPSP);
    _imp_bt_int(VAR);
    _imp_end_backtrace();
  } /* end of EVAL */

  //   113  %real val

  float VAL = __builtin_nanf("");

  //   114
  //   115    %on %event 9,15 %start    {to trap everything}

  if (!_imp_on_event(9, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31)) goto L_0026;

    //   116  {*} %stop %if Event_Event = 9

  if ((_imp_EVENT().EVENT) != (9)) goto L_0027;
  _imp_stop(0);
L_0027:;

//   117      print string("?

  _imp_printf("%s", "?\n");

  //   118+ ")
  //   119     ->again ; %finish

  goto U_1000;
L_0026:;

//   120
//   121  select input(0); select output(0)

  _imp_SELECTINPUT(0);
  _imp_SELECTOUTPUT(0);

  //   122        prompt("eval:")

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

  //   123  again:val=eval

U_1000:;
  VAL = EVAL();

  //   124        %if val=int(val) %then write(int(val),0) { imp77 perms missing} {%else print(val,0,4)}

  if ((VAL) != (_imp_INT(VAL))) goto L_0028;
  _imp_WRITE(_imp_INT(VAL), 0);
L_0028:;

//   125        newline

  _imp_NEWLINE();

  //   126        ->again

  goto U_1000;

  //   127
  //   128  %endofprogram

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(VAL);
  _imp_end_backtrace();
  _imp_stop(0);
}