forked from ming1016/study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOCInterpreter_2.swift
More file actions
269 lines (238 loc) · 6.77 KB
/
Copy pathOCInterpreter_2.swift
File metadata and controls
269 lines (238 loc) · 6.77 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
//
// OCInterpreter.swift
// HTN
//
// Created by DaiMing on 2018/6/5.
//
import Foundation
public enum OCConstant {
case integer(Int)
case float(Float)
case boolean(Bool)
case string(String)
}
public enum OCOperation {
case plus
case minus
case mult
case intDiv
}
public enum OCDirection {
case left
case right
}
public enum OCToken {
case constant(OCConstant)
case operation(OCOperation)
case paren(OCDirection)
case eof
case whiteSpaceAndNewLine
}
extension OCConstant: Equatable {
public static func == (lhs: OCConstant, rhs: OCConstant) -> Bool {
switch (lhs, rhs) {
case let (.integer(left), .integer(right)):
return left == right
case let (.float(left), .float(right)):
return left == right
case let (.boolean(left), .boolean(right)):
return left == right
case let (.string(left), .string(right)):
return left == right
default:
return false
}
}
}
extension OCOperation: Equatable {
public static func == (lhs: OCOperation, rhs: OCOperation) -> Bool {
switch (lhs, rhs) {
case (.plus, .plus):
return true
case (.minus, .minus):
return true
case (.mult, .mult):
return true
case (.intDiv, .intDiv):
return true
default:
return false
}
}
}
extension OCDirection: Equatable {
public static func == (lhs: OCDirection, rhs: OCDirection) -> Bool {
switch (lhs, rhs) {
case (.left, .left):
return true
case (.right, .right):
return true
default:
return false
}
}
}
extension OCToken: Equatable {
public static func == (lhs: OCToken, rhs: OCToken) -> Bool {
switch (lhs, rhs) {
case let (.constant(left), .constant(right)):
return left == right
case let (.operation(left), .operation(right)):
return left == right
case (.eof, .eof):
return true
case (.whiteSpaceAndNewLine, .whiteSpaceAndNewLine):
return true
case let (.paren(left), .paren(right)):
return left == right
default:
return false
}
}
}
public class OCLexer {
private let text: String
private var currentIndex: Int
private var currentCharacter: Character?
public init(_ input: String) {
if input.count == 0 {
fatalError("Error! input can't be empty")
}
self.text = input
currentIndex = 0
currentCharacter = text[text.startIndex]
}
// 流程函数
func nextTk() -> OCToken {
if currentIndex > self.text.count - 1 {
return .eof
}
if CharacterSet.whitespacesAndNewlines.contains((currentCharacter?.unicodeScalars.first!)!) {
skipWhiteSpaceAndNewLines()
return .whiteSpaceAndNewLine
}
if CharacterSet.decimalDigits.contains((currentCharacter?.unicodeScalars.first!)!) {
return number()
}
if currentCharacter == "+" {
advance()
return .operation(.plus)
}
if currentCharacter == "-" {
advance()
return .operation(.minus)
}
if currentCharacter == "*" {
advance()
return .operation(.mult)
}
if currentCharacter == "/" {
advance()
return .operation(.intDiv)
}
if currentCharacter == "(" {
advance()
return .paren(.left)
}
if currentCharacter == ")" {
advance()
return .paren(.right)
}
advance()
return .eof
}
// 数字处理
private func number() -> OCToken {
var numStr = ""
while let character = currentCharacter, CharacterSet.decimalDigits.contains((character.unicodeScalars.first!)) {
numStr += String(character)
advance()
}
return .constant(.integer(Int(numStr)!))
}
// 辅助函数
private func advance() {
currentIndex += 1
guard currentIndex < text.count else {
currentCharacter = nil
return
}
currentCharacter = text[text.index(text.startIndex, offsetBy: currentIndex)]
}
// 往前探一个字符,不改变当前字符
private func peek() -> Character? {
let peekIndex = currentIndex + 1
guard peekIndex < text.count else {
return nil
}
return text[text.index(text.startIndex, offsetBy: peekIndex)]
}
private func skipWhiteSpaceAndNewLines() {
while let character = currentCharacter, CharacterSet.whitespacesAndNewlines.contains((character.unicodeScalars.first!)) {
advance()
}
}
}
public class OCInterpreter {
private var lexer: OCLexer
private var currentTk: OCToken
public init(_ input: String) {
lexer = OCLexer(input)
currentTk = lexer.nextTk()
}
public func expr() -> Int {
var result = term()
while [.operation(.plus), .operation(.minus)].contains(currentTk) {
let tk = currentTk
eat(currentTk)
if tk == .operation(.plus) {
result = result + self.term()
} else if tk == .operation(.minus) {
result = result - self.term()
}
}
return result
}
// 语法解析中对数字的处理
private func term() -> Int {
var result = factor()
while [.operation(.mult), .operation(.intDiv)].contains(currentTk) {
let tk = currentTk
eat(currentTk)
if tk == .operation(.mult) {
result = result * factor()
} else if tk == .operation(.intDiv) {
result = result / factor()
}
}
return result
}
private func factor() -> Int {
let tk = currentTk
switch tk {
case let .constant(.integer(result)):
eat(.constant(.integer(result)))
return result
case .paren(.left):
eat(.paren(.left))
let result = expr()
eat(.paren(.right))
return result
default:
return 0
}
}
private func eat(_ token: OCToken) {
if currentTk == token {
currentTk = lexer.nextTk()
if currentTk == OCToken.whiteSpaceAndNewLine {
currentTk = lexer.nextTk()
}
} else {
error()
}
}
func error() {
fatalError("Error!")
}
}