Home > Net >  I'm trying to call a function in a Swift playground, but it is asking for me to declare the fun
I'm trying to call a function in a Swift playground, but it is asking for me to declare the fun

Time:11-05

I have already declared a function in a class in the Swift playground, but when I try to call the function to test it with input, it's acting like I am trying to define the function again

    func evaluate(_ input: String) {
        print("Evaluating: \(input)")
        let lexer = Lexer(input: input)
        do {
            let tokens = try lexer.lex()
            print("Lexer output: \(tokens)")
        } catch {
            print("An error occurred: \(error)")
        }
    }
    evaluate("10   3   5")
    evaluate("1   2   abcdefg")

It's specifically giving the error: "Expected '{' in body of function declaration" How do I just get it to run the function??

This is the entire code:

import Cocoa

enum Token {
    case number(Int)
    case plus
}

class Lexer {
    enum Error: Swift.Error {
        case invalidCharacter(Character)
    }
    let input: String
    var position: String.Index
    init(input: String) {
        self.input = input
        self.position = input.startIndex
    }
    
    func peek() -> Character? {
        guard position < input.endIndex else {
            return nil
        }
        return input[position]
    }
    
    func advance() {
        assert(position < input.endIndex, "Cannot advance past endIndex!")
        position = input.index(after: position)
    }

    func getNumber() -> Int {
        var value = 0
        while let nextCharacter = peek() {
            switch nextCharacter {
            case "0" ... "9":
                let digitValue = Int(String(nextCharacter))!
                value = 10*value   digitValue
                advance()
            default:
                return value
            }
        }
    }

    func lex() throws -> [Token] {
        var tokens = [Token]()
    
        while let nextCharacter = peek() {
            switch nextCharacter {
            case "0" ... "9":
                let value = getNumber()
                tokens.append(.number(value))
            case " ":
                tokens.append(.plus)
                advance()
            case " ":
                advance()
            default:
                throw Lexer.Error.invalidCharacter(nextCharacter)
            }
        }
        return tokens
    }

    func evaluate(_ input: String) {
        print("Evaluating: \(input)")
        let lexer = Lexer(input: input)
        do {
            let tokens = try lexer.lex()
            print("Lexer output: \(tokens)")
        } catch {
            print("An error occurred: \(error)")
        }
    }
    evaluate("10   3   5")
    evaluate("1   2   abcdefg")
}

CodePudding user response:

You can't run imperative code like evaluate(...) at the top level of a class like you're trying to do. You should move this section outside the body of the Lexer class (eg outside it's closing }):

func evaluate(_ input: String) {
    print("Evaluating: \(input)")
    let lexer = Lexer(input: input)
    do {
        let tokens = try lexer.lex()
        print("Lexer output: \(tokens)")
    } catch {
        print("An error occurred: \(error)")
    }
}

evaluate("10   3   5")
evaluate("1   2   abcdefg")

Note that in order to run your code, you'll also have to add a return to the end of getNumber() -- probably return value

  • Related