Project 14: Script Interpreter
A shell scripting interpreter supporting
if/then/else/fi,while/do/done,for/in/do/done,case/esac, functions, local variables, andreturn/exit.
Quick Reference
| Attribute | Value |
|---|---|
| Primary Language | C |
| Alternative Languages | Rust, Go, OCaml |
| Difficulty | Level 4: Expert (The Systems Architect) |
| Time Estimate | 2-3 weeks |
| Knowledge Area | Interpreters / Language Design |
| Tooling | Shell Scripting |
| Prerequisites | Projects 1-7, understanding of interpreters |
What You Will Build
A shell scripting interpreter supporting if/then/else/fi, while/do/done, for/in/do/done, case/esac, functions, local variables, and return/exit.
Why It Matters
This project builds core skills that appear repeatedly in real-world systems and tooling.
Core Challenges
- Control flow parsing (if/while/for/case grammar) → maps to language design
- Condition evaluation (exit status determines truth) → maps to shell semantics
- Function definition/call (name() { body; }) → maps to subroutines
- Variable scope (local keyword, positional parameters) → maps to scoping
- Reading scripts (shebang, sourcing vs executing) → maps to execution modes
Key Concepts
- Shell compound commands: POSIX Shell Specification Section 2.9.4 - The Open Group
- Shell functions: POSIX Shell Specification Section 2.9.5 - The Open Group
- Interpreter patterns: “Language Implementation Patterns” Chapter 8 - Parr
Real-World Outcome
$ cat test.sh
#!/path/to/mysh
greet() {
local name="$1"
echo "Hello, $name!"
}
for i in 1 2 3; do
greet "User$i"
done
if [ -f /etc/passwd ]; then
echo "System file exists"
else
echo "Not a Unix system"
fi
count=0
while [ $count -lt 5 ]; do
echo "Count: $count"
count=$((count + 1))
done
$ ./mysh test.sh
Hello, User1!
Hello, User2!
Hello, User3!
System file exists
Count: 0
Count: 1
Count: 2
Count: 3
Count: 4
Implementation Guide
- Reproduce the simplest happy-path scenario.
- Build the smallest working version of the core feature.
- Add input validation and error handling.
- Add instrumentation/logging to confirm behavior.
- Refactor into clean modules with tests.
Milestones
- Milestone 1: Minimal working program that runs end-to-end.
- Milestone 2: Correct outputs for typical inputs.
- Milestone 3: Robust handling of edge cases.
- Milestone 4: Clean structure and documented usage.
Validation Checklist
- Output matches the real-world outcome example
- Handles invalid inputs safely
- Provides clear errors and exit codes
- Repeatable results across runs
References
- Main guide:
SHELL_INTERNALS_DEEP_DIVE_PROJECTS.md - “Language Implementation Patterns” by Terence Parr