Skip to content

Interpreter

A pattern that interprets small domain language rules through grammar objects and computes a result.

  • When building a quest condition DSL.
  • When dialogue branch conditions should be data-driven.
  • Expression
  • Terminal
  • Nonterminal
  • Context

The code below is a simplified Unity example based on the scenario described above.

public interface IConditionExpression
{
bool Evaluate(PlayerContext context);
}
public sealed class LevelAtLeastExpression : IConditionExpression
{
private readonly int requiredLevel;
public LevelAtLeastExpression(int requiredLevel)
{
this.requiredLevel = requiredLevel;
}
public bool Evaluate(PlayerContext context)
{
return context.PlayerLevel >= requiredLevel;
}
}
public sealed class AndExpression : IConditionExpression
{
private readonly IConditionExpression leftExpression;
private readonly IConditionExpression rightExpression;
public AndExpression(IConditionExpression leftExpression, IConditionExpression rightExpression)
{
this.leftExpression = leftExpression;
this.rightExpression = rightExpression;
}
public bool Evaluate(PlayerContext context)
{
return leftExpression.Evaluate(context) && rightExpression.Evaluate(context);
}
}
  • Behavior is separated into smaller units, which reduces the impact of changes.
  • Adding or swapping rules is relatively safe.
  • As the number of objects and indirect calls increases, the flow can become harder to follow.
  • Ordering bugs should be pinned down with tests.

This shows the flow where grammar rules are interpreted as an expression tree to compute a result.

Interpreter Flow