|Home | Download | Screenshots | Contact|
ILYC is an interpreted programming language with a simple and friendly syntax, which is suitable for beginners and hobbyist. The purposes of creating such a language was for the author to practice programming and for others to study how to implement an interpreter in the .NET framework.
The first example
The following code demonstates a simple program that shows a message, "Hello World!", to the user:
function void main() do puts("Hello World!") done
In the given code, there is a function with the name main. This is the main function of the program and will be called automatically when the program runs.
Note: ILYC is a case-sensitive language, which means capital and small letters are considered different. Thus, if you write the main function as Main or MAIN, it will NOT be recognized.
In our first example, there is only one instruction in the main function: puts("Hello World!"). This is a function call, meaning it calls a function defined somewhere to accomplish some task. In this case, the function called is puts, which will show the given argument on the screen. Since we gave it the text "Hello World!", it should show "Hello World!" as expected.
One more thing to say here is that the main function is of void type. Therefore it does not return any value. That is a rule for the main function, but if you define another function, you can use other data type. This will be discussed more later.
Maybe you need to store the message somewhere so that you can re-use it. This is done by declaring a variable and store your text in it:
function void main() do string msg = "Hello World!" puts(msg) done
Here we have declared a variable called msg. Because it is used to store a text, we should typed it a string. A string is a data element that stores characters.
Say you want a function that show a greeting message including the user's name. One solution is to define a function to ask for the user's name, and another to show the message. Please take a look at the code below:
function string askname() do print("May I have your name: ") return reads() done function void greeting(string name) do puts("Hello " + name + "!") done function void main() do string name = askname() greeting(name) done
Oh my! A lot of code! But don't worry, you will soon understand all of them.
Now we have three functions. The first is askname, which will ask for the user's name. The second is greeting, which will greet the user. And the last is the main function. You can always change their order since the interpreter looks for all definitions before executing the program. So it knows what you are mentioning.
In askname(): we should politely ask the user for his/her name by using the print function. This function is slightly different from puts in that it does not include a newline character, which helps the prompt look more pretty. Then we used reads to read user input. The user needs to press Enter after typing the name. Since askname is typed string, it should return a string. That string was got from reads as you can see.
In greeting: this function is a void function, and it needs one string argument called name. All it does was concatenating the argument with a greeting message.
Finally, in the main function, we declared a variable, name to store the returned value of askname and passed it to greeting.
Playing with numbers
Now you have met strings. Let's make friends with numbers. In ILYC, there are two data types for numbers. The first one, called int, is for whole numbers. The other, called float, is for real numbers.
function void main() do int a = 10 int b = 20 puts(a + b) done
As you can see, the code above declared two int variables, initialized their values and finally showed their sum.
Please look at another example:
function void main() do int a = 9 int b = 2 puts(a / b) done
What did you expect to see? I guess that was 4.5. But you saw 4 instead. So what happened? The reason is that the operation between two integers, a and b, is an integer division. To get a real division, at least the dividend must be a real number.
function void main() do float a = 9 int b = 2 puts(a / b) done
Now you have the correct answer: 4.5.
There are far more features in ILYC, which can not be demonstrated enough on this page. Please read the documentation included in the software package for more details.
|Copyright © 2010-2011 AnhSoftware. All rights reserved.|