Shop OBEX P1 Docs P2 Docs Learn Events
One Line of Spin Code On Two Lines — Parallax Forums

One Line of Spin Code On Two Lines

kt88seampkt88seamp Posts: 112
edited 2010-03-16 04:53 in Propeller 1
I have one heck of a long line of SPIN code. I like to make my code presentable when I print it. It will travel off the edge of an 8.5 x 11 sheet of paper. Is there a way, like in most progamming languages, to place the following line of code so it takes up two lines?

Registers[noparse][[/noparse]Instructions[noparse][[/noparse]Registers[noparse][[/noparse]0]] & 255] := Registers[noparse][[/noparse](Instructions[noparse][[/noparse]Registers[noparse][[/noparse]0]] & 16711680) >> 16] 'Copy source to destination.

Comments

  • JonnyMacJonnyMac Posts: 9,208
    edited 2010-03-14 22:44
    Use a curly-brace comment like this:

    Registers[noparse][[/noparse]Instructions[noparse][[/noparse]Registers[noparse][[/noparse]0]] & 255] := {
    } Registers[noparse][[/noparse](Instructions[noparse][[/noparse]Registers[noparse][[/noparse]0]] & 16711680) >> 16]
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon McPhalen
    Hollywood, CA
  • wyzard28wyzard28 Posts: 24
    edited 2010-03-14 23:20
    Great tip. This should definitely be documented somewhere!
    Thanks, JonnyMac!
  • K2K2 Posts: 693
    edited 2010-03-14 23:38
    Very good to know. Thanks for both the question and the answer.
  • TappermanTapperman Posts: 319
    edited 2010-03-15 16:48
    JonnyMac said...
    Use a curly-brace comment like this:

    Registers[noparse][[/noparse]Instructions[noparse][[/noparse]Registers[noparse][[/noparse]0]] & 255] := {
    } Registers[noparse][[/noparse](Instructions[noparse][[/noparse]Registers[noparse][[/noparse]0]] & 16711680) >> 16]
    

    Now this is information!
  • heaterheater Posts: 3,370
    edited 2010-03-15 19:26
    Do yourself and any one who has to read your code a favour and split huge long expressions into smaller meaningful parts.

    Rewriting your expression in a more readable fashion takes up exactly the same amount of Program, Var space leaving the same Stack/Free.

    For example:
    VAR
    long registers
    long instructions
    
    PUB start | src
      src := Registers[noparse][[/noparse](Instructions[noparse][[/noparse]Registers[noparse][[/noparse]0]] & 16711680) >> 16]
      Registers[noparse][[/noparse]Instructions[noparse][[/noparse]Registers[noparse][[/noparse]0]] & 255] := src
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    For me, the past is not over yet.
  • jazzedjazzed Posts: 11,803
    edited 2010-03-15 20:40
    heater said...
    Do yourself and any one who has to read your code a favour and split huge long expressions into smaller meaningful parts.
    Indeed! ... and putting all code on one line also doesn't make it execute any faster.
    If you can't figure out what code does at a glance, it's probably not very reader friendly.

    I will not read code that "makes my eyes bleed" for very long even if it was written by a genius.

    Long symbol names (registers, instructions, etc...) can make the task harder.
    Some people like 20 letter symbols, but that's really overboard to me at least.

    Use meaningful abbreviations if possible. Single letter index variables help readability
    for small segments of code, but longer more descriptive variable names are better if
    your code spans several screens of the editor ... which begs the question "why so long?".
    PASM code may require longer names, but Spin usually does not.

    The {} thing is good to know of course. You can find this and many other ideas reading posted code.

    Good luck.
    --Steve

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Short answers? Not available at this time since I think you deserve more information than you requested.
  • JonnyMacJonnyMac Posts: 9,208
    edited 2010-03-15 20:42
    I'm with heater; lately I've been seeing a lot of compound instructions in Spin that are just itching to be buggy.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon McPhalen
    Hollywood, CA
  • CannibalRoboticsCannibalRobotics Posts: 535
    edited 2010-03-15 20:55
    Now that's just clever, putting the line feed in a comment - beautiful!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Signature space for rent!
    Send $1 to CannibalRobotics.com.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-03-15 21:01
    I'm not so disinclined to use compound instructions (it's a sickness that comes from Perl, I'm afraid), but overlong variable and constant names really bug me. I would simply abbreviate the OP's code from:

    Registers[noparse][[/noparse]Instructions[noparse][[/noparse]Registers[noparse][[/noparse]0]] & 255] := Registers[noparse][[/noparse](Instructions[noparse][[/noparse]Registers[noparse][[/noparse]0]] & 16711680) >> 16] 'Copy source to destination.
    
    
    


    to:

    Reg[noparse][[/noparse]Inst[noparse][[/noparse]Reg] & $ff] := Reg[noparse][[/noparse](Inst[noparse][[/noparse]Reg] & $ff_0000) >> 16]  'Copy source to destination.
    
    
    


    Notice also that I got rid of all the zero subscripts (they're redundant) and changed the decimal literals to hex. If a number makes more semantic sense in hex than in decimal, use hex. And always use hex or binary for a literal involved in a bitwise Boolean operation, unless it can be expressed as a single decimal digit.

    But I'd take this simplification even further if it were my program:

    Reg[noparse][[/noparse]Inst[noparse][[/noparse]Reg] & $ff] := Reg[noparse][[/noparse]Inst[noparse][[/noparse]Reg] >> 16 & $ff]  'Copy source to destination.
    
    
    


    Now it's readable! smile.gif

    -Phil
  • jazzedjazzed Posts: 11,803
    edited 2010-03-15 21:11
    Phil Pilgrim (PhiPi) said...
    I'm not so disinclined to use compound instructions (it's a sickness that comes from Perl, I'm afraid), ...

    But I'd take this simplification even further if it were my program:

    Reg[noparse][[/noparse]Inst[noparse][[/noparse]Reg] & $ff] := Reg[noparse][[/noparse]Inst[noparse][[/noparse]Reg] >> 16 & $ff]  'Copy source to destination.
    
    
    


    Now it's readable! smile.gif

    -Phil
    Considering the other write-once-and-forget-it aspects of perl syntax, it seems you only have a small cold. [noparse]:)[/noparse]

    Better:
    Reg[noparse][[/noparse]Inst[noparse][[/noparse]Reg] & $ff] := Reg[noparse][[/noparse](Inst[noparse][[/noparse]Reg] >> 16) & $ff]  'Copy source to destination.
    
    
    


    The precedence of shift is confusing between spin and other languages ... parens add clarity.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Short answers? Not available at this time since I think you deserve more information than you requested.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-03-15 21:45
    jazzed said...
    Better: ... The precedence of shift is confusing between spin and other languages ... parens add clarity.
    True enough. (I had to look it up again myself before removing the parens. That should have told me something. smile.gif )

    -Phil
  • BradCBradC Posts: 2,601
    edited 2010-03-15 23:48
    Phil Pilgrim (PhiPi) said...

    Notice also that I got rid of all the zero subscripts (they're redundant)

    They're also slower. The compiler is not smart enough to know that Regs[noparse][[/noparse]0] == Regs, so it generates the code for an indexed variable causing an extra stack push and pop for each iteration.

    Spin Block Fred with 0 Parameters and 1 Extra Stack Longs. Method 1
    Pub Fred | X
    
    Local Parameter DBASE:0000 - Result
    Local Variable  DBASE:0004 - X
    |===========================================================================|
    2                       X := X
    Addr : 0018:             64  : Variable Operation Local Offset - 1 Read
    Addr : 0019:             65  : Variable Operation Local Offset - 1 Write
    3                       X := X[noparse][[/noparse]0]
    Addr : 001A:             35  : Constant 1 $00000000
    Addr : 001B:          DC 04  : Memory Op Long DBASE + POP Index READ Address = 0004
    Addr : 001D:             65  : Variable Operation Local Offset - 1 Write
    Addr : 001E:             32  : Return
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    You only ever need two tools in life. If it moves and it shouldn't use Duct Tape. If it does not move and it should use WD40.

    Post Edited (BradC) : 3/15/2010 11:56:01 PM GMT
  • ElectricAyeElectricAye Posts: 4,561
    edited 2010-03-16 02:00
    Phil Pilgrim (PhiPi) said...
    .... overlong variable and constant names really bug me....

    Then you would really hate to see my code (which is one reason why I never post anything in the OBEX): my variable names read like short obituaries in the New York Times. And I am continuously being forced to condense the originals down to something that the Propeller can SPIN on. cry.gif
  • jazzedjazzed Posts: 11,803
    edited 2010-03-16 03:21
    ElectricAye said...

    Then you would really hate to see my code (which is one reason why I never post anything in the OBEX): ...
    Long public method names can be useful to convey meaning of the "user interface" API. Of course a good comment can be very valuable (if it matches the code [noparse]:)[/noparse].

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Short answers? Not available at this time since I think you deserve more information than you requested.
  • Dave HeinDave Hein Posts: 6,347
    edited 2010-03-16 03:44
    BradC said...
    Phil Pilgrim (PhiPi) said...

    Notice also that I got rid of all the zero subscripts (they're redundant)
    They're also slower. The compiler is not smart enough to know that Regs[noparse][[/noparse]0] == Regs, so it generates the code for an indexed variable causing an extra stack push and pop for each iteration.
    For me, using zero subscripts makes the code more readable.· However, it does make the code larger and slower.· I use a method where I put zero subscripts between braces instead of brackets, which makes them comments.· As an example, I have a routine in my malloc code that prints out a linked-list of malloc'ed memory chunks.· The code is as follows:
    CON
    · NEXT_BLK = 0
    · BLK_SIZE = 1
    PRI PrintList(list)
    · repeat while (list)
    ··· c.printf2(STRING("%8x %d\n"), list, word[noparse][[/noparse]list][noparse][[/noparse]BLK_SIZE])
    ··· list := word[noparse][[/noparse]list]{NEXT_BLK}
    The next pointer is the first word in the header, and the block size is the second word.· I think the meaning of the last line of the function is clearer when written with the zero index then if it were just "list := word[noparse][[/noparse]list]".· By using the braces instead of brackets I don't add any extra program size or compute cycles.
    Dave
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-03-16 04:09
    Dave,

    That's very clever! And probably one of the few times you get anything with braces for free -- ask any orthodontist!

    -Phil
  • Cluso99Cluso99 Posts: 18,069
    edited 2010-03-16 04:53
    Neat trick with the braces to enclose the cr. My answer to the original question would have been a no. However, none of my code would be that long anyway - too hard to read.

    All the comments are good info and I am in total agreement.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Links to other interesting threads:

    · Home of the MultiBladeProps: TriBlade,·RamBlade,·SixBlade, website
    · Single Board Computer:·3 Propeller ICs·and a·TriBladeProp board (ZiCog Z80 Emulator)
    · Prop Tools under Development or Completed (Index)
    · Emulators: CPUs Z80 etc; Micros Altair etc;· Terminals·VT100 etc; (Index) ZiCog (Z80) , MoCog (6809)·
    · Prop OS: SphinxOS·, PropDos , PropCmd··· Search the Propeller forums·(uses advanced Google search)
    My cruising website is: ·www.bluemagic.biz·· MultiBlade Props: www.cluso.bluemagic.biz
Sign In or Register to comment.