@ 2015-12-21T22:35:10Z <?php
class interpreter {
private $_code;
public function __construct($code) {
if(!is_string($code)) exit('Error while parsing code; Code not conformal.');
$this->_code = $code;
$this->parser();
}
public function parser($code = null) {
if($code === null) $code = $this->_code;
$calls = [];
$variables = [];
$conditions = [];
preg_match('#(.*)\((.*)\)#', $code, $calls);
preg_match('#if\((.*)\) \{(.*)\}#s', $code, $conditions);
if($conditions && isset($conditions[0])) $this->conditions_ifelse($conditions);
if($calls && isset($calls[0])) $this->calls($calls);
}
private function calls(array $calls) {
switch($calls[1]) {
case 'print': $this->func_print($calls[2]); break;
}
}
private function condition_ifelse(array $conditions) {
if(!preg_match('#(.*)(==|===|\<|\<=|\>|\>=|!=|!==|&&|\|\||AND|OR|XOR)(.*)#i', $conditions[1])) exit('Condition parsing error.');
}
}
abstract class lang {
const version = 0.1;
}
class console extends lang {
}
class out extends console {
public function println($arg) {
if(preg_match('#^("|\')(.*)("|\')$#', $arg)) $arg = preg_replace('#^("|\')(.*)("|\')$#', '$2', $arg);
echo($arg);
}
}
$code = <<<CODE
int var = 1;
if(var == 1) {
console.out.println("Hello World!");
}
else {
console.out.println("Hi!");
}
CODE;
$program = new interpreter($code);
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 7.0.0 - 7.0.20 , 7.1.0 - 7.1.20 , 7.2.0 - 7.2.33 , 7.3.16 - 7.3.33 , 7.4.0 - 7.4.33 , 8.0.0 - 8.0.30 , 8.1.0 - 8.1.28 , 8.2.0 - 8.2.18 , 8.3.0 - 8.3.6 Fatal error: Uncaught Error: Call to undefined method interpreter::conditions_ifelse() in /in/uth5B:16
Stack trace:
#0 /in/uth5B(7): interpreter->parser()
#1 /in/uth5B(49): interpreter->__construct('int var = 1;\nif...')
#2 {main}
thrown in /in/uth5B on line 16
Process exited with code 255 . Output for 5.5.24 - 5.5.35 , 5.6.8 - 5.6.28 Fatal error: Call to undefined method interpreter::conditions_ifelse() in /in/uth5B on line 16
Process exited with code 255 . preferences:dark mode live preview
151.76 ms | 402 KiB | 182 Q