swift swmat中遗忘vm的自动模型
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift swmat中遗忘vm的自动模型相关的知识,希望对你有一定的参考价值。
//: Playground - noun: a place where people can play
import Cocoa
//playground to simulate the VM for the oblivion
//static, unchangable symbol struct
struct Symbol {
let value:String
init(value:String){
self.value = value
}
}
//small, single state automata that can accept a symbol and match it against it's stored symbol
struct single {
var symbol:Symbol
var accepting:Bool
init(symbol:Symbol){
self.symbol = symbol
self.accepting = true
}
mutating func input(symbol:Symbol) -> Bool {
if self.accepting {
if symbol.value == self.symbol.value {
self.accepting = false
return true
}
else {
return false
}
}
else {
return false
}
}
}
//sample struct to store cached methods on the vm
struct vmfuncs {
let methods = [
"newsym":{(vm:OblivVM, name:String, value:String) -> Void in vm.symmap[name] = Symbol(value:value)}
]
}
//main vm class that process commands to automatan
class OblivVM {
var singmap:[String:single]
var symmap:[String:Symbol]
let machfuncs:vmfuncs
init(){
self.singmap = [String:single]()
self.symmap = [String:Symbol]()
self.machfuncs = vmfuncs()
}
//splits code and calls it via closure dictionary
func instruction(input:String) -> Void {
let pieces = input.componentsSeparatedByString(" ")
self.machfuncs.methods[pieces[0]]!(self, pieces[1], pieces[2])
}
}
var tester = OblivVM()
tester.instruction("newsym phil op")
tester.symmap["phil"]!.value
//"op"
以上是关于swift swmat中遗忘vm的自动模型的主要内容,如果未能解决你的问题,请参考以下文章