pastojs: writestr and -Sm

git-svn-id: trunk@39125 -
This commit is contained in:
Mattias Gaertner 2018-05-27 22:01:09 +00:00
parent 164587d798
commit df71ab3ecf
5 changed files with 96 additions and 39 deletions

View File

@ -146,7 +146,7 @@ Works:
- enums - enums
- type with values and names - type with values and names
- option to write numbers instead of variables - option to write numbers instead of variables
- ord(), low(), high(), pred(), succ() - ord(), low(), high(), pred(), succ(), str(), writestr()
- type cast alias to enumtype - type cast alias to enumtype
- type cast number to enumtype, enumtype to number - type cast number to enumtype, enumtype to number
- const aliasname = enumvalue - const aliasname = enumvalue
@ -346,9 +346,6 @@ Works:
- typecast byte(longword) -> value & $ff - typecast byte(longword) -> value & $ff
ToDos: ToDos:
-Sm Support macros like C (global)
- writestr(out s: string; args); varargs;
- widestrings + FPC_HAS_FEATURE_WIDESTRINGS + FPC_WIDESTRING_EQUAL_UNICODESTRING
- check rtl.js version - check rtl.js version
- 'new', 'Function' -> class var use .prototype - 'new', 'Function' -> class var use .prototype
- btArrayLit - btArrayLit
@ -365,7 +362,6 @@ ToDos:
- interfaces - interfaces
- array of interface - array of interface
- record member interface - record member interface
- remove cmsIgnoreInterfaces from codetools
Not in Version 1.0: Not in Version 1.0:
- make records more lightweight - make records more lightweight
@ -1660,6 +1656,7 @@ type
Function ConvertBuiltIn_StrProc(El: TParamsExpr; AContext: TConvertContext): TJSElement; virtual; Function ConvertBuiltIn_StrProc(El: TParamsExpr; AContext: TConvertContext): TJSElement; virtual;
Function ConvertBuiltIn_StrFunc(El: TParamsExpr; AContext: TConvertContext): TJSElement; virtual; Function ConvertBuiltIn_StrFunc(El: TParamsExpr; AContext: TConvertContext): TJSElement; virtual;
Function ConvertBuiltInStrParam(El: TPasExpr; AContext: TConvertContext; IsStrFunc, IsFirst: boolean): TJSElement; virtual; Function ConvertBuiltInStrParam(El: TPasExpr; AContext: TConvertContext; IsStrFunc, IsFirst: boolean): TJSElement; virtual;
Function ConvertBuiltIn_WriteStr(El: TParamsExpr; AContext: TConvertContext): TJSElement; virtual;
Function ConvertBuiltIn_ConcatArray(El: TParamsExpr; AContext: TConvertContext): TJSElement; virtual; Function ConvertBuiltIn_ConcatArray(El: TParamsExpr; AContext: TConvertContext): TJSElement; virtual;
Function ConvertBuiltIn_CopyArray(El: TParamsExpr; AContext: TConvertContext): TJSElement; virtual; Function ConvertBuiltIn_CopyArray(El: TParamsExpr; AContext: TConvertContext): TJSElement; virtual;
Function ConvertBuiltIn_InsertArray(El: TParamsExpr; AContext: TConvertContext): TJSElement; virtual; Function ConvertBuiltIn_InsertArray(El: TParamsExpr; AContext: TConvertContext): TJSElement; virtual;
@ -7796,6 +7793,7 @@ begin
bfSucc: Result:=ConvertBuiltIn_PredSucc(El,AContext,false); bfSucc: Result:=ConvertBuiltIn_PredSucc(El,AContext,false);
bfStrProc: Result:=ConvertBuiltIn_StrProc(El,AContext); bfStrProc: Result:=ConvertBuiltIn_StrProc(El,AContext);
bfStrFunc: Result:=ConvertBuiltIn_StrFunc(El,AContext); bfStrFunc: Result:=ConvertBuiltIn_StrFunc(El,AContext);
bfWriteStr: Result:=ConvertBuiltIn_WriteStr(El,AContext);
bfConcatArray: Result:=ConvertBuiltIn_ConcatArray(El,AContext); bfConcatArray: Result:=ConvertBuiltIn_ConcatArray(El,AContext);
bfCopyArray: Result:=ConvertBuiltIn_CopyArray(El,AContext); bfCopyArray: Result:=ConvertBuiltIn_CopyArray(El,AContext);
bfInsertArray: Result:=ConvertBuiltIn_InsertArray(El,AContext); bfInsertArray: Result:=ConvertBuiltIn_InsertArray(El,AContext);
@ -9717,6 +9715,51 @@ begin
end; end;
end; end;
function TPasToJSConverter.ConvertBuiltIn_WriteStr(El: TParamsExpr;
AContext: TConvertContext): TJSElement;
// convert 'writestr(aString,v:width,p)' to 'aString = <string of v> + (<string of p>+"")'
// for the conversion see ConvertBuiltInStrParam
var
AssignContext: TAssignContext;
StrVar: TPasExpr;
TypeEl: TPasType;
JS: TJSElement;
AddJS: TJSAdditiveExpressionPlus;
i: Integer;
begin
Result:=nil;
AssignContext:=TAssignContext.Create(El,nil,AContext);
try
StrVar:=El.Params[0];
AContext.Resolver.ComputeElement(StrVar,AssignContext.LeftResolved,[rcNoImplicitProc]);
// create right side
for i:=1 to length(El.Params)-1 do
begin
JS:=ConvertBuiltInStrParam(El.Params[i],AContext,false,true);
if AssignContext.RightSide=nil then
AssignContext.RightSide:=JS
else
begin
AddJS:=TJSAdditiveExpressionPlus(CreateElement(TJSAdditiveExpressionPlus,El));
AddJS.A:=AssignContext.RightSide;
AssignContext.RightSide:=AddJS;
AddJS.B:=JS;
end;
end;
TypeEl:=AContext.Resolver.BaseTypes[btString];
SetResolverValueExpr(AssignContext.RightResolved,btString,
TypeEl,TypeEl,El,[rrfReadable]);
// create 'StrVar = rightside'
Result:=CreateAssignStatement(StrVar,AssignContext);
finally
AssignContext.RightSide.Free;
AssignContext.Free;
end;
end;
function TPasToJSConverter.ConvertBuiltIn_ConcatArray(El: TParamsExpr; function TPasToJSConverter.ConvertBuiltIn_ConcatArray(El: TParamsExpr;
AContext: TConvertContext): TJSElement; AContext: TConvertContext): TJSElement;
// concat(array1, array2) // concat(array1, array2)

View File

@ -102,6 +102,7 @@ type
coAssertions, coAssertions,
// features // features
coAllowCAssignments, coAllowCAssignments,
coAllowMacros,
// output // output
coLowerCase, coLowerCase,
coUseStrict, coUseStrict,
@ -145,6 +146,7 @@ const
'Method call checking', 'Method call checking',
'Assertions', 'Assertions',
'Allow C assignments', 'Allow C assignments',
'Allow macros',
'Lowercase identifiers', 'Lowercase identifiers',
'Use strict', 'Use strict',
'Write pas2jsdebug.log', 'Write pas2jsdebug.log',
@ -794,6 +796,8 @@ var
bs: TBoolSwitches; bs: TBoolSwitches;
begin begin
bs:=[bsWriteableConst]; bs:=[bsWriteableConst];
if coAllowMacros in Compiler.Options then
Include(bs,bsMacro);
if coOverflowChecks in Compiler.Options then if coOverflowChecks in Compiler.Options then
Include(bs,bsOverflowChecks); Include(bs,bsOverflowChecks);
if coRangeChecks in Compiler.Options then if coRangeChecks in Compiler.Options then
@ -3505,17 +3509,19 @@ begin
for i:=1 to length(Enabled) do begin for i:=1 to length(Enabled) do begin
case Enabled[i] of case Enabled[i] of
'a': Options:=Options+[coAssertions]; 'a': Options:=Options+[coAssertions];
'2': Mode:=p2jmObjFPC;
'c': Options:=Options+[coAllowCAssignments]; 'c': Options:=Options+[coAllowCAssignments];
'd': Mode:=p2jmDelphi; 'd': Mode:=p2jmDelphi;
'm': Options:=Options+[coAllowMacros];
'2': Mode:=p2jmObjFPC;
end; end;
end; end;
for i:=1 to length(Disabled) do begin for i:=1 to length(Disabled) do begin
case Disabled[i] of case Disabled[i] of
'a': Options:=Options-[coAssertions]; 'a': Options:=Options-[coAssertions];
'2': ;
'c': Options:=Options-[coAllowCAssignments]; 'c': Options:=Options-[coAllowCAssignments];
'd': ; 'd': ;
'm': Options:=Options-[coAllowMacros];
'2': ;
end; end;
end; end;
end; end;
@ -4041,6 +4047,7 @@ begin
l(' a : Turn on assertions'); l(' a : Turn on assertions');
l(' c : Support operators like C (*=,+=,/= and -=)'); l(' c : Support operators like C (*=,+=,/= and -=)');
l(' d : Same as -Mdelphi'); l(' d : Same as -Mdelphi');
l(' m : Support macros');
l(' 2 : Same as -Mobjfpc (default)'); l(' 2 : Same as -Mobjfpc (default)');
l(' -SI<x> : Set interface style to <x>'); l(' -SI<x> : Set interface style to <x>');
l(' -SIcom : COM compatible interface (default)'); l(' -SIcom : COM compatible interface (default)');

View File

@ -3811,36 +3811,39 @@ end;
procedure TTestModule.TestEnum_Functions; procedure TTestModule.TestEnum_Functions;
begin begin
StartProgram(false); StartProgram(false);
Add('type TMyEnum = (Red, Green);'); Add([
Add('var'); 'type TMyEnum = (Red, Green);',
Add(' e: TMyEnum;'); 'var',
Add(' i: longint;'); ' e: TMyEnum;',
Add(' s: string;'); ' i: longint;',
Add(' b: boolean;'); ' s: string;',
Add('begin'); ' b: boolean;',
Add(' i:=ord(red);'); 'begin',
Add(' i:=ord(green);'); ' i:=ord(red);',
Add(' i:=ord(e);'); ' i:=ord(green);',
Add(' i:=ord(b);'); ' i:=ord(e);',
Add(' e:=low(tmyenum);'); ' i:=ord(b);',
Add(' e:=low(e);'); ' e:=low(tmyenum);',
Add(' b:=low(boolean);'); ' e:=low(e);',
Add(' e:=high(tmyenum);'); ' b:=low(boolean);',
Add(' e:=high(e);'); ' e:=high(tmyenum);',
Add(' b:=high(boolean);'); ' e:=high(e);',
Add(' e:=pred(green);'); ' b:=high(boolean);',
Add(' e:=pred(e);'); ' e:=pred(green);',
Add(' b:=pred(b);'); ' e:=pred(e);',
Add(' e:=succ(red);'); ' b:=pred(b);',
Add(' e:=succ(e);'); ' e:=succ(red);',
Add(' b:=succ(b);'); ' e:=succ(e);',
Add(' e:=tmyenum(1);'); ' b:=succ(b);',
Add(' e:=tmyenum(i);'); ' e:=tmyenum(1);',
Add(' s:=str(e);'); ' e:=tmyenum(i);',
Add(' str(e,s);'); ' s:=str(e);',
Add(' s:=str(e:3);'); ' str(e,s);',
Add(' e:=TMyEnum(i);'); ' str(red,s);',
Add(' i:=longint(e);'); ' s:=str(e:3);',
' writestr(s,e:3,red);',
' e:=TMyEnum(i);',
' i:=longint(e);']);
ConvertProgram; ConvertProgram;
CheckSource('TestEnum_Functions', CheckSource('TestEnum_Functions',
LinesToStr([ // statements LinesToStr([ // statements
@ -3876,7 +3879,9 @@ begin
'$mod.e=$mod.i;', '$mod.e=$mod.i;',
'$mod.s = $mod.TMyEnum[$mod.e];', '$mod.s = $mod.TMyEnum[$mod.e];',
'$mod.s = $mod.TMyEnum[$mod.e];', '$mod.s = $mod.TMyEnum[$mod.e];',
'$mod.s = $mod.TMyEnum[$mod.TMyEnum.Red];',
'$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3);', '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3);',
'$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3)+$mod.TMyEnum[$mod.TMyEnum.Red];',
'$mod.e=$mod.i;', '$mod.e=$mod.i;',
'$mod.i=$mod.e;', '$mod.i=$mod.e;',
''])); '']));

View File

@ -186,6 +186,7 @@ Put + after a boolean switch option to enable it, - to disable it
a : Turn on assertions a : Turn on assertions
c : Support operators like C (*=,+=,/= and -=) c : Support operators like C (*=,+=,/= and -=)
d : Same as -Mdelphi d : Same as -Mdelphi
m : Support macros
2 : Same as -Mobjfpc (default) 2 : Same as -Mobjfpc (default)
-SI&lt;x&gt; : Set interface style to &lt;x&gt; -SI&lt;x&gt; : Set interface style to &lt;x&gt;
-SIcom : COM compatible interface (default) -SIcom : COM compatible interface (default)
@ -2868,12 +2869,13 @@ End.
e.g. <i>uses unit1 in 'sub/Unit1.pas'</i>.<br> e.g. <i>uses unit1 in 'sub/Unit1.pas'</i>.<br>
In <i>$mode objfpc</i> units can use in-filenames too and In <i>$mode objfpc</i> units can use in-filenames too and
alias are allowed, e.g. <i>uses foo in 'bar.pas'</i>.</li> alias are allowed, e.g. <i>uses foo in 'bar.pas'</i>.</li>
<li>The built-in procedure <b>str</b> works with boolean, integer, float and enumvalue.<br> <li>The intrinsic procedure <b>str</b> works with boolean, integer, float and enumvalue.<br>
Additionally there is <b>str</b> function, that takes an arbitrary number of Additionally there is <b>str</b> function, that takes an arbitrary number of
arguments and returns a concatenated string. It supports string as parameter too. arguments and returns a concatenated string. It supports string as parameter too.
For example s:=str(i,' ',d:1:5).<br> For example s:=str(i,' ',d:1:5).<br>
Width and precision is supported. str(i:10) will add spaces to the left to fill up to 10 characters.</b> Width and precision is supported. str(i:10) will add spaces to the left to fill up to 10 characters.</b>
str(aDouble:1:5) returns a string in decimal format with 5 digits for the fraction.</li> str(aDouble:1:5) returns a string in decimal format with 5 digits for the fraction.</li>
<li>Intrinsic procedure WriteStr(out s: string; params...)</li>
</ul> </ul>
</div> </div>

View File

@ -1,4 +1,4 @@
{ Author: Mattias Gaertner 2017 mattias@freepascal.org { Author: Mattias Gaertner 2018 mattias@freepascal.org
Abstract: Abstract:
Command line interface for the pas2js compiler. Command line interface for the pas2js compiler.