Shop OBEX P1 Docs P2 Docs Learn Events
fastspin BASIC improvements — Parallax Forums

fastspin BASIC improvements

ersmithersmith Posts: 5,914
edited 2020-01-02 15:35 in Propeller 2
Edit: I found and was able to fix the bug with using multiple function results as parameters, and have updated the .zip file on github with the new binary.

I've added a number of features to the BASIC support in fastspin, including:

- support for multiple assignment and functions which return multiple values
- new assignment operators like += and -=
- shorter aliases for bitwise operators (& for AND, | for OR, ^ for XOR)
- some additional P2 intrinsics like WRPIN and RDPIN
- support for BYVAL and BYREF parameter modifiers to force parameters to be passed by reference or value

The multiple assignment syntax is very much like that of Spin2, and doesn't need any parentheses. So you can write:
dim as integer a,b
function quotrem(x as integer, y as integer) as integer,integer
  return x/y, x mod y
end function

a,b = 7, 128 ' assign multiple values
a,b = b,a ' swap values
a,b = quotrem(a, b) ' assign multiple function results
a,b = quotrem(quoterem(a,b)) ' use multiple function results as parameters

The code isn't very well polished yet and there are probably bugs, which is why this is labelled as a "beta" release. But if you'd like to try it out, the binaries for the new fastspin and spin2cpp are up on github in https://github.com/totalspectrum/spin2cpp/releases.

Comments

  • dMajodMajo Posts: 855
    edited 2020-01-02 17:32
    ^ is often a math operator for "power of" eg 2^3=8. I don't know if it is fine to use it as xor.
  • SWEET! Fresh meat for the new P2 metal! Thanks @ersmith !
  • dMajo wrote: »
    ^ is often a math operator for "power of" eg 2^3=8. I don't know if it is fine to use it as xor.

    Oh, ouch! In my zeal to allow Spin and PASM code to port over easily to BASIC I forgot about that :(. I'll probably revert the &|^ operator change before the final 4.1 release; "&" is also used in some BASICs for concatenation, and while I was willing to give that one up giving up both concatenation and exponentiation is probably a bridge too far.
  • Did we get the BASIC exponent operator (^) in 4.1.0-beta? I’d been hoping that would arrive.
  • Cluso99Cluso99 Posts: 18,069
    Python uses ^ as XOR and ** as exponential
    from https://w3schools.com/python/python_operators.asp
    Python Arithmetic Operators
    Operator  Name            Example
    +         Addition        x + y   
    -         Subtraction     x - y   
    *         Multiplication  x * y   
    /         Division        x / y   
    %         Modulus x % y   
    **        Exponentiation  x ** y  
    //        Floor division  x // y  
    
    Python Assignment Operators
    Operator  Example         Same As
    =         x = 5           x = 5   
    +=        x += 3          x = x + 3       
    -=        x -= 3          x = x - 3       
    *=        x *= 3          x = x * 3       
    /=        x /= 3          x = x / 3       
    %=        x %= 3          x = x % 3       
    //=       x //= 3         x = x // 3      
    **=       x **= 3         x = x ** 3      
    &=        x &= 3          x = x & 3       
    |=        x |= 3          x = x | 3       
    ^=        x ^= 3          x = x ^ 3       
    >>=       x >>= 3         x = x >> 3      
    <<=       x <<= 3         x = x << 3      
    
    Python Comparison Operators
    Operator  Name                      Example
    ==        Equal                     x == y  
    !=        Not equal                 x != y  
    >         Greater than              x > y   
    <         Less than                 x < y   
    >=        Greater than or equal to  x >= y  
    <=        Less than or equal to     x <= y  
    
    Python Logical Operators
    Operator  Description                                              Example
    and       Returns True if both statements are true                 x < 5 and  x < 10       
    or        Returns True if one of the statements is true            x < 5 or x < 4  
    not       Reverse the result, returns False if the result is true  not(x < 5 and x < 10)   
    
    Python Identity Operators
    Operator  Description                                                                       Example
    in        Returns True if a sequence with the specified value is present in the object      x in y  
    not in    Returns True if a sequence with the specified value is not present in the object  x not in y      
    
    Python Bitwise Operators
    Operator  Name                  Description
    &         AND                   Sets each bit to 1 if both bits are 1
    |         OR                    Sets each bit to 1 if one of two bits is 1
    ^         XOR                   Sets each bit to 1 if only one of two bits is 1
    ~         NOT                   Inverts all the bits
    <<        Zero fill left shift  Shift left by pushing zeros in from the right and let the leftmost bits fall off
    >>        Signed right shift    Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off
    
  • cgraceycgracey Posts: 14,133
    For BASIC, you could do POWER(base,exponent) to free ^ for XOR.
  • Cluso99Cluso99 Posts: 18,069
    edited 2020-01-03 05:10
    Chip,
    I wonder how easy it would be to align the spin operators with python? Seems a lot are already the same.

    Then it might be possible to use the spin interpreter for much of the python interpreter.
  • cgraceycgracey Posts: 14,133
    Cluso99 wrote: »
    Chip,
    I wonder how easy it would be to align the spin operators with python? Seems a lot are already the same.

    Then it might be possible to use the spin interpreter for much of the python interpreter.

    Maybe, but it seems that if Micro Python takes over 300K, there must be a lot going on that I'm not aware of.
  • Cluso99Cluso99 Posts: 18,069
    edited 2020-01-03 06:17
    Probably nowhere near as efficient as the spin interpreter.
    Then there is the I/O which includes all the formatting options, arrays and lists, date and time, SD drivers, etc.
    But I’m sure the basic functions that ate common would be much faster and less code than micropython.
  • cgracey wrote: »
    For BASIC, you could do POWER(base,exponent) to free ^ for XOR.

    I guess it boils down to how compatible we want to be with other BASICs. If people writing BASIC code expect ^ to be exponentiation, and it's actually XOR, then their code will compile (^ is a legal operator) but won't work at all correctly :(. Granted, BASIC isn't a very standardized language so some variation between compilers is inevitable, but I don't want to stray too far from what users will expect from a .bas file.
  • cgraceycgracey Posts: 14,133
    ersmith wrote: »
    cgracey wrote: »
    For BASIC, you could do POWER(base,exponent) to free ^ for XOR.

    I guess it boils down to how compatible we want to be with other BASICs. If people writing BASIC code expect ^ to be exponentiation, and it's actually XOR, then their code will compile (^ is a legal operator) but won't work at all correctly :(. Granted, BASIC isn't a very standardized language so some variation between compilers is inevitable, but I don't want to stray too far from what users will expect from a .bas file.

    Understood. Maybe for BASIC, it's best to use AND, OR, and XOR.
  • Cluso99 wrote: »
    Chip,
    I wonder how easy it would be to align the spin operators with python? Seems a lot are already the same.

    Then it might be possible to use the spin interpreter for much of the python interpreter.

    I'm afraid that's not going to be feasible at all. In Spin "a+b" always means 32 bit addition, so it's very straightforward to compile (fastspin makes an "add a, b" instruction, Spin2 can make an "add" byte code, etc.). In Python "a+b" has to translate into something very complicated: there is something like an "add" bytecode, but to implement it the interpreter has to look up the types of a and b, do any conversions necessary, and invoke the __add__ method of the appropriate class. That's because the types of "a" and "b" are not known in Python and can change dynamically at run time; they could be strings, floats, integers, user classes, or anything else, and the same code may execute with different types at different times. So Spin bytecode and Python bytecode may superficially look similar, but they have very different meanings.
  • Cluso99Cluso99 Posts: 18,069
    ersmith wrote: »
    Cluso99 wrote: »
    Chip,
    I wonder how easy it would be to align the spin operators with python? Seems a lot are already the same.

    Then it might be possible to use the spin interpreter for much of the python interpreter.

    I'm afraid that's not going to be feasible at all. In Spin "a+b" always means 32 bit addition, so it's very straightforward to compile (fastspin makes an "add a, b" instruction, Spin2 can make an "add" byte code, etc.). In Python "a+b" has to translate into something very complicated: there is something like an "add" bytecode, but to implement it the interpreter has to look up the types of a and b, do any conversions necessary, and invoke the __add__ method of the appropriate class. That's because the types of "a" and "b" are not known in Python and can change dynamically at run time; they could be strings, floats, integers, user classes, or anything else, and the same code may execute with different types at different times. So Spin bytecode and Python bytecode may superficially look similar, but they have very different meanings.
    Ah yes. Still getting used to this in python as this wasn’t possible in other languages.

    Still, using common operators makes some sense.
  • MicksterMickster Posts: 2,611
    edited 2020-01-03 12:56
    BASIC isn't a very standardized language so some variation between compilers is inevitable, but I don't want to stray too far from what users will expect from a .bas file.

    Heck, I don't know of a better example of a non-issue for me.

    -MS QuickBASIC (MS PDS-7.1 actually)
    -BASIC 4 Android (B4A)
    -BASIC 4 Arduino (B4R) (for ESP32)
    -RFO BASIC
    -PropBASIC
    -PowerBASIC
    -Micromite BASIC
    -Tibbo BASIC

    And now about to try ANNEX 32 (ESP32 BASIC interpreter) on the M5Stack platform.

    They all have their differences but so do the respective platforms. I constantly hop from one to the next because they are still the most productive tools for this part-time programmer which is why I am delighted that BASIC appears to not be going away.

    Self employed....time really is money.
Sign In or Register to comment.