//: 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"