Crates.io | vm_lang |
lib.rs | vm_lang |
version | 0.1.1 |
source | src |
created_at | 2022-11-18 22:13:44.27787 |
updated_at | 2022-11-18 22:15:20.151566 |
description | A simple interpreted language written in Rust. |
homepage | |
repository | |
max_upload_size | |
id | 718283 |
size | 93,642 |
A basic interpreted programming language written in Rust as a fun exercise.
A program written in the language is made up of a number of functions, one of which must be named main
, take no arguments and have return type void
.
Has a name, a list of arguments and an optional return type followed by a block statement.
Ex:
fn some_func(a: int, b: [string]): boolean {
// Statements
return true
}
If a function has a return type it MUST use a return
statement on every path through the function, otherwise a return
is optional.
Any text after //
in a line is ignored (unless the //
are within a string literal).
Declares a variable, type is inferred.
Ex:
let a = 0;
A loop that continues as long as the provided expression holds true.
Ex:
while true {
// Statements
}
A for-each loop going iterating through a list.
Ex:
let some_list = [1, 4, 9];
for (a in some_list) {
// Statements
}
A branching statement, runs the block if the expression holds true.
Ex:
if true {
// Statements
}
A branching statement, runs the first block if the expression holds true, otherwise runs the else
block.
Ex:
let i = 4;
if i < 2 {
// Statements
} else {
// Statements
}
Returns from the current function with the provided expression, the type of the expression must be the same as the return type of the function.
Ex:
return 54;
Returns from the current function when the function has return type void
.
Ex:
return;
An expression ended with a semi-colon ;
is also a statement.
Ex:
i++;
Wraps a list of statements creating a new scope.
Ex:
{
// Statements
}
There are a number of expressions in the language, in order of priority they are:
Reassigns a previously declared variable.
Ex:
a = 2;
Compares two expressions of the same type returning a boolean.
Comparisons supporting only boolean expressions:
- ||
| 'or' is true if either of the expressions are true.
- &&
| 'and' is true if both of the expressions are true.
Comparisons supporting all(?) types:
- ==
| equals
is true if the expressions are the same.
- !=
| not equals
is true if the expressions are no the same.
- <=
| Less than or equal to
is true if the first expression is less than or equal to the second expression.
- >=
| Greater than or equal to
is true if the first expression is greater than or equal to the second expression.
- <
| Less than
is true if the first expression is strictly less than the second expression.
- >
| Greater than
is true if the first expression is strictly greater than the second expression.
Ex:
2 < 4
Performs an arithmetic operation between two expressions of type int
.
The currently suppported arithmetic operations are:
+
| plus-
| minus*
| times/
| divisionEx:
2 + 4
Operations on singular expressions.
The currently supported unary operations are:
!
| not++
| Increases the value by 1, depending on if put before or after the expression this will be done before or after the value is read.--
| Decreases the value by 1, depending on if put before or after the expression this will be done before or after the value is read.Ex:
i++;
Calls a function.
Ex:
some_func(42, "this function has two args");
Retrieves a value from a list by its index.
Ex:
let some_list = [1, 5, 9];
some_list[1]; // Returns 5
A literal of any of the available types (except void).
Ex:
54
Any expression wrapped in parenthesis.
Ex:
(1 + 2)
There are currently four types available in the language:
int
, ex: -54
bool
, ex: true
string
, ex: "Hello! This is a string"
[TYPE]
where TYPE
can be any of the above types, ex: [0, 1, 99, 200]
void
, never directly used but is used when no other type has been declared for e.g. functions that have no return type.There are a few built-in functions available for specific tasks.
Prints the number to standard out.
Args:
- number: int
| The number to print.
Returns: `Void
Prints the string to standard out.
Args:
- text: string
| The string to print.
Returns:
Void
Prints the boolean to standard out.
Args:
- val: bool
| The boolean to print.
Returns:
Void
Reads a number from standard in.
Args: None
Returns:
- number: int
| The number read.
Reads a string from standard in.
Args: None
Returns:
- val: string
| The string read.
Reads a boolean from standard in.
Args: None
Returns:
- val: bool
| The boolean read.
Given a path, reads a file to a string.
Args:
- file_path: string
| The filepath relative to the current directory.
Returns:
- file_content: string
| The content of the file.
Given a string and a 'splitter', splits the string on each occurance of the 'splitter', returning a list of the parts.
Args:
- text: string
| The text to be split.
- splitter: string
| The text to split on.
Returns:
- parts: [string]
| A list containing every part of text
split on each occurance of the splitter
.
Parses a string to a number.
Args:
- text: string
| The text to parse.
Returns:
- num: int
| The parsed text.