"Hello world" - run and compile


Console "Hello world":

A "Hello World" that runs on the console was described in the chapter "Setting up a makeshift IDE". Write the code below on Notepad++, save it as "MyFirst.red" in the "programs" folder you created inside your IDE folder and execute it. You should have:

The window on the right is the console, sometimes called REPL. Click there, type print "I can use the console too!" and press enter:

Now type 3 + 7 and press enter:

Notice that you must have a space between words. Spaces are the delimiters and without them you get errors (from now on, I'll show the console ascode text):

Hello world!
>> print "I can use the console too!"
I can use the console too!
>> 3 + 7
== 10
>> 3+7             ;no spaces!!!!!
*** Syntax Error: invalid integer! at "3+7"
*** Where: do
*** Stack: load 
>>

Notice that after 3+7 I wrote ;no spaces!!!!! . Red ignores words that come after a semi-colon, that's how you make comments to your code.

Interpreted programming languages execute one line of code at a time. Programs for interpreted languages are called "scripts". Red is not really interpreted, as it does some compiling before running (sort of), but it's programs are generally called scripts anyway.

Back to the program (aka. script):

On the first line we have Red [ ]. As we mentioned before, every Red script must start with Red, Not RED nor red, but Red. Following Red we have brackets. In Red, anything inside brackets is called a "block". This first block is intended to contain information about your program. This information is mostly optional with a few exceptions, the most relevant being the declaration of libraries (more on that in a while).

A nice first block would be:

"Hello world" with graphic user interface - GUI:

One of the most striking features of Red is it's easy-to-use graphic interface. It makes a very clever use of the Operating System's own graphic APIs. A simple "Hello world" with GUI would be:

Notice I wrote needs: 'view in the header block. That tells Red to load the "view" graphic library.

Compiling your "Hello world" to an executable file:

You can create an executable from your GUI "Hello World".

If you saved the GUI program above as "MyFirst.red" in the "programs" folder of you makeshift IDE, you should have something like this in your computer:

For the sake of clarity, make a copy of your Red executable and paste it in the same folder as your program, otherwise the results of the compilation will be in the RedIDE folder, lost among all those files.

Open the Command Prompt window. If you don't know how, write "cmd" in Window's search field and click on the Command Prompt icon:

In the Command prompt, type the path to your Red executable (the executable you just copied in the "programs" folder), followed by -r -t windows and the name of your program:

Red will give you a series of messages in the Command Prompt and, after about a minute you will have the standalone executable in your "programs" folder:

Double click on it and you will have your GUI "Hello World" message on your screen.

Extra notes on compiling:

I found that the compiled version of a program may not behave as the interpreted one. I had problems with "print" statements I left for debugging, so I guess calling console commands in executable mode is not OK. I also had problems with global variables inside functions, the compiler does not seem to recognize them as global variables. I solved this problem in two different ways:

  1. I "declared" my variables, that is: I assigned values to them in the beginning of my program. The values are not important, as they change later.
  2. I used the "-e" compiler option (not listed in github, as it seems to be experimental). (" -e means "encap" which works like Rebol did. So you get a single EXE but all code is internally interpreted, so any issues that are compiler limitations can be worked around" - Gregg Irwin.)

You could compile the MyFirst.red program using only the -c (compile) option:

You will then have the following files in your "programs" folder:

The only two files you need to run your program are the libRedRT.dll and your program's executable, in this case MyFirst.exe.

However, when your run your executable, you will notice that Red keeps a very annoying Command Prompt window open as the program runs. If you want to avoid this (who doesn't?) use the target option -t. The option -t compiles it to a specific platform.

It will result in those same extra files, including the dll, but it won't open the Command Prompt while your program runs.

You should be able to compile to the platforms listed below but, as of this writing, Red is still evolving, and you may find some (few) issues (e.g. compiling to android does not seem to work yet).

From Red's github:

Cross-compilation targets:
MSDOS        : Windows, x86, console (+ GUI) applications
Windows      : Windows, x86, GUI applications
WindowsXP    : Windows, x86, GUI applications, no touch API
Linux        : GNU/Linux, x86
Linux-ARM    : GNU/Linux, ARMv5, armel (soft-float)
RPi          : GNU/Linux, ARMv5, armhf (hard-float)
Darwin       : macOS Intel, console-only applications
macOS        : macOS Intel, applications bundles
Syllable     : Syllable OS, x86
FreeBSD      : FreeBSD, x86
Android      : Android, ARMv5
Android-x86  : Android, x86
Compiler options:
-c, --compile                  : Generate an executable in the working
                                 folder, using libRedRT. (developement mode)

-d, --debug, --debug-stabs     : Compile source file in debug mode. STABS
                                 is supported for Linux targets.

-dlib, --dynamic-lib           : Generate a shared library from the source
                                 file.

-h, --help                     : Output this help text.

-o <file>, --output <file>     : Specify a non-default [path/][name] for
                                 the generated binary file.

-r, --release                  : Compile in release mode, linking everything
                                 together (default: development mode).

-s, --show-expanded            : Output result of Red source code expansion by
                                 the preprocessor.

-t <ID>, --target <ID>         : Cross-compile to a different platform
                                 target than the current one (see targets
                                 table above).

-u, --update-libRedRT          : Rebuild libRedRT and compile the input script
                                  (only for Red scripts with R/S code).

-v <level>, --verbose <level>  : Set compilation verbosity level, 1-3 for
                                 Red, 4-11 for Red/System.

-V, --version                  : Output Red's executable version in x.y.z
                                 format.

--config [...]                 : Provides compilation settings as a block
                                 of `name: value` pairs.

--cli                          : Run the command-line REPL instead of the
                                 graphical console.

--no-runtime                   : Do not include runtime during Red/System
                                 source compilation.

--red-only                     : Stop just after Red-level compilation.
                                 Use higher verbose level to see compiler
                                 output. (internal debugging purpose)

There is also -e option. See note above.

results matching ""

    No results matching ""