Spin Indention
Dave Hein
Posts: 6,347
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
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
Very handy feature.
Bill
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.
I appear to need a primer titled, "minimal indentation."
Edit: That was my own personal experience exactly. Funny how that works!
(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.
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.
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