Shop OBEX P1 Docs P2 Docs Learn Events
PBASIC Syntax: How Come Department — Parallax Forums

PBASIC Syntax: How Come Department

Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
edited 2007-09-02 18:25 in BASIC Stamp
I know the following is not allowed:

Comparator VAR Bit
Value      VAR Byte

Comparator = Value > 128




But why not? The expression on the righthand side makes as much sense here as it does in an IF or ELSEIF. Plus, it's just downright useful sometimes, being much less awkward than:

Comparator VAR Bit
Value      VAR Byte

IF (Value > 128) THEN
  Comparator = 1
ELSE
  Comparator = 0
ENDIF




'Just wondering...
-Phil

Comments

  • FranklinFranklin Posts: 4,747
    edited 2007-09-02 02:03
    Have you tried Comapator = (Value > 128)

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2007-09-02 02:15
    Yup. Conditional operators just aren't allowed in assignments.

    -Phil
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2007-09-02 03:21
    Hi Phil, just for fun I came up with this not as elegant as yor first suggestion but certainly an abreviation of If..Then..Else..EndIf.

    comparator = value/(128+1)· MAX 1

    Jeff T.
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2007-09-02 04:18
    You're spoiled by the Prop syntax, Phil!

    Just
    comparator = value/129
    value is a byte so the MAX 1 is not necessary.

    Or,
    threshold = 128
    comparator = value MIN threshold - threshold MAX 1 ' zero for values up to 128

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2007-09-02 04:22
    Thats true Tracy, my format was to allow 128 to be replaced by a variable and to take into account the variable being 0.

    Jeff T
  • cyplesmacyplesma Posts: 76
    edited 2007-09-02 04:42
    the

    Comparator = Value > 128

    is more of a C syntax

    the

    IF (Value > 128) THEN
    Comparator = 1
    ELSE
    Comparator = 0
    ENDIF

    is more of a BASIC syntax


    I know there's a few companies out there try to combine the two in the last 5 - 10 years, but they have been different for 30+ years for many reasons. I've done quite a bit in both these and assembler and straight machine, after you go back a forth a couple of times it becomes second nature just like learning any language the more you use it the better you get. Some people do take a little more time to get better at different languages.

    no biggie.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2007-09-02 05:37
    Assignment statement-resident conditional operators have been a part of BASIC at least since Microsoft QuickBASIC (1987) and, if memory serves correctly, before that in TRS-80 Level II BASIC (1978?). There's nothing intrinsically unique about comparison operators that sets them apart from other math operators: they take two arguments and return a result. The Microsoft standard has been to return -1 for true and 0 for false. While I prefer 1 for true, I know that -1 is the standard for PBASIC within IF...THEN conditionals, and I'd be satisfied with that in assignments, too.

    -Phil
  • LittleTykeLittleTyke Posts: 34
    edited 2007-09-02 07:35
    Yep, I've just fired up VB6 to confirm:

    Dim comparator As Boolean
    Dim value As Byte

    ' value = 89 ' returns False
    value = 130 ' returns True
    comparator = value > 128

    MsgBox comparator
  • TechnoRobboTechnoRobbo Posts: 323
    edited 2007-09-02 13:18
    My recollection is that processors determine greater than comparisons via the carry bit (OK, negative and zero bit too)·so this should work good:

    comparator=x - 128 >>15
    substitute 128 with a variable:
    comparator=x - y >>15

    Since the stamp appears to do all internal math at 16 bits this should work with any size variable.
    What the stamp really needs is a CARRY BIT!! The carry bit would simplify math ops greatly and all processors provide it.·It could be·a global variable. We could use it for multi-word math and turning bit shifts into bit-rolls.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Have Fun


    TR

    Post Edited (TechnoRobbo) : 9/2/2007 2:32:00 PM GMT
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2007-09-02 15:09
    Yeh, for the Tandy 100 BASIC I remember well using comparison operators on the right side of equations and it was very useful.

    TR's is nice, and using the sign bit will work so long as x and y are considered as twos complement, and the comparison is valid on the number line even if both numbers are negative, e.g., -20 < -15. As written it would give a 1=true output for x<128 or x<y. And
    comparator=x - 128 >>15
    substitute 128 with a variable:
    comparator=x - y >>15
    I agree, it would be great to have a carry bit available in an auxiliary variable. But that is not going to happen, nor the other direct comparison syntax, I bet $2!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2007-09-02 16:03
    Neither this, nor other suggestions I've made, would break current PBASIC programs or require modifications to the on-chip pcode interpreter. PBASIC is an amazingly capable language, but there are some omissions and asymmetries that could be enhanced away quite easily. To summariize:

    1. Comparisons in assignments.
    2. Enhanced control over variable aliasing (including arrays).
    3. Generalization of bracketed [noparse][[/noparse]I/O lists] to work with auxilliary RAM and other I/O issues.

    One aside regarding comparison operators is their combined use with AND, OR, and XOR. These three Booleans are the only operators that break the strict left-to-right execution order of PBASIC expressions. What's interesting is that, like &, |, and ^, they operate bitwise and differ only in their precedence. Although it would be useful if they always returned 0/-1 or 0/1, this is likely a limitation of the pcode interpreter. Moreover, to change it would break some current PBASIC programs.

    As to signed comparisons, an interesting syntax could employ operators like these: $> $>= $< $<=. But I'm not sure they can translated to meaningful pcode. (I once wrote a BASIC interpreter for the Z8 where signed comparisons were the norm, and unsigned comparisons were indicated by prepending a plus sign (e.g. +>) to the normal operators.)

    In the same vein, a trinary operator like Forth's */ would be handy for ratiometric scaling. This operator performs division on a 32-bit internal product:

    a */ b c = (a16 * b16)32 / c16

    before the high-order 16 bits disappear. Although PBASIC's own */ and ** operators reveal the existence of an internal 32-bit product, these operators may have their own pcode tokens and not be decomposable to accommodate other uses.

    -Phil

    Post Edited (Phil Pilgrim (PhiPi)) : 9/2/2007 4:09:10 PM GMT
  • cyplesmacyplesma Posts: 76
    edited 2007-09-02 18:25
    well I didn't mean to upset people again.

    just remembering the code I have written over the years.

    I haven't used the comparison type assignment syntax in basic (not often enough to remember any way), I do remember using it in C, with almost every assignment and I don't use the if then else block in C:
Sign In or Register to comment.