1
0
mirror of https://github.com/moparisthebest/Simba synced 2024-12-03 06:12:14 -05:00
Simba/Projects/lape-wrappers/main.pas
Merlijn Wajer 7f00bb6805 Squashed commit of the following:
commit 7426db160f
Author: Merlijn Wajer <merlijn@wizzup.org>
Date:   Fri Aug 5 11:40:27 2011 +0200

    Lape: Update HEAD.

commit 6fa1740ea7
Author: Niels <niels.a.d@gmail.com>
Date:   Thu Aug 4 23:07:35 2011 +0200

    Simba compiles again now.

commit 27b1c42b6a
Author: Niels <niels.a.d@gmail.com>
Date:   Thu Aug 4 00:31:28 2011 +0200

    Little bugfix in lape wrapper generator + new wrappers.

commit f46f841658
Author: Niels <niels.a.d@gmail.com>
Date:   Thu Aug 4 00:00:54 2011 +0200

    Commented functions that cannot be imported yet for lape.

commit b7aa543b6b
Author: Niels <niels.a.d@gmail.com>
Date:   Wed Aug 3 23:54:54 2011 +0200

    New lape wrappers.

commit ecfc435fab
Author: Niels <niels.a.d@gmail.com>
Date:   Wed Aug 3 23:49:54 2011 +0200

    Fixed a bug in lape wrapper generation.

commit 2d9d65000b
Author: Merlijn Wajer <merlijn@wizzup.org>
Date:   Wed Aug 3 21:52:01 2011 +0200

    Lape: Most types added. Interpreter starts.

    Lacks a few methods, but most tests just work. :-)

commit bf0f806ecf
Author: Niels <niels.a.d@gmail.com>
Date:   Wed Aug 3 21:06:56 2011 +0200

    Added debug message for when initializing the interpreter fails.

commit a143016beb
Author: Merlijn Wajer <merlijn@wizzup.org>
Date:   Wed Aug 3 20:30:11 2011 +0200

    Lape-wrappers: Fix by nielsie95.

commit 9deb9a1b67
Author: Merlijn Wajer <merlijn@wizzup.org>
Date:   Wed Aug 3 20:29:48 2011 +0200

    Lape: Add wrappers.

commit 510674570b
Author: Merlijn Wajer <merlijn@wizzup.org>
Date:   Wed Aug 3 20:12:26 2011 +0200

    Lape: Update HEAD.

commit 8260840b89
Author: Merlijn Wajer <merlijn@wizzup.org>
Date:   Wed Aug 3 14:34:48 2011 +0200

    Lape: Update HEAD.

commit 64bf115d30
Merge: 0701e91 aaafd6b
Author: Merlijn Wajer <merlijn@wizzup.org>
Date:   Mon Aug 1 19:26:15 2011 +0200

    Merge branch 'master' into lape-integration

commit 0701e911d1
Merge: 26693f7 a20a31a
Author: Merlijn Wajer <merlijn@wizzup.org>
Date:   Sun Jul 31 18:11:58 2011 +0200

    Merge branch 'master' into lape-integration

commit 26693f7d4d
Merge: e124ae3 da0de6e
Author: Merlijn Wajer <merlijn@wizzup.org>
Date:   Sat Jul 30 00:16:12 2011 +0200

    Merge branch 'master' into lape-integration

commit e124ae396a
Merge: 4c6f304 c1051ef
Author: Merlijn Wajer <merlijn@wizzup.org>
Date:   Thu Jul 28 20:53:01 2011 +0200

    Merge branch 'lape-integration' of github.com:MerlijnWajer/Simba into lape-integration

commit 4c6f30462c
Author: Merlijn Wajer <merlijn@wizzup.org>
Date:   Thu Jul 28 20:52:13 2011 +0200

    Lape: Update head.

commit 2d8fdc4d7a
Author: Merlijn Wajer <merlijn@wizzup.org>
Date:   Thu Jul 28 20:08:01 2011 +0200

    Lape: Add Wrappers project.

commit e4f8e06f1c
Merge: 7228a88 1fd51ff
Author: Merlijn Wajer <merlijn@wizzup.org>
Date:   Thu Jul 28 20:03:39 2011 +0200

    Merge branch 'master' into lape-integration

    Conflicts:
    	Projects/Simba/Simba.inc

commit c1051efbeb
Merge: 7228a88 b83a277
Author: Merlijn Wajer <merlijn@wizzup.org>
Date:   Sat Jul 23 13:41:15 2011 -0700

    Merge pull request #30 from Dgby714/lape-integration

    TLPThread Update

commit 7228a888c4
Merge: ad2e3ca e63dcbc
Author: Merlijn Wajer <merlijn@wizzup.org>
Date:   Sat Jul 23 21:54:58 2011 +0200

    Merge branch 'master' into lape-integration

    Conflicts:
    	Projects/Simba/Simba.inc
    	Projects/Simba/simbaunit.pas
    	Units/lape

commit b83a277502
Author: John P (Dgby714) <Dgby714@gmail.com>
Date:   Thu Jul 7 12:22:51 2011 -0400

    TLPThread Update
    	- Init Result to False in OnHandleDirective.
    	- Override lape _writeln, Now adds to Debug memo.

commit ad2e3ca3c3
Merge: 1a22452 c79eadf
Author: Merlijn Wajer <merlijn@wizzup.org>
Date:   Sat Jun 25 15:50:58 2011 -0700

    Merge pull request #25 from Dgby714/la-pe

    Lape

commit 1a224529f3
Merge: 5dc7611 89f1a9b
Author: Merlijn Wajer <merlijn@wizzup.org>
Date:   Sun Jun 26 00:49:03 2011 +0200

    Merge branch 'master' into lape-integration

commit c79eadf1ef
Author: John P (Dgby714) <Dgby714@gmail.com>
Date:   Sat Jun 25 18:43:17 2011 -0400

    TLPThread update, Uncommented Lape Defines

commit 4d15ee07d5
Merge: 89f1a9b 5dc7611
Author: John P (Dgby714) <Dgby714@gmail.com>
Date:   Sat Jun 25 17:23:24 2011 -0400

    Merge branch 'lape-integration' of git://github.com/MerlijnWajer/Simba into la-pe

commit 5dc7611dba
Author: John P (Dgby714) <Dgby714@gmail.com>
Date:   Sat Jun 25 07:35:20 2011 -0400

    TLPThread update, Lape settings, Add Lape unit to OtherUnitFiles

commit e5812a8000
Author: John P (Dgby714) <Dgby714@gmail.com>
Date:   Thu Jun 16 21:26:03 2011 -0400

    Lape Integration, TLPThread work

commit ecab4d5801
Author: Merlijn Wajer <merlijn@wizzup.org>
Date:   Sat Jun 25 17:45:24 2011 +0200

    Lape: Add lape submodule.
2011-08-05 13:04:52 +02:00

205 lines
4.6 KiB
ObjectPascal

unit main;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
StdCtrls, SynEdit, SynHighlighterPas,wrapfiles;
type
{ TfrmMain }
TfrmMain = class(TForm)
btnGo: TButton;
btnAdvanced: TButton;
pnlMain: TPanel;
Splitter1: TSplitter;
Splitter2: TSplitter;
eIn: TSynEdit;
eOut: TSynEdit;
eDebug: TSynEdit;
PasHL: TSynPasSyn;
procedure btnAdvancedClick(Sender: TObject);
procedure btnGoClick(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
end;
procedure ConvertRT(Input, Dbg, Output : TStrings; procnames : TStrings = nil);
var
frmMain: TfrmMain;
implementation
uses
v_ideCodeParser;
{$R *.lfm}
{ TfrmMain }
procedure ConvertRT(Input, Dbg, Output : TStrings; procnames : TStrings = nil);
procedure Debug(s: string); overload;
begin
if (Trim(Output.Text) <> '') then
Dbg.Append(s)
else
Dbg.Text := s;
end;
procedure Debug(v: Variant); overload;
begin
Debug(string(v));
end;
procedure Write(s: string); overload;
begin
if (Trim(Output.Text) <> '') then
Output.Text := Output.Text + s
else
Output.Text := s;
end;
procedure Write(v: Variant); overload;
begin
Write(string(v));
end;
function FixName( str : string) : string;
begin
if (length(str) > 3) and (str[1] = 'p') and (str[2] = 's') and (str[3] = '_') then
result := Copy(str,4,length(str)-3)
else
result := str;
end;
function PtrName ( str : string) : String;
begin
debug(str);
if (length(str) > 1) and (str[1] in ['T','t']) then
result := 'P' + copy(str,2,length(str)-1)
else
result := 'P' + str;
debug(result);
end;
var
p: TCodeParser;
m: TMemoryStream;
a, b, c: TDeclarationArray;
i, ii, iii, pc: Integer;
s: string;
d: TDeclaration;
Fail: Boolean;
begin
p := TCodeParser.Create;
m := TMemoryStream.Create;
try
Output.BeginUpdate;
Output.Clear;
Dbg.BeginUpdate;
Dbg.Clear;
Input.SaveToStream(m);
try
p.Run(m);
except on E : Exception do
Debug(e.Message);
end;
a := p.Items.GetItemsOfClass(TciProcedureDeclaration);
Debug('Start converting '+IntToStr(Length(a))+' methods!');
for i := 0 to High(a) do
with TciProcedureDeclaration(a[i]) do
begin
if (Name = nil) then
begin
Debug('No name found, skipping..');
Continue;
end;
d := Items.GetFirstItemOfClass(TciReturnType);
if (d <> nil) then
begin
s := 'procedure Lape_'+FixName(Name.ShortText)+
'(const Params: PParamArray; const Result: Pointer);'+LineEnding+
'begin'+LineEnding+
' '+PtrName(d.ShortText)+'(Result)^ := ';
end else
begin
s := 'procedure Lape_'+FixName(Name.ShortText)+'(const Params: PParamArray);'+LineEnding+
'begin'+LineEnding+' ';
end;
s := s+Name.ShortText+'(';
Fail := False;
pc := 0;
b := GetParamDeclarations();
for ii := 0 to High(b) do
begin
d := b[ii].Items.GetFirstItemOfClass(TciParameterType);
if (d = nil) then
begin
Debug('No parameter type found in '+Name.ShortText+', skipping..');
Fail := True;
Break;
end;
c := b[ii].Items.GetItemsOfClass(TciParameterName);
if (Length(c) < 1) then
begin
Debug('No parameter names found in '+Name.ShortText+', skipping..');
Fail := True;
Break;
end;
for iii := 0 to High(c) do
begin
if (pc > 0) then
s := s+', ';
s := s+PtrName(d.ShortText)+'(Params^['+IntToStr(pc)+'])^';
Inc(pc);
end;
end;
if Fail then
Continue;
s := s+');'+LineEnding+'end;';
if (i > 0) then
s := LineEnding+s;
Write(s);
if procnames <> nil then
procnames.Add('AddGlobalFunc('#39 + CleanDeclaration + #39', @Lape_'+FixName(name.ShortText)+');')
else
Debug('Prog-name "AddGlobalFunc('#39 + CleanDeclaration + #39', @Lape_'+FixName(name.ShortText)+');"');
Debug('Done "'+Name.ShortText+'"!');
end;
finally
m.Free;
p.Free;
Output.EndUpdate;
Dbg.EndUpdate;
end;
Debug('Done :)');
end;
procedure TfrmMain.btnGoClick(Sender: TObject);
begin
ConvertRT(eIn.Lines,eDebug.Lines,eOut.Lines);
end;
procedure TfrmMain.btnAdvancedClick(Sender: TObject);
begin
WrapFilesForm.ShowModal;
end;
end.