Factor (programming language)

Factor is a free programming language, which is designed by Slava Pestov and developed since 2003. It takes concepts from Forth, LISP and Smalltalk 80th It is intended as a practical successor of Joy and an implementation is available under the BSD license.

Words and stack

All functions read their arguments from the stack, write their results to the stack and are called words. A program is a sequence of lexical for objects that are stored on the stack and applied the words on the stack.

! comment 2 3 . 2 and 3 are first placed on the stack. Takes two numbers from the stack and sets the sum of the two pushed onto the stack. . takes the top object from the stack and outputs it. Comments begin with! and go to the end of the line.

: Add2 ( n - n ') 2 ; 5 add2 New words are to: define. add2 adds 2 to the top number on the stack.

Words are grouped into vocabularies. With USE: is specified, in which vocabulary words to be searched for and with IN: in which are stored all the vocabulary words following predefined.

It can be any objects such as numbers, sequences (arrays, Vectors, strings, ... ), etc. on the stack. For all object types a lexical representation is defined.

SYMBOL: foo "Hello" foo set foo get Names for variables with SYMBOL: defines set with set with get and read and stored on the stack. Memory for the objects is automatically allocated and deallocated by the garbage collection.

Functional programming

Anonymous functions are written in square brackets and referred to as quotations.

{1 2 3 } [ 3 ] map 10 [" Hello world" print] times 4 [2 ] [ 3 * ] bi 10 0 < [" yes " print ] [ "no " print ] if map takes an array and a quotation from the stack. The quotation is applied to each element of the array and again the results are stored in an array on the stack. times takes a number n and a quotation from the stack. The quotation is then executed n times in a row. bi takes an object and two quotations from the stack. Both quotations are applied to the object and stored the two results on the stack. if takes a truth value and two quotations from the stack. If true, the first quotation is executed, otherwise the second quotation.

Object orientation

The object orientation factor takes concepts from Common Lisp Object System. Classes and methods are defined independently.

TUPLE: rectangle width height; : ( Width height - rectangle ) rectangle boa; GENERIC: area (object - x) M: rectangle area [width >>] [height >>] bi *; 10 20 area For the encapsulation a Tuple class rectangle with the name and the two slots defined width and height. The following defined thereon construction -word reads two numbers from the stack and has the two slots with it. The defined area for rectangle method takes a rectangle object from the stack and calculates the area. If a Tuple object on the stack, the data can be written and read with >> the data slot in the slot of the object with slot. >>

Library

In Factor an extensive library of predefined functions is included. For expansion, there is an interface to C libraries.

Listener

The listener is the interactive development environment of Factor. Everything you enter is read, compiled and optionally run the same. With control -h leads to a comprehensive help and with control -w, you can go through each row in the single-step procedure step by step. All currently stored in the memory definitions and the compiled code is saved with save to an image file.

Implementation

The VM Factor is written in C. The majority of Factor how the parser and the compiler are written even in Factor. There are implementations for FreeBSD, Linux, Mac OS X and Windows as well as the processors x86, x86 -64 and PowerPC. At the beginning of the project the VM was written in Java. This was soon replaced due to technical inferiority by today's C- VM. The implementation for ARM processors is not currently up to date

Self- Hosting

Since Factor is written mainly in self- Factor, first the base system must be created with a boot image. A suitable boot image can be found on the homepage of Factor.

Factor- i = boot. . image The factor.image thereby created is system dependent and contains the loaded every time you start basic system of Factor.

324367
de