/
reph.php
executable file
·127 lines (110 loc) · 3.23 KB
/
reph.php
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#! /usr/bin/env php
<?php
namespace reph;
require_once('/home/scriptor/pharen/lang.php');
use Pharen\Lexical as Lexical;
use \Seq as Seq;
use \FastSeq as FastSeq;
Lexical::$scopes['_reph'] = array();
include_once 'pharen/repl.php';
use pharen\repl as repl;
error_reporting(E_ALL);
set_time_limit(0);
ob_implicit_flush();
function get_repl_vars($file){
if($file){
$phpfile = str_replace(".phn", ".php", $file);
return repl\get_file_vars($phpfile, arr(\PharenVector::create_from_array(array())));
}
else{
return arr(\PharenVector::create_from_array(array()));
}
}
function _reph__lambdafunc18($prstr, $__closure_id){
$msgsock = Lexical::get_lexical_binding('_reph', 224, '$msgsock', isset($__closure_id)?$__closure_id:0);;
$prompt_str = format($prstr, "prompt");
socket_write($msgsock, $prompt_str, strlen($prompt_str));
$msgsock = Lexical::get_lexical_binding('_reph', 224, '$msgsock', isset($__closure_id)?$__closure_id:0);;
$code = socket_read($msgsock, 2048, PHP_NORMAL_READ);
if(false__question($code)){
return "quit";
}
else{
return $code;
}
}
function _reph__lambdafunc19($result, $__closure_id){
$msgsock = Lexical::get_lexical_binding('_reph', 224, '$msgsock', isset($__closure_id)?$__closure_id:0);;
$res_line = format(($result . "\n"), "result");
return socket_write($msgsock, $res_line, strlen($res_line));
}
function repl_loop($msgsock, $repl_vars){
$__scope_id = Lexical::init_closure("_reph", 224);
Lexical::bind_lexing("_reph", 224, '$msgsock', $msgsock);
$prompt = new \PharenLambda("reph\\_reph__lambdafunc18", Lexical::get_closure_id("_reph", $__scope_id));
Lexical::bind_lexing("_reph", 224, '$prompt', $prompt);
$reph_prn = new \PharenLambda("reph\\_reph__lambdafunc19", Lexical::get_closure_id("_reph", $__scope_id));
Lexical::bind_lexing("_reph", 224, '$reph_prn', $reph_prn);
if(false__question(repl\work("", $repl_vars, $prompt, $reph_prn))){
socket_shutdown($msgsock);
return TRUE;
}
else{
return NULL;
}
}
function accept_loop($sock, $repl_vars){
while(1){
$msgsock = socket_accept($sock);
$msg = format(repl\intro(), "result");
var_dump($msg);
socket_write($msgsock, $msg, strlen($msg));
if(false__question(repl_loop($msgsock, $repl_vars))){
return socket_close($sock);
}
}
}
function run($port=10000, $opts=array()){
global $argv;
$addr = "127.0.0.1";
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$__condtmpvar4 = Null;
if(isset($opts["file"])){
$__condtmpvar4 = $opts["file"];
}
else{
$__condtmpvar4 = NULL;
}
$file = $__condtmpvar4;
$repl_vars = get_repl_vars($file);
if($file){
compile_file($file);
}
else{
NULL;
}
\NamespaceNode::$repling = TRUE;
socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($sock, $addr, $port);
socket_listen($sock, 5);
prn("Initializing Reph server on ", $addr, ":", $port);
accept_loop($sock, $repl_vars);
return socket_close($sock);
}
if(!(count(debug_backtrace()))){
$opts = getopt("j::", arr(\PharenVector::create_from_array(array("file:"))));
if(isset($opts["j"])){
function format($string, $type){
return (json_encode(arr(hashify(array("s" => $string, "type" => $type)))) . "\n");
}
}
else{
function format($string, $type){
return $string;
}
}
run(10000, $opts);
}
else{
NULL;
}