g-i-o- / pyphp

Php parser, compiler and interpreter for python
MIT License
10 stars 4 forks source link

pyphp

Php parser, compiler and interpreter for python

Usage

Executing php code

You can execute php code using the pyphp.executer module. The execute_file reads a php file and executes it, while execute_php executes given php code.

Using the parser

The parser breaks up the php code into a list of tokens.

Using the compiler

The compiler takes a list of tokens a creates a syntax tree representing the code

phpcode = r'Hello <?php echo "World.\n";?>';

syntax_tree = pyphp.compiler.compile_php(phpcode)


- Or compile the list of tokens directly:
``` python
import pyphp.parser
import pyphp.compiler

phpcode = r'Hello <?php echo "World.\n";?>';

token_list = pyphp.parser.parse_php(phpcode)

syntax_tree = pyphp.compiler.compile_php(token_list)

PHP Library support

Currently, only a minimal set of PHP's language features and built in library is supported, but the plan is to support as most of it as possible.

Language features not supported yet are namespaces, anonymous functions, lots of other stuff.

Below is the official subset of PHP's built in features currently supported.

name description supported
true true literal yes
false false literal yes
null null literal yes
E_ERROR error constant yes
E_WARNING error constant yes
E_PARSE error constant yes
E_NOTICE error constant yes
E_CORE_ERROR error constant yes
E_CORE_WARNING error constant yes
E_COMPILE_ERROR error constant yes
E_COMPILE_WARNING error constant yes
E_USER_ERROR error constant yes
E_USER_WARNING error constant yes
E_USER_NOTICE error constant yes
E_STRICT error constant yes
E_RECOVERABLE_ERROR error constant yes
E_DEPRECATED error constant yes
E_USER_DEPRECATED error constant yes
E_ALL error constant yes
isset Checks if a variable is set yes
empty checks if a variable is empty yes
die outputs to stdout and halts execution yes
defined returns whether a constant is defined or not yes
require_once requires a php script once yes
require requires a php script yes
include_once includes a php script once yes
include includes a php script yes
var_dump dumps a variable to stdout yes
ini_set modifies settings derived from php.ini no-op
error_reporting modifies error reporting level yes
date_default_timezone_set sets default datetime zone no-op