Last active
November 22, 2019 11:02
-
-
Save gollilla/7598d7520ce0baeddc2da9942367f17b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Your code here! | |
class Switcher { | |
public static $calls = []; | |
public static function register($case, Closure $callback){ | |
self::$calls[$case] = new Executor($callback); | |
} | |
public static function onCase($case){ | |
return isset(self::$calls[$case]) ? self::$calls[$case] : new Executor(function(){}); | |
} | |
} | |
class Executor { | |
public function __construct(Closure $callback){ | |
$this->callback = $callback; | |
} | |
public function call(...$args){ | |
$callback = $this->getCallBack(); | |
return call_user_func_array($callback, $args); | |
} | |
public function getCallBack(){ | |
return $this->callback; | |
} | |
} | |
class BrainFucker{ | |
const MAX_REGISTER = 8; | |
public $register = []; | |
public $pointer = 0; | |
function __construct($string){ | |
$this->register = array_fill(0, self::MAX_REGISTER, 0); | |
$this->string = $this->cut_space($string); | |
$self = $this; | |
Switcher::register("+", function() use($self) { | |
$self->register[$self->pointer] += 1; | |
}); | |
Switcher::register("-", function() use($self) { | |
$self->register[$self->pointer] -= 1; | |
}); | |
Switcher::register(">", function() use($self) { | |
$self->pointer += 1; | |
}); | |
Switcher::register("<", function() use($self) { | |
$self->register[$self->pointer] -= 1; | |
}); | |
Switcher::register(".", function() use($self) { | |
echo chr($self->register[$self->pointer]); | |
}); | |
} | |
function cut_space($string){ | |
return str_replace([" ", " "], "", $string); | |
} | |
function go(){ | |
$exe = $this->string; | |
foreach(str_split($exe) as $p){ | |
Switcher::onCase($p)->call(); | |
} | |
} | |
} | |
$bf = new BrainFucker("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>++++++++++++++++++++++++++++++++++++++++++++++++++++++++."); | |
$bf->go(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment