13 December 2015

Calculating in 8th

Today we will have a look at some practical examples of 8th. We will use 8th as a calculator.

In earlier blog entries I created nice pictures to explain the inner workings of the stack.
As I might assume you now have an idea of how the stack works.

The stack in 8th is a data stack. This means that the stack only contains data needed for computations and operations.

In the following examples I will use the 8th prompt "ok>".

Example 1


First we show that the stack is empty in our example case.

.s \ displays the current stack contents
stack empty

ok> 3 4

This means the following:
   1. push the value 3 on the data stack
   2. push the value 4 on the data stack

Let's see what is on the stack now:

ok> .s
2 n: 0000000002ccbe90 1   4
1 n: 0000000002ccbe60 1   3

The output means (per line for each item on the stack):
   1st  value = the index of this item on the stack
   2nd value = the class of this item on the stack (we will talk about that later)
   3rd  value = the memory address of this item on the stack
   4th  value = the reference count of this item on the stack (needed for garbage collection)

Now we are going to use the + word with SED \ + x y -- z 

ok> +

The + word takes the 2 topmost items from the stack and adds them. Then pushes the result back on the stack. Let's check the stack contents again:

ok> .s
1 n: 0000000002ccbec0 1   7

Using the earlier explanation we now see that:
   - items with index 1 and 2 (having values 3 and 4) have disappeared from the stack
   - a new item is now on the stack with the resulting value 7

In 8th is does not matter how you enter these things; you also might have done:
  ok> 3
  ok> 4
  ok> +
or the more common way:
  ok> 3 4 +

Example 2


Now a more complicated example. We assume that the stack is empty when we begin.

ok> 3 4 + 8 * 6 -
ok> .s
1 n: 0000000002ccc400 1   50

Explanation:
   1. 8th pushes in turn the values 3 and 4 on the stack
   2. the + word takes those values, adds them and pushes the result back on the stack,
       so the stack then contains 1 item with value 7
   3. 8th pushes value 8 on the stack,
       so the stack then contains 2 items with values 7 and 8
   4. the * word takes those values, multiplies them and pushes the result back on the stack,
       so the stack then contains 1 item with value 56
   5. 8th pushes the value 6 on the stack,
       so the stack then contains 2 items with values 56 and 6
   6. the - word takes those values from the stack, subtracts the TOS value from the other value
       and pushes the result back on the stack,
       so then the stack contains 1 item with value 50

We mentioned the * and - words here. They have the following SED's:
  * \ x y -- z
      where z = x * y
   - \ x y -- z
      where z = x - y


Conclusion


It is very easy to calculate in 8th. Always keep in mind that the stack should contain the appropriate data when you'd like to evaluate an 8th word.

It is strongly advised to keep the number of items on the stack as low as possible.
Reasons:
   - much easier to reason about what is going on at a given moment
   - the stack has a maximum number of items it can hold

Now you have seen the words for calculation in action, you will be able to understand other 8th words also. Please try it yourself.

Please have a look at the 8th website and grab your free version there: http://8th-dev.com/

07 October 2015

Little steps with 8th

Today I will show you a few more stack operations.

In the following I will always show you an 8th word followed by it's SED (Stack Effect diagram).
Also I will add pictures to enhance the explanation visually.

=======================================================================

drop \ x -- 
The drop word takes an item from TOS (Top Of Stack) and "drops" it. Effectively it just removes that item from the stack.

Stack before drop:                      Stack after drop:
<--- item removed
In the SED we see that x is at TOS before the drop. After the drop it is gone.
In the pictures you see the same pattern. The TOS item (light blue) is removed from TOS.

=======================================================================

2drop \ x y --
The word 2drop does exactly the same as two consecutive drop words.

Stack before 2drop:                    Stack after 2drop:

<---
    2 items removed
<---






Later on in this series I will show you how you could easily define your own words for e.g. 4drop .etc.

=======================================================================

swap \ x y -- y x
This word swaps the two topmost items on the stack.

Stack before swap:                    Stack after swap:

<--->
     2 items swapped
<--->







=======================================================================

over \ x y -- x y x
This word copies the second item on the stack and puts the copy at TOS.

Stack before over:                      Stack after over

<-------------------------
                          |
                          |
> item copied to -





=======================================================================

nip \ x y -- y
This word removes the second item from the stack. It is like drop, but then for the second item.

Stack before nip:                        Stack after nip



]  middle item
]  removed 







=======================================================================

That should be enough for today.
In the next article I hope to write about a bit more advanced 8th words.
If you follow these tutorials along, you will get a thorough knowledge of the language in the end.

For more information please visit: http://8th-dev.com/
There you can also find a very friendly forum, where the developer himself will answer your questions patiently!

There you can also download a completely free version you can play with!

09 September 2015

Tidbits of 8th


Basics


In this post I will show you the very basics of 8th.

8th is a so called stack based language, just like it's predecessor Forth.

What is a stack?

To illustrate that, I've added a picture of a toy which often is used by toddlers.

The pole of the toy has a bottom plate attached to it, so rings cannot be removed at the bottom.

To remove the topmost ring, you just pick it. This is called popping.
So, when you pop the stack, you remove the topmost "item".

When, instead you wish to remove the second ring, you must remove (pop) the first ring first and only after that you can remove (pop) the next ring!

OK. So far the analogy with the toy. In 8th the stack is "virtual", which means that it is an area in main memory where you can stack "items" (where items can be numbers, strings etc.).

More operations on the stack

It would be a real problem if we could only pop an item from the stack.
So there are quite a lot of operations available. We'll see a few here.

Push is the operation to put a new item on top of the stack. In analogy with the toy this would mean we place a new ring upon the topmost one.

Thinking about that, we must realize that this is special in some way. Why? Well assume the length of the pole of our toy is to short and we push a new ring onto it, what would happen?

Same applies for an 8th stack. Space for the stack is reserved in advance. If you run out of stack space we get a nasty error message from 8th, warning that we use too much stack space.

Also be careful when popping the stack. What happens when you pop and there is no stack item left to pop?

So, an 8th programmer should use the 8th stack thoughtfully. If you do so, it won't ever be a problem!

Magic

Here the analogy with the toy must come to a real halt.

In 8th there are stack operations possible which would be uinimaginable for the toy.

For example the dup operation.  The dup operation magically creates a copy of the topmost stack item and pushes that onto the stack. So, after the operation the topmost two items are copies of each other.

By the way: we call the topmost item on the stack TOS (Top Of Stack).

Another magic operation is over. The over operation makes a copy of the one but TOS item and pushes it on TOS.

The 8th notation for stack operations

In 8th we use a special notation for stack operations. These are called SED (Stack Effect Diagrams).

A SED is only a description and not a compiled or interpreted something.
Hence it must start with a \ in order to make it a comment.

The SED for pop is as follows:
        \   x   --
OR
        \   st   x   --   st
Explanation:
     1. the \ character (in 8th terms called the \ word) make the SED an 8th comment
     2. left of the -- are the stack items relevant for this operation before the operation starts;
         this means that there can be much more items on the stack, but they are not relevant now
     3. the -- stands for the operation that is performed (pop in this example)
     4. to the right of the -- are the relevant stack items resulting after the operation

In the latter form of SED st stands for the rest of the stack items (not relevant for this operation).

Example of SED for push:
       \   --   x
OR
       \   st   --   st   x
This means that a new item x will be put on TOS.


Example of over:
      \   x   y   --   x   y   x
OR
      \   st   x   y   --   st   x   y   x
This means that before the operation, first the item x has been pushed onto the stack and after that y has been pushed onto the stack. So y is on TOS when the operation begins.
After the operation we see that x and y are still present on the stack (and in the same order) but that a copy of x has been pushed to TOS. Because the copy of x kind of jumps over y, the operation is called over.

NOTE: the letters or names for stack items in an SED are free to choose. Shorter is nicer, but you can make them a bit meaningful. E.g. numbers could be represented by n1, n2, etc. and strings by s1, s2 etc.

How to use the stack operations in 8th according to their SED?

Example of pop:
      pop
Huh? Yes, that's the only thing we have to write.
Now we need to remember (in our poor head) what it does from the SED of pop.
Effectively the TOS was taken from the stack and the second item became TOS.

Now an example for over, which is a bit more understandable:
      2   3   over
On TOS there is the number 3 and in second position is 2before the operation.
After the operation the second item (2) has been copied and pushed over 3.
That means that the relevant part of the stack now is:   2   3   2

Conclusion

We have seen a few tidbits about 8th and the stack.

If you have any questions feel free to ask here.

Better still is to go to the website of 8th http://8th-dev.com where is also a very friendly forum!

06 September 2015

Why 8th?

Why 8th?

This is always the question. So, why another language again?

First I wil give a very short intro about 8th.

8th is a general purpose language built on the filosophy of the famous language Forth, once created by Charles Moore.

Where Forth shines especially close to the the hardware level, 8th has to offer much more.

First and foremost 8th is a tool to develop native apps for almost any platform, including Android, IOS, Linux and Windows as well as several embedded devices like the Raspberry Pi.

So, why 8th?
  • really multiplatform
  • can be embedded
  • ready for the IOT (Internet Of Things)
  • easy and clean syntax
  • single language (e.g. no mix of Javascript, CSS and HTML)
  • not dependent on a platform like JVM or .NET
  • cheap
  • fast development cycle due to 8th's dual nature (interpreter / compiler)
  • very helpful developer answering quickly
  • friendly community forum
In following blogposts I will try to give short code examples to bring you up to speed with 8th's syntax and filosophy.

Have a look at their website http://8th-dev.com

Disclaimer

This is the first entry of this blog and immediately I need to add a disclaimer ;-)

DISCLAIMER: I am not a successor of the writers of the Gospel: Matthew, Marc, Luke and John.

That said, personally I believe that what they wrote is the truth.

What is this blog about then?

Well, it is all about a new programming language called 8th. You van read more here: http://8th-dev.com

Wish you happy reading in the future!