The Interpreter of DSLs
Parsing custom domain-specific languages natively in the shell.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Interpreter
ARCANUM SCHOOL: Divination // Linguamancy
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// EXECUTABLE BASH SCRIPT //
#!/usr/bin/env bash
# A simple DSL interpreter for moving a cyber-drone
# Syntax: MOVE UP/DOWN/LEFT/RIGHT <steps>
interpret_drone_cmd() {
local action=$1
local dir=$2
local steps=$3
if [[ "$action" != "MOVE" ]]; then
echo "Syntax Error: Unknown action '$action'"
return 1
fi
case "$dir" in
"UP") echo "Drone thrusters fired. Ascending $steps units." ;;
"DOWN") echo "Drone descending $steps units." ;;
"LEFT") echo "Drone panning left by $steps." ;;
"RIGHT") echo "Drone panning right by $steps." ;;
*) echo "Syntax Error: Unknown direction '$dir'" ;;
esac
}
# Client / Context
dsl_script="
MOVE UP 10
MOVE RIGHT 5
ATTACK FRONT
MOVE DOWN 2
"
echo "$dsl_script" | while read -r line; do
# Skip empty lines
[[ -z "$line" ]] && continue
interpret_drone_cmd $line
done
The Interpreter pattern comes alive through Bash’s read loops and case statements. By constructing a simple text-parsing engine, one can craft domain-specific languages—turning plaintext instructions into dynamic actions that direct external agents or manipulate the environment.
Runic Tags:
#bash
#interpreter
#behavioral
#dsl