stores swift closures in a dictionary and caches them with a subscript on a scirpt
//cached functions that compile to javascript
struct Stringpiler {
let funcs = [
"=":{(args:[String]) -> String in "var \(args[0]) = \(args[1])"},
"+=":{(args:[String]) -> String in "\(args[0]) += \(args[1])"},
"-=":{(args:[String]) -> String in "\(args[0]) -= \(args[1])"}
]
subscript (args:String) ->String {
get {
var arguments = args.componentsSeparatedByString(" ")
let method = funcs[arguments[0]]!
arguments.removeFirst()
return method(arguments)
}
}
}
let test = Stringpiler()
let result = test["= x 6"] //var x = 6
print(test["+= foo 4"])
//"foo += 4\n"