Shop OBEX P1 Docs P2 Docs Learn Events
SPIN: Multiple independent commands on 1 line... — Parallax Forums

SPIN: Multiple independent commands on 1 line...

DToolanDToolan Posts: 11
edited 2011-09-19 20:54 in Propeller 1
I have searched the forum but did not find.... is there a command separator character to put multiple short commands on one line in SPIN?

Example...

if something
shortcommand1 : shortcommand2 : shortcommand3

Thank you

Comments

  • StefanL38StefanL38 Posts: 2,292
    edited 2011-09-18 14:22
    No. There isn't. Reasons:

    1.) A structured programming language like SPIN does have only one command per line.

    2.) Indention is critical in SPIN

    blocks of code belonging to a loop or an if-condition are indicated through indention. You can't combine indention-sensivity with multiple commands per line


    I don't think that it is an advantage to have 150 to 200 commands on the screen at the same time.
    Structured programming means: dividing the code into subroutines. Each subroutine executes commands that belong together as a senseful unit.
    Each subroutine has a selfexplaining name.

    So basic-spaghetti-code like
      if started == true
        i2cObject.i2cStart :    i2cObject.i2cWrite (DS1307_Address | 0,8) :    i2cObject.i2cwrite(0,8) :    DS1307_Hours   := int2bcd(ds_hour)
        DS1307_Minutes := int2bcd(ds_minute) :    DS1307_Seconds := int2bcd(ds_seconds)  
        i2cObject.i2cWrite (DS1307_Seconds,8) :    i2cObject.i2cWrite (DS1307_Minutes,8) :    i2cObject.i2cWrite (DS1307_Hours,8)        
        i2cObject.i2cStop
    

    shrinks down to
      setTime(hour, minute, seconds) 
    

    through coding a subroutine
    PUB setTime(ds_hour, ds_minute, ds_seconds)
      if started == true
        ' set the time
        i2cObject.i2cStart
        i2cObject.i2cWrite (DS1307_Address | 0,8)
        i2cObject.i2cwrite(0,8)
        DS1307_Hours   := int2bcd(ds_hour)
        DS1307_Minutes := int2bcd(ds_minute)
        DS1307_Seconds := int2bcd(ds_seconds)  
        i2cObject.i2cWrite (DS1307_Seconds,8)
        i2cObject.i2cWrite (DS1307_Minutes,8)
        i2cObject.i2cWrite (DS1307_Hours,8)        
        i2cObject.i2cStop
    

    This is the main advantage of structured programming languages over old-basic-style spaghetti-code

    keep the questions coming
    best regards

    Stefan
  • Heater.Heater. Posts: 21,230
    edited 2011-09-18 14:23
    There is no such facility in Spin.
    I generally find code easier to read if it is not written like that in other languages.
    There is no need for it.

    Hmm...thinking about it I guess you could write something like:
    a := (b := 2) + (c := 3)
    where the assignments to b and c are the short statements that you want and the result assigned to a is ignored. Yuk, if that even compiles I would hate to see people do it.
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2011-09-18 16:12
    To my slightly warped way of thinking, back in the olden days, the ":" was banned for the reasons stefanL38 says.... only to be replaced by the complexity of multiple nested bracket code.

    I never really liked either structure and I think they can be just as bad as each other because often the comments that go at the end of the line are far less expressive than the comments one would have put in if the colons had been removed, or the brackets unrolled.

    Of course I have been guilty of writing both multiple colon code and multiple nested bracket code because I grew up coding in an age where sometimes the only way to transport code from one computer was to print it out and key it into the next machine. And paper costs money so the aim was to put as much code as possible on each line. Now I suspect most code is never printed out on paper - much easier to email it or put it on a website.

    As a hypothetical question, from the point of view of final compiled code, does multiple nested bracket code end up smaller or the same size as the equivalent code unrolled?
  • Heater.Heater. Posts: 21,230
    edited 2011-09-18 16:29
    Dr_A,
    With any proper language formatting like that makes no difference to th resulting code.
    Anyway I have no idea which languages you are talking about. What is all this "multiple nested bracket" stuff?
    Example please.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-09-18 17:16
    StefanL38 wrote:
    You can't combine indention-sensivity [sic] with multiple commands per line
    Sure you can, and without ambiguity. The simple rules are these:

    1. Anything coming after a block statement in a multiple-command line belongs to that block.
    2. Anything indented immediately below a multiple command line belongs to the last block begun in that line.
    3. Any following line on the same indent level as the multiple-command line closes all blocks begun in that line.
    4. Any block alternative statement (e.g. else) begins on the same level as its immediately preceding mate.

    For example, given the above rules, there is no ambiguity in the following:
    repeat i from 0 to 5 ; repeat j from 0 to 1 ; s[i * 2 + j] := x[i] * y[j]
    
    if (a < b) ; n := 5 ; repeat i from 0 to n
      s[i]++ ; t[i]++
    
    if (a == b) ; repeat 2 ; do_this ; else ; repeat 6 ; do_that
    
    case ch
      "A": do_this ; x := y
      "B": do_that ; y := x
    

    The delimiters after if conditionals and those surrounding else and elseif could easily be optional as well as those after repeat and before while and until loop terminators. In fact, since the space is not an operator, I'm pretty sure all the delimiters are nothing more than eye candy and could be eliminated altogether without adding ambiguity. The exception would be before unary operators that can be used solo.

    I would not be opposed to having this feature incorporated into Spin. Can it be abused? Of course! What language feature can't be?

    -Phil
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2011-09-18 17:19
    That is a tricky request heater. All the good examples are from code written by people I really respect and I don't want to post their examples. Suffice to say, examples where >> and << are used repeatedly on one "if" line, along with constants defined earlier (not at the beginning but somewhere random in the code), combined with multiple logical "and" and logical "or", and to obfuscate things more, some of the values are binary, some are in hex and some are in decimal.

    I know that : is not allowed as it can cause obfuscation, but Spin can just as easily be obfuscated.

    If someone was keen, you could andd : to Spin and then run it through a pre-processor before compiling it.
  • potatoheadpotatohead Posts: 10,261
    edited 2011-09-18 17:19
    I would like that feature very much.

    Readability is significantly enhanced with it. I find vertical distances to be distracting, once something exceeds your average display capability. +1 to that one.

    Didn't somebody post a clever hack, where they encased the carriage return, or line feed inside the bracket comments?

    Something like: b := a { } c := d, etc...

    @Heater, I've written some similar code, encoding if-then constructs, and multi-piece expressions like that. Who did I get it from? Chip, LOL!!
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-09-18 17:28
    The brace thing is used for the opposite effect: continuing a long statement on the next line by commenting out the line terminator.

    -Phil
  • potatoheadpotatohead Posts: 10,261
    edited 2011-09-18 17:29
    Yeah, just realized that. You beat me to the post edit to come... In any case, I would find that addition very readable.
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2011-09-18 17:54
    @PhiPi, some interesting examples there.

    Would you use ; or : (neither are reserved in Spin are they?)

    Would the rule be that if the precompiler came across a colon or semicolon it would start a new line and indent it the same amount as the beginning of the line is indented?
  • smbakersmbaker Posts: 164
    edited 2011-09-18 18:02
    potatohead wrote: »
    Readability is significantly enhanced with it. I find vertical distances to be distracting, once something exceeds your average display capability.

    Any time I have a nested block that exceeds the height of the screen, I find that I usually should have made a subroutine... or two... or three! :)

    That being said, I don't think it's the job of a language to enforce coding standards. Writing code is where the "art" meets the science. The language should provide a statement separator, and if people think they have a valid reason for putting multiple statements on the same line, then they should be allowed to do so.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-09-18 18:42
    Dr_A,

    The colon is used in Spin in the OBJ section and for case clauses.
    Dr_Acula wrote:
    Would the rule be that if the precompiler came across a colon or semicolon it would start a new line and indent it the same amount as the beginning of the line is indented?

    No. Block begins can occur in the compound line. See my four rules above for how these are handled.

    -Phil
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2011-09-18 19:26
    Ok, so would the best divider be a semicolon, like C?
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-09-18 19:39
    Either a semicolon or a backslash. Chip pretty much ran rampant through the ASCII character set when he designed Spin, leaving only the semicolon, backslash, and backtick uncommitted. Who knows how they might get used in Spin2, though!

    -Phil
  • Heater.Heater. Posts: 21,230
    edited 2011-09-19 01:02
    Amazing, I am very surprised that Potatohead and Phil would be in favor of adding this syntactic feature to Spin.
    Phil's example alone is enough to convince me it's a really ugly unreadable mess.

    Dr_A, thing is I don't think I've ever used a language where ":" was a separator between statements so I'm wondering what languages you had in mind that did and then when or how was it "banned".
    Similarly , horribly complex nested expressions with lots of brackets, as you describe, have been a part of every language I have ever used (apart from assemblers) so again in which languages do you have in mind that didn't have it and was that introduced.

    Also the issue of statement separators/delimiters and block structure seems independent of the issue of nested expression/operators etc in my mind.
    (Except obviously the syntax has to make sense i.e. using ";" as a separator/delimiter and an operator might cause confusion to the compiler as well as the human code author/reader.)
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-09-19 01:15
    heater wrote:
    Phil's example alone is enough to convince me it's a really ugly unreadable mess.
    LOL! 'Reminds me of a certain class of infomercials: "Turn your unreadable mess into an organized success with the Ronco Syntaxolator!"

    It looks better without the delimiters, except where they're absolutely needed. Programs in any language, regardless of syntax, can be written elegantly or as an unreadable mess. The programmer simply has to use discretion in choosing when and when not to program idiomatically. Perl programmers, for example, slowly lose this discretion as their infatuation with the language grows; but they regain it when, after a few months, they have to go back and understand what they've written.

    -Phil
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2011-09-19 02:01
    @heater
    Dr_A, thing is I don't think I've ever used a language where ":" was a separator between statements

    MBASIC - http://en.wikipedia.org/wiki/MBASIC - the very first program I used to write, compile and run something on a propeller with no outside help from a PC. On your Zicog emulator! (thanks++ btw).

    And yes, you can make a mess of code with colons, but if you are careful how you write it, you can also make code more readable by getting the contents of an 'if' statement onto one screen/page.
  • bsnutbsnut Posts: 521
    edited 2011-09-19 02:34
    I am the one that likes my code too look neat and the Doctor is right. Its how you write it. If, I had a choice I would do it the way Spin was designed.
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2011-09-19 03:55
    For those that haven't seen this, I've always liked the obfuscated C program that is a flight simulator in the shape of a plane http://blog.aerojockey.com/post/iocccsim and scroll down a bit.

    I think C lets you put multiple statements on one line, though I don't think I've ever seen code that does this. But it is allowed. I guess nothing terrible happens with C even though this is allowed so ditto with spin? (Mind you, modern IDEs for C would probably reorganise that airplane so it had all the indents etc in the right place).
  • Heater.Heater. Posts: 21,230
    edited 2011-09-19 04:03
    Phil,
    Programs in any language, regardless of syntax, can be written elegantly or as an unreadable mess.
    Yes indeed. I don't see any benefit in making it easier for programmers to make an ungodly mess and this particular syntactic feature exactly does that for no useful purpose.

    Dr_A,

    I had a sneaking suspicion that this might be down to MBASIC or the like. To be honest apart from some very trivial programs back in the day and then again when we got MBASIC on the Prop working I have never used the thing.

    My experience of BASIC predates MS BASIC by a couple of years. Running on a mainframe. Don't remember anything about using ":" separators, but then my expressions were long enough to run off the side of the 80 column teletype page anyway:)
  • Heater.Heater. Posts: 21,230
    edited 2011-09-19 04:07
    C uses the semicolon as a terminator for statements. White space has pretty much no meaning. You can put the entire program on one line if you wish. (Hmm...might have issues with any preprocessor directives though)

    Spin takes the tack of using end of line as a statement terminator. Not a bad idea in my book, removes the need for all those ugly semi-colons.
    So why would anyone what them back again?
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2011-09-19 04:47
    I kind of have grown to like the semi-colon over the years. I guess I like ascii characters I can see.

    Thinking in hex, a semicolon is 0x3b. Taking some of PhiPi's ideas, in Spin that might replace 0x0d, 0x0a,0x20,0x20 - all of which are invisible but they are still there and they do mean something quite specific to the compiler (and possibly different to 0x0d,0x0d,0x20,0x20,0x20,0x20 depending on the context and how many indents there are). So one could think about an intelligent 'find and replace' that could add a semicolon to the syntax.
  • ericballericball Posts: 774
    edited 2011-09-19 07:36
    In the era of MicroSoft BASIC, the advantage of putting multiple statements in a single line number was it saved space as the line number took up RAM. In C I will occasionally put more than one statement on a single line if they are relatively short and closely related. e.g. temp = x; x = y; y = temp; But I don't think that doing so improves readability or maintanability.

    But for SPIN (and C) from a language basis there's no advantage to the compiler or the binary to having multiple statements on a single line. And I think that Chip made a conscious design decision with SPIN to avoid this construct by using a newline as a statement terminator instead of something like a semicolon. (A similar decision is why SPIN uses indentation rather than braces or begin/end for nesting.)
  • potatoheadpotatohead Posts: 10,261
    edited 2011-09-19 07:40
    I like it in the form above because I like small groupings of things that can be seen as more than a single statement or expression, and less than a subroutine, method, or procedure.

    There are times I abuse the grammar too, for similar purposes. I will at times put a rather complex thing into one sentence: (This in response to somebody who would take parts of sentences, string them together in order to misconstrue intent, while calling out others on same, edited to show form:)

    If

    you are going to call out people for not properly considering full expressions

    , then

    I expect you to perform in like kind, which you did not do

    ; additionally:

    it has been clearly shown here that something is true

    , and

    it has been clearly shown here that something else is true

    , therefore

    you have not met your burden on your primary statement

    ; namely,

    "Primary statement quoted in it's entirety."


    The programmer part of me really likes that long form expression. It's got significantly enhanced readability, which generally is the point when I use it with reading challenged people, and it's broken down such that each atomic idea stands as such, without losing track of the whole. And that "whole" is generally marked with a period, where denying it will capture somebody's attention through to the end. The chain of logic is very easily seen as well.

    Abuse of the language? I know my English teacher would cringe, but I can tell you from experience, where I've done that there are NO questions afterword. People get it cold.

    That same thing, presented without the LF/CR style bit:

    If you are going to call out people for not properly considering full expressions, then I expect you to perform in like kind, which you did not do; additionally: it has been clearly shown here that something is true, and it has been clearly shown here that something else is true, therefore you have not met your burden on your primary statement; namely, "Primary statement quoted in it's entirety."

    And without the bold, it gets worse, though it is a perfectly viable, practical expression. When it's not contained in one sentence, it gets worse still, requiring context, supporting statements, paragraph lead ins, outs, and all that other cruft leading to a short book.

    My point here is that addition to the syntax forms of SPIN would offer up some favorable style choices, and the grouping of things in the way shown is of high enough value, I would tolerate the abuses. (scripts can unabuse those easily enough, right?) Being able to group a few things on a line can significantly increase the clarity of something complex, just as dropping some things on new lines can do the same.

    How many people have we seen stumble on this:
    screen[dy * tv_hc + dx] := display_base >> 6 + dy + dx * tv_vc + ((dy & $3F) << 10) 
    
    That's from Graphics Demo, written by Chip, and written that way very few people grok how the tiles and colors are initially assigned.

    Clearly, too much on one line increases difficulty. But, sometimes there are a couple of mundane things that just eat up some vertical space too. Why not group them?
    'increment the pointers
    a++ ; b++ ; c++
    
    One size does not fit all, in other words. Why not add the option? IMHO, people can make a perfect mess in SPIN now, what is a different kind of mess?

    So we will get the messes, but we will get some great expressions too. I want those to be on the table.
  • Heater.Heater. Posts: 21,230
    edited 2011-09-19 07:55
    I think there is a basic point to block structured languages.
    It was realized a long time ago that there are some very basic things needed for composing algorithms:
    Sequential statements as in "a := b; c:=b * b;"
    Decision statements as in "if conda do something else do somethingelse;"
    Loop constructs as in the familiar "for", "while", "repeat" statements.
    It was also realized that GOTO and noodle soup code was not required and that programs were much easier to think about and analyze without them.

    Enter the block structured style of syntax with blocks delimited by braces or indentation level. The layout of code in such languages is intended to convey or highlight those fundamental building blocks of algorithms.
    Now, writing each sequential statement on a line of it's own clearly indicates the sequentialness of the program, the time order.
    It could be construed that writing multiple statements on a line indicates that those statements should happen at the same time. Parallel processing.
    OK that is never the case in the languages we are familiar with but I have seen it carry such a meaning in at least one language.
  • Dave HeinDave Hein Posts: 6,347
    edited 2011-09-19 07:58
    I can think of a couple of uses for multiple statements per per line. One case is where a code block consists of a single statement, such as
      if x
        y := 1
      repeat while y > 0
        y -= x
    
    It would be nice to be able to do something like
      if x ; y := 1
      repeat while y > 0 ; y -= x
    
    The other case would be if macro capabilty was added to Spin, and we wanted to define multi-statement macros, such as
    #define UPDATE_Y(a) if a ; y := 1 ;; repeat while y > 0 ; y -= a
    
    In this case I am using the double semi-colon to indicate the end of a block. The macro could then be used in the code as
      UPDATE_Y(x)
    
    This would generate the four statements I showed above. Of course, if someone created a preprocessor for Spin that had macro capability there is no reason they couldn'nt implement multple statements per line as well.

    Dave
  • potatoheadpotatohead Posts: 10,261
    edited 2011-09-19 08:41
    @Heater, that would be a very interesting proposition. It's not applicable here, due to how the COGS work, but I like it overall.

    Really, in short form, it's just nice to group a few things that don't express enough to warrant the vertical space consumed. The secondary point, I made overly verbose, is having a few style choices isn't a bad thing.

    @Phil, having had to decode some Perl that looked like line noise, I must agree! My skill in Perl is modest. I've gotta work for it usually, and I don't touch it often, but each time I do, I find myself strangely attracted to how potent Perl really is.
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2011-09-19 17:40
    Clearly, too much on one line increases difficulty. But, sometimes there are a couple of mundane things that just eat up some vertical space too. Why not group them?
    'increment the pointers
    a++ ; b++ ; c++
    

    For that example it should be fairly easy to translate.
    1) Note how many indents at the beginning = indents eg in vb.net, indents = strings.len(lineoftext) - strings.len(strings.ltrim(lineoftext)) ' leading spaces = length - length with the leading spaces trimmed off
    2) Search for ";" and replace with <CR>,<LF>,strings.space(indents)

    So adding semicolons may not be all that hard. Maybe add to BST?
  • frank freedmanfrank freedman Posts: 1,983
    edited 2011-09-19 20:54
    Spin !eq unix
Sign In or Register to comment.