The Interpreter Lexicon
Designing an abstract syntax tree to parse and execute forgotten runic languages.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Interpreter
ARCANUM SCHOOL: Divination // Linguistics
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// FSHARP PATTERN //
type Context() =
let mutable knowledge = Map.empty<string, int>
member _.Set(k, v) = knowledge <- knowledge.Add(k, v)
member _.Get(k) = knowledge.TryFind(k) |> Option.defaultValue 0
type IExpression =
abstract member Interpret: Context -> int
type Variable(name: string) =
interface IExpression with
member _.Interpret(ctx) = ctx.Get(name)
type AddExpression(left: IExpression, right: IExpression) =
interface IExpression with
member _.Interpret(ctx) = left.Interpret(ctx) + right.Interpret(ctx)
// Parsing "Mana + Intellect"
let ctx = Context()
ctx.Set("Mana", 50)
ctx.Set("Intellect", 20)
let mana = Variable("Mana") :> IExpression
let intl = Variable("Intellect") :> IExpression
let spellPower = AddExpression(mana, intl) :> IExpression
printfn "Total Spell Power: %d" (spellPower.Interpret(ctx))
Through Active Patterns and Abstract Syntax Trees, F# is uniquely suited for the Interpreter pattern. Here, we build a basic evaluator for arcane formulas, parsing the leylines of raw syntax into calculable magical force.
Runic Tags:
#behavioral
#interpreter
#fsharp
#lexicon