-1

I'm trying to understand what kind of approach is used by Nim to distinguish between commands.

There's the "separatist approach" where a semicolon just separates commands (used in Pascal for example), the "terminist approach" where a semicolon completely terminates the command (used in C, C++, Java, etc.) and the "liberal approach" where the programmer can decide whether or not to use a semicolon.

My thoughts are that Nim belongs to the liberal approach, but that would mean that semicolons could be added at the end of commands and Nim doesn't support that.

Any other thoughts?

squirl
  • 1,636
  • 1
  • 16
  • 30
BeH
  • 9
  • 4
  • Actually Nim does support optional semicolons at the ends of the lines. This comes in handy when porting chunks of code from other semicolonic languages :) – uran Dec 29 '16 at 00:22
  • "This comes in handy when porting chunks of code from other semicolonic languages" -- hardly, since they could readily be removed with perl, sed, etc. ... and should be, regardless of being syntactically allowed. – Jim Balter Jul 17 '20 at 09:59

2 Answers2

1

I'm trying to understand what kind of approach is used by Nim to distinguish between commands.

Why? This doesn't help in any way ... Nim has a complex syntax that doesn't readily fit into such boxes.

Your question is confused in several ways. First, what is a "command"? Semicolons separate statements or expressions. The difference between your categories matter mostly in expression languages--it determines whether the value of a block ending with a semicolon is the bottom value, or the value of the previous expression. "separatist" languages are confusing, error-prone, bad design, and obsolete--the mistakes of Algol are ancient history. Second, the categories don't make a lot of sense in languages like Nim where end-of-line is syntactically significant--a "missing" semicolon before a newline isn't really missing because the newline serves the same function. Thirdly, Nim most certainly does allow semicolons at the ends of expressions or statements (but it doesn't allow empty statements or expressions, so ;; is disallowed).

Consider:

proc a: int = 5  # returns 5

proc b: int = 5; # syntax error

proc c: int = # returns 5
    5

proc d: int = # returns 5
    5;

proc e: int = # syntax error
    5;;

Since the ; that differentiates c and d makes no semantic difference, one could say that it's closer to "liberal" than to "separatist" or "terminist", but it isn't very liberal ... you can't just put semicolons anywhere.

Jim Balter
  • 16,163
  • 3
  • 43
  • 66
0

Nim, like Python, is a whitespace-aware language. It uses newlines as statement separators and indentation to produce block structures.

Not all languages have visible statement separators, although some allow a visible statement separator in some circumstances. (For example, in Python simple statements can be separated by semicolons, but not compound statements.)

"There are more things in heaven and earth, Horatio, Than are dreamt of in your philosophy" (Hamlet I.5:159–167)

rici
  • 234,347
  • 28
  • 237
  • 341
  • This doesn't address the question. The fact is that Nim's statements can be separated and/or terminated by semicolons as well as off-side whitespace: `stmt = (IND{>} complexOrSimpleStmt^+(IND{=} / ';') DED) / simpleStmt ^+ ';'` – Jim Balter Jul 17 '20 at 09:55