(* 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 script;

{$APPTYPE CONSOLE}

uses
  SysUtils, uPSCompiler, uPSRuntime;

type
  TF = function() : string of object;

function eval(code : string) : string;

var
  i : integer;
  f : TF;
  comp : TPSPascalCompiler;
  icode : string;
  exec : TPSExec;

begin
  comp := TPSPascalCompiler.Create;
  comp.AllowNoBegin := True;
  comp.AllowNoEnd := True;
  if not comp.Compile('function f:string; begin result := IntToStr(' + code + '); end;') then begin
    for i := 0 to comp.MsgCount-1 do begin
      writeln(comp.Msg[i].Pos, ' ', comp.Msg[i].Param);
    end;
  end;
  comp.GetOutput(icode);
  comp.Free;
  exec := TPSExec.Create;
  exec.LoadData(icode);
  f := TF(exec.GetProcAsMethodN('f'));
  result := f();
  exec.Free;
end;

begin
  writeln('1+2+3=',eval('1+2+3'));
  writeln('2*2+3*3=',eval('2*2+3*3'));
end.
