The Interpreter Pattern in VBA
Parse and evaluate arcane syntax to execute custom domain-specific incantations.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Interpreter
ARCANUM SCHOOL: Divination // Spreadsheet Necromancy
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// VBA PATTERN //
' Interface: IExpression (Class Module)
Public Function Evaluate(context As Object) As Boolean
End Function
' Class: CellContainsExpression (Class Module)
Implements IExpression
Private pKeyword As String
Public Sub Init(keyword As String)
pKeyword = keyword
End Sub
Private Function IExpression_Evaluate(context As Object) As Boolean
Dim target As Range
Set target = context ' Cast context to Range
IExpression_Evaluate = InStr(1, target.Value, pKeyword, vbTextCompare) > 0
End Function
' Client
Public Sub RunInterpreter()
Dim expr As New CellContainsExpression
expr.Init "Soul"
Dim cell As Range
Set cell = Sheet1.Range("A1")
cell.Value = "Fragment of a Soul"
If expr.Evaluate(cell) Then
Debug.Print "The cell contains the dark essence."
End If
End Sub
The Interpreter: The Runecarver’s Parse
When you find yourself constantly stringing together wildly complex search queries or parsing highly specific, bespoke text commands from a cell, regular expressions often fall short or become unreadable.
The Interpreter pattern dictates creating a syntax tree of expression objects. Each node in the tree evaluates a small piece of the grammar. In Spreadsheet Necromancy, this allows you to define an entirely new domain-specific language within Excel, reading cell strings not as raw data, but as complex runic equations to be decoded and executed dynamically.
Runic Tags:
#vba
#interpreter
#behavioral
#spreadsheet-necromancy