Menu

[r50]: / trunk / python / python.eval.php  Maximize  Restore  History

Download this file

57 lines (37 with data), 1.1 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
require_once __DIR__.'/python.tokenize.php';
class lang_python_eval extends lang_python_tokenize {
public static function python_eval($code,$file) {
$p = static::getParser();
$l = 0;
if (!strlen($code)) return;
try {
$t = static::python_tokenize($code);
$a = count($t);
$p->reset();
$c = 0;
while ($c < $a) {
$k = $t[$c];
$p-> eat($k->name,$k->value);
$c++;
}
$p->eat_eof();
} catch (Exception $e) {
echo '<pre style="padding:8px;background:f2f2f2;">';
echo ' File "'.$file.'", line '.$k->row."\n";
echo ' '.static::get_code_line($code,$k->row)."\n";
echo ' '.'^'."\n";
switch($k->name) {
//case "NAME": echo "NameError: name '{$k->value->name}' is not defined";break;
case "INDENT": echo 'IndentationError: unexpected indent';break;
default: echo 'SyntaxError: invalid syntax';break;
}
var_dump($e);
echo '</pre>';
}
}
public static function get_code_line($code,$row) {
$code = explode("\n",$code);
return trim($code[$row-1]);
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.