Interpreter for kef language. HW - compilers & interpreters
All programs on Kef language start from function "main".
Variable can be assigned with another variable or a number.
[name] = [value | variable];
Variable names must match [a-zA-Z][a-zA-Z0-9]*
regular expression and values must match [0-9]+
x = 5;
b = x;
x123 = 15;
Kef supports conditional expressions. Second if-branch is optional and can be omitted.
if ([statement]) {
...
} else {
...
}
if (a) {
a = a + 1;
} else {
a = a - 1;
}
if (1) {
x = 5;
}
Kef supports for
loops with syntax cimilar to C. It has three optinal blocks:
- pre-action, executed once before loop body
- predicate, conditional expression to determine whether to execute current step of loop
- post-action, executed every time after loop body is executed
for [preaction]; [forpredicate]; [forpostaction] {
//for-body
}
Count sum of numbers from 1 to 5.
n = 5;
sum = 0;
for i = 1; (i - n); i = i + 1 {
sum = sum + i;
}
print(sum);
Functions declarations starts with func
keyword, arguments are passed separated with spaces. All functions return number.
- name must start with [a-z], then can be [a-z] or [0-9]
- args = Variable | Num | Function
- returnValue = Variable | Num | Function
func name(args) {
...
return returnValue;
}
func mul(a b) {
return a * b;
}
func main() {
a = 2;
b = mul(a 10);
return b;
}
print function outputs values to standard output
print([statement]);
a = 10;
b = 15;
func sum(a b) {
return a + b;
}
func main() {
print(5);
print(a);
print(sum(2 a));
}
- Read N from console and calculate factorial of N
func f(n) {
if (n) {
return n * f( (n - 1) );
} else {
return 1;
}
}
func main() {
n = readInt();
print(f(n));
return 0;
}
- Read N from console and calulate N'th Fibonacci number
func is1(n) {
if ((n - 1)) {
return 0;
}
return 1;
}
func is0(n) {
if (n) {
return 0;
}
return 1;
}
func fib(n) {
if (is0(n)) {
return 1;
}
if (is1(n)) {
return 1;
}
return fib((n-2)) + fib((n - 1));
}
func main() {
n = readInt();
m = fib(n);
print(m);
return 0;
}
- Read N from console and print squares of all numbers from 1 to N (inclusive).
func sq(n) {
return n * n;
}
func main() {
n = readInt();
for i = 1; (i - n - 1); i = i + 1 {
print(sq(i));
}
return 0;
}