<?php
$_PYTHON = array(
"DEBUG" => array(
"DUMP_TOKEN" => false
),
"PARSER" => null,
);
function python_start() {
global $_PYTHON;
require_once("lime/parse_engine.php");
require_once("python.class");
require_once("python-tokenizer.php");
require_once("python-interface.php");
$_PYTHON["PARSER"] = new parse_engine(new python());
}
function python_eval($pycode) {
global $_PYTHON;
$parser = &$_PYTHON["PARSER"];
$codeln = 1;
if (!strlen($pycode)) return;
try {
$parser-> reset();
$tokens = python_tokenize($pycode);
$alltok = count($tokens);
$cursor = 0;
while($cursor < $alltok) {
$token0 = $tokens[$cursor];
$token1 = isset($tokens[$cursor+1]);
if ($_PYTHON["DEBUG"]["DUMP_TOKEN"]) python_dump_token($token0);
$parser->eat($token0["name"], $token0["value"]);
if ($token0["name"] == "NEWLINE") {
$codeln++;
}
$cursor++;
}
$parser->eat_eof();
} catch (parse_error $e) {
echo '<pre>';
echo $e->getMessage(), "\nLine: $codeln;", "\n";
echo '</pre>';
}
}
function python_file($filename) {
$code = file_get_contents($filename);
python_eval($code);
}
function python_exec($filename) {
$code = file_get_contents($filename);
python_eval($code);
}
function python_build() {
require_once("lime/lime.php");
$code = parse_lime_grammar(dirname(__FILE__)."/python.lime");
file_put_contents(dirname(__FILE__)."/python.class","<?php \n".$code."\n?>");
}
function python_sanitize($code) {
return $code;
}
function re($regex,$input,&$output) {
return preg_match($regex,$input,&$output);
}
function token($name,$value=null) {
return array(
"name" => $name,
"value" => $value
);
}
function python_parse_comment($comment) {
global $_PYTHON;
if (re("/^#\{([a-zA-Z_][a-zA-Z0-9_]*)\}:(.*)/",$comment,$r)){
$_PYTHON["DEBUG"][$r[1]] = trim($r[2]);
#var_dump($_PYTHON["DEBUG"]);
}
}
function python_dump_token($token) {
echo '<span style="margin:0 2px 0 0;display:inline-block;padding:2px 4px;font-family:arial;font-size:10px;background:yellow;color:#323232;border:1px solid #323232;">';
echo $token["name"].'('.$token["value"].')';
echo '</span>';
}