Skip to content

Write an interpreter

An interpreter is the consumer's function from the surface as it stands and the words a person said or typed to a move, or to a reason it could not make one. The endpoint calls it for every say and every hear. The library ships none, because what words mean belongs to the consumer's domain.

The seam

trait Interpreter:
  def interpret(surface: Surface, text: String): Interpretation

enum Interpretation:
  case Understood(move: Move)
  case NotUnderstood(reason: String)

That block is the library's own declaration, in Interpreter.scala.

A first interpreter

This one turns any words naming Newcastle into a shelf anchored on the theme, and understands nothing else:

object NewcastleInterpreter extends Interpreter:
  def interpret(surface: Surface, text: String): Interpretation =
    if text.toLowerCase.contains("newcastle") then
      Interpretation.Understood(
        Move(shape = Some("shelf"), anchor = Some(Anchor("theme", "newcastle")))
      )
    else Interpretation.NotUnderstood(s"nothing on the shelves matches '$text'")

What the endpoint does with the answer

  • Understood: the endpoint adds the words as the move's request, the time from its clock, and the say's utterance when it has one, and sends the move to the surface. The answer to the say is the surface after it.
  • Not understood: nothing changes. The answer carries the reason and the surface as it was.

So the interpreter's move needs no request, at or utterance of its own; whatever it sets there for a say is replaced. For a hear the move is returned as the interpreter made it, and nothing is kept.

Rules an interpreter follows

  1. It moves the surface and does nothing else. No order placed, no message sent, no call that changes anything outside the surface. An act the words begin becomes pending: Move(pending = Some(Pending("forecast", Map("outcome" -> "Yes", "estimate" -> "70")))). The surface never acts says why.
  2. It uses the vocabulary's words. Its move is checked like any other, and a word outside the vocabulary fails the say with 400.
  3. It reads the surface it is given. "Sort by newest" means something different on a shelf than on an item; the surface is passed in so the interpreter can tell. It is read before the interpreter is called, and a move another device makes in between is not seen.
  4. Its reasons are for a person. NotUnderstood's reason is shown on the device, so it says what was not found in the person's terms, not the interpreter's.
  5. It is safe on a prefix, if the device hears partial words. Each prefix of what a person says should move the surface a step towards where the whole would, because a device may say "new", then "newcastle", then "newcastle this weekend" under one utterance.
  6. It answers quickly. interpret is a plain function called on the request: a slow interpreter is a slow say for every device.

Test it

An interpreter is a pure function of its two arguments, so a unit test calls it with a surface made by Surface.empty(id) and copied into shape, and checks the Interpretation. The library's own suites test the whole say over HTTP with this interpreter, in SurfaceHttpSuite.scala.