Factor

from Wikipedia, the free encyclopedia
Factor
Paradigms : multi-paradigmatic
Publishing year: 2003
Designer: Slava Pestov
Developer: Slava Pestov
Current  version : 0.98   (July 31, 2018)
Typing : dynamic
Influenced by: Forth , Lisp , Smalltalk , Joy
License : BSD license
factorcode.org

Factor is a free programming language designed by Slava Pestov and developed since 2003. It adopts concepts from Forth , Lisp and Smalltalk-80 . It is intended to be the practical successor to Joy and an implementation is available under a BSD license .

Words and Stack

All functions read their arguments from the stack , write their results to the stack and are referred to as words . A program is a sequence of lexicals for objects that are placed on the stack and words that are applied to the stack.

! Kommentar
2 3 + .

First, 2 and 3 are put on the stack. +  takes two numbers off the stack and puts the sum of the two on the stack. .  takes the top object from the stack and prints it out. Comments start with  ! and go to the end of the line.

: add2 ( n -- n' ) 2 + ;
5 add2

New words are defined with :. add2 adds 2 to the top number on the stack.

Words are summarized in vocabularies . With USE: is indicated, in which searched vocabulary words and IN: in which all defined vocabulary words following are stored.

Any objects such as numbers, sequences (arrays, vectors, strings, ...) etc. can be placed on the stack. A lexical representation is defined for all object types.

SYMBOL: foo
"Hello" foo set
foo get

Names for variables are defined with SYMBOL:, set with set and read out with get and stored on the stack. Memory for the objects is automatically allocated and released again by garbage collection .

Functional programming

Anonymous functions are written in square brackets and are 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 the results are returned to an array on the stack. times takes a number n and a quotation from the stack. The quotation is then carried out n times in a row. bi takes an object and two quotations from the stack. Both quotations are applied to the object and the two results are placed on the stack. if takes a truth value and two quotations from the stack. If the value is true , the first quotation is carried out, otherwise the second quotation.

Object orientation

When it comes to object orientation, Factor adopts concepts from the Common Lisp Object System . Classes and methods are defined independently of one another.

TUPLE: rectangle width height ;
: <rectangle> ( width height -- rectangle ) rectangle boa ;

GENERIC: area ( object -- x )
M: rectangle area [ width>> ] [ height>> ] bi * ;

10 20 <rectangle> area

For data encapsulation , a tuple class with the name rectangle and the two slots width and height is defined. The following construction word <rectangle> reads two numbers from the stack and occupies the two slots with them. The area method defined for rectangle takes a rectangle object from the stack and calculates the area. If there is a tuple object on the stack, the data can be read out with slot >> and the data can be written into the slot of the object with >> slot .

Library

A comprehensive library of predefined functions is included in Factor . There is an interface to C libraries for extensions.

Listener

Factor Listener 0.92

The listener is Factor's interactive development environment . Everything you enter is read, compiled and, if necessary, executed immediately. With control-h you get to comprehensive help and with control-w you can step through each line in a single-step process. All definitions currently in memory and the compiled code are saved in an image file with save .

implementation

The VM of Factor is C written. Most of Factor, like the parser and compiler, is written in Factor itself. There are implementations for FreeBSD , Linux , macOS 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 by today's C-VM due to its technical inferiority.

Self hosting

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

$ factor -i=boot.<cpu>.image

The created factor.image depends on the system and contains the basic system of Factor, which is loaded at every start.

Web links

Individual evidence

  1. Factor / Java Factor ( en ) In: concatenative.org . Retrieved May 23, 2012.