mel-project / melodeon

1 stars 0 forks source link

Loop issues #8

Open thisbefruit opened 2 years ago

thisbefruit commented 2 years ago
  1. Parentheses parsing seems problematic in loop:

    melorun> let x = 0 :: Nat in (loop 0 do (set! x = x+1 return x))
    Melodeon compilation failed
    /home/thisbefruit/Themelio/.melorun.melo.tmp:17: error: expected set_bang
    let x = 0 :: Nat in (loop 0 do (set! x = x+1 return x))
                                   ^
  2. Loop appears to fail when "loop guard" is 0: In demo.melo: let x = 0 :: Nat in (loop 0 do (set! x = x+1 return x)) REPL:

❯ melorun -i ../demo.melo
result: Early termination from program failure
-- PROGRAM --
0    pushic 0
1    storeimm 33
2    loadimm 33
3    storeimm 34
4    loop 0 4
5 <-     pushic 1
6    loadimm 34
7    add
8    storeimm 34
9    loadimm 34
-- STACK --
-- HEAP --
33: 0
34: 0
value: (none)
melorun> 
thisbefruit commented 2 years ago
  1. Edge case of when there are no set! expressions in the loop body also breaks:
    
    melorun> let x = 0 :: Nat in (loop 1 do return x)
    Internal error, failed to parse mil output
    Syntax(Failure(VerboseError { errors: [("set-let ())) x)))\n", Nom(Tag)), ("(set-let ())) x)))\n", Context("S expression")), ("(set-let ())) x)))\n", Context("if expression")), ("(set-let ())) x)))\n", Nom(Alt)), ("(loop 1 (set-let ())) x)))\n", Context("S expression")), ("(loop 1 (set-let ())) x)))\n", Context("loop expression")), ("(let () (loop 1 (set-let ())) x)))\n", Context("S expression")), ("(let () (loop 1 (set-let ())) x)))\n", Context("let binding")), ("(let () (let () (loop 1 (set-let ())) x)))\n", Context("S expression")), ("(let () (let () (loop 1 (set-let ())) x)))\n", Context("let binding")), ("(let (x 0) (let () (let () (loop 1 (set-let ())) x)))\n", Context("S expression")), ("(let (x 0) (let () (let () (loop 1 (set-let ())) x)))\n", Context("let binding"))] }))
nullchinchilla commented 2 years ago

The zero-length loop problem turned out to be a VM bug, fixed here: https://github.com/themeliolabs/themelio-stf/commit/c8e064ddbf84f21fa9134739e77518981b891f51

nullchinchilla commented 2 years ago

As for parentheses in the loop body, loop 0 do (set! x = x+1 return x) is intended to be incorrect. This is because set! x = x + 1 return x is not a syntactically valid expression, and thus, it cannot be grouped with parentheses.