And one more blog post by me! It’s going to be in brand new topic than the others. For about an year and a half I’ve got an idea which I wanted to accomplish…To create my own simple programming language. Actually it’s very useful task and quite interesting. Creating it you can get better understanding how the compilers and interpreters work and probably write better code. Unfortunately there were two problems…I didn’t have enough time for such task and…I’ve not studied formal grammars (and as I mentioned I don’t have enough time to study them…there are just so much more interesting things to read about :-P ). Last weekend I wasn’t in very good health condition. I think that I have a tradition to make different cool stuffs when I don’t feel very well :-)…Soo for two days I’ve wrote about forty Java classes which actually were ELang.
I opened my Java IDE (I decided to not write the language in JavaScript) and I created few different classes…Actually I had idea to create something very basic, just variables, if, while, number expressions and boolean expressions. Oh…I almost forgot to tell that my language is interpreted so that’s why I decided to have three different stages in the execution process: lexing, parsing and interpreting. The lexing process was straight-forward so there’s nothing interesting about it…I’ve just created different tokens from the long string which is the actual program. Here’s more information about that https://en.wikipedia.org/wiki/Tokenization. After I finished with the lexing…ahh after that…it was the Parsing…Actually that was the process which has been scaring me :-). I’ve created an interface IStatement which was something that has a void method called “execute”. Here’s the interface:
public interface IStatement {
void execute();
}
public interface IExpression {
Value evaluate();
}
public void interpret() {
Iterator iter = this.statements.iterator();
IStatement current;
while (iter.hasNext()) {
current = iter.next();
current.execute();
}
}
I also want to have interaction with the user. So there are also implementations of read and print methods. That’s why I’ve created special kind of IExpression (actually there’s nothing special but just separated in different package :-) ). The print function is a list of IExpressions. After the list is evaluated everything is just put on the user’s screen…Here is the implementation:
public Value evaluate() {
Value result = this.toPrint.evaluate();
System.out.print(result.getValue());
return result;
}
Easy, right? With this kind of language I’ve achieved programs like that:
def fibonacci(n)
if n >= 0:
a1 = 0;
a2 = 1;
current = 0;
while n > current:
temp = a2;
a2 = a1 + a2;
a1 = temp;
current = current + 1;
endwhile;
result = a1;
else:
result = 0;
endif;
return result;
enddef;
def factorial(n)
result = 1;
current = 1;
while current <= n:
result = result * current;
current = current + 1;
endwhile;
return result;
enddef;
print 'Enter n for fibonacci number: ';
n = read;
print 'The result for the fibonacci number entered is: ';
print fibonacci(n);
print '\n';
print 'Enter n for factorial: ';
n = read;
print 'The result for the number you entered for factorial is: ';
print factorial(n);
print '\n';
It also allows recursion:
def recursiveFibonacci(n)
if n == 0:
return 0;
endif;
if n == 1:
return 1;
endif;
return recursiveFibonacci(n - 1) + recursiveFibonacci(n - 2);
enddef;
The github repository can be found here: https://github.com/mgechev/ELang/
After I finished with that I have a desire to improve it a little…Including feature for importing external files will be very useful and may make the language even useful :D. The language can be extended to object-oriented easy by adding hash. If the hash values can be primitives, other hashes and methods this extension can be accomplished without much troubles.
Actually now ELang is quite useful for writing simple programs and teaching beginner programmers how to write different basic constructs but it’s potential ends very quickly after these programmers learn how to use if, while, print, read and declare different functions. :-).