The Interpreter Sigils
Given a language, defining a representation for its grammar along with an interpreter.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Interpreter
ARCANUM SCHOOL: Divination // Rune Translation
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// CSHARP PATTERN //
using System;
using System.Collections.Generic;
namespace EnterpriseEvocation
{
public interface IExpression
{
bool Interpret(string context);
}
public class TerminalExpression : IExpression
{
private readonly string _data;
public TerminalExpression(string data) => _data = data;
public bool Interpret(string context) => context.Contains(_data);
}
public class OrExpression : IExpression
{
private readonly IExpression _expr1;
private readonly IExpression _expr2;
public OrExpression(IExpression expr1, IExpression expr2)
{
_expr1 = expr1;
_expr2 = expr2;
}
public bool Interpret(string context) => _expr1.Interpret(context) || _expr2.Interpret(context);
}
}
When parsing ancient dead languages, an enterprise system must establish a domain-specific grammar. The Interpreter maps semantic meaning to abstract syntax trees of terminal and non-terminal nodes.
Runic Tags:
#behavioral
#interpreter
#parsing
#csharp