views:

67

answers:

1

What's wrong with this code?

let vm_run vm =
    let guard = ref true in
    while !guard do
        if vm.cur_pc = -1 && not (Stack.empty vm.call_stack) then vm_pop_ar vm
        else if vm.cur_pc = -1 then guard := false
        else if vm.cur_pc < Array.length vm.cur_code then
            execute vm Array.get vm.cur_code vm.cur_pc;
            vm.cur_pc <- vm.cur_pc + 1
        else vm_pop_ar vm
    done

Error is Error: Syntax error related to the last else keyword.

I reached good confidence with OCaml but an if/else chain still gives me some troubles.. that's not the first time (last time I exploited flow to avoid using the else keyword).

I think it's something small but have no clues, according to syntax specification it should be ok

+4  A: 

The semicolon has lower precedence than if-else, so when you need to have a block of two or more statements separated by semicolons inside an if, you need to enclose them in parentheses or a begin...end block (the two are equivalent):

    else if vm.cur_pc < Array.length vm.cur_code then begin
        execute vm Array.get vm.cur_code vm.cur_pc;
        vm.cur_pc <- vm.cur_pc + 1
    end
    else vm_pop_ar vm
newacct
Thank you, that was a difficult one :)So it tried to parse ending the if body before the intended end finding the `else` unpaired.. silly thing. Now it's clear!
Jack