SPIN: Multiple independent commands on 1 line...
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
Example...
if something
shortcommand1 : shortcommand2 : shortcommand3
Thank you
Comments
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
shrinks down to
through coding a subroutine
This is the main advantage of structured programming languages over old-basic-style spaghetti-code
keep the questions coming
best regards
Stefan
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.
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?
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.
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:
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
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.
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
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?
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.
The colon is used in Spin in the OBJ section and for case clauses.
No. Block begins can occur in the compound line. See my four rules above for how these are handled.
-Phil
-Phil
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.)
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
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.
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).
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:)
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?
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.
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.)
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:
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?
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.
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
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.
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?