jweinst1
7/28/2016 - 11:03 PM

linker list to implement an operator language

linker list to implement an operator language

//playground to test evaluatable string

class TokenLinker {
    
    //static method that links together a list of string tokens
    static func LinkTokens(tokens:[String]) -> TokenLinker {
        let root = TokenLinker(token:tokens[0])
        var pointer = root
        for string in tokens.dropFirst() {
            pointer.setForward(string)
            pointer.forward!.setBackLink(pointer)
            pointer = pointer.forward!
        }
        return root
    }
    
    var token:String
    var back:TokenLinker?
    var forward:TokenLinker?
    
    init(token:String) {
        self.token = token
    }
    
    var hasForward:Bool {
        return self.forward != nil
    }
    
    var hasBack:Bool {
        return self.back != nil
    }
    
    var backToken:String? {
        return self.back?.token
    }
    
    var forwardToken:String? {
        return self.forward?.token
    }
    //gets the amount of nodes ahead of the current node
    var forwardLength:Int {
        var len = 0
        var pointer = self
        while pointer.hasForward {
            len += 1
            pointer = pointer.forward!
        }
        return len
    }
    
    func setForward(token:String) {
        self.forward = TokenLinker(token: token)
    }
    
    func setForwardLink(link:TokenLinker){
        self.forward = link
    }
    
    func setBack(token:String) {
        self.back = TokenLinker(token: token)
    }
    
    func setBackLink(link:TokenLinker){
        self.back = link
    }
    //if a forward link exists, it will insert the new token, then insert the old forward behind it
    func insertForward(token:String) {
        if self.hasForward {
            let holder = self.forward
            self.forward = TokenLinker(token:token)
            self.forward?.setForwardLink(holder!)
        }
        else {
            self.setForward(token)
        }
    }
    
    func insertBack(token:String) {
        if self.hasBack {
            let holder = self.back
            self.back = TokenLinker(token:token)
            self.back?.setBackLink(holder!)
        }
        else {
            self.setBack(token)
        }
    }
    //removes the forward node
    func removeForward() {
        if self.hasForward {
            if self.forward!.hasForward {
                self.forward = self.forward!.forward
            }
            else {
                self.forward = nil
            }
        }
    }
    //removes the back node
    func removeBack() {
        if self.hasBack {
            if self.back!.hasBack {
                self.back = self.back!.back
            }
            else {
                self.back = nil
            }
        }
    }
}

//storing indexes on the token linker class
struct TokenIndexer {
    var map:[String:[TokenLinker]]
    
    init() {
        self.map = [String:[TokenLinker]]()
    }
    //adds a new index
    mutating func addLink(link:TokenLinker) {
        if self.map[link.token] != nil {
            self.map[link.token]!.append(link)
        }
        else {
            self.map[link.token] = [TokenLinker]()
            self.map[link.token]!.append(link)
        }
    }
    //subscripting inner dictionary
    subscript(index:String) -> [TokenLinker]? {
        get {
            return self.map[index]
        }
    }
}