Search code examples
ozmozart

Expected 'end' in case statement


The following code compiles and runs as expected:

fun {Tokenize Lexemes}
        case Lexemes of
        Head|Tail then
            case Head of
            "+" then
                operator(type:plus)|{Tokenize Tail}
            else
                if {String.isFloat Head} then
                    number(Head)|{Tokenize Tail}
                else
                    nil
                end
            end
        else
            nil
        end
    end

However, if I add another case clause, like the code below, I get an error when compiling about a missing 'end' statement.

fun {Tokenize Lexemes}
        case Lexemes of
        Head|Tail then
            case Head of
            "+" then
                operator(type:plus)|{Tokenize Tail}
            "*" then
                operator(type:multiply)|{Tokenize Tail}
            else
                if {String.isFloat Head} then
                    number(Head)|{Tokenize Tail}
                else
                    nil
                end
            end
        else
            nil
        end
    end

Error:

** expected 'end' 
** inside case phrase (at the line of the "*")

What gives?


Solution

  • If you have multiple branches within a case statement, you have to separate them with []. For example:

    case Head of "+" then
        operator(type:plus)|{Tokenize Tail}
    [] "*" then
        operator(type:multiply)|{Tokenize Tail}
    end