From 212e41ecd105399bca045bcee3315d51d973b310 Mon Sep 17 00:00:00 2001 From: Raymond Date: Mon, 18 Jan 2010 13:24:41 +0000 Subject: [PATCH] Writeln now writeln's *EVERY* data type... Made a ToStr that accepts *ANY* data type. See stringtest.mufa git-svn-id: http://www.villavu.com/repositories/merlijn/mufasa@426 3f818213-9676-44b0-a9b4-5e4c4e03d09d --- Tests/PS/stringtest.mufa | 39 ++++++++++++++++++++++ Units/MMLAddon/PSInc/Wrappers/other.inc | 22 ------------ Units/MMLAddon/PSInc/Wrappers/strings.inc | 26 +++++++++++++++ Units/MMLAddon/PSInc/psexportedmethods.inc | 13 +++++++- Units/MMLAddon/mmlpsthread.pas | 35 +++++++++++++++---- 5 files changed, 105 insertions(+), 30 deletions(-) create mode 100644 Tests/PS/stringtest.mufa create mode 100644 Units/MMLAddon/PSInc/Wrappers/strings.inc diff --git a/Tests/PS/stringtest.mufa b/Tests/PS/stringtest.mufa new file mode 100644 index 0000000..c3e911a --- /dev/null +++ b/Tests/PS/stringtest.mufa @@ -0,0 +1,39 @@ +program new; +type + LetsDoThis = record + str : string; + x,y : integer; + end; + LetsDoThis2 = record + str : string; + pt : TPoint; + end; + Yeah = array of LetsDoThis2; + ArrThis = array of yeah; + + +var + x : LetsDoThis; + y : arrthis; + j : TForm; + i,ii : integer; +begin + x.str := 'Testmebitch'; + x.x := 500; + x.y := -900; + Writeln(x); + SetLength(y,2); + for i := 0 to high(y) do + begin; + setlength(y[i],2); + for ii := 0 to high(y[i]) do + begin; + y[i][ii].pt := Point(i*5,-random(9000)); + y[i][ii].str := format('[%d][%d]=%s',[i,ii,tostr(y[i][ii].pt)]); + end; + end; + Writeln(y); + Writeln(TPointArray([Point(5,5),Point(20,1337),point(1,2)])); + J := TForm.Create(nil); + Writeln(j.canvas); +end. diff --git a/Units/MMLAddon/PSInc/Wrappers/other.inc b/Units/MMLAddon/PSInc/Wrappers/other.inc index 2b5a5d7..18eae0d 100644 --- a/Units/MMLAddon/PSInc/Wrappers/other.inc +++ b/Units/MMLAddon/PSInc/Wrappers/other.inc @@ -67,28 +67,6 @@ begin; result.y2 := y2; end; -function Capitalize(str : string) : string; -var - i , l : integer; - cap : boolean; -begin; - result := str; - l := length(str); - cap := true; - for i := 1 to l do - if cap and (str[i] in ['a'..'z'] + ['A'..'Z']) then - begin; - result[i] := UpperCase(str[i])[1]; - cap := false; - end else if not (str[i] in ['a'..'z'] + ['A'..'Z']) then - cap := true; -end; - -function psFormat(const fmt : string;const args : array of const) : string; -begin; - Result := Format(fmt,Args); -end; - function Distance(x1, y1, x2, y2: Integer): Integer; begin; Result := Round(Sqrt(Sqr(x2-x1) + Sqr(y2-y1))); diff --git a/Units/MMLAddon/PSInc/Wrappers/strings.inc b/Units/MMLAddon/PSInc/Wrappers/strings.inc new file mode 100644 index 0000000..d33bfc5 --- /dev/null +++ b/Units/MMLAddon/PSInc/Wrappers/strings.inc @@ -0,0 +1,26 @@ +function psFormat(const fmt : string;const args : array of const) : string; +begin; + Result := Format(fmt,Args); +end; + +function Capitalize(str : string) : string; +var + i , l : integer; + cap : boolean; +begin; + result := str; + l := length(str); + cap := true; + for i := 1 to l do + if cap and (str[i] in ['a'..'z'] + ['A'..'Z']) then + begin; + result[i] := UpperCase(str[i])[1]; + cap := false; + end else if not (str[i] in ['a'..'z'] + ['A'..'Z']) then + cap := true; +end; + +function psBoolToStr(bool : boolean) : string; +begin; + result := BoolToStr(bool,true); +end; diff --git a/Units/MMLAddon/PSInc/psexportedmethods.inc b/Units/MMLAddon/PSInc/psexportedmethods.inc index a4fae77..3431bf3 100644 --- a/Units/MMLAddon/PSInc/psexportedmethods.inc +++ b/Units/MMLAddon/PSInc/psexportedmethods.inc @@ -22,7 +22,7 @@ } AddFunction(@ThreadSafeCall,'function ThreadSafeCall(ProcName: string; var V: TVariantArray): Variant;'); -AddFunction(nil,'procedure writeln(s : string);'); //We use special function for this +AddFunction(nil,'procedure writeln(x);'); //We use special function for this { DTM } SetCurrSection('DTM'); @@ -96,8 +96,19 @@ AddFunction(@NewThreadCall,'function NewThreadCall(procname : string) : cardinal {string} +SetCurrSection('String'); AddFunction(@Capitalize,'function Capitalize(str : string) : string;'); AddFunction(@psFormat,'function Format(const fmt : string;const args : array of const) : string;'); +AddFunction(nil,'function ToStr(x) : string;'); +AddFunction(@IntToStr, 'function IntToStr(value: Integer): String;'); +AddFunction(@FloatToStr, 'function FloatToStr(value: Extended): String;'); +AddFunction(@psBoolToStr, 'function BoolToStr(value: Boolean): String;'); +AddFunction(@StrToInt, 'function StrToInt(value: String): Integer;'); +AddFunction(@StrToIntDef, 'function StrToIntDef(value: String; default: Integer): Integer;'); +AddFunction(@StrToFloat, 'function StrToFloat(value: String): Extended;'); +AddFunction(@StrToFloatDef, 'function StrToFloatDef(value: String; default: Extended): Extended;'); +AddFunction(@StrToBool, 'function StrToBool(value: String): Boolean;'); +AddFunction(@StrToBoolDef, 'function StrToBoolDef(value: String; default: Boolean): Boolean;'); {web} SetCurrSection('Web'); diff --git a/Units/MMLAddon/mmlpsthread.pas b/Units/MMLAddon/mmlpsthread.pas index d21bb75..73051b0 100644 --- a/Units/MMLAddon/mmlpsthread.pas +++ b/Units/MMLAddon/mmlpsthread.pas @@ -143,18 +143,31 @@ begin writeln(str); end; +function MakeString(data : TPSVariantIFC) : string; +begin; + if data.aType.basetype in [btString,btChar] then + result := PSGetAnsiString(Data.Dta,data.aType) + else if data.aType.ExportName = 'BOOLEAN' then + result := BoolToStr(PSGetInt(Data.Dta,data.aType) <> 0,true) + else + result := PSVariantToString(data,''); +end; + function writeln_(Caller: TPSExec; p: TPSExternalProcRec; Global, Stack: TPSStack): Boolean; var arr: TPSVariantIFC; begin Result:=true; - arr:=NewTPSVariantIFC(Stack[Stack.Count-1],false); - case arr.aType.BaseType of - btString,btChar : psWriteln(stack.GetString(-1)); - btU8, btS8, btU16, btS16, btU32, btS32: psWriteln(inttostr(stack.GetInt(-1))); - {$IFNDEF PS_NOINT64}btS64 : psWriteln(IntToStr(stack.GetInt64(-1))); {$ENDIF} - else Result:=false; - end; + psWriteln(makeString(NewTPSVariantIFC(Stack[Stack.Count-1],false))); +end; + +function ToStr_(Caller: TPSExec; p: TPSExternalProcRec; Global, Stack: TPSStack): Boolean; +var + data: TPSVariantIFC; +begin + result := true; + Stack.SetAnsiString(-1, MakeString(NewTPSVariantIFC(Stack[Stack.Count-2],false))); + end; function NewThreadCall(Procname : string) : Cardinal; @@ -240,6 +253,7 @@ end; {$I PSInc/Wrappers/other.inc} {$I PSInc/Wrappers/bitmap.inc} {$I PSInc/Wrappers/window.inc} +{$I PSInc/Wrappers/Strings.inc} {$I PSInc/Wrappers/colour.inc} {$I PSInc/Wrappers/math.inc} @@ -412,6 +426,12 @@ begin OrgName:= 'x'; Mode:= pmIn; end; + with x.AddFunction('function ToStr:string').decl do + with addparam do + begin + OrgName:= 'x'; + Mode:= pmIn; + end; end; procedure TMMLPSThread.OnExecImport(Sender: TObject; se: TPSExec; @@ -426,6 +446,7 @@ begin RIRegister_ExtCtrls(x); RIRegister_Mufasa(x); se.RegisterFunctionName('WRITELN',@Writeln_,nil,nil); + se.RegisterFunctionName('TOSTR',@ToStr_,nil,nil); end; procedure TMMLPSThread.OutputMessages;