Compare commits

..

No commits in common. "5f720ad7c3f9a8f01ebac3019849d0b6b5de7df9" and "1ade6cae504359b6d6530b501e54e80bfc9033c6" have entirely different histories.

2 changed files with 21 additions and 30 deletions

View File

@ -43,7 +43,7 @@ accessed.
### Declaration ### Declaration
- Declare and initialize a new variable - Declare and initialize a new variable
- Declaring a previously declared variable again will shadow the previous variable - Declaring a previously declared variable again is currently equivalent to an assignment
- Declaration is needed before assignment or other usage - Declaration is needed before assignment or other usage
- The variable name is on the left side of the `<-` operator - The variable name is on the left side of the `<-` operator
- The assigned value is on the right side and can be any expression - The assigned value is on the right side and can be any expression
@ -69,6 +69,7 @@ The available variable datatypes are `i64` (64-bit signed integer), `string` (`"
- Can be created by just writing an integer literal like `546` - Can be created by just writing an integer literal like `546`
- Inside the number literal `_` can be inserted for visual separation `100_000` - Inside the number literal `_` can be inserted for visual separation `100_000`
- The i64 values can be used as expected in calculations, conditions and so on - The i64 values can be used as expected in calculations, conditions and so on
-
``` ```
my_i64 <- 123_456; my_i64 <- 123_456;
``` ```
@ -79,7 +80,7 @@ my_i64 <- 123_456;
- There is no way to access or change the characters of the string - There is no way to access or change the characters of the string
- Unicode characters are supported `"Hello 🌎"` - Unicode characters are supported `"Hello 🌎"`
- Escape characters `\n`, `\r`, `\t`, `\"`, `\\` are supported - Escape characters `\n`, `\r`, `\t`, `\"`, `\\` are supported
- String can be assigned to variables, just like i64 - String can still be assigned to variables, just like i64
``` ```
world <- "🌎"; world <- "🌎";
@ -91,13 +92,12 @@ print "\n";
### Array ### Array
- Arrays can contain any other datatypes and don't need to have the same type in all cells - Arrays can contain any other datatypes and don't need to have the same type in all cells
- Arrays can be created by using brackets with the size in between `[size]` - Arrays can be created by using brackets with the size in between `[size]`
- Arrays must be assigned to a variable in order to be used - Arrays must be assigned to a variable to be used
- All cells will be initialized with i64 0 values - All cells will be initialized with i64 0 values
- The size can be any expression that results in a positive i64 value - The size can be any expression that results in a positive i64 value
- The array size can't be changed after creation - The array size can't be changed after creation
- The arrays data is always allocated on the heap - The arrays data is always allocated on the heap
- The array cells can be accessed by using the variable name and specifying the index in brackets - The array cells can be accessed by using the variable name and brackets `my_arr[index]`
`my_arr[index]`
- The index can be any expression that results in a positive i64 value in the range of the arrays - The index can be any expression that results in a positive i64 value in the range of the arrays
indices indices
- The indices start with 0 - The indices start with 0
@ -106,14 +106,13 @@ print "\n";
width <- 5; width <- 5;
heigt <- 5; heigt <- 5;
// Initialize array of size 25, initialized with 25x 0 // Initialize array of size 25 with 25x 0
my_array = [width * height]; my_array = [width * height];
// Modify first value // Modify first value
my_array[0] = 5; my_array[0] = 5;
// Print first value // Print first value
// Outputs `5`
print my_array[0]; print my_array[0];
``` ```
@ -146,9 +145,7 @@ Supported mathematical operations:
- "Bit flip" (One's complement) `~a` - "Bit flip" (One's complement) `~a`
### Logical Operators ### Logical Operators
The logical operators evaluate the operands as `false` if they are equal to `0` and `true` if they are not equal to `0`. The logical operators evaluate the operands as `false` if they are equal to `0` and `true` if they are not equal to `0`
Note that logical operators like AND / OR do not support short-circuit evaluation. So Both sides of
the logical operation will be evaluated, even if it might not be necessary.
- And `a && b` - And `a && b`
- Or `a || b` - Or `a || b`
- Not `!a` (if `a` is equal to `0`, the result is `1`, otherwise the result is `0`) - Not `!a` (if `a` is equal to `0`, the result is `1`, otherwise the result is `0`)
@ -163,14 +160,13 @@ The equality and relational operations result in `1` if the condition is evaluat
- Less or equal than `a <= b` - Less or equal than `a <= b`
## Control-Flow ## Control-Flow
For conditions like in if or loops, every non-zero value is equal to `true`, and `0` is `false`. For conditions like in if or loops, every non zero value is equal to `true`, and `0` is `false`.
### Loop ### Loop
- The `loop` keyword can be used as an infinite loop, as a while loop or as a while loop with - The `loop` keyword can be used as an infinite loop, as a while loop or as a while loop with advancement (an expression that is executed after the loop body)
advancement (an expression that is executed after each loop)
- If only `loop` is used, directly followed by the body, it is an infinite loop that needs to be - If only `loop` is used, directly followed by the body, it is an infinite loop that needs to be
terminated by using the `break` keyword terminated by using the `break` keyword
- The `loop` keyword can be followed by the condition (an expression) without needing parentheses - The `loop` keyword is followed by the condition (an expression) without needing parentheses
- *Optional:* If there is a `;` after the condition, there must be another expression which is used as the advancement - *Optional:* If there is a `;` after the condition, there must be another expression which is used as the advancement
- The loops body is wrapped in braces (`{ }`) just like in C/C++ - The loops body is wrapped in braces (`{ }`) just like in C/C++
- The `continue` keyword can be used to end the current loop iteration early - The `continue` keyword can be used to end the current loop iteration early
@ -204,12 +200,11 @@ loop k < 10; k = k + 1 {
``` ```
### If / Else ### If / Else
- The language supports `if` and an optional `else` - The language supports `if` and an optional `else`
- After the `if` keyword must be the deciding condition, parentheses are not needed - After the `if` keyword must be the deciding condition, parentheses are not needed
- The blocks are wrapped in braces (`{ }`) - The block *if-true* block is wrapped in braces (`{ }`)
- *Optional:* If there is an `else` after the *if-block*, there must be a following *if-false*, aka. else block - *Optional:* If there is an `else` after the *if-block*, there must be a following *if-false*, aka. else block
- NOTE: Logical operators like AND / OR do not support short-circuit evaluation. So Both sides of
the logical operations will be evaluated, even if it might not be necessary
``` ```
a <- 1; a <- 1;
b <- 2; b <- 2;
@ -250,14 +245,13 @@ print var_in_inner_scope;
### Function definition ### Function definition
- Functions can be defined by using the `fun` keyword, followed by the function name and the - Functions can be defined by using the `fun` keyword, followed by the function name and the
parameters in parentheses. After the parentheses, the body is specified inside a braces block parameters in parentheses. After the parentheses, the body is specified inside a braces block
- The function parameters are specified by only their names - The function parameters are specified by only the names
- The function body has its own scope - The function body has its own scope
- Parameters are only accessible inside the body - Parameters are only accessible inside the body
- Variables from the outer scope can be accessed and modified if the are defined before the function - Variables from the outer scope can be accessed and modified if the are defined before the function
- Variables from the outer scope are shadowed by parameters or local variables with the same name - Variables from the outer scope are shadowed by parameters with the same name
- The `return` keyword can be used to return a value from the function and exit it immediately - The `return` keyword can be used to return a value from the function and exit it immediately
- If no return is specified, a special `void` value is returned. That value can't be used in - If no return is specified, a `void` value is returned
calculations or comparisons, but can be stored in a variable (even tho it doesn't make sense)
- Functions can only be defined at the top-level. So defining a function inside of any other scoped - Functions can only be defined at the top-level. So defining a function inside of any other scoped
block (like inside another function, if, loop, ...) is invalid block (like inside another function, if, loop, ...) is invalid
- Functions can only be used after definition and there is no forward declaration right now - Functions can only be used after definition and there is no forward declaration right now
@ -295,15 +289,12 @@ println(result);
### Print ### Print
Printing is implemented via the `print` keyword Printing is implemented via the `print` keyword
- The `print` keyword is followed by an expression, the value of which will be printed to the terminal - The `print` keyword is followed by an expression, the value of which will be printed to the terminal.
- To add a line break a string print can be used `print "\n";` - Print currently automatically adds a linebreak
``` ```
a <- 1; a <- 1;
// Outputs `1` to the terminal // Outputs `"1"` to the terminal
print a; print a;
// Outputs a new-line to the terminal
print "\n";
``` ```
## Comments ## Comments

View File

@ -12,7 +12,7 @@ use crate::{
#[derive(Debug, Error)] #[derive(Debug, Error)]
pub enum RuntimeError { pub enum RuntimeError {
#[error("Invalid array Index: {0:?}")] #[error("Invalid array Index: {}", 0.to_string())]
InvalidArrayIndex(Value), InvalidArrayIndex(Value),
#[error("Variable used but not declared: {0}")] #[error("Variable used but not declared: {0}")]
@ -21,10 +21,10 @@ pub enum RuntimeError {
#[error("Can't index into non-array variable: {0}")] #[error("Can't index into non-array variable: {0}")]
TryingToIndexNonArray(String), TryingToIndexNonArray(String),
#[error("Invalid value type for unary operation: {0:?}")] #[error("Invalid value type for unary operation: {}", 0.to_string())]
UnOpInvalidType(Value), UnOpInvalidType(Value),
#[error("Incompatible binary operations. Operands don't match: {0:?} and {1:?}")] #[error("Incompatible binary operations. Operands don't match: {} {}", 0.to_string(), 1.to_string())]
BinOpIncompatibleTypes(Value, Value), BinOpIncompatibleTypes(Value, Value),
#[error("Array access out of bounds: Accessed {0}, size is {1}")] #[error("Array access out of bounds: Accessed {0}, size is {1}")]