+ All Categories
Home > Technology > Interpreter

Interpreter

Date post: 17-Jun-2015
Category:
Upload: hiremaga
View: 915 times
Download: 4 times
Share this document with a friend
Description:
The slides from a presentation I did on the Interpreter design pattern at the Melbourne Patterns group.
Popular Tags:
29
Interpreter Abhijit Hiremagalur Cogent Consulting
Transcript
Page 1: Interpreter

InterpreterAbhijit HiremagalurCogent Consulting

Page 2: Interpreter

Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language [GOF, 243]

Page 3: Interpreter

DomainLanguageGrammar

Object Hierarchy

Page 4: Interpreter

Composite + interpret()

Page 5: Interpreter
Page 6: Interpreter

Reverse Polish Notation

Page 7: Interpreter

3 + 4 is expressed as

3 4 +

Page 8: Interpreter

3 - 4 + 5

Page 9: Interpreter

(3 - 4) + 5 or 3 - (4 +5)

Which one is it?

Page 10: Interpreter

5 4 3 - + 5 (4 3 -) +

5 (1) +6

Page 11: Interpreter

The code

Page 12: Interpreter

‘41 2 1 - +’ equals 42

Page 13: Interpreter

Stack what?

[](41) => [41](2) => [41, 2](1) => [41, 2, 1](-) => [41, 1](+) => [42]

Page 14: Interpreter

Easy to implement, change & extend?

Page 15: Interpreter

Doesn’t address parsing

Page 16: Interpreter

Use something else if grammar is complex

Page 17: Interpreter

Refactor to an Interpreter when...

Page 18: Interpreter

“Numerous methods on a class combine elements of an

implicit language”

Page 19: Interpreter

Replace Implicit Language with Interpreter [Kerievsky]

Page 20: Interpreter
Page 21: Interpreter
Page 22: Interpreter

“Define classes for the elements of the implicit

language so that instances may be combined to form interpretable expressions.”

Page 23: Interpreter

A.K.A ?

• Little language

• Macro

• Domain specific language

Page 24: Interpreter

Domain Specific Language (DSL)

Page 25: Interpreter

Internal vs External DSL

Page 26: Interpreter

Spec productSpec = both(priceIsBelow(9.00f)).and(not(coloured(WHITE)));

Spec productSpec = parse(“price below 9.00f and not coloured white”);

Page 27: Interpreter

Share symbols with Flyweight

Page 28: Interpreter

Move operations to Visitor

Page 29: Interpreter

The end.


Recommended