diff --git a/C2Delphi.Forms.Main.pas b/C2Delphi.Forms.Main.pas index 9ce9577..8c6d5f8 100644 --- a/C2Delphi.Forms.Main.pas +++ b/C2Delphi.Forms.Main.pas @@ -236,15 +236,17 @@ procedure TfrmMain.BCEditor2CaretChanged(ASender: TObject; X, Y: Integer); end; procedure TfrmMain.BCEditor2Change(Sender: TObject); -var fn{$IFDEF USE_DELPHIAST},t{$ENDIF}:string; +{$IFDEF USE_DELPHIAST} +var fn,t:string; +{$ENDIF} begin - fn := Pas.Name+'.pas'; - BCEditor2.Lines.SaveToFile(fn); - {$IFDEF USE_DELPHIAST} + fn := TPath.Combine(TPath.GetTempPath, Pas.Name+'_tmp.pas'); + BCEditor2.Lines.SaveToFile(fn); t := Parse(fn,ex); ListBox1.Clear; ListBox1.Items.Add(t); + TFile.Delete(fn); {$ENDIF} end; @@ -427,8 +429,11 @@ procedure TfrmMain.WMDROPFILES(var msg: TWMDropFiles); begin DragQueryFile(msg.Drop, cnt, fileName, MAXFILENAME); cfn := fileName; - p := c_to_pas(ReadCCodeFromFile(cfn),t,changefileext(ExtractFilename(cfn),'')); - TFile.WriteAllText( ChangeFileExt(fileName,'.pas'), p.toPascal); + if fileCount>1 then + begin + p := c_to_pas(ReadCCodeFromFile(cfn),t,changefileext(ExtractFilename(cfn),'')); + TFile.WriteAllText( ChangeFileExt(fileName,'.pas'), p.toPascal); + end; end; if fileCount>0 then diff --git a/C2Delphi.dproj b/C2Delphi.dproj index 35c5ebb..29c8572 100644 --- a/C2Delphi.dproj +++ b/C2Delphi.dproj @@ -3,7 +3,7 @@ {3A5D71F7-635F-4E30-A692-1E2E1EC8BE4F} C2Delphi.dpr True - Debug + Release 1 Application VCL diff --git a/Releases/C2Delphi-0.9.0.zip b/Releases/C2Delphi-0.9.0.zip new file mode 100644 index 0000000..431922c Binary files /dev/null and b/Releases/C2Delphi-0.9.0.zip differ diff --git a/Releases/C2Delphi-0.9.1.zip b/Releases/C2Delphi-0.9.1.zip new file mode 100644 index 0000000..e269849 Binary files /dev/null and b/Releases/C2Delphi-0.9.1.zip differ diff --git a/Releases/C2Delphi-0.9.2.zip b/Releases/C2Delphi-0.9.2.zip new file mode 100644 index 0000000..ddf12cd Binary files /dev/null and b/Releases/C2Delphi-0.9.2.zip differ diff --git a/WvN.Pascal.CReader.pas b/WvN.Pascal.CReader.pas index 6c5d367..066c86d 100644 --- a/WvN.Pascal.CReader.pas +++ b/WvN.Pascal.CReader.pas @@ -22,7 +22,7 @@ implementation PARSED_MARKER_STR = PARSED_MARKER+PARSED_MARKER+PARSED_MARKER; rxID = '(\*?)[a-zA-Z_\$][\w_]*(\*?)'; - rxT = '(\*?)(?:unsigned\s+)?(?:long\s+)?[a-zA-Z_\$][\w_]*(\*?)'; + rxT = '(\*?)(?:unsigned|signed\s+)?(?:long\s+)?[a-zA-Z_\$][\w_]*(\*?)'; // rxType = '('+rxT+')|('+rxT+'<\s*'+rxT+'\s*>)'; rxType = rxT; // rxNum = '\d*'; @@ -54,6 +54,55 @@ implementation type TLoc=(None,InStringQ1,InStringQ2,InLineComment,InMultiLineComment); +function StripComments(aCCode:string):string; +var + loc:TLoc; + i,j: Integer; +begin + Loc := None; + + setlength(result,length(aCCode)); + + I := 1; J:=1; + while I<=aCCode.length do + begin + case loc of + None: + begin + if I < aCCode.Length-1 then + if aCCode[I] = '/' then + if aCCode[I + 1] = '/' then + Loc := InLineComment; + + if I < aCCode.Length then + if aCCode[I] = '/' then + if aCCode[I + 1] = '*' then + Loc := InMultiLineComment; + + end; + + InLineComment: + if CharInSet(aCCode[I], [#13, #10]) then + Loc := None; + + InMultiLineComment: + if I > 1 then + if aCCode[I - 1] = '*' then + if aCCode[I] = '/' then + loc := None; + + end; + + if loc = None then + begin + Result[J] := aCCode[I]; + Inc(J); + end; + inc(I); + end; + Setlength(Result,J); +end; + function ReplaceOutsideCommentsAndStrings(aCCode,aSearch,aReplace:string):string; var loc:TLoc; @@ -525,6 +574,9 @@ function ConvertCLinesToPas(var lines:TArray):string; expr: string; begin c := 0; + // replace lines that contain a variable declaration. + // when it also contains an assignment, leave the assignment, + // otherwise remove the line setlength(linesAr,length(lines)); for I := 0 to high(lines) do begin @@ -542,15 +594,24 @@ function ConvertCLinesToPas(var lines:TArray):string; end; end; end; + + // strip emtpy lines at then end + i := length(linesAr)-1; + while (i>=0) and (linesAr[i].Trim='') do + begin + setlength(linesAr,i); + dec(i); + end; + setlength(linesAr,c); lines := linesAr; if Length(Lines)>0 then begin - l := Lines[high(Lines)]; - lines[high(Lines)] := TRegEx.Replace(l,'^(\s*)Exit\s*\((?.*)\)\s*[;]?\s*;?$','\1Result := \2;') ; - l := Lines[high(Lines)]; - lines[high(Lines)] := TRegEx.Replace(l,'^(\s*)return\s*(?[^;]+)\s*;?$','\1Result := \2;') ; + l := Lines[high(Lines)]; + lines[high(Lines)] := TRegEx.Replace(l,'^(\s*)Exit\s*\((?.*)\)\s*[;]?\s*;?$','\1Result := \2;') ; + l := Lines[high(Lines)]; + lines[high(Lines)] := TRegEx.Replace(l,'^(\s*)return\s*(?[^;]+)\s*;?$','\1Result := \2;') ; end; for I := 0 to high(lines) do @@ -1752,6 +1813,7 @@ function c_to_pas(const aCCode:string; var t:string; aName:string='tmp'):TPascal Result.usesListIntf.&Unit := Result; Result.usesListImpl.&Unit := Result; s := aCCode; + s := StripComments(s); FixTypes(s); t := s; diff --git a/WvN.Pascal.Model.pas b/WvN.Pascal.Model.pas index a0bb29b..ec2a8c6 100644 --- a/WvN.Pascal.Model.pas +++ b/WvN.Pascal.Model.pas @@ -397,13 +397,13 @@ function TVariableList.ToPascal(indent:Boolean): String; begin Result := Result + Esc(Items[i].name.Trim) + ', '; if align then - Result := Result + sLineBreak+''; + Result := Result + sLineBreak+' '; continue; end; if Align then - Result := Result + ' '+copy(Esc(Items[i].name)+ StringOfChar(' ',longest) ,1,longest) + Result := Result +copy(Esc(Items[i].name)+ StringOfChar(' ',longest) ,1,longest) else Result := Result + Esc(Items[i].name); @@ -411,7 +411,7 @@ function TVariableList.ToPascal(indent:Boolean): String; begin Result := Result + ' : '; - if Items[I].&Type='^nil' then + if Items[I].&Type='^' then Result := Result + 'pointer' else begin