13 January 2016

Lesson 6 – A look at datatypes and namespaces in 8th


Updated version of this lesson


Because of a few breaking changes in 8th, this lesson uses some new specs:

  • before 16.02 new words and variables were stored by default in class G 
  • from 16.02 onward they will be stored in the default namespace called user
  • Before 16.03 the term class  and cls: was used
  • From 16.03 onward we use the term namespace and namespace ns: instead

A last remark on calculations

In the previous lesson I used the term operator for the + - * and / word. As far as 8th is concerned these words are not special. Those words just happen to act as operators.

Datatypes

So far we have only used (whole) numbers in our examples.

In 8th there are a few basic datatypes:

  • Numbers;    some examples:
    • -11.3   gives -11.30000 (float)
    • 22.3e-3 gives   0.02230 (e notation)
    • 22.3E-3 gives   0.02230 (e notation)
    • 12      gives   12      (integer)
    • 0xff    gives  255      (hex)
    • 0Xfe    gives  254      (hex)
    • $10     gives   16      (hex)
    • &77     gives   63      (octal)
    • %1111   gives   15      (binary)
    • #123    gives  123      (# forces decimal base)
  • Strings like “The big brown fox”. The \ (backslash) may be used as an escape character for:
    • "       double-quote    ASCII 34
    • a       alarm           ASCII  7
    • b       backspace       ASCII  8
    • f       formfeed        ASCII 12
    • n       newline         ASCII 10
    • r       carriage-return ASCII 13
    • t       tab             ASCII  9
    • v       vertical tab    ASCII 11
    • x       next 2 chars are hex digits (\x20 is space)
    • u       next 4 chars are hex digits (\u201c is )

But there are a lot more (and more advanced) datatypes as well.

8th namespaces

As you may know, a lot of programming languages have namespaces.

A namespace is what it says, a separate space for names. That means that you can use the same name for different words or variables, but then they have to be in different namespaces.

In 8th, when you define a word or create a variable, these will always be defined in some namespace.

If you don’t specify in which namespace, they will be stored in the default namespace named user.

An example to clear the fog

The datatype number belongs to the namespace n.

All words related to numbers are grouped together in that namespace. That means the following:
words like   +   -   *   etc. are defined in the n namespace
those words are actually the words   n:+   n:-   n:*
the n is the namespace, the colon : separates the word from the namespace
this allows that there are other words called   +   *  -  in another namespace!

An example of a word having the same name in another namespace is   s:+   The namespace s is the “string” namespace and   s:+   happens to be string concatenation.

Standard namespaces in 8th

There are a lot of namespaces already defined in 8th. We will encounter them later. Have a look in the 8th manual!

You, being the developer may freely add new words to an existing namespace.

User defined namespaces in 8th

The developer may also freely define new namespaces.

That way a neat way exists to separate different pieces of code and avoid clutter and duplicate names.

This is especially important because 8th does not have the concept of local variables. You could say: namespaces are the 8th way of having local variables.

Intermezzo

When we wish to print the item at TOS (Top Of the Stack) we can use the  .  (dot) word.

In order to print a newline we use the   cr   word.

Without cr the next prompt is “glued” to the output.



With cr the line is terminated neatly.




Representation of a namespace

Internally a namespace is represented by a unique number.

It is easy to determine the namespace for a given item using the word >kind like so:

  • 1 >kind . cr     \ gives 1 (represents the n namespace)
  • “abc” >kind . cr \ gives 2 (represents the s namespace)

More info about 8th

There is a central website for 8th here: http://8th-dev.com/
Correspondig videos: https://goo.gl/DscYVD

No comments:

Post a Comment