Layout & scope
Defining some terms
Arplan is an indentation based language. Let us first quickly introduce some terminology using the example velow.
this is some line
bla bla bla
bli bli bli
some more content
even more content
blo blo blo
We use the terms "header line" and "indented body". In the previous example, this is some line is the header of the corresponding green indented body. Likewise, bli bli bli is the header of the orange content body.
Indentation are used for two purposes:
- Defining the scope: what is in the indented block is only seen by the header line
- Defining the syntax: the header defines the syntax used in the indented block
The meaning of "="
Before starting, let us mention that '=' in Arplan means a true definition, like in mathematics. When writing:
x = a + b
It means that x is equal to the sum of a and b, no matter what these are. It is not assignation! Redefining x would raise an error at compile time. Since it is a definition, it does not matter if a and b are defined before or after, the result is the same. Therefore:
x = a + b
a = 2
b = 3
print x # prints 5
Is a valid code snipet
The scope
Here is a small example:
x = a + b
a = 2
b = 3
print x # prints 5
print a # error: unknown 'a'
In this example, only the "definition" of 'x' sees 'a' and 'b'. But they are
not visible to the rest of the program. That the greenish body is only visible by the "header line" allows to define things locally without
poluting the namespace.
It also makes it possible to redefine symbols.
a = 2
b = 3
x = a + b
a = 10
b = 20
y = a + b
print x # prints 30
print y # prints 5
This is useful to redefine symbols just for the expression in the header line. You do not have to care wether the symbol was already declared elsewhere or not. When defining it in the body, it will be seen in priority by the header and remain hidden to the remaining of the program.
This rule holds recursively to any level of indentation.
a = 2
b = 3
x = a + b
a = 10
b = 20
print x # prints 13
The definition x = a + b sees the green body, but not the orange one. Since what is in an indented block has highest priority, the symbol a refers to the a = 10 defined in its body.
On the opposite, since x = a + b does not see the orange section, the symbol b refers to the first b = 3. The b = 20 is only seen by the definition of a and is therefore useless.
Here is one more example to for clarity:
a = 1
a = 22
x = a # x ==
333
a = 333
a = 4444 # not seen by x
That is, what is in the "first-level" of the body has priority. If there was no body, then a = 22 would be used.
So what is all this good for? Well, it leads to a top down way of programming. You start from a the concept/result you want to obtain and refine it from level to level in always more basic
blocks.
my-objective = term-t...
term-to-be-refined-1 ...
part-of-term...
term-to-be-refined-2 ...
...
What is inside the block is only there to define parts of the header line. It is like a tree of definitions that you can unfold to investigate always deeper in the details. This also provides a very
clean namespace and where visibility is fine grained. It is a style of programming to experience.
Syntax changes
TODO
In Arplan, the ++ symbol stands for concatenation:
print ( "Hi " ++ "John!" ) # prints "Hi John!"
Let us look now at a simple example using sub-syntaxes.
myPage = myHeader ++ myBody
syntax-html myHeader
<head>Hello Web!</head>
syntax-html myBody
<body>This is a standart html page!</body>
The syntax-html "keyword" indicates that the content of the indented block should be written in html. As such, the indented block contains normal html.
Notice that inside this html block, indentation and keywords are taken like normal text. Indeed, html is parsed and not Arplan anymore.
Hence, the following example is perfectly valid:
syntax-html myOtherPage
<head>Hello Web!</head>
<body>
<p>I don't care about indentation anymore!</p>
<p>Nor about any keywords!</p>
<p>if then else is just plain html text</p>
</body>
You will see later that it is possible to embbed Arplan expression in sub-languages like html but this is for later.