💎 CRYSTAL GEM DIFFICULTY: Archmage Behavioral

The Interpreter: The Runecarver's Lexicon

Given a language, defining a representation for its grammar along with an interpreter that uses the representation to evaluate sentences in the language.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Interpreter
ARCANUM SCHOOL: Divination // Lexicography
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// CRYSTAL PATTERN //
class ArcaneContext
  property output : String = ""
end

abstract class Expression
  abstract def interpret(context : ArcaneContext)
end

class TerminalRune < Expression
  @meaning : String
  def initialize(@meaning : String)
  end

  def interpret(context : ArcaneContext)
    context.output += "#{@meaning} "
  end
end

class SequenceRune < Expression
  @expr1 : Expression
  @expr2 : Expression

  def initialize(@expr1 : Expression, @expr2 : Expression)
  end

  def interpret(context : ArcaneContext)
    @expr1.interpret(context)
    @expr2.interpret(context)
  end
end

context = ArcaneContext.new

fire = TerminalRune.new("Ignis")
ball = TerminalRune.new("Globus")
fireball = SequenceRune.new(fire, ball)

fireball.interpret(context)

puts "Deciphered spell intent: #{context.output.strip}"

When dealing with deeply nested arcane syntax, the Runecarver’s Lexicon builds a syntactic tree. The Interpreter walks this tree, translating sequences of ancient terminal runes into an execution context the modern Crystal engine can fire upon.

Runic Tags: #behavioral #interpreter #crystal #lexicon