Automatic variable type conversion

Issue #14 closed
Bernat Romagosa created an issue

If we assign a variable to a string containing a number it should be internally cast into a number. I don't know whether that's a task for the GUI or the VM though.

Comments (5)

  1. Bernat Romagosa reporter

    Wait, this is weird, this is what I'm noticing:

    Create a variable. Assign a string containing a number to it. The board hangs completely.

    On the other hand, if I assign a regular string or an actual number (using any arithmetic reporter), it all works as expected.

    I need to dig a bit more into this to see whether it's a problem with the Snap! GUI or the VM.

  2. John Maloney repo owner

    Hi, Bernat.

    I also ran into this problem. This is an issue with the Snap version of the uBlocks compiler: it needs to recognize the difference between string and numeric constants (what many languages call "literal values") and generate the appropriate code.

    My temporary fix was to make the input slot of the variable "set" block be a number (in the Snap block spec for "set"). That allowed me to set a variable to a number and increment it. (You shouldn't even be able to enter a string into the "set" block.) Do numbers and the increment block work for you? If not, perhaps I didn't push my change or maybe it got reverted somehow.

    However, my temporary fix allows only numbers to be stored in variables. Of course, we want to be able to set variables to strings, too. What the GP compiler does is to analyze the string in the input slot. If it can be parsed as a number, it is converted to a number at compile time.

    The above rule does what the user expects 99% of the time. But what if you actually want to set a variable to a string such as "0123456789"? GP allows the user to change the type of the input slot from "string or number" to "string only". Alternatively, it has a "quote" reporter block that forces the input to be a string. I think the latter is probably the preferred solution for uBlocks, since it will work in Snap, Scratch, and other blocks languages that don't have GP's user-changable slot types.

    -- John
    
  3. Bernat Romagosa reporter

    Oh I see, so the change was yours. I changed the set var input slot back to a text one because I wanted to store a string into a var. That's when I noticed it wasn't parsing strings into numbers.

    If that's a job for the IDE, I can fix it in the Snap-µBlocks repo. Or maybe @jmoenig can do it, since he's more familiar with the compiler than I am.

  4. John Maloney repo owner

    Hi, Bernat.

    Interesting that you wanted to store a string in a variable. There's currently not much you can do with a string right now except print it. Were you just testing?

    To fix this, the compiler needs to examine string constants in the blocks being compiled. If the string can be parsed as a number (integer, right now), then it should be converted to a number and compiled into the appropriate code. Otherwise, it should be compiled as a string.

    You may be able to use the Javascript string-to-integer function to detect when a string is a legitimate integer, or you may need to write your own format checker. I can send you the one I wrote for GP if you'd like. My version also detects strings that represent floating point numbers. We don't need that yet, but we will eventually.

    -- John
    
  5. Log in to comment