translate

we can add a default translate method that shows errors.

this language bites off "pasta" and emits it. it throws an error if it can't.

TRANSLATED:



it's easy. [just] add a translate method to the term class. if there's something to emit, then emit it. OTHERWISE, throw an error.

class Term {
	//...
	translate(text) {
		const result = this.emit(text)
		if (result === null) {
			throw new Error(this.error(text))
		}
		return result
	}
}

then you can use it out-of-the-box.

const pastaTerm = new StringTerm("pasta")

function translate(text) {
	return pastaTerm.translate(text)
}

back to the dream