a bunch of grammar fixes
This commit is contained in:
+1
-1
@@ -4,7 +4,7 @@ Noja is a high level programming language implemented as a learning exercise. St
|
||||
The use-cases of Noja are the same as Python since their abstraction level is comparable. The syntax is more similar to the C-family of languages though (curly brackets to denote scope).
|
||||
|
||||
## A Noja program
|
||||
A Noja program is a sequence of statements separated by semi-colons (with some exceptions). The statements can be o various kinds:
|
||||
A Noja program is a sequence of statements separated by semi-colons (with some exceptions). The statements can be of various kinds:
|
||||
* expressions
|
||||
* function definitions
|
||||
* if-else branches
|
||||
|
||||
@@ -128,12 +128,12 @@ Lists are defined as a comma-separated list of expressions between square bracke
|
||||
[none, 2 + 1, true];
|
||||
```
|
||||
|
||||
The `n`-th list can be accessed using the `[]` notation:
|
||||
The `n`-th list element can be accessed using the `[]` notation:
|
||||
```py
|
||||
ls = [true, false, none];
|
||||
ls[1]; # false
|
||||
```
|
||||
where the list value is followed by `[..]` which contain the index of the item to retrieve.
|
||||
where the list value is followed by `[..]` which contains the index of the item to retrieve.
|
||||
|
||||
The index may be evaluated dynamically, but it will trigger a runtime error if it doesn't evaluate to an integer value
|
||||
```py
|
||||
@@ -142,7 +142,7 @@ ls[0 + 2]; # OK!
|
||||
ls[true or false]; # Runtime error!!
|
||||
```
|
||||
|
||||
To append a value to a list, increasing it's size, you can just insert the new value at the first unused position (which will have index equal to the current list size). As we'll see in the built-in chapter, to get the size of a list one can use the `count` function. Which means appending to a list can be done like this:
|
||||
To append a value to a list, increasing its size, you can just insert the new value at the first unused position (which will have index equal to the current list size). As we'll see in the built-in chapter, to get the size of a list one can use the `count` function. Which means appending to a list can be done like this:
|
||||
```py
|
||||
ls = [1, 2, 3];
|
||||
ls[count(ls)] = 4;
|
||||
@@ -170,7 +170,7 @@ a = {"name": "Francesco", "age": 25};
|
||||
b = {name: "Francesco", age: 25};
|
||||
```
|
||||
|
||||
If you don't want to use the identifier as key but evaluate it as a variable to use it's value as key, then you can explicitly tell the interpreter that it's an expression
|
||||
If you don't want to use the identifier's text as key but evaluate it as a variable to use it's value as key, then you must explicitly tell the interpreter that it's an expression
|
||||
```py
|
||||
name = "x";
|
||||
|
||||
@@ -179,6 +179,7 @@ a = { "x": "Francesco", "age": 25};
|
||||
b = {(name): "Francesco", age: 25};
|
||||
c = { +name: "Francesco", age: 25};
|
||||
```
|
||||
Note that the `+` unary operator and the `(..)` are no-operations in this context.
|
||||
|
||||
Because of compound statements (discussed in the next chapter), an expression can't start with the `{` of a map literal because the interpreter will assume it's a compount statement.
|
||||
The following statement is invalid
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@ Unlike expressions statements, they don't end with a `;`.
|
||||
|
||||
The condition may be any type of expression, but must evaluate to a boolean type. No implicit casts are performed.
|
||||
|
||||
When the `else` block is empty, in can me omitted:
|
||||
When the `else` block is empty, it can me omitted:
|
||||
```py
|
||||
if condition: {
|
||||
# Executed when the condition is true
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@ while condition: {
|
||||
```
|
||||
as you can see, it doesn't need an ending `;`.
|
||||
|
||||
when the interpreter encounters a while statement, it evaluates the condition. If the condition is true, it executes it's body. When the execution of the inner block of code is completed, the execution jumps back to the condition, evaluating it again. If it's again true, the inner code is executed again, else the execution jumps after the while statement. This mechanism can go on potentially for ever!
|
||||
When the interpreter encounters a while statement, it evaluates the condition. If the condition is true, it executes its body. When the execution of the inner block of code is completed, the execution jumps back to the condition, evaluating it again. If it's again true, the inner code is executed again, else the execution jumps after the while statement. This mechanism can go on potentially for ever!
|
||||
|
||||
Like for if-else statements, when the inner block only has one statement, you can drop the `{}`
|
||||
```py
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
# Functions
|
||||
Function definition statements always start with the `fun` keyword, followed by it's name, argument list and then body.
|
||||
Function definition statements always start with the `fun` keyword, followed by its name, argument list and then body.
|
||||
|
||||
Here are some examples of function definitions:
|
||||
```py
|
||||
@@ -46,7 +46,7 @@ abc = 10;
|
||||
```
|
||||
|
||||
## Functions calls and return values
|
||||
To execute a function in an expression, it's name must be followed by an argument list enclosed in brackets `(..)`. All functions return at least one value, which may be `none` when a function has no useful value to be returned.
|
||||
To execute a function in an expression, its name must be followed by an argument list enclosed in brackets `(..)`. All functions return at least one value, which may be `none` when a function has no useful value to be returned.
|
||||
|
||||
To specify the return value of a function, one must use the `return` statement
|
||||
```py
|
||||
|
||||
Reference in New Issue
Block a user