Compiler for MiNI language written in C# with GPLEX and GPPG.
The project was carried out during the "Translation Methods" course at the MiNI Faculty of Warsaw University of Technology.
Basic elements of the language are:
- keywords - program if else while read write return int double bool true false
- operators and special symbols - = || && | & == != > >= < <= + - * / ! ~ ( ) { } ;
- numbers and identifiers
Each program starts with program
keyword and declarations of variables(int
, double
, bool
).
The next part of the program is a sequence of statements such as:
- Block statement
- Expression statement
- Conditional statement
- While loop statement
- Input statement
- Output statement
- Return statement
The following table illustrates all expressions operators available in the language and theirs basic properties.
No. | Group - priority | Name | Symbol | Associativity |
---|---|---|---|---|
1 | unary | unary minus | - |
right-to-left |
bitwise negation | ~ |
|||
logical negation | ! |
|||
conversion to int | (int) |
|||
conversion to double | (double) |
|||
2 | bitwise | bitwise sum | | |
left-to-right |
bitwise product | & |
|||
3 | multiplicative | multiplication | * |
left-to-right |
division | / |
|||
4 | additive | addition | + |
left-to-right |
subtraction | - |
|||
5 | relations | equal | == |
left-to-right |
different | != |
|||
greater than | > |
|||
greater than or equal to | >= |
|||
less than | < |
|||
less than or equal to | <= |
|||
6 | logical | logical sum | || |
left-to-right |
logical product | && |
|||
7 | assignment | assignment | = |
right-to-left |
NOTICE:
- Logical operators realize short-circuit evaluation.
- Brackets
( )
can be included into expressions and have the highest priority. - Wrong operator usage should result in an explanatory compilation error.
These examples are valid programs written in MiNI language.
program
{
int i;
double d;
bool b;
i = 5;
d = 123.456;
b = true;
write i;
write "\n";
write d;
write "\n";
write b;
write "\n";
}
output:
5
123.456000
True
// By Kuba Janaszkiewicz
program {
double a; double c; double m; // RNG magic numbers
double seed; // RNG SEED
double x; double y; // Coordinates
int i; int iters; // Loop index
int inside;
double pi;
iters = 1000;
seed = 123456789;
a = 48271;
c = 0;
m = 2147483647; // 2^31 - 1, max int
while (i < iters)
{
// Create a new pseudorandom coordinate using an LCG
seed = a * seed + c;
while (seed > m) {
seed = seed - m;
}
x = 2 * seed / m - 1;
// Create another pseudorandom coordinate
seed = (a * seed + c);
while (seed > m) {
seed = seed - m;
}
y = 2 * seed / m - 1;
// Check if point is inside the circle
if (x * x + y * y <= 1)
inside = inside + 1;
i = i + 1; // Increment loop counter
}
pi = 4.0 * inside / iters;
write pi;
write "\nPI between 3 and 3.3: ";
write pi > 3 && pi < 3.3;
}
output:
3.124000
PI between 3 and 3.3: True