The Interpreter in AWK
Construct an arcane parser to evaluate micro-languages embedded in text files.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Interpreter
ARCANUM SCHOOL: Divination // Syntax-Decoding
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// CODEX INCANTATION //
# The Evaluator for a simple mathematical grammar: "LHS OP RHS"
function eval_expression(expr, parts, lhs, op, rhs) {
split(expr, parts, " ")
lhs = parts[1] + 0
op = parts[2]
rhs = parts[3] + 0
if (op == "+") return lhs + rhs
if (op == "-") return lhs - rhs
if (op == "*") return lhs * rhs
if (op == "/") return (rhs != 0) ? lhs / rhs : "DIV_BY_ZERO"
return "SYNTAX_ERROR"
}
BEGIN {
print "Interpreting '10 + 5' => " eval_expression("10 + 5")
print "Interpreting '42 * 2' => " eval_expression("42 * 2")
print "Interpreting '9 / 0' => " eval_expression("9 / 0")
}
AWK is fundamentally an interpreter, but building a recursive descent parser or a simple expression evaluator within AWK elevates the scribe to Archmage. This pattern parses strings, separates syntax tokens, and executes the semantic meaning bound within.
Runic Tags:
#awk
#text-processing
#behavioral