óCoffeeScript Cookbook

Interpreter Pattern


Someone else needs to run parts of your code in a controlled fashion. Alternately, your language of choice cannot express the problem domain in a concise fashion.


Use the Interpreter pattern to create a domain-specific language that you translate into specific code.

Assume, for example, that the user wants to perform math inside of your application. You could let them forward code to eval but that would let them run arbitrary code. Instead, you can provide a miniature "stack calculator" language that you parse separately in order to only run mathematical operations while reporting more useful error messages.

class StackCalculator
    parseString: (string) ->
        @stack = [ ]
        for token in string.split /\s+/
            @parseToken token

        if @stack.length > 1
            throw "Not enough operators: numbers left over"

    parseToken: (token, lastNumber) ->
        if isNaN parseFloat(token) # Assume that anything other than a number is an operator
            @parseOperator token
            @stack.push parseFloat(token)

    parseOperator: (operator) ->
        if @stack.length < 2
            throw "Can't operate on a stack without at least 2 items"

        right = @stack.pop()
        left = @stack.pop()

        result = switch operator
            when "+" then left + right
            when "-" then left - right
            when "*" then left * right
            when "/"
                if right is 0
                    throw "Can't divide by 0"
                    left / right
                throw "Unrecognized operator: #{operator}"

        @stack.push result

calc = new StackCalculator

calc.parseString "5 5 +" # => { result: 10 }

calc.parseString "4.0 5.5 +" # => { result: 9.5 }

calc.parseString "5 5 + 5 5 + *" # => { result: 100 }

    calc.parseString "5 0 /"
catch error
    error # => "Can't divide by 0"

    calc.parseString "5 -"
catch error
    error # => "Can't operate on a stack without at least 2 items"

    calc.parseString "5 5 5 -"
catch error
    error # => "Not enough operators: numbers left over"

    calc.parseString "5 5 5 foo"
catch error
    error # => "Unrecognized operator: foo"


As an alternative to writing our own interpreter, you can co-op the existing CoffeeScript interpreter in a such a way that its normal syntax makes for more natural (and therefore more comprehensible) expressions of your algorithm.

class Sandwich
    constructor: (@customer, @bread='white', @toppings=[], @toasted=false)->

white = (sw) ->
    sw.bread = 'white'

wheat = (sw) ->
    sw.bread = 'wheat'

turkey = (sw) ->
    sw.toppings.push 'turkey'

ham = (sw) ->
    sw.toppings.push 'ham'

swiss = (sw) ->
    sw.toppings.push 'swiss'

mayo = (sw) ->
    sw.toppings.push 'mayo'

toasted = (sw) ->
    sw.toasted = true

sandwich = (customer) ->
    new Sandwich customer

to = (customer) ->

send = (sw) ->
    toastedState = sw.toasted and 'a toasted' or 'an untoasted'

    toppingState = ''
    if sw.toppings.length > 0
        if sw.toppings.length > 1
            toppingState = " with #{sw.toppings[0..sw.toppings.length-2].join ', '} and #{sw.toppings[sw.toppings.length-1]}"
            toppingState = " with #{sw.toppings[0]}"
    "#{sw.customer} requested #{toastedState}, #{sw.bread} bread sandwich#{toppingState}"

send sandwich to 'Charlie' # => "Charlie requested an untoasted, white bread sandwich"
send turkey sandwich to 'Judy' # => "Judy requested an untoasted, white bread sandwich with turkey"
send toasted ham turkey sandwich to 'Rachel' # => "Rachel requested a toasted, white bread sandwich with turkey and ham"
send toasted turkey ham swiss sandwich to 'Matt' # => "Matt requested a toasted, white bread sandwich with swiss, ham and turkey"

This example allows for layers of functions by how it returns the modified object so that outer functions can modify it in turn. By borrowing a very and the particle to, the example lends natural grammar to the construction and ends up reading like an actual sentence when used correctly. This way, both your CoffeeScript skills and your existing language skills can help catch code problems.