berry-lang/berry

ternary assign to local variable bug

Closed this issue · 3 comments

if true
  a=1
  a=true ? a+1 : a+2
  print(a)
end

this prints 1

I will look at it next week

I confirm the bug. Compiling the following code:

def f()
  if true
    var a = 1
    a = true ? a+1 : a+2
    print(a)
  end
end

then doing import solidify solidify.dump(f) gives the following:

    ( &(const bvalue[ 2]) {     /* constants */
    /* K0   */  be_const_int(1),
    /* K1   */  be_const_int(2),
    }),
    &be_const_str_f,
    &be_const_str_solidified,
    ( &(const binstruction[12]) {  /* code */
      0x50000200,  //  0000  LDBOOL	R0	1	0
      0x78020008,  //  0001  JMPF	R0	#000B
      0x58000000,  //  0002  LDCONST	R0	K0               <- var a is in R0
      0x50040200,  //  0003  LDBOOL	R1	1	0
      0x78060001,  //  0004  JMPF	R1	#0007
      0x00040100,  //  0005  ADD	R1	R0	K0       <- ERROR: the result of a+1 is stored in R1
      0x70020000,  //  0006  JMP		#0008
      0x00000101,  //  0007  ADD	R0	R0	K1       <- CORRECT: the result of a+2 is stored in R0
      0x60040001,  //  0008  GETGBL	R1	G1
      0x5C080000,  //  0009  MOVE	R2	R0
      0x7C040200,  //  000A  CALL	R1	1
      0x80000000,  //  000B  RET	0
    })

It's fixed in #404

Thanks for reporting