Recent

Author Topic: Pas2Php (Pascal to Php Translator for Web App's)  (Read 10981 times)

derek.john.evans

  • Guest
Pas2Php (Pascal to Php Translator for Web App's)
« on: October 07, 2014, 06:21:49 pm »
I wanted to write native code CGI app's in Lazarus which I could convert to PHP. So, here is Pas2Php.

http://www.wascal.net/pas2php/
(Note: The source code as of now is also attached to this post)

When you run the demo project in Lazarus, the unit "index.pas" and all its available units are translated to php files. It then opens the url to where the php file is being served.

The demo can be run a php script or as native cgi. More testing is needed and I'll be adding more PHP functions to the Pascal libraries. The demo also shows how to write Pascal versions of common PHP functions which will run if the app is used as CGI.

The main issues atm are:

functions which can not be found (which have no parameters)
Workaround: Use UnknownFunction() to show you are calling a function, and not accessing a variable.
Calls to methods which can not be found:
Workaround: Use Self.UnknownFunction()
Adding strings together which are unknown variables.
Workaround: Use   GetApple() + ' ' + GetOrange() to force string concatenation.

What Im thinking is, using PascalScript to "syntax check" code, and then translate to php. Since PascalScript is a subset of PasTree, I should be able to get close to a 100% translation success rate.

Anyway, cheers

derek.john.evans

  • Guest
Re: Pas2Php (Pascal to Php Translator for Web App's)
« Reply #1 on: October 10, 2014, 09:44:41 pm »
Just an update. Pas2Php is usable. I got so hooked on it, I rewrote my website in Pascal and converted to PHP, so no more PHP coding for me!   :P

Anyway, Ive attached the latest code v0.4, which also includes the pascal code to my site (minus the images), which should given you enough example code. Attached is the current code, or download from the pas2php page.

http://www.wascal.net/?page=pas2php

Hope you find it useful. Personally, Ive been looking for a way to ditch coding in PHP for years.
« Last Edit: October 10, 2014, 10:08:14 pm by derek.john.evans »

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1251
Re: Pas2Php (Pascal to Php Translator for Web App's)
« Reply #2 on: October 10, 2014, 10:06:47 pm »
I'm not a web programmer, and have never used PHP, but I like your ideas and efforts :-)  Well done :-)
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

Leledumbo

  • Hero Member
  • *****
  • Posts: 8279
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Pas2Php (Pascal to Php Translator for Web App's)
« Reply #3 on: October 11, 2014, 04:59:19 pm »
Personally, Ive been looking for a way to ditch coding in PHP for years.
Well why don't you code your web in Pascal then? We have powerful web libraries/frameworks that can be easily integrated with existing Pascal code.

derek.john.evans

  • Guest
Re: Pas2Php (Pascal to Php Translator for Web App's)
« Reply #4 on: October 11, 2014, 05:45:58 pm »
Quote
Well why don't you code your web in Pascal then?

Doesn't suit PHP only web hostings that don't support native code CGI.

Basiclly, I am coding in Pascal. The example generates a native code CGI app that works as well. But, then, I cant use it unless I run my own server.
« Last Edit: October 11, 2014, 05:48:07 pm by derek.john.evans »

mirce.vladimirov

  • Full Member
  • ***
  • Posts: 221
Re: Pas2Php (Pascal to Php Translator for Web App's)
« Reply #5 on: October 11, 2014, 08:05:37 pm »
I dont use PHP but it seems very interesting to code in Lazarus and then convert the code to PHP, very nice way to start making web pages. Thank you very much.
I downloaded the zip-file and unzipped it but i dont know how to use it. I found the project sources writen in pascal but i dont know what to do with it. When i press F9 to compile and run there are messages about "replacestr" (see attached picture).
Suppose I compile this project , what next , how to convert my own pascal project into PHP ?

derek.john.evans

  • Guest
Re: Pas2Php (Pascal to Php Translator for Web App's)
« Reply #6 on: October 11, 2014, 08:10:02 pm »
What version of Lazarus are you using? ReplaceStr is in StrUtils and I dont know how long it has been there. The comments say "Delphi compat", so it may be a new alias function.

Try changing it to AnsiReplaceStr.


mirce.vladimirov

  • Full Member
  • ***
  • Posts: 221
Re: Pas2Php (Pascal to Php Translator for Web App's)
« Reply #7 on: October 11, 2014, 08:16:33 pm »
Try changing it to AnsiReplaceStr.

I use Lazarus 1.0.2 under windows 7 32 bit.
I already changed that to AnsiReplaceStr, and then it complained about replacetext so i changed that to AnsiReplaceText. Now it complains about having "TList" instead of "TFPList", see attached picture. Should I change that too ?

derek.john.evans

  • Guest
Re: Pas2Php (Pascal to Php Translator for Web App's)
« Reply #8 on: October 11, 2014, 08:19:51 pm »
Oops. If that works, then you will have to expose AnsiReplaceStr/AnsiReplaceText to PHP. In "P2PSystem.pas" are the PHP versions of common Pascal functions. You will need to insert:

Code: Pascal  [Select][+][-]
  1. //// FIND THESE
  2.  
  3. function ReplaceText(const AStr, AFrom, ATo: String): String;
  4. begin
  5.   Result := str_ireplace(AFrom, ATo, AStr);
  6. end;
  7.  
  8. function ReplaceStr(const AStr, AFrom, ATo: String): String;
  9. begin
  10.   Result := str_replace(AFrom, ATo, AStr);
  11. end;
  12.  
  13. //////    INSERT THESE NEW FUNCTIONS
  14.  
  15. function AnsiReplaceText(const AStr, AFrom, ATo: String): String;
  16. begin
  17.   Result := str_ireplace(AFrom, ATo, AStr);
  18. end;
  19.  
  20. function AnsiReplaceStr(const AStr, AFrom, ATo: String): String;
  21. begin
  22.   Result := str_replace(AFrom, ATo, AStr);
  23. end;      
  24.  

Basiclly how it works is, when you compile the project and run, it will iterate from your project starting unit ("index.pas"), and create a PHP file for each pascal file it finds in the search paths you have provided.

It will then call OpenUrl() to open the browser to the location of your PHP files. So, you will need to have a HTTP server running that supports PHP. I use my own Wascal IDE for that. Just set the Wascal server path to the location of the "index.php" file and connect. 127.0.0.1:8008 is the host the OpenURL() command is expecting, so, if you use something else, then you need to change that.

If you get it working as PHP, then try to change the address to:   "http://127.0.0.1:8008/index.cgi"

You should see the same output, but in this case, the native code CGI app which Lazarus created is generating the code.

I know, its weird, but it works.
« Last Edit: October 02, 2015, 03:40:37 am by Geepster »

derek.john.evans

  • Guest
Re: Pas2Php (Pascal to Php Translator for Web App's)
« Reply #9 on: October 11, 2014, 08:25:36 pm »
Thats odd. There must be some changes in the PasTree unit from v1.0.2. The old version must be using TList over TFPList

I'm using Lazarus 1.2.4.

You will need to upgrade I think.
« Last Edit: October 11, 2014, 08:27:11 pm by derek.john.evans »

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1072
    • Burdjia
Re: Pas2Php (Pascal to Php Translator for Web App's)
« Reply #10 on: October 14, 2014, 09:40:04 am »
I'm wondering if I can use it to build CodeIgniter's applications...  ::)

Good job, anyway.
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

derek.john.evans

  • Guest
Re: Pas2Php (Pascal to Php Translator for Web App's)
« Reply #11 on: October 23, 2014, 06:07:05 am »
Quote
I'm wondering if I can use it to build CodeIgniter's applications...

Should be possible with v0.5. You would need to write stub/dummy classes which expose classes & class members to Pas2Php. You would then need to make sure the search paths in "system.php" are setup so they find CodeIgniter units first.

Anyway, v0.5 is done. Its a fairly major upgrade. Lots of bug fixes. Full identifier searching, so a method in a subclass in another module refereed to via a class pointer, will get found. I've added session support which works for both PHP and native CGI app's. More of the standard RTL has been converted, and I've started a component model, with some basic HTML controls built on top.

http://www.wascal.net/?page=pas2php

These are some examples of component design in Pas2Php:
http://www.wascal.net/?page=pas2php/library

Again, this all works as a native CGI app. Full source code to my site is included in the package.

Cheers


Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1072
    • Burdjia
Re: Pas2Php (Pascal to Php Translator for Web App's)
« Reply #12 on: November 13, 2014, 01:30:51 pm »
Downloading.  I'll test it and try to "link" with CodeIgniter.  If it works, I'll "build" and release a library.  8)
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

derek.john.evans

  • Guest
Re: Pas2Php (Pascal to Php Translator for Web App's)
« Reply #13 on: November 14, 2014, 03:28:55 pm »
Quote
Downloading.  I'll test it and try to "link" with CodeIgniter.  If it works, I'll "build" and release a library.

Sounds interesting. I never thought/tried binding to a class library. (only PHP functions). The idea should work, but I never spent time on PHP only development, since I wanted my code to work as a CGI app.

Cheers and Good luck.

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1072
    • Burdjia
Re: Pas2Php (Pascal to Php Translator for Web App's)
« Reply #14 on: November 20, 2014, 11:47:30 am »
Right now I'm working on another projects (not web related).

Anyway, I was looking at it and I'm a bit confused.  I didn't found how to ... do anithing. Does it install? What should be compilded? I mean, how is it used?

Is there documentation somewhere? The documents I found seems to be uncomplete.
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

 

TinyPortal © 2005-2018