Building the CLI Interface
The Read-Eval-Print Loop
Construct an infinite loop that prompts the user for inputs, processes them, and prints the result.
Video Walkthrough
Core Concepts
- Process standard input stream continuously
- Dynamically allocate and grow the input buffer to support variable-length queries
- Graceful terminal exits and resource deallocation
What is a REPL?
A Read-Eval-Print Loop (REPL) is the interactive Command-Line Interface for relational databases. Whether you invoke sqlite3, psql, or mysql from your shell, you are greeted by an infinite prompt loop that reads user queries, parses and executes them, and formats the table output back to stdout.
The REPL is the gateway to your storage engine. Because human input is unpredictable, a systems-level REPL must enforce strict resiliency guarantees:
- It must never crash or segfault on long queries, empty newlines, or unusual formatting.
- It must gracefully clean up dynamic heap buffers when encountering an End-of-File (EOF) signal (such as
Ctrl+Dor piped input streams).
The Two Types of Commands
When designing the evaluator routing loop, user input is divided into two distinct architectural tracks:
- Meta-Commands (Dot Commands): Any query beginning with a '.' character (such as
.exitor.help) represents an internal administrative instruction. These are parsed directly by the REPL interface layer and never touch the SQL compiler pipeline. - SQL Statements: All other valid queries (such as
SELECT,INSERT, orCREATE) represent logical relational data operations and are delegated to the Lexer and Syntax Parser stages.
Conceptual Execution Algorithms
REPL Main Execution Loop
The core lifecycle of a CLI-based database interface is an infinite loop that repeats three primary operations: prompt, read, evaluate.
- [1]Initialize input state: allocate a buffer descriptor with null pointer and zero size.
- [2]Print the database prompt command line sign (e.g., "db > ") to standard output.
- [3]Read input line from standard input stream into the dynamically resizing buffer.
- [4]Check if the read failed (End of File / EOF). If failed, clean up memory and terminate.
- [5]Evaluate the input line to check if it matches database commands.
- [6]Print results or feedback, reset the buffer, and loop back to the beginning.
Meta-Command Dispatcher
Commands starting with a dot (e.g., .exit) are meta-commands handled separately from SQL execution.
- [1]Extract the first token from the input line.
- [2]Verify if the token starts with the '.' character.
- [3]If yes: check if it matches '.exit'. If matched, invoke clean-up routines and exit process with code 0.
- [4]If the dot command is unrecognized, print 'Unrecognized meta-command' and return to the main prompt loop.
- [5]If no dot is present, route the input to the SQL Parser.