23 January 2016

Lesson 11 – 8th general program structure

More than a REPL is needed

So far we only have shown the REPL, the interactive 8th console.

In order to create complete programs we need to expand our possibilities a bit.

Using a text editor

We will need a (simple) text editor to write an 8th program.

These days there are all kinds of advanced tools, like IDE’s (Interactive Development Environment). However, for our purposes we only need a simple editor.

The editor I am using is called Textpad and costs a little. I also created a simple, but nice looking 8th syntax highlighter for it. You can ask in the 8th forum about this.

A few possible (free) editors (in no specific order):


NOTE stay away from text processing programs like MS Word, Wordpad, Open Office, Libre Office etc. Those “editors” make heavy use of markup language. But we need a strict plain text editor for programming purposes!

Program structure in 8th


  • if we reference a variable x from within a word w we have defined, that variable x must have been defined before the definition of word w
  • if we reference a word w1 from within a word w2 we have defined, that word w1  must have been defined before the definition of word w2 (there is a special case (deferred words) which we will explain later)
  • at the end of a program you should define the special word app:main which is the word invoked automatically when your 8th program starts
  • the app:main definition should normally end with the bye word. For this rule there are 2 valid exceptions:
    • in GUI apps quitting 8th will be done elsewhere in the program; you must leave out bye there
    • for debugging reasons; from version 16.01 onwards there will be a word available to start the REPL; you could then use that word, instead of bye to debug your program

Work flow


  1. open your text editor
  2. open a command prompt / terminal
  3. switch to the directory where your source is stored
  4. create, add or modify a small piece of code in the text editor; never change big pieces of your program at once, because then it is very difficult to track bugs
  5. start your program in the command prompt / terminal:    8th   progname.8th
  6. check the results of your program
  7. go back to step 4

Our first program

In the text editor I now show you the first (very simple) program:


We then open a Command Prompt or Terminal, change to the directory where the source is located and then start the program:

Namespaces revisited

In the program we used the with: word. Doing so, 8th will search for words and variables  in this namespace as well.

If you don’t use with: you will need to qualify the defined words / variables in such a namespace. In this example case: @arie.space:squared

NOTE that it is possible to have the same word or variable name in different namespaces. In that case the word found first (in the search order of 8th) will prevail!

Conclusion

Of course this is just the beginning. You should get acquainted with this stuff and try out small snippets of code.

Do not hesitate to sprinkle .s words throughout your code. That way you can easily see what is happening on the stack!

More info about 8th

There is a central website for 8th here: http://8th-dev.com/

Video

You can watch the complementary video here: https://youtu.be/SmiRwTNWkSs

No comments:

Post a Comment