Difference between revisions of "Pascal Script Examples/es"

From Lazarus wiki
Jump to navigationJump to search
Line 1: Line 1:
 
[[Category:Castellano]][[Category:Español]]{{Pascal Script Examples}}
 
[[Category:Castellano]][[Category:Español]]{{Pascal Script Examples}}
  
<delphi>program psce;
+
<delphi> program psce;
{$APPTYPE CONSOLE}
+
{$APPTYPE CONSOLE}
  
{$IFDEF FPC}
+
{$IFDEF FPC}
{$mode delphi}{$H+}
+
{$mode delphi}{$H+}
{$ENDIF}
+
{$ENDIF}
  
uses
+
uses
  SysUtils,interfaces,Classes,Forms,uPSCompiler, uPSR_std, uPSC_std, uPSR_classes, uPSC_classes, uPSRuntime,
+
  SysUtils,interfaces,Classes,Forms,uPSCompiler, uPSR_std, uPSC_std, uPSR_classes, uPSC_classes, uPSRuntime,
  uPSComponent,uPSDisassembly,uPSR_dateutils,uPSC_dateutils,uPSC_forms,uPSR_forms,uPSC_controls,uPSR_controls,
+
  uPSComponent,uPSDisassembly,uPSR_dateutils,uPSC_dateutils,uPSC_forms,uPSR_forms,uPSC_controls,uPSR_controls,
  uPSR_dll,uPSC_dll;
+
  uPSR_dll,uPSC_dll;
  
type
+
type
  TPSCE = class
+
  TPSCE = class
  protected
+
    protected
    FScr: TPSScript;
+
    FScr: TPSScript;
    procedure SaveCompiled(var Data : String);
+
    procedure SaveCompiled(var Data : String);
    procedure SaveDissasembly(var Data : String);
+
    procedure SaveDissasembly(var Data : String);
    procedure OnCompile(Sender: TPSScript);
+
    procedure OnCompile(Sender: TPSScript);
    procedure OnExecImport(Sender: TObject; se: TPSExec; x: TPSRuntimeClassImporter);
+
    procedure OnExecImport(Sender: TObject; se: TPSExec; x: TPSRuntimeClassImporter);
  public
+
    public
    constructor Create;
+
    constructor Create;
    destructor Destroy; override;
+
    destructor Destroy; override;
    function Compile(const FileName: string): Boolean;
+
    function Compile(const FileName: string): Boolean;
    function Execute: Boolean;
+
    function Execute: Boolean;
  end;
+
  end;
  
 +
var
 +
  aPSCE: TPSCE;
 +
  SFile,Data : String;
  
var
+
procedure MWritedt(d : TDateTime);
   aPSCE: TPSCE;
+
var
  SFile,Data : String;
+
   s : String;
 +
begin
 +
  s := DateToStr(d) + ' ' + TimeToStr(d);
 +
  Write(s);
 +
end;
  
 +
procedure MWrites(const s: string);
 +
begin
 +
  Write(s);
 +
end;
  
procedure MWritedt(d : TDateTime);
+
procedure MWritei(const i: Integer);
var
+
  begin
  s : String;
+
  Write(i);
begin
+
end;
  s := DateToStr(d) + ' ' + TimeToStr(d);
 
  Write(s);
 
end;
 
  
 +
procedure MWrited(const d: Double);
 +
begin
 +
  Write(d:0:1);
 +
end;
  
procedure MWrites(const s: string);
+
procedure MWriteln;
begin
+
begin
  Write(s);
+
  Writeln;
end;
+
end;
  
 +
procedure MVal(const s: string; var n, z: Integer);
 +
begin
 +
  Val(s, n, z);
 +
end;
  
 +
constructor TPSCE.Create;
 +
begin
 +
  FScr:=TPSScript.Create(nil);
 +
  FScr.OnCompile:= OnCompile;
 +
  FScr.OnExecImport := OnExecImport;
 +
end;
  
procedure MWritei(const i: Integer);
+
destructor TPSCE.Destroy;
begin
+
begin
  Write(i);
+
  FScr.Free;
end;
+
end;
  
procedure MWrited(const d: Double);
+
procedure TPSCE.SaveCompiled(var Data : String);
begin
+
var
  Write(d:0:1);
+
  OutFile: string;
end;
+
  Fx : Longint ;
 +
begin
 +
  OutFile := ExtractFilePath(ParamStr(0)) + ChangeFileExt(SFile,'.out');
 +
  Fx:= FileCreate(OutFile) ;
 +
  FileWrite(Fx,Data[1],Length(Data));
 +
  FileClose (Fx) ;
 +
end;
  
procedure MWriteln;
+
procedure TPSCE.SaveDissasembly(var Data : String);
begin
+
var
  Writeln;
+
  OutFile: string;
end;
+
  Fx : Longint ;
 +
begin
 +
  OutFile := ExtractFilePath(ParamStr(0)) + ChangeFileExt(SFile,'.dis');
 +
  Fx:= FileCreate(OutFile) ;
 +
  FileWrite(Fx,Data[1],Length(Data));
 +
  FileClose (Fx) ;
 +
end;
  
procedure MVal(const s: string; var n, z: Integer);
+
procedure TPSCE.OnExecImport(Sender: TObject; se: TPSExec; x: TPSRuntimeClassImporter);
begin
+
begin
   Val(s, n, z);
+
   RIRegister_Std(x);
end;
+
  RIRegister_Classes(x,true);
 +
  RIRegister_Forms(x);
 +
  RIRegister_Controls(x);
 +
  RegisterDateTimeLibrary_R(se);
 +
  RegisterDLLRuntime(se);
 +
end;
  
constructor TPSCE.Create;
+
procedure TPSCE.OnCompile(Sender: TPSScript);
begin
+
begin
  FScr:=TPSScript.Create(nil);
+
  RegisterDateTimeLibrary_C(Sender.Comp);
  FScr.OnCompile:= OnCompile;
+
  Sender.AddFunction(@MWrites, 'procedure Writes(const s: string)');
  FScr.OnExecImport := OnExecImport;
+
  Sender.AddFunction(@MWritedt,'procedure WriteDT(d : TDateTime)');
end;
+
  Sender.AddFunction(@MWritei, 'procedure Writei(const i: Integer)');
 +
  Sender.AddFunction(@MWrited, 'procedure Writed(const f: Double)');
 +
  Sender.AddFunction(@MWriteln, 'procedure Writeln');
 +
  Sender.AddFunction(@MVal, 'procedure Val(const s: string; var n, z: Integer)');
 +
  SIRegister_Std(Sender.Comp);
 +
  SIRegister_Classes(Sender.Comp,true);
 +
  SIRegister_Forms(Sender.Comp);
 +
  SIRegister_Controls(Sender.Comp);
 +
end;
  
destructor TPSCE.Destroy;
+
function TPSCE.Compile(const FileName: string): Boolean;
begin
+
var
  FScr.Free;
+
  S: TStringList;
end;
+
  i: Integer;
 +
begin
 +
  Result:=False;
 +
  if FileExists(FileName) then begin
 +
    S:=TStringList.Create;
 +
    S.LoadFromFile(FileName);
 +
    FScr.Script:=S;
 +
    Result:=FScr.Compile;
 +
    S.Free;
 +
    if not Result then
 +
      if FScr.CompilerMessageCount &gt; 0 then
 +
        for i:=0 to FScr.CompilerMessageCount-1 do
 +
          Writeln(FScr.CompilerErrorToStr(i));
 +
  end else Writeln('File not found: ', FileName);
 +
end;
  
procedure TPSCE.SaveCompiled(var Data : String);
+
function TPSCE.Execute: Boolean;
var
+
begin
  OutFile: string;
+
  Result:=FScr.Execute;
  Fx : Longint ;
+
  if not Result then
begin
+
    Writeln('Run-time error:' + FScr.ExecErrorToString);
  OutFile := ExtractFilePath(ParamStr(0)) + ChangeFileExt(SFile,'.out');
+
end;
  Fx:= FileCreate(OutFile) ;
 
  FileWrite(Fx,Data[1],Length(Data));
 
  FileClose (Fx) ;
 
end;
 
  
procedure TPSCE.SaveDissasembly(var Data : String);
+
begin
var
+
  Application.Initialize;
  OutFile: string;
+
  aPSCE:=TPSCE.Create;
  Fx : Longint ;
+
  if ParamCount = 0 then  
begin
+
  begin
  OutFile := ExtractFilePath(ParamStr(0)) + ChangeFileExt(SFile,'.dis');
+
    Writeln('Utilización: ', ExtractFileName(ParamStr(0)), ' [--compile|--dissasembly] &lt;script.pss&gt;');
  Fx:= FileCreate(OutFile) ;
+
    Writeln('');
  FileWrite(Fx,Data[1],Length(Data));
+
    Writeln('--compile : Guardar el 'bytecode' del programitia compilado');
  FileClose (Fx) ;
+
    Writeln('--dissasembly: Guardar el desensamblado del programita');
end;
+
    Exit;
 
+
  end;
 
 
 
 
procedure TPSCE.OnExecImport(Sender: TObject; se: TPSExec; x: TPSRuntimeClassImporter);
 
begin
 
RIRegister_Std(x);
 
RIRegister_Classes(x,true);
 
RIRegister_Forms(x);
 
RIRegister_Controls(x);
 
RegisterDateTimeLibrary_R(se);
 
RegisterDLLRuntime(se);
 
end;
 
 
 
procedure TPSCE.OnCompile(Sender: TPSScript);
 
begin
 
  RegisterDateTimeLibrary_C(Sender.Comp);
 
  Sender.AddFunction(@MWrites, 'procedure Writes(const s: string)');
 
  Sender.AddFunction(@MWritedt,'procedure WriteDT(d : TDateTime)');
 
  Sender.AddFunction(@MWritei, 'procedure Writei(const i: Integer)');
 
  Sender.AddFunction(@MWrited, 'procedure Writed(const f: Double)');
 
  Sender.AddFunction(@MWriteln, 'procedure Writeln');
 
  Sender.AddFunction(@MVal, 'procedure Val(const s: string; var n, z: Integer)');
 
  SIRegister_Std(Sender.Comp);
 
  SIRegister_Classes(Sender.Comp,true);
 
  SIRegister_Forms(Sender.Comp);
 
  SIRegister_Controls(Sender.Comp);
 
end;
 
 
 
function TPSCE.Compile(const FileName: string): Boolean;
 
var
 
  S: TStringList;
 
  i: Integer;
 
begin
 
  Result:=False;
 
  if FileExists(FileName) then begin
 
    S:=TStringList.Create;
 
    S.LoadFromFile(FileName);
 
    FScr.Script:=S;
 
    Result:=FScr.Compile;
 
    S.Free;
 
    if not Result then
 
      if FScr.CompilerMessageCount &gt; 0 then
 
        for i:=0 to FScr.CompilerMessageCount-1 do
 
          Writeln(FScr.CompilerErrorToStr(i));
 
  end else Writeln('File not found: ', FileName);
 
end;
 
 
 
function TPSCE.Execute: Boolean;
 
begin
 
  Result:=FScr.Execute;
 
  if not Result then
 
    Writeln('Run-time error:' + FScr.ExecErrorToString);
 
end;
 
 
 
 
 
 
 
 
 
begin
 
  Application.Initialize;
 
  aPSCE:=TPSCE.Create;
 
  if ParamCount = 0 then  
 
  begin
 
    Writeln('Utilización: ', ExtractFileName(ParamStr(0)), ' [--compile|--dissasembly] &lt;script.pss&gt;');
 
    Writeln('');
 
    Writeln('--compile : Guardar el 'bytecode' del programitia compilado');
 
    Writeln('--dissasembly: Guardar el desensamblado del programita');
 
    Exit;
 
  end;
 
 
    
 
    
SFile := ParamStr(1);
+
  SFile := ParamStr(1);
if (ParamStr(1)='--compile') or (ParamStr(1)='--dissasembly') then
+
  if (ParamStr(1)='--compile') or (ParamStr(1)='--dissasembly') then
begin
 
  SFile := ParamStr(2);
 
  aPSCE.Compile(SFile);
 
  aPSCE.FScr.GetCompiled(Data);
 
  if Paramstr(1)='--compile' then aPSCE.SaveCompiled(Data);
 
  if Paramstr(1)='--dissasembly' then
 
 
   begin
 
   begin
     if not IFPS3DataToText(Data,Data) then Writeln('¡No puedo crear el desensamblado!')
+
     SFile := ParamStr(2);
 +
    aPSCE.Compile(SFile);
 +
    aPSCE.FScr.GetCompiled(Data);
 +
    if Paramstr(1)='--compile' then aPSCE.SaveCompiled(Data);
 +
    if Paramstr(1)='--dissasembly' then
 +
    begin
 +
      if not IFPS3DataToText(Data,Data) then Writeln('¡No puedo crear el desensamblado!')
 
     else
 
     else
    aPSCE.SaveDissasembly(Data);
+
    aPSCE.SaveDissasembly(Data);
  end;
+
  end;
Exit;
+
  Exit;
 
  end;
 
  end;
 
   
 
   
  
aPSCE.Compile(SFile);
+
  aPSCE.Compile(SFile);
aPSCE.Execute;
+
  aPSCE.Execute;
aPSCE.Free;
+
  aPSCE.Free;
end.</delphi>
+
end.</delphi>
  
 
--[[User:Forest|Forest]] 23:00, 22 Oct 2005 (CEST)
 
--[[User:Forest|Forest]] 23:00, 22 Oct 2005 (CEST)
 
--Versión española: [User:iskarelectrica|iskarelectrica (jldc)]- julio 2008.
 
--Versión española: [User:iskarelectrica|iskarelectrica (jldc)]- julio 2008.

Revision as of 14:40, 8 July 2008

English (en) español (es)

<delphi> program psce;

{$APPTYPE CONSOLE}
{$IFDEF FPC}
{$mode delphi}{$H+}
{$ENDIF}
uses
  SysUtils,interfaces,Classes,Forms,uPSCompiler, uPSR_std, uPSC_std, uPSR_classes, uPSC_classes, uPSRuntime,
  uPSComponent,uPSDisassembly,uPSR_dateutils,uPSC_dateutils,uPSC_forms,uPSR_forms,uPSC_controls,uPSR_controls,
  uPSR_dll,uPSC_dll;
type
  TPSCE = class
   protected
    FScr: TPSScript;
    procedure SaveCompiled(var Data : String);
    procedure SaveDissasembly(var Data : String);
    procedure OnCompile(Sender: TPSScript);
    procedure OnExecImport(Sender: TObject; se: TPSExec; x: TPSRuntimeClassImporter);
   public
    constructor Create;
    destructor Destroy; override;
    function Compile(const FileName: string): Boolean;
    function Execute: Boolean;
  end;
var
  aPSCE: TPSCE;
  SFile,Data : String;
procedure MWritedt(d : TDateTime);
var
 s : String;
begin
  s := DateToStr(d) + ' ' + TimeToStr(d);
  Write(s);
end;
procedure MWrites(const s: string);
begin
  Write(s);
end;
procedure MWritei(const i: Integer);
begin
  Write(i);
end;
procedure MWrited(const d: Double);
begin
  Write(d:0:1);
end;
procedure MWriteln;
begin
  Writeln;
end;
procedure MVal(const s: string; var n, z: Integer);
begin
  Val(s, n, z);
end;
constructor TPSCE.Create;
begin
  FScr:=TPSScript.Create(nil);
  FScr.OnCompile:= OnCompile;
  FScr.OnExecImport := OnExecImport;
end;
destructor TPSCE.Destroy;
begin
  FScr.Free;
end;
procedure TPSCE.SaveCompiled(var Data : String);
var
  OutFile: string;
  Fx : Longint ;
begin
  OutFile := ExtractFilePath(ParamStr(0)) + ChangeFileExt(SFile,'.out');
  Fx:= FileCreate(OutFile) ;
  FileWrite(Fx,Data[1],Length(Data));
  FileClose (Fx) ;
end;
procedure TPSCE.SaveDissasembly(var Data : String);
var
  OutFile: string;
  Fx : Longint ;
begin
  OutFile := ExtractFilePath(ParamStr(0)) + ChangeFileExt(SFile,'.dis');
  Fx:= FileCreate(OutFile) ;
  FileWrite(Fx,Data[1],Length(Data));
  FileClose (Fx) ;
end;
procedure TPSCE.OnExecImport(Sender: TObject; se: TPSExec; x: TPSRuntimeClassImporter);
begin
 RIRegister_Std(x);
 RIRegister_Classes(x,true);
 RIRegister_Forms(x);
 RIRegister_Controls(x);
 RegisterDateTimeLibrary_R(se);
 RegisterDLLRuntime(se);
end;
procedure TPSCE.OnCompile(Sender: TPSScript);
begin
  RegisterDateTimeLibrary_C(Sender.Comp);
  Sender.AddFunction(@MWrites, 'procedure Writes(const s: string)');
  Sender.AddFunction(@MWritedt,'procedure WriteDT(d : TDateTime)');
  Sender.AddFunction(@MWritei, 'procedure Writei(const i: Integer)');
  Sender.AddFunction(@MWrited, 'procedure Writed(const f: Double)');
  Sender.AddFunction(@MWriteln, 'procedure Writeln');
  Sender.AddFunction(@MVal, 'procedure Val(const s: string; var n, z: Integer)');
  SIRegister_Std(Sender.Comp);
  SIRegister_Classes(Sender.Comp,true);
  SIRegister_Forms(Sender.Comp);
  SIRegister_Controls(Sender.Comp);
end;
function TPSCE.Compile(const FileName: string): Boolean;
var
  S: TStringList;
  i: Integer;
begin
  Result:=False;
  if FileExists(FileName) then begin
    S:=TStringList.Create;
    S.LoadFromFile(FileName);
    FScr.Script:=S;
    Result:=FScr.Compile;
    S.Free;
    if not Result then
      if FScr.CompilerMessageCount > 0 then
        for i:=0 to FScr.CompilerMessageCount-1 do
          Writeln(FScr.CompilerErrorToStr(i));
  end else Writeln('File not found: ', FileName);
end;
function TPSCE.Execute: Boolean;
begin
  Result:=FScr.Execute;
  if not Result then
    Writeln('Run-time error:' + FScr.ExecErrorToString);
end;
begin
  Application.Initialize;
  aPSCE:=TPSCE.Create;
  if ParamCount = 0 then 
  begin
    Writeln('Utilización: ', ExtractFileName(ParamStr(0)), ' [--compile|--dissasembly] <script.pss>');
    Writeln();
    Writeln('--compile : Guardar el 'bytecode' del programitia compilado');
    Writeln('--dissasembly: Guardar el desensamblado del programita');
    Exit;
  end;
 
 SFile := ParamStr(1);
 if (ParamStr(1)='--compile') or (ParamStr(1)='--dissasembly') then
  begin
   SFile := ParamStr(2);
   aPSCE.Compile(SFile);
   aPSCE.FScr.GetCompiled(Data);
   if Paramstr(1)='--compile' then aPSCE.SaveCompiled(Data);
   if Paramstr(1)='--dissasembly' then
    begin
     if not IFPS3DataToText(Data,Data) then Writeln('¡No puedo crear el desensamblado!')
   else
    aPSCE.SaveDissasembly(Data);
  end;
 Exit;
end;

 aPSCE.Compile(SFile);
 aPSCE.Execute;
 aPSCE.Free;
end.</delphi>

--Forest 23:00, 22 Oct 2005 (CEST) --Versión española: [User:iskarelectrica|iskarelectrica (jldc)]- julio 2008.