Skip to content

Commit f46c075

Browse files
committed
- updated to Swift 5 (untested)
1 parent 88e7ec7 commit f46c075

17 files changed

+76
-59
lines changed

Common/Analyzer/CyclicReferenceIdentifier.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ fileprivate struct JohnsonCircuitFindingAlgorithm {
281281
items.append(item)
282282
}
283283
mutating func remove(item: Int) {
284-
if let i = items.index(where: {$0 == item}) {
284+
if let i = items.firstIndex(where: {$0 == item}) {
285285
items.remove(at: i)
286286
}
287287
}

Common/Analyzer/RulesetAnalyser.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,4 @@ extension Tracery {
3737
}
3838
analyzers.forEach { $0.end() }
3939
}
40-
4140
}

Common/Kalk/Kalk.Interpreter.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,11 @@ public class KalkInterpreter {
7474
(Double(arc4random()) / Double(UINT32_MAX)) * (value2.getDouble()!-value.getDouble()!))
7575
}
7676
}
77-
77+
7878
public static func interpret(_ string: String) -> String {
7979
let lexer = KalkLexer(input: string)
8080
let tokens = lexer.tokenize()
8181
let parser = KalkParser(tokens: tokens)
82-
registerBuiltins()
8382
var expandedString: String = ""
8483
do {
8584
let nodes = try parser.parse()

Common/Kalk/Kalk.Regex.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Foundation
1010

1111
var expressions = [String: NSRegularExpression]()
1212
public extension String {
13-
public func match(regex: String) -> String? {
13+
func match(regex: String) -> String? {
1414
let expression: NSRegularExpression
1515
if let exists = expressions[regex] {
1616
expression = exists

Common/Lexer.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ enum Token : CustomStringConvertible {
4545
static let RIGHT_ROUND_BRACKET = Token.op(")")
4646
static let LEFT_CURLY_BRACKET = Token.op("{")
4747
static let RIGHT_CURLY_BRACKET = Token.op("}")
48-
48+
49+
static let ASSIGN = Token.op("=")
50+
4951
static let EQUAL_TO = Token.op("==")
5052
static let NOT_EQUAL_TO = Token.op("!=")
5153

@@ -82,7 +84,7 @@ func ==(lhs: Token, rhs: Token) -> Bool {
8284
extension Character {
8385
var isReserved: Bool {
8486
switch self {
85-
case "[","]",":","#",",","(",")",".","{","}": return true
87+
case "[","]",":","#",",","(",")",".","{","}","=": return true
8688
default: return false
8789
}
8890
}

Common/Modifiers.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ public class StandardModifiers {
168168

169169
// t.add(modifier: "k", transform: KalkInterpreter.interpret)
170170

171+
KalkInterpreter.registerBuiltins()
171172
t.add(method: "k") { input, args in
172173
return input + KalkInterpreter.interpret(args[0])
173174
}

Common/Parser.Gen2.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,17 @@ extension Parser {
309309
guard let c = currentToken, c == token else { return }
310310
advance()
311311
}
312+
313+
func parseFormula() throws -> ParserNode {
314+
315+
// calculations
316+
// [=3+2]
317+
// [=sin(3.2)]
318+
let formula = try parseText("expected formula")
319+
// nodes.append(ParserNode.kalk(name))
320+
// try parseAny([.HASH, .RIGHT_CURLY_BRACKET], "closing # or } not found for rule '\(name)'")
321+
return ParserNode.kalk(formula)
322+
}
312323

313324
// a fragment is the most basic block in tracery
314325
// a fragement can contain multiple parser nodes though
@@ -323,6 +334,8 @@ extension Parser {
323334
case Token.LEFT_SQUARE_BRACKET:
324335
guard let next = nextToken else { return nil }
325336
switch next {
337+
case Token.ASSIGN:
338+
nodes.append(try parseFormula())
326339
case Token.KEYWORD_IF:
327340
nodes.append(contentsOf: try parseIfBlock())
328341
case Token.KEYWORD_WHILE:

Common/Parser.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ enum ParserNode : CustomStringConvertible {
6464

6565
case text(String)
6666
case rule(name:String, mods:[Modifier])
67+
case kalk(String)
6768
case any(values:[ValueCandidate], selector:RuleCandidateSelector, mods:[Modifier])
6869
case tag(name:String, values:[ValueCandidate])
6970
case weight(value: Int)
@@ -98,6 +99,9 @@ enum ParserNode : CustomStringConvertible {
9899
}
99100
return "RULE_\(name)"
100101

102+
case let .kalk(formula):
103+
return "KALK_\(formula)"
104+
101105
case let .createRule(name, values):
102106
if values.count == 1 { return "NEW_RULE(\(name)=\(values[0]))" }
103107
return "+RULE_\(name)=\(values)"

Common/Tracery.Eval.swift

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Foundation
1010

1111

1212
extension Tracery {
13-
13+
1414
// transforms input text to expanded text
1515
// based on the rule set, run time tags, and modifiers
1616
func eval(_ text: String) throws -> String {
@@ -29,7 +29,9 @@ extension Tracery {
2929
}
3030

3131
func eval(_ nodes: [ParserNode]) throws -> String {
32-
32+
33+
var choices = 1
34+
3335
// the execution stack
3436
// var stack = [ExecutionContext]()
3537
contextStack.reset()
@@ -98,15 +100,7 @@ extension Tracery {
98100
}
99101
}
100102

101-
// do {
102-
// let i = depth
103-
// let context = stack[i]
104-
// trace("\(i) \(context)")
105-
// }
106-
107-
108-
// have we have finished processing
109-
// the stack?
103+
// have we finished processing the stack?
110104
if contextStack.executionComplete {
111105
break
112106
}
@@ -130,7 +124,10 @@ extension Tracery {
130124
trace("📘 text '\(text)'")
131125
// commit result to context
132126
contextStack.contexts[top].result.append(text)
133-
127+
128+
case let .kalk(formula):
129+
contextStack.contexts[top].result = KalkInterpreter.interpret(formula)
130+
134131
case let .evaluateArg(nodes):
135132
// special node that evaluates
136133
// child nodes and adds result
@@ -145,7 +142,8 @@ extension Tracery {
145142
try applyMods(nodes: choice.nodes, mods: mods)
146143
// try pushContext(choice.nodes, affectsEvaluationLevel: false)
147144
trace("🎲 picked \(choice.nodes)")
148-
145+
choices *= values.count
146+
149147
case let .tag(name, values):
150148

151149
// push a context with
@@ -239,9 +237,7 @@ extension Tracery {
239237
try pushContext(elseBlock, affectsEvaluationLevel: false)
240238
}
241239
}
242-
243-
244-
240+
245241
case let .runMod(name):
246242
guard let mod = mods[name] else { break }
247243
let context = contextStack.contexts[top]
@@ -281,12 +277,10 @@ extension Tracery {
281277
return
282278
}
283279

284-
// guard let candidate = mapping.select() else {
285-
// state = .noExpansion(reason: "no candidates found")
286-
// return
287-
// }
288280
state = .apply(mapping.candidates[index].value.nodes)
289281
trace("📙 eval \(runTime ? "runtime" : "") \(node)")
282+
283+
choices *= mapping.candidates.count
290284
}
291285

292286
if name.isEmpty {
@@ -297,6 +291,7 @@ extension Tracery {
297291
let value = mapping.candidates[i]
298292
trace("📗 get tag[\(name)] --> \(value)")
299293
state = .apply([.text(value)])
294+
choices *= mapping.candidates.count
300295
}
301296
else if let object = objects[name] {
302297
let value = "\(object)"
@@ -319,6 +314,8 @@ extension Tracery {
319314
}
320315
}
321316
}
317+
318+
trace("🧾 had \(choices) options to chose from")
322319

323320
// finally pop the last
324321
// context and

Common/Tracery.Logging.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func error(_ message: @autoclosure () -> String) {
5858
}
5959

6060
func trace(_ message: @autoclosure () -> String) {
61-
Tracery.log(level: .verbose, message: message)
61+
Tracery.log(level: .verbose, message: message())
6262
}
6363

6464

0 commit comments

Comments
 (0)