-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcesil.php
69 lines (52 loc) · 1.79 KB
/
cesil.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
<?php
error_reporting(0);
ini_set('display_errors', 0);
// load the functions
require('functions.php');
// set variables
$cli = (php_sapi_name() == "cli") ? TRUE : FALSE;
$debug = FALSE;
$cmds = array('LOAD', 'STORE', 'IN', 'ADD', 'SUBTRACT', 'MULTIPLY', 'DIVIDE', 'JUMP', 'JIZERO', 'JINEG', 'PRINT', 'OUT', 'LINE', 'HALT');
$cmdsp = array('LOAD', 'STORE', 'ADD', 'SUBTRACT', 'MULTIPLY', 'DIVIDE', 'JUMP', 'JIZERO', 'JINEG', 'PRINT');
$data = '';
// is there a file to run?
if ($argc == 2 && $cli){
$filename = $argv[1];
if (str_ends_with(strtolower($filename), '.csl')) $filename = substr($filename, 0, strlen($data)-4);
$data = load($filename, FALSE);
if (!empty($data)){
run($data, $debug);
die;
}else{
echo 'File '.$filename.' not found or not a valid CESIL file.'.PHP_EOL;
die;
}
}
// interactive mode
if ($cli){
echo 'C.E.S.I.L for PHP'.PHP_EOL;
echo ' '.PHP_EOL;
echo ' '.PHP_EOL;
echo '(c)2024 Neil Thompson '.PHP_EOL;
while(TRUE){
echo 'ok>';
// get and process the command
$line = ($cli) ? trim(fgets(STDIN)) : trim($_REQUEST['cmd']);
$cmd = (strpos($line,' ')===FALSE) ? strtolower($line) : strtolower(substr($line,0,strpos($line,' ')));
list($data, $output, $debug) = process_command($line, $cmd, $data, $debug);
// write out any output
echo $output;
}
}else{
// start the session
session_start();
$data = unserialize($_SESSION['data']);
// get and process the command
$line = ($cli) ? trim(fgets(STDIN)) : trim($_REQUEST['cmd']);
$cmd = (strpos($line,' ')===FALSE) ? strtolower($line) : strtolower(substr($line,0,strpos($line,' ')));
list($data, $output, $debug) = process_command($line, $cmd, $data, $debug);
$_SESSION['data'] = serialize($data);
// write out any output
echo nl2br($output);
}
?>