* First implementation of pas2js converter program

git-svn-id: trunk@27476 -
This commit is contained in:
michael 2014-04-05 09:19:55 +00:00
parent e05b77e759
commit 181892cc5f
13 changed files with 3118 additions and 0 deletions

12
.gitattributes vendored
View File

@ -15076,6 +15076,18 @@ utils/pas2jni/pas2jni.pas svneol=native#text/plain
utils/pas2jni/ppuparser.pas svneol=native#text/plain
utils/pas2jni/readme.txt svneol=native#text/plain
utils/pas2jni/writer.pas svneol=native#text/plain
utils/pas2js/Makefile svneol=native#text/plain
utils/pas2js/Makefile.fpc svneol=native#text/plain
utils/pas2js/fpmake.pp svneol=native#text/plain
utils/pas2js/pas2js.lpi svneol=native#text/plain
utils/pas2js/pas2js.pp svneol=native#text/plain
utils/pas2js/samples/arraydemo.pp svneol=native#text/plain
utils/pas2js/samples/fordemo.pp svneol=native#text/plain
utils/pas2js/samples/fordowndemo.pp svneol=native#text/plain
utils/pas2js/samples/hello.pas svneol=native#text/plain
utils/pas2js/samples/ifdemo.pp svneol=native#text/plain
utils/pas2js/samples/repeatdemo.pp svneol=native#text/plain
utils/pas2js/samples/whiledemo.pp svneol=native#text/plain
utils/pas2ut/Makefile svneol=native#text/plain
utils/pas2ut/Makefile.fpc svneol=native#text/plain
utils/pas2ut/Makefile.fpc.fpcmake svneol=native#text/plain

2691
utils/pas2js/Makefile Normal file

File diff suppressed because it is too large Load Diff

93
utils/pas2js/Makefile.fpc Normal file
View File

@ -0,0 +1,93 @@
#
# Makefile.fpc for running fpmake
#
[package]
name=pas2js
version=2.7.1
[require]
packages=rtl pastojs fcl-js fcl-passrc
[install]
fpcpackage=y
[default]
fpcdir=../..
[prerules]
FPMAKE_BIN_CLEAN=$(wildcard ./fpmake$(SRCEXEEXT))
ifdef OS_TARGET
FPC_TARGETOPT+=--os=$(OS_TARGET)
endif
ifdef CPU_TARGET
FPC_TARGETOPT+=--cpu=$(CPU_TARGET)
endif
LOCALFPMAKE=./fpmake$(SRCEXEEXT)
[rules]
# Do not pass the Makefile's unit and binary target locations. fpmake uses it's own.
override FPCOPT:=$(filter-out -FU%,$(FPCOPT))
override FPCOPT:=$(filter-out -FE%,$(FPCOPT))
# Compose general fpmake-parameters
ifdef FPMAKEOPT
FPMAKE_OPT+=$(FPMAKEOPT)
endif
FPMAKE_OPT+=--localunitdir=../..
FPMAKE_OPT+=--globalunitdir=../../packages
FPMAKE_OPT+=$(FPC_TARGETOPT)
FPMAKE_OPT+=$(addprefix -o ,$(FPCOPT))
FPMAKE_OPT+=--compiler=$(FPC)
FPMAKE_OPT+=-bu
.NOTPARALLEL:
fpmake: fpmake.pp
$(FPCFPMAKE) fpmake.pp $(FPMAKE_SKIP_CONFIG) $(addprefix -Fu,$(COMPILER_FPMAKE_UNITDIR)) $(FPCMAKEOPT) $(OPT)
all: fpmake
$(LOCALFPMAKE) compile $(FPMAKE_OPT)
smart: fpmake
$(LOCALFPMAKE) compile $(FPMAKE_OPT) -o -XX -o -CX
release: fpmake
$(LOCALFPMAKE) compile $(FPMAKE_OPT) -o -dRELEASE
debug: fpmake
$(LOCALFPMAKE) compile $(FPMAKE_OPT) -o -dDEBUG
# If no fpmake exists and (dist)clean is called, do not try to build fpmake, it will
# most often fail because the dependencies are cleared.
# In case of a clean, simply do nothing
ifeq ($(FPMAKE_BIN_CLEAN),)
clean:
else
clean:
$(FPMAKE_BIN_CLEAN) clean $(FPMAKE_OPT)
endif
# In case of a distclean, perform an 'old'-style distclean. This to avoid problems
# when the package is compiled using fpcmake prior to running this clean using fpmake
ifeq ($(FPMAKE_BIN_CLEAN),)
distclean: $(addsuffix _distclean,$(TARGET_DIRS)) fpc_cleanall
else
distclean:
ifdef inUnix
{ $(FPMAKE_BIN_CLEAN) distclean $(FPMAKE_OPT); if [ $$? != "0" ]; then { echo Something wrong with fpmake exectable. Remove the executable and call make recursively to recover.; $(DEL) $(FPMAKE_BIN_CLEAN); $(MAKE) fpc_cleanall; }; fi; }
else
$(FPMAKE_BIN_CLEAN) distclean $(FPMAKE_OPT)
endif
-$(DEL) $(LOCALFPMAKE)
endif
cleanall: distclean
install: fpmake
ifdef UNIXHier
$(LOCALFPMAKE) install $(FPMAKE_OPT) --prefix=$(INSTALL_PREFIX) --baseinstalldir=$(INSTALL_LIBDIR)/fpc/$(FPC_VERSION) --unitinstalldir=$(INSTALL_UNITDIR)
else
$(LOCALFPMAKE) install $(FPMAKE_OPT) --prefix=$(INSTALL_BASEDIR) --unitinstalldir=$(INSTALL_UNITDIR)
endif
# distinstall also installs the example-sources
distinstall: fpmake
ifdef UNIXHier
$(LOCALFPMAKE) install $(FPMAKE_OPT) --prefix=$(INSTALL_PREFIX) --baseinstalldir=$(INSTALL_LIBDIR)/fpc/$(FPC_VERSION) --unitinstalldir=$(INSTALL_UNITDIR) -ie
else
$(LOCALFPMAKE) install $(FPMAKE_OPT) --prefix=$(INSTALL_BASEDIR) --unitinstalldir=$(INSTALL_UNITDIR) -ie
endif
zipinstall: fpmake
$(LOCALFPMAKE) zipinstall $(FPMAKE_OPT) --zipprefix=$(DIST_DESTDIR)/$(ZIPPREFIX)
zipdistinstall: fpmake
$(LOCALFPMAKE) zipinstall $(FPMAKE_OPT) --zipprefix=$(DIST_DESTDIR)/$(ZIPPREFIX) -ie

45
utils/pas2js/fpmake.pp Normal file
View File

@ -0,0 +1,45 @@
{$ifndef ALLPACKAGES}
{$mode objfpc}{$H+}
program fpmake;
uses fpmkunit;
{$endif ALLPACKAGES}
procedure add_pas2js(const ADirectory: string);
Var
P : TPackage;
T : TTarget;
begin
With Installer do
begin
P:=AddPackage('pas2js');
P.Author := 'Free Pascal Team';
P.License := 'LGPL with modification';
P.HomepageURL := 'www.freepascal.org';
P.Description := 'Convert pascal sources to javascript.';
P.Email := 'michael@freepascal.org';
P.NeedLibC:= false;
P.Directory:=ADirectory;
P.Version:='2.7.1';
P.Dependencies.Add('fcl-js');
P.Dependencies.Add('fcl-passrc');
P.Dependencies.Add('pastojs');
T:=P.Targets.AddProgram('pas2js.pp');
end;
end;
{$ifndef ALLPACKAGES}
begin
add_pas2js('');
Installer.Run;
end.
{$endif ALLPACKAGES}

78
utils/pas2js/pas2js.lpi Normal file
View File

@ -0,0 +1,78 @@
<?xml version="1.0" encoding="UTF-8"?>
<CONFIG>
<ProjectOptions>
<Version Value="9"/>
<General>
<Flags>
<SaveOnlyProjectUnits Value="True"/>
<MainUnitHasCreateFormStatements Value="False"/>
<MainUnitHasTitleStatement Value="False"/>
<SaveJumpHistory Value="False"/>
<SaveFoldState Value="False"/>
</Flags>
<SessionStorage Value="InProjectDir"/>
<MainUnit Value="0"/>
<Title Value="pas2js"/>
<UseAppBundle Value="False"/>
<ResourceType Value="res"/>
</General>
<i18n>
<EnableI18N LFM="False"/>
</i18n>
<VersionInfo>
<StringTable ProductVersion=""/>
</VersionInfo>
<BuildModes Count="1">
<Item1 Name="Default" Default="True"/>
</BuildModes>
<PublishOptions>
<Version Value="2"/>
</PublishOptions>
<RunParams>
<local>
<FormatVersion Value="1"/>
</local>
</RunParams>
<Units Count="2">
<Unit0>
<Filename Value="pas2js.pp"/>
<IsPartOfProject Value="True"/>
<UnitName Value="pas2js"/>
</Unit0>
<Unit1>
<Filename Value="fppas2js.pp"/>
<IsPartOfProject Value="True"/>
<UnitName Value="fppas2js"/>
</Unit1>
</Units>
</ProjectOptions>
<CompilerOptions>
<Version Value="11"/>
<Target>
<Filename Value="pas2js"/>
</Target>
<SearchPaths>
<IncludeFiles Value="$(ProjOutDir)"/>
<UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)"/>
</SearchPaths>
<Other>
<CompilerMessages>
<MsgFileName Value=""/>
</CompilerMessages>
<CompilerPath Value="$(CompPath)"/>
</Other>
</CompilerOptions>
<Debugging>
<Exceptions Count="3">
<Item1>
<Name Value="EAbort"/>
</Item1>
<Item2>
<Name Value="ECodetoolError"/>
</Item2>
<Item3>
<Name Value="EFOpenError"/>
</Item3>
</Exceptions>
</Debugging>
</CONFIG>

121
utils/pas2js/pas2js.pp Normal file
View File

@ -0,0 +1,121 @@
{
This file is part of the Free Component Library (FCL)
Copyright (c) 2014 by Michael Van Canneyt
Pascal to Javascript converter program.
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**********************************************************************}
{$mode objfpc}
{$h+}
program pas2js;
uses
sysutils, classes, pparser, fppas2js, pastree, jstree, jswriter;
Type
{ TContainer }
TContainer = Class(TPasTreeContainer)
public
function CreateElement(AClass: TPTreeElement; const AName: String;
AParent: TPasElement; AVisibility: TPasMemberVisibility;
const ASourceFilename: String; ASourceLinenumber: Integer): TPasElement;
overload; override;
function FindElement(const AName: String): TPasElement; override;
end;
{ TConvertPascal }
TConvertPascal = Class(TComponent)
Procedure ConvertSource(ASource, ADest : String);
end;
{ TContainer }
function TContainer.CreateElement(AClass: TPTreeElement; const AName: String;
AParent: TPasElement; AVisibility: TPasMemberVisibility;
const ASourceFilename: String; ASourceLinenumber: Integer): TPasElement;
begin
Result:=AClass.Create(AName,AParent);
end;
function TContainer.FindElement(const AName: String): TPasElement;
begin
Result:=Nil;
end;
{ TConvertPascal }
Procedure TConvertPascal.ConvertSource(ASource, ADest: String);
Var
p : TPasParser;
C : TPAsTreeContainer;
M : TPasModule;
CV : TPasToJSConverter;
JS : TJSElement;
W : TJSWriter;
begin
C:=TContainer.Create;
try
M:=ParseSource(C,ASource,'','',True);
try
CV:=TPasToJSConverter.Create;
try
JS:=CV.ConvertElement(M);
If JS=nil then
Writeln('No result');
finally
CV.Free;
end;
W:=TJSWriter.Create(ADest);
try
W.Options:=[woUseUTF8,woCompactArrayLiterals,woCompactObjectLiterals,woCompactArguments];
W.IndentSize:=2;
W.WriteJS(JS);
finally
W.Free;
end;
finally
M.Free;
end;
finally
C.Free;
end;
end;
Var
Src,Dest : String;
begin
Src:=Paramstr(1);
Dest:=ParamStr(2);
if Dest='' then
Dest:=ChangeFileExt(Src,'.js');
With TConvertPascal.Create(Nil) do
try
ConvertSource(Src,Dest);
finally
Free;
end;
With TStringList.Create do
try
LoadFromFile(Dest);
Writeln(Text);
finally
Free;
end;
end.

View File

@ -0,0 +1,12 @@
var
a : Array[1..100] of integer;
i,j : Integer;
begin
for I:=1 to 100 do
A[I]:=I;
for j:=1 to 100 do
writeln(A[j]);
end.

View File

@ -0,0 +1,8 @@
program fordemo;
var i : integer;
begin
for I:=0 to 100 do
Writeln(i);
end.

View File

@ -0,0 +1,8 @@
program fordemo;
var i : integer;
begin
for I:=100 downto 0 do
Writeln(i);
end.

View File

@ -0,0 +1,14 @@
program hello;
Procedure DoHello;
begin
Writeln('Hello again');
end;
Var
A : Integer = 1;
begin
Writeln('Hello, world');
end.

View File

@ -0,0 +1,13 @@
program ifdemo;
var
a : integer = 0;
begin
if a=1 then
Writeln('This should not be');
if a=2 then
Writeln('This should also not be')
else
Writeln('This should be OK');
end.

View File

@ -0,0 +1,11 @@
program fordemo;
var i : integer;
begin
i:=0;
Repeat
writeln(i);
i:=i+1;
Until i=100;
end.

View File

@ -0,0 +1,12 @@
program fordemo;
var i : integer;
begin
i:=0;
While (i<100) do
begin
writeln(i);
i:=i+1;
end;
end.