Discord
Login
Community
DARK THEME

definition of a local variable problem

When I define a local variable multiple times (see functions getNumber) an error occurs which causes the program to malfunction.

Additionally, if I remove the comment characters in the getNumber function, the program will loop in and call the init function all the time.

The endless "Init start." Printout will appear on the console. and "Init end." will never be displayed. .

I think this is due to the unclear definition of local variables. If the program has a branch, it should be allowed to define a local variable multiple times - or it should be written in the documentation that the variable will be local (on each line of code below) even if the program execution does not pass through the place where this variable is defined as local.

Sample code:

getNumber = function( num_args )
  if num_args == 0 then 
    local num = 12345
  elsif num_args == 1 then
    local num = 98765 
  elsif num_args == 2 then
  //  local num = 11111111
  end
  return num
end

getNumber2 = function( num_args )
  //local num2 = 'string'
  if num_args == 0 then 
    local num2 = 123
  elsif num_args == 1 then
    num2 = 987 
  elsif num_args == 2 then 
     num2 = 567
  end
  return num2
end

getNumber3 = function( num_args )
  //local num2 = 'string'
  if num_args == 0 then 
    num3 = 123
  elsif num_args == 1 then
    local num3 = 987 
  elsif num_args == 2 then 
     num3 = 567
  end
  return num3
end

init = function()
  print( 'Init start.')
  if getNumber( 1 ) == 98765 then 
    print( getNumber( 1 ) + ' == 98765  PASS')
  else 
    print( getNumber( 1 ) + ' diff 98765 ERROR')
  end
  if getNumber( 0 ) == 12345 then 
    print( getNumber( 0 ) + ' == 12345  PASS')
  else 
    print( getNumber( 0 ) + ' diff 12345 ERROR')
  end
  if getNumber( 2 ) == 11111111 then 
    print( getNumber( 2 ) + ' == 11111111  PASS')
  else 
    print( getNumber( 2 ) + ' diff 11111111 ERROR')
  end
  print( 'getNumber2(2)=='+getNumber2(2) + '  num2 == '+num2)
  print( 'getNumber2(1)=='+getNumber2(1) + '  num2 == '+num2)
  print( 'getNumber2(0)=='+getNumber2(0) + '  num2 == '+num2)

  print( 'getNumber3(2)=='+getNumber3(2) + '  num3 == '+num3)
  print( 'getNumber3(1)=='+getNumber3(1) + '  num3 == '+num3)
  print( 'getNumber3(0)=='+getNumber3(0) + '  num3 == '+num3)
  print( ' Init end.')
end

update = function()
end

draw = function()
  system.pause()
end

Replicated with simpler code

getNumber = function( num_args )
  local num = 42
  if num_args == 1 then
    local num = 98765
  end
  if num_args == 2 then
    local num = 234
  end
  return num
end

init = function()
  print( "Init start.")
  
  if getNumber( 1 ) == 98765 then 
    print( getNumber( 1 ) + " == 98765  PASS")
  else 
    print( getNumber( 1 ) + " diff 98765 ERROR")
  end
  print( " Init end.")
end

update = function()
end

draw = function()
end

Error:

TypeError: Cannot read properties of undefined (reading '+')
    at Processor.Processor.add (processor.js:133:12)
    at Processor.Processor.run (processor.js:748:39)
    at Runner.Runner.process (runner.js:173:22)
    at Runner.Runner.tick (runner.js:203:12)
    at Runner.Runner.call (runner.js:143:14)
    at MicroVM.MicroVM.call (play.js:367:25)
    at Runtime.Runtime.drawCall (play.js:1090:15)
    at Runtime.Runtime.timer (play.js:1049:10)
    at play.js:825:22

Now... I'm not sure if this is really a "bug". I don't think code like this should work in the first place, but it should result in error message, not infinite loop :D Either way, You should add bug report on GitHub about this.

There is definitely a bug in the microScript engine there, the engine should not fail like this. I think it might be related to this bug too: https://github.com/pmgl/microstudio/issues/151

However, please note that a local variable's visibility is limited to the enclosing block ; the space between an if and an elsif or between an elsif and a end is an enclosing block. Thus:

getNumber = function( num_args )
  local num = 42            // this creates a local variable num
  if num_args == 1 then 
    local num = 98765       // this creates another local variable num within a narrower block...
                            // ... which is closed just here with the elsif
  elsif num_args == 2 then

    local num = 234         // this creates yet another local variable num...
  end                       // <- ... which visibility ends right here

  return num       // this should return 42 in all cases, but now microScript also seems to have a bug with that.
end  

This is the correct version, which will work as expected:

getNumber = function( num_args )
  local num = 42    
  if num_args == 1 then 
    num = 98765       
  elsif num_args == 2 then
    num = 234     
  end            

  return num
end

See: https://github.com/pmgl/microstudio/wiki/en-microScript-cheatsheet#local

Thanks for the report and test case ; I will fix that when I am back to work (in 10 days :blush:)

Actually a different bug than #151

https://github.com/pmgl/microstudio/issues/156

Bug is now fixed, see https://github.com/pmgl/microstudio/issues/156

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community