pas2js.pp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. {
  2. This file is part of the Free Component Library (FCL)
  3. Copyright (c) 2014 by Michael Van Canneyt
  4. Pascal to Javascript converter program.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {$mode objfpc}
  12. {$h+}
  13. program pas2js;
  14. uses
  15. sysutils, classes, pparser, fppas2js, pastree, jstree, jswriter, pasresolver;
  16. Type
  17. { TConvertPascal }
  18. TConvertPascal = Class(TComponent)
  19. Procedure ConvertSource(ASource, ADest : String);
  20. end;
  21. { TConvertPascal }
  22. Procedure TConvertPascal.ConvertSource(ASource, ADest: String);
  23. Var
  24. C : TPas2JSResolver;
  25. M : TPasModule;
  26. CV : TPasToJSConverter;
  27. JS : TJSElement;
  28. W : TJSWriter;
  29. begin
  30. W:=nil;
  31. M:=Nil;
  32. CV:=Nil;
  33. C:=TPas2JSResolver.Create;
  34. try
  35. M:=ParseSource(C,ASource,'','',[poUseStreams]);
  36. CV:=TPasToJSConverter.Create;
  37. JS:=CV.ConvertPasElement(M,C);
  38. If JS=nil then
  39. Writeln('No result')
  40. else
  41. begin
  42. W:=TJSWriter.Create(ADest);
  43. W.Options:=[woUseUTF8,woCompactArrayLiterals,woCompactObjectLiterals,woCompactArguments];
  44. W.IndentSize:=2;
  45. W.WriteJS(JS);
  46. end
  47. finally
  48. W.Free;
  49. CV.Free;
  50. M.Free;
  51. C.Free;
  52. end;
  53. end;
  54. Var
  55. Src,Dest : String;
  56. begin
  57. Src:=Paramstr(1);
  58. Dest:=ParamStr(2);
  59. if Dest='' then
  60. Dest:=ChangeFileExt(Src,'.js');
  61. With TConvertPascal.Create(Nil) do
  62. try
  63. ConvertSource(Src,Dest);
  64. finally
  65. Free;
  66. end;
  67. With TStringList.Create do
  68. try
  69. LoadFromFile(Dest);
  70. Writeln(Text);
  71. finally
  72. Free;
  73. end;
  74. end.