1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
* 表达式接口 */ protocol IntegerExp { 取值 - parameter context: 上下文 - returns: 值 */ func evaluate(context: IntegerContext) -> Int 替换 - parameter character: 字符 - parameter integerExp: 表达式 - returns: <#return value description#> */ func replace(character: Character, integerExp: IntegerExp) -> IntegerExp 复制 - returns: 表达式 */ func copy() -> IntegerExp }
class IntegerContext { private var data: [Character:Int] = [:] 查找 - parameter name: 字符 - returns: 字符代表的值 */ func lookup(name: Character) -> Int { return self.data[name]! } 分配值 - parameter integerVarExp: 变量表达式 - parameter value: 值 */ func assign(integerVarExp: IntegerVarExp, value: Int) { self.data[integerVarExp.name] = value } }
class IntegerVarExp: IntegerExp { let name: Character init(name: Character) { self.name = name } func evaluate(context: IntegerContext) -> Int { return context.lookup(self.name) } func replace(name: Character, integerExp: IntegerExp) -> IntegerExp { if name == self.name { return integerExp.copy() } else { return IntegerVarExp(name: self.name) } } func copy() -> IntegerExp { return IntegerVarExp(name: self.name) } }
class AddExp: IntegerExp { private var operand1: IntegerExp private var operand2: IntegerExp init(op1: IntegerExp, op2: IntegerExp) { self.operand1 = op1 self.operand2 = op2 } func evaluate(context: IntegerContext) -> Int { return self.operand1.evaluate(context) + self.operand2.evaluate(context) } func replace(character: Character, integerExp: IntegerExp) -> IntegerExp { return AddExp(op1: operand1.replace(character, integerExp: integerExp), op2: operand2.replace(character, integerExp: integerExp)) } func copy() -> IntegerExp { return AddExp(op1: self.operand1, op2: self.operand2) } }
class SubtractExp: IntegerExp { private var operand1: IntegerExp private var operand2: IntegerExp init(op1: IntegerExp, op2: IntegerExp) { self.operand1 = op1 self.operand2 = op2 } func evaluate(context: IntegerContext) -> Int { return self.operand1.evaluate(context) - self.operand2.evaluate(context) } func replace(character: Character, integerExp: IntegerExp) -> IntegerExp { return AddExp(op1: operand1.replace(character, integerExp: integerExp), op2: operand2.replace(character, integerExp: integerExp)) } func copy() -> IntegerExp { return AddExp(op1: self.operand1, op2: self.operand2) } }
var a = IntegerVarExp(name: "A") var b = IntegerVarExp(name: "B") var c = IntegerVarExp(name: "C")
var expression: IntegerExp = SubtractExp(op1: a, op2: AddExp(op1: b, op2: c))
var intContext = IntegerContext() intContext.assign(a, value: 2) intContext.assign(b, value: 1) intContext.assign(c, value: 3)
var result = expression.evaluate(intContext)
|