Shop OBEX P1 Docs P2 Docs Learn Events
Spin Indention — Parallax Forums

Spin Indention

Dave HeinDave Hein Posts: 6,347
edited 2011-02-27 10:08 in Propeller 1
I thought I would play around with indention to better understand the Spin requirements. The following code is a slightly modified version of the FullDuplexSerial dec method. It compiles and works just like the original. So now I understand the rule a bit better.

Spin doesn't require that every line in a block start in the same column. The starting column is defined by the line that begins that block, such as "repeat" or "if. Each line in the block must start after that column. An "else" corresponding to an "if" must start in the same column as the "if".

The Prop manual does explain how indention works (three times), but it wasn't clear to me until I tried this code.

Dave
PUB dec(value) | i, x
         x := value == NEGX
 if value < 0
    value := {
}       ||(value+x)                                                      
  ser.tx("-")                                                                    
 i := 1_000_000_000                                                           
repeat 10                                                                    
   if value => i                                                               
      ser.tx(value / i {
}   + "0" + x*(i == 1))                                         
              value //= i                                                              
    result~~                                                                  
   elseif result or i == 1
               ser.tx("0")                                                                  
 i /= 10                                                                    

Comments

  • wjsteelewjsteele Posts: 697
    edited 2011-02-26 07:09
    The prop tool has a feature that allows you to "see" the indentation. It's called "Block Group Indicators" and is under the Edit|Preferences menu option. Turning it on will show you the indentation groups for a particular line.

    Very handy feature.

    Bill
  • Dave HeinDave Hein Posts: 6,347
    edited 2011-02-26 07:59
    I use the block group indicator mode most of the time. It does get confused by continuation lines using the {newline} technique, and it covers up characters in comment lines. However, it is a very useful feature.

    I'm writing a Spin compiler, and I needed to understand exactly how indention works. Normally, all the lines in a block should be indented the same, but it might be useful to indent some lines of code more to make them stand out.
  • HumanoidoHumanoido Posts: 5,770
    edited 2011-02-26 08:28
    I'm glad that's clear to you!
    I appear to need a primer titled, "minimal indentation."
  • JonnyMacJonnyMac Posts: 9,208
    edited 2011-02-26 10:47
    Professional programmers have been using clear, consistent indenting for years -- long before languages forced it on them. I don't understand those that sloppily slosh out code with seemingly little regard for those that may have to work through the code (including themselves when they come back to the program sometime later).
  • potatoheadpotatohead Posts: 10,261
    edited 2011-02-26 10:52
    It's usually that they have not come back to work through code later. When they do, they begin to see the value clarity has.

    Edit: That was my own personal experience exactly. Funny how that works!
  • LeonLeon Posts: 7,620
    edited 2011-02-26 11:01
    For some languages, C for instance, there are "pretty-printers" that tidy up messy code automatically.
  • potatoheadpotatohead Posts: 10,261
    edited 2011-02-26 11:18
    Yep. Wrote one in PERL a long time ago. Needed it to clean up a project that I was supposed to make modifications to.

    (professionally written code that looked worse than what we see here regularly)

    I would add that a feature similar to the Parallax intent lines is "show spaces". Kedit has it, and I usually write in that one with it turned on. Makes indenting easy to see. Just make the space color, a bit brighter, or a different hue from the background, and it works fairly well for this kind of thing. I suspect that got in there for Python programmers.
  • YodaYoda Posts: 132
    edited 2011-02-26 12:54
    The problem with pretty printers is they reformat the code. That may be fine until you start using real source control systems. You need to enforce formatting if you are going to use source control and multiple people including yourself is changing the code.
  • potatoheadpotatohead Posts: 10,261
    edited 2011-02-26 13:47
    Yep.

    The PERL project, BTW was to get a baseline formatted, not printed. Debug that, then build on it proper. Just using them each time to view isn't recommended, IMHO.
  • Dave HeinDave Hein Posts: 6,347
    edited 2011-02-26 15:25
    Just to make it clear, I'm not against indenting. I'm just trying to understand Spin's rules for indention. I have used the GNU indent utility on many C programs to make them more readable. Personally, I like the use of braces and indenting. It makes it very easy to distinguish where blocks of code start and end.
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2011-02-26 22:08
    I have a need for a little program to process spin indentation. I think microcontrolled was looking for a software project. Or anyone else for that matter.

    Take a spin program and replace all indents with { and replace all outdents with }

    Of course, it is not quite that simple. "space space" might be the same as "space space space" depending on the context. And detecting the end of a PUB might be either the next pub, or a pri, or a dat statement or a comment block etc etc.

    Still, if anyone is interested. Code can be in any language - input is a text file "source.spin" and output is "output.txt".
  • rosco_pcrosco_pc Posts: 468
    edited 2011-02-27 05:06
    Dr_Acula wrote: »
    I have a need for a little program to process spin indentation. I think microcontrolled was looking for a software project. Or anyone else for that matter.

    Take a spin program and replace all indents with { and replace all outdents with }

    Of course, it is not quite that simple. "space space" might be the same as "space space space" depending on the context. And detecting the end of a PUB might be either the next pub, or a pri, or a dat statement or a comment block etc etc.

    Still, if anyone is interested. Code can be in any language - input is a text file "source.spin" and output is "output.txt".

    Once upon a time, many moons ago, I started to write a SPIN compiler in Python. Here is the lexer/scanner for that compiler. I think it could easily be used as basis for your program.
    HOWEVER I came to the same conclusion as Dave on the indentation and you most likely need to build a little extra logic in to discard unwanted indents/dedents.
  • Dave HeinDave Hein Posts: 6,347
    edited 2011-02-27 10:08
    Dr_Acula wrote: »
    I have a need for a little program to process spin indentation.
    Take a spin program and replace all indents with { and replace all outdents with }
    The attached C program will read a Spin file from the standard input and insert braces and write it to the standard output. It doesn't handle comments except for those starting with a ' character. It doesn't process the : lines within a case block, and it doesn't handle a few other things, such as "while" lines. It also doesn't handle the case where a keyword such as "repeat" starts in the same column as the PUB or PRI line. I believe the Parallax Spin compiler allows for this.

    Dave
    c
    c
    3K
    br.c 3.5K
Sign In or Register to comment.