This is an interpreter written in C for the Lotus language (currently WIP). I want to achieve a multithreaded functional PL with optimizations for Pure code.
Installation is easy if you are on a Unix system. For Windows users please get help.
git clone https://github.com/SpanishInquisition49/lotus.git
cd lotus
make # Compile the project with the debug flag
./lotus [source.lts]
make check # Run some test for the logic
make valgrind # Run valgrind
The lotus interpreter can read the file ~/.config/lotus/lotus.conf
(not created by default), below a list of options and their possible values
Option | Possible values | Short description |
---|---|---|
LOG_LEVEL | WARNING/ERROR/INFO | verbosity of errors |
PRINT_REPORT | TRUE/FALSE | an overview of warnings and errors between every phase |
LOG_LEVEL=WARNING
PRINT_REPORT=FALSE
Every declaration extend the environment with a pair identifier value, also the declaration evaluate with the same value obtained evaluating the expression.
let ide = exp;
//example
let x = 2+3; // add to the env x:=2 and yield 2
Functions are declared with the keyword fun, like normal declarations a function declaration extend the environment and yield the closure as a value. A function implicitly return the result of the last statement/expression or they can explicitly return a value with the return
keyword.
fun ide([formals]) body
// Implicit return example
fun double(x) x*2;
// Explicit return example
fun fact(x) {
if(x==0)
return 1;
else
return x*fact(x-1);
}
Declarations binds identifiers with values, but this values are mutable, a value associated to an identifier can be changed with an assignment. An assignment yield as a result the new value.
ide = exp;
//example
x = 5;
Like the majority of programming languages the if/else evaluate a condition and depending on the result execute a block or if present the other one. In addition to this the evaluation of a conditional statement yield the result of the executed block.
if(condition)
//block
[else]
//block
//example
if(x%2==0)
print "Even";
else
print "Odd";
The print statement write on stdout the value obtained by evaluating the given expression. Every print yield a nil value
print exp;
//example
print "Hello";
print x;
A function call is formally an expression, a call is formed by an identifier followed by the list of actual parameters (a list of expressions).
ide([actual]);
//example
double(2);
Lotus is a strongly and dynamic typed language, there are just four atomic types:
-
$S$ String -
$N$ Numbers -
$B$ Booleans -
$F$ Functions -
$Nil$ (for now there is no usage for the nil type)
A lower priority operator are evaluated before other higher operators, functions call have the precedence above every other operators.
Unary operator | Usage | Priority | Domain |
---|---|---|---|
Logical not | !exp | 2 | |
Minus | -exp | 2 |
Binary operator | Usage | Priority | Domain |
---|---|---|---|
Multiplication | exp * exp | 3 | |
Division | exp / exp | 3 | |
Remainder | exp % exp | 3 | |
String concatenation | exp + exp | 4 | |
Addition | exp + exp | 4 | |
Subtraction | exp - exp | 4 | |
Greater than | exp > exp | 5 | |
Greater or equal | exp >= exp | 5 | |
Lower than | exp < exp | 5 | |
Lower or equal | exp <= exp | 5 | |
Logical and | exp and exp | 6 | |
Logical or | exp or exp | 6 | |
Equality | exp == exp | 7 | |
Not equality | exp != exp | 7 | |
Forwarding | exp |> call | 8 |
Nested functions call can be written with the forward operator. The value on the left side of the operator will be used as the first argument of the right function call.
factorial(double(5))
// Is equivalent to
double(5) |> factorial()
// Or
5 |> double() |> factorial()
// If a function have multiple arguments
sum(5, y)
// Then you can write (all arguments in the call will be shifted by 1 position)
5 |> sum(y)
- Add return statement
- Add string concatenation
- Add arrays
- Add list
- Add pattern matching
- Fix garbage collector
- Add stack management
- Add the ability to read input from stdin
- Add the ability to format strings