The Interpreter of Custom Query Languages
Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Interpreter
ARCANUM SCHOOL: Divination // Sysadmin Telepathy
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// POWERSHELL PATTERN //
class Context {
[hashtable]$Variables = @{}
}
class IExpression {
[bool] Interpret([Context]$ctx) { throw "Not Implemented" }
}
class TerminalExpression : IExpression {
[string]$_data
TerminalExpression([string]$data) { $this._data = $data }
[bool] Interpret([Context]$ctx) {
return $ctx.Variables.ContainsKey($this._data) -and $ctx.Variables[$this._data] -eq $true
}
}
class AndExpression : IExpression {
[IExpression]$_expr1; [IExpression]$_expr2
AndExpression([IExpression]$e1, [IExpression]$e2) {
$this._expr1 = $e1; $this._expr2 = $e2
}
[bool] Interpret([Context]$ctx) {
return $this._expr1.Interpret($ctx) -and $this._expr2.Interpret($ctx)
}
}
# Grammar check
$ctx = [Context]::new()
$ctx.Variables["IsAdmin"] = $true
$ctx.Variables["IsDomainJoined"] = $true
$isAdmin = [TerminalExpression]::new("IsAdmin")
$isJoined = [TerminalExpression]::new("IsDomainJoined")
$canManage = [AndExpression]::new($isAdmin, $isJoined)
Write-Host "Can manage system? $($canManage.Interpret($ctx))"
To parse the esoteric dialects of compliance and security policies, the Interpreter maps raw strings into executable logical constructs, giving the sysadmin a native tongue for complex rulesets.
Runic Tags:
#powershell
#sysadmin
#behavioral
#interpreter
#parsing