Skip to content

Commit 2c42b3a

Browse files
committed
generation
1 parent 1b4ad9d commit 2c42b3a

File tree

5 files changed

+189
-1233
lines changed

5 files changed

+189
-1233
lines changed

Design-Patterns.playground.zip

474 Bytes
Binary file not shown.

Design-Patterns.playground/contents.swift

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,96 @@ let doorModule = HAL9000DoorsOperations(doors:podBayDoors)
172172
doorModule.open()
173173
doorModule.close()
174174
/*:
175+
🎶 Interpreter
176+
--------------
177+
178+
The interpreter pattern is used to evaluate sentences in a language.
179+
180+
### Example
181+
*/
182+
183+
protocol IntegerExp {
184+
func evaluate(context: IntegerContext) -> Int
185+
func replace(character: Character, integerExp: IntegerExp) -> IntegerExp
186+
func copy() -> IntegerExp
187+
}
188+
189+
class IntegerContext {
190+
private var data: [Character:Int] = [:]
191+
192+
func lookup(name: Character) -> Int {
193+
return self.data[name]!
194+
}
195+
196+
func assign(integerVarExp: IntegerVarExp, value: Int) {
197+
self.data[integerVarExp.name] = value
198+
}
199+
}
200+
201+
class IntegerVarExp: IntegerExp {
202+
let name: Character
203+
204+
init(name: Character) {
205+
self.name = name
206+
}
207+
208+
func evaluate(context: IntegerContext) -> Int {
209+
return context.lookup(self.name)
210+
}
211+
212+
func replace(name: Character, integerExp: IntegerExp) -> IntegerExp {
213+
if name == self.name {
214+
return integerExp.copy()
215+
} else {
216+
return IntegerVarExp(name: self.name)
217+
}
218+
}
219+
220+
func copy() -> IntegerExp {
221+
return IntegerVarExp(name: self.name)
222+
}
223+
}
224+
225+
class AddExp: IntegerExp {
226+
private var operand1: IntegerExp
227+
private var operand2: IntegerExp
228+
229+
init(op1: IntegerExp, op2: IntegerExp) {
230+
self.operand1 = op1
231+
self.operand2 = op2
232+
}
233+
234+
func evaluate(context: IntegerContext) -> Int {
235+
return self.operand1.evaluate(context) + self.operand2.evaluate(context)
236+
}
237+
238+
func replace(character: Character, integerExp: IntegerExp) -> IntegerExp {
239+
return AddExp(op1: operand1.replace(character, integerExp: integerExp),
240+
op2: operand2.replace(character, integerExp: integerExp))
241+
}
242+
243+
func copy() -> IntegerExp {
244+
return AddExp(op1: self.operand1, op2: self.operand2)
245+
}
246+
}
247+
/*:
248+
### Usage
249+
*/
250+
var expression: IntegerExp?
251+
var intContext = IntegerContext()
252+
253+
var a = IntegerVarExp(name: "A")
254+
var b = IntegerVarExp(name: "B")
255+
var c = IntegerVarExp(name: "C")
256+
257+
expression = AddExp(op1: a, op2: AddExp(op1: b, op2: c)) // a + (b + c)
258+
259+
intContext.assign(a, value: 2)
260+
intContext.assign(b, value: 1)
261+
intContext.assign(c, value: 3)
262+
263+
var result = expression?.evaluate(intContext)
264+
/*:
175265
🍫 Iterator
176266
-----------
177267

README.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,100 @@ doorModule.open()
182182
doorModule.close()
183183
```
184184

185+
🎶 Interpreter
186+
--------------
187+
188+
The interpreter pattern is used to evaluate sentences in a language.
189+
190+
### Example
191+
192+
```swift
193+
194+
protocol IntegerExp {
195+
func evaluate(context: IntegerContext) -> Int
196+
func replace(character: Character, integerExp: IntegerExp) -> IntegerExp
197+
func copy() -> IntegerExp
198+
}
199+
200+
class IntegerContext {
201+
private var data: [Character:Int] = [:]
202+
203+
func lookup(name: Character) -> Int {
204+
return self.data[name]!
205+
}
206+
207+
func assign(integerVarExp: IntegerVarExp, value: Int) {
208+
self.data[integerVarExp.name] = value
209+
}
210+
}
211+
212+
class IntegerVarExp: IntegerExp {
213+
let name: Character
214+
215+
init(name: Character) {
216+
self.name = name
217+
}
218+
219+
func evaluate(context: IntegerContext) -> Int {
220+
return context.lookup(self.name)
221+
}
222+
223+
func replace(name: Character, integerExp: IntegerExp) -> IntegerExp {
224+
if name == self.name {
225+
return integerExp.copy()
226+
} else {
227+
return IntegerVarExp(name: self.name)
228+
}
229+
}
230+
231+
func copy() -> IntegerExp {
232+
return IntegerVarExp(name: self.name)
233+
}
234+
}
235+
236+
class AddExp: IntegerExp {
237+
private var operand1: IntegerExp
238+
private var operand2: IntegerExp
239+
240+
init(op1: IntegerExp, op2: IntegerExp) {
241+
self.operand1 = op1
242+
self.operand2 = op2
243+
}
244+
245+
func evaluate(context: IntegerContext) -> Int {
246+
return self.operand1.evaluate(context) + self.operand2.evaluate(context)
247+
}
248+
249+
func replace(character: Character, integerExp: IntegerExp) -> IntegerExp {
250+
return AddExp(op1: operand1.replace(character, integerExp: integerExp),
251+
op2: operand2.replace(character, integerExp: integerExp))
252+
}
253+
254+
func copy() -> IntegerExp {
255+
return AddExp(op1: self.operand1, op2: self.operand2)
256+
}
257+
}
258+
```
259+
260+
### Usage
261+
262+
```swift
263+
var expression: IntegerExp?
264+
var intContext = IntegerContext()
265+
266+
var a = IntegerVarExp(name: "A")
267+
var b = IntegerVarExp(name: "B")
268+
var c = IntegerVarExp(name: "C")
269+
270+
expression = AddExp(op1: a, op2: AddExp(op1: b, op2: c)) // a + (b + c)
271+
272+
intContext.assign(a, value: 2)
273+
intContext.assign(b, value: 1)
274+
intContext.assign(c, value: 3)
275+
276+
var result = expression?.evaluate(intContext)
277+
```
278+
185279
🍫 Iterator
186280
-----------
187281

0 commit comments

Comments
 (0)