Difference between revisions of "Line continuation"

From MiniScript Wiki
Jump to navigation Jump to search
(Created page with "MiniScript statements are separated by line breaks rather than explicit punctuation (such as the semicolon in C). It is nonetheless possible to break a long MiniScript statem...")
 
 
Line 5: Line 5:
 
* any binary [[Operators|operator]]: <c>+</c>, <c>-</c>, <c>and</c>, <c>or</c>, etc.
 
* any binary [[Operators|operator]]: <c>+</c>, <c>-</c>, <c>and</c>, <c>or</c>, etc.
  
== Example ==
+
== Examples ==
 
<ms>x = {
 
<ms>x = {
 
     "foo": "A commonly used variable name in example code",
 
     "foo": "A commonly used variable name in example code",
Line 11: Line 11:
 
         "A second commonly used example variable name" }</ms>
 
         "A second commonly used example variable name" }</ms>
  
Note that the final <c>}</c> in this example cannot be put on the next line, since it does not follow one of the special line-continuing tokens.
+
Note that the final <c>}</c> in this example is placed on the same line as the last enty.  As an alternative, when writing a multi-line list or map, you can put the closing <c>]</c> or <c>}</c> on its own line by appending a <c>,</c> to the last item, as in the following example.
 +
 
 +
<ms>states = [
 +
  "Init",
 +
  "Loading",
 +
  "Running",
 +
  "GameOver",
 +
]</ms>
 +
 
 +
This has the added benefit that if you add or reorder your items, you don't have to fix up the commas — every item is followed by a comma, so they are all the same in that regard.
  
 
[[Category:Language]]
 
[[Category:Language]]

Latest revision as of 14:40, 1 April 2024

MiniScript statements are separated by line breaks rather than explicit punctuation (such as the semicolon in C). It is nonetheless possible to break a long MiniScript statement into multiple lines, by placing the line break immediately after any of:

  • an opening (, [, or {
  • a comma or colon: , or :
  • any binary operator: +, -, and, or, etc.

Examples

x = {
     "foo": "A commonly used variable name in example code",
     "bar":
        "A second commonly used example variable name" }

Note that the final } in this example is placed on the same line as the last enty. As an alternative, when writing a multi-line list or map, you can put the closing ] or } on its own line by appending a , to the last item, as in the following example.

states = [
   "Init",
   "Loading",
   "Running",
   "GameOver",
]

This has the added benefit that if you add or reorder your items, you don't have to fix up the commas — every item is followed by a comma, so they are all the same in that regard.