Pseudo Toy SQLite clone in php
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 | |
define("EXIT_SUCCESS", 0); | |
define("EXIT_FAILURE", 1); | |
class InputBuffer | |
{ | |
public ?string $buffer; | |
public int $buffer_length; | |
public int $input_length; | |
public function __construct() { | |
$this->buffer = null; | |
$this->buffer_length = 0; | |
$this->input_length = 0; | |
} | |
} | |
function print_prompt(): void { | |
echo "db > "; | |
} | |
function read_input(InputBuffer $input_buffer): void { | |
$input = fgets(STDIN); | |
$input_buffer->buffer_length = strlen($input); | |
if ($input === false) { | |
echo "Error reading input", PHP_EOL; | |
exit(EXIT_FAILURE); | |
} | |
$input_buffer->buffer = trim($input); | |
$input_buffer->input_length= strlen($input_buffer->buffer); | |
} | |
function close_input_buffer(InputBuffer $input_buffer): void { | |
$input_buffer = null; | |
} | |
/** | |
* main | |
*/ | |
$input_buffer = new InputBuffer(); | |
while (true) { | |
print_prompt(); | |
read_input($input_buffer); | |
if ($input_buffer->buffer === ".exit") { | |
close_input_buffer($input_buffer); | |
exit(EXIT_SUCCESS); | |
} else { | |
echo "Unrecognized command ", $input_buffer->buffer, PHP_EOL; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment