Everything should be as simple as it is, but not simpler.Albert Einstein
In this first tutorial I will be showing you how to write a simple Hello, world! program. You probably won't find this hard, certainly if you already now another programming language.
The only thing we must do is to use a system subroutine to show some text on the terminal and then we must terminate the program. This can be done using the following 2-line program:
!out(~"Hello, world!");The first statement(the first line) calls(executes) the subroutine(also know as module, procedure, function, method(inside a class) or routine) named "out" with one parameter: our text. All text(strings) in TBS are surrounded by double quotes and in most situations, we use something called the string operator to let TBS know that it's dealing with a string. The string operator is the '~' character. So we precede "Hello, world!" with ~ and place it between () behind the name of the subroutine. A parameter or argument is a value that we pass to a subroutine.
_die();
The second statement(the second line) calls the language construct named die. Language constructs in TBS are some sort of subroutines that allow the manipulation of the stream of data that is send to the TBS interpreter. The die language construct terminates the program. If you run this program you will notice that it outputs "Hello, world!" as we wanted or maybe not exactly as we wanted. When we meant writing "Hello, world!" to the terminal we really meant writing "Hello world!" followed by a new line character. But how do we represent a newline character in a string? We could write:
!out(~"Hello, world!But that's not very useful. This is when we use escape sequences. An escape in TBS starts with a '\'. For a newline we use "\n" in our string. So we can write the script like this:
");
_die();
!out(~"Hello, world!\n");That all works fine, but it's a bit boring. It would be much more fun if we could ask our user's name, and then display the name instead of "world". For this, we can use a subroutine called "in". This subroutine reads for characters until the user of our program presses enter. Before we ask input, we should probably notify the user that we are asking for input. So we could write our program like this:
_die();
!out(~"Please enter your name: ");You may have noticed that the second statement is completely new. We create a new variable here. A variable is a name we give to a place in your computer's memory. In this place we store a value. Creating a new variable is often called declaring a variable. Thus, we speak of the declaration here. But we do not only declare a variable, we also give this variable a value. This is called the assignment or initialization of a variable. Where does this value come from? As you might already have guessed, it comes from the "in" subroutine that we call. In fact, this subroutine returns a value. This value is (logically) called the return value. Again, we must to use the string operator('~') after the assignment operator('=') and before every argument because we want to show TBS that we are dealing with a string value. The subroutine "in" always returns a string value. Also note that "in" doesn't require any arguments.
?name =~ !in();
!out(~"Hello, ", ~name, ~"!\n");
_die();
You may also have noticed that on the third line, we now pass 3 arguments to "out". "out" is a special subroutine because it accepts an unending number of arguments. Instead we could also use '+' to concatenate all of the values like this:
!out(~"Please enter your name: ");In this case, we only need to use the string operator once in the arguments list, because we only have one argument.
?name =~ !in();
!out(~"Hello, " + name + "!\n");
_die();
Congratulations, you have written your first TBS program. If you have any questions, make sure to comment or to send an email.
No comments:
Post a Comment