This is the first version of the language and includes some basic features, however, it is still a work in progress.
Try it yourself here -> https://replit.com/@timtak/Toylang
factor: PLUS factor
| MINUS factor
| INT
| LPAR expression RPAR
| VAR
term: factor ((PLUS|MINUS) factor)*
expression: term ((MULT|DIV) term)*
logic-expression: expression LOGIC expression
increment-expression: factor (INC|DEC|INCBY|DECBY)
arr-expression: [expression (, expression)*?]
arr-index: arr[expression]
assignment: var ASSGN expression
| var INC
| var INCBY factor
if: if logic-expression { body } (elif logic-expression { body })*? (else { body })?
for: FOR (assignment, logic-expression, increment-expression) { loop body }
while: while condition { loop body }
func: func var ((expression, )*?) { func body }
func-call: var((expression, )*?)
statement: compound_statement
| increment-expression
| assignment
| if
| while
| for
| func
| func-call
compound_statement: statement; (statement;)*?
-
It has garbage collection for local function variables.
-
It has built in functions: print(), len(), and append()
-
It supports recursion and catches stack overflows
Hello World
print("Hello World");
If statements
num1 = 10;
num2 = 6;
if num1 > num2 {
print("num1 is greater");
}
elif num1 == num2 {
print("num1 and num2 are the same");
}
else {
print("num2 is greater");
}
Loops
array = [];
for (i = 0; i < 10; i++) {
append(array, i);
}
num1 = 10;
num2 = 20;
while num2 > num1 {
num1++;
num2--;
}
print(num1);
print(num2);
Functions and Recursion
func factorial(n) {
if n == 1 {
return n;
}
else {
return n * factorial(n - 1);
}
}
Quicksort
func part(array, low, high) {
pivot = array[high];
i = low - 1;
for (j = low; j < high; j++) {
if array[j] <= pivot {
i++;
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
temp = array[i + 1];
array[i + 1] = array[high];
array[high] = temp;
return i + 1;
}
func quicksort(array, low, high, part) {
if low < high {
index = part(array, low, high);
quicksort(array, low, index - 1);
quicksort(array, index + 1, high);
}
}
somearray = [6, 71, 8, 61, 3, 9, 13];
quicksort(somearray, 0, len(somearray) - 1, part);
Binary Search
func binarysearch(array, n, low, high) {
middle = (high - low) // 2;
if array[middle] == n {
return middle;
}
else {
if array[middle] > n {
return binarysearch(array, n, middle + 1, high);
}
else {
return binarysearch(array, n, low, middle - 1);
}
}
}
-
Type checking and semantic analysis
-
Rewrite in a compiled language