You can edit almost every page by Creating an account and confirming your email.

8th (programming language)

From EverybodyWiki Bios & Wiki


8th
ParadigmProcedural, stack-oriented, concatenative
Designed byRon Aaron
First appeared2013; 13 years ago (2013)
Typing disciplinetyped
Website{{#property:P856}}
Influenced by
Forth, Reva Forth

Search 8th (programming language) on Amazon.

8th is an imperative stack-based computer programming language and programming environment. Language features include structured programming, reflection (the ability to modify the program structure during program execution), concatenative programming (functions are composed with juxtaposition) and extensibility (the programmer can create new commands). It has a small but enthusiastic and growing user community[1][2].

Overview

8th combines the compiler with an interactive shell, where the user defines and runs subroutines called "words". Words can be tested, redefined, and debugged as the source is entered without recompiling or restarting the whole program. All syntactic elements, including variables and basic operators are defined as words.

Stacks

8th does not pass parameters in the manner of most languages; rather, like Forth, it uses a "data stack" to provide input to words and to accept their output. Likewise, parameters are not given names. 8th initially provides two stacks: the "data stack" already mentioned, and the "r-stack" which is simply an auxiliary stack.

Due to the passing of parameters via the data-stack, 8th words are inherently reentrant (unless one uses a "variable").

Most words are specified in terms of their effect on the stack. Typically, parameters are placed on the top of the stack before the word executes. After execution, the parameters have been erased and replaced with any return values. For arithmetic operators, this follows the rule of reverse Polish notation. See below for examples illustrating stack usage.

Differences from standard Forth

While quite similar to Forth, 8th differs from it in several ways:

  • It does not have the CREATE... DOES> construct
  • It is strongly typed, with a unique class system
  • It is designed to be cross-platform (same exact code can run on different platforms)
  • It is designed to be secure: there is no direct memory allocation or access, nor is there any built-in assembler
  • It is designed to protect user’s IP by encrypting deployed executables
  • The "r-stack" has no connection to return addresses, and modifying it is always safe
  • All items (classes, data types) occupy only one space on the stack (or in a variable)
  • All items on the stack are references, and duplicating an item merely provides a second reference to the item
  • Numbers automatically convert between integer, float and “big” versions of the same

Specifics

Platforms

8th currently supports the following platforms:

  • Windows desktop (32 and 64 bit)
  • OS X (32 and 64 bit)
  • Linux (Ubuntu derivatives, 32 and 64 bit)
  • Android
  • iOS (32 and 64 bit)
  • Raspberry Pi (and similar ARM-based Linux)

Syntax

In common with Forths in general, 8th has a very free-form reverse Polish notation (RPN) syntax, with no reserved words. However, the parser does recognize certain constructs specifically:

  • The use of JSON will cause an appropriate “object” or “array” to be defined
  • As in JSON, arrays and objects may contain any other data type
  • Strings are declared using an opening double-quote character, and may contain “escapes” just as in the C language
  • “classes” in 8th are essentially namespaces, and are typically denoted by invoking the class name, followed by a colon character, and then the name of the word to invoke
  • Numbers may be prefixed with certain characters to change the base being used for that number

As an example:

 \ Declare an array with three numeric elements (the last is 16 decimal, 10 hex)...
 [1,2,$10]  
 \ and access element #1 of the array and print it (the “a:” accesses the “@” word in the array class)
 1 a:@ .     
 \ Declare a string having two lines
 "one\ntwo"  
 \ Declare an object ...
 {"one":1, "two":2, "arr" : [1,2,3]} 
 \ ... and access the value for key "one" in the object (the “o:” accesses the “@” word in the object class)
 "one" o:@ . 

Strings

All strings are UTF-8 encoded, allowing applications to use text from all spoken languages. As mentioned above, the syntax for declaring strings is much the same as for the C language, though 8th includes some extensions (such as Unicode escapes).

As part of the standard string support, 8th includes facilities for L10N making it simple to support multiple languages in the same application.

Garbage collection

In order to enhance security as well as make programming easier, 8th uses reference counting garbage-collection. When an item (number, string, etc.) is put on the stack or inserted into a variable, its reference-count is incremented. When it is removed from the stack or the variable, its reference-count is decremented. When the reference-count becomes zero, the item is deallocated and its resources released.

When the dup word is invoked, a second reference to the item being duplicated is put on the stack. Changes to that item will then change the original item. In order to break that dependency, one uses the word clone, which creates a new item with the same value as the original.

Variables

Variables are always global in scope, and there are several varieties of containers available. All containers may contain any kind of item.

The most simple is the var, which is a single-item variable (e.g. a container which holds just one item at a time). As mentioned above, when an item is placed in a var (or any other container), its reference-count is increased.

Other variable containers include:

  • arrays - which are dynamically sized containers accessed by numeric index
  • objects - which are dynamically sized containers accessed by string index. They are the equivalent of associative arrays or maps in other languages.
  • stacks - fixed-size containers implementing LIFO access
  • queues - fixed-size containers implementing FIFO access
  • heaps - dynamically sized containers implementing sorted access

Code samples

The ubiquitous “hello world” in its simplest form:

 "Hello, world!\n" . bye

Factorial using iteration:

 : n!
   1 ' n:* rot 
   1 swap loop ;
 5 n! . cr

(prints 120 as you might expect)

Split a string into an array:

 "One;two;three"
 ";" s:/

(produces the array ["One","two","three"])

External links

References


This article "8th (programming language)" is from Wikipedia. The list of its authors can be seen in its historical. Articles copied from Draft Namespace on Wikipedia could be seen on the Draft Namespace of Wikipedia and not main one.