(* Collection of code snippets by Arne Vajhøj *)
(* (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) *)
program expression_evaluation(input,output);

type
   etype = (econstant,eplus,eminus,emul,ediv);
   e = record
          typ : etype;
          val : integer;
       end;

var
   elm : array [1..9] of e;   
   stack : array[1..100] of integer;
   top,i,res : integer;

begin
   elm[1].typ := econstant;
   elm[1].val := 34;
   elm[2].typ := econstant;
   elm[2].val := 34;
   elm[3].typ := eplus;
   elm[4].typ := econstant;
   elm[4].val := 4;
   elm[5].typ := econstant;
   elm[5].val := 2;
   elm[6].typ := ediv;
   elm[7].typ := econstant;
   elm[7].val := 3;
   elm[8].typ := emul;
   elm[9].typ := eminus;
   top := 0;
   for i := 1 to 9 do begin
      case elm[i].typ of
         econstant :
            begin
               top := top + 1;
               stack[top] := elm[i].val;
            end;
         eplus :
            begin
               res := stack[top-1] + stack[top];
               stack[top-1] := res;
               top := top - 1;
            end;
         eminus :
            begin
               res := stack[top-1] - stack[top];
               stack[top-1] := res;
               top := top - 1;
            end;
         emul :
            begin
               res := stack[top-1] * stack[top];
               stack[top-1] := res;
               top := top - 1;
            end;
         ediv :
            begin
               res := stack[top-1] div stack[top];
               stack[top-1] := res;
               top := top - 1;
            end;
      end;
   end;
   res := stack[1];
   writeln('result=',res);
end.
