The Interpreter Hex
Given a language, define a representation for its grammar and an interpreter.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Interpreter
ARCANUM SCHOOL: Divination // Rune Translation
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// CODEX INCANTATION //
use fmt;
type Expression = struct {
interpret: *fn(e: *Expression, context: str) bool,
data: str,
};
fn interpret_terminal(e: *Expression, context: str) bool = {
// In standard Hare, we would use strings::contains, but we'll mock it for simplicity.
if (context == e.data) {
return true;
};
return false;
};
export fn main() void = {
let is_fire = Expression { interpret = &interpret_terminal, data = "ignis" };
let context1 = "ignis";
let context2 = "aqua";
fmt::printfln("Context 1 matches fire: {}", is_fire.interpret(&is_fire, context1))!;
fmt::printfln("Context 2 matches fire: {}", is_fire.interpret(&is_fire, context2))!;
};
When parsing ancient grimoires or alien signal dialects, the Interpreter builds an abstract syntax tree of understanding, unraveling the meaning of the arcane expressions.
Runic Tags:
#behavioral
#interpreter
#simple-systems-hexes