Factor is a
stack-oriented programming language
Stack-oriented programming, is a programming paradigm which relies on a stack machine model for passing Parameter (computer programming), parameters. Stack-oriented languages operate on one or more Stack (data structure), stacks, each of which ...
created by Slava Pestov. Factor is
dynamically typed and has
automatic memory management, as well as powerful metaprogramming features. The language has a single implementation featuring a self-hosted
optimizing compiler and an
interactive development environment
An integrated development environment (IDE) is a software application that provides comprehensive facilities to computer programmers for software development. An IDE normally consists of at least a source code editor, build automation tools a ...
. The Factor distribution includes a large
standard library
In computer programming, a standard library is the library made available across implementations of a programming language. These libraries are conventionally described in programming language specifications; however, contents of a language's as ...
.
History
Slava Pestov created Factor in 2003 as a
scripting language for a
video game. The initial implementation, now referred to as JFactor, was implemented in
Java and ran on the
Java Virtual Machine
A Java virtual machine (JVM) is a virtual machine that enables a computer to run Java programs as well as programs written in other languages that are also compiled to Java bytecode. The JVM is detailed by a specification that formally describes ...
. Though the early language resembled modern Factor superficially in terms of
syntax
In linguistics, syntax () is the study of how words and morphemes combine to form larger units such as phrases and sentences. Central concerns of syntax include word order, grammatical relations, hierarchical sentence structure ( constituency) ...
, the modern language is very different in practical terms and the current implementation is much faster.
The language has changed significantly over time. Originally, Factor programs centered on manipulating Java objects with Java's
reflection capabilities. From the beginning, the design philosophy has been to modify the language to suit programs written in it. As the Factor implementation and standard libraries grew more detailed, the need for certain language features became clear, and they were added. JFactor did not have an
object system
Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of pro ...
where the programmer could define their own
class
Class or The Class may refer to:
Common uses not otherwise categorized
* Class (biology), a taxonomic rank
* Class (knowledge representation), a collection of individuals or objects
* Class (philosophy), an analytical concept used differentl ...
es, and early versions of native Factor were the same; the language was similar to
Scheme A scheme is a systematic plan for the implementation of a certain idea.
Scheme or schemer may refer to:
Arts and entertainment
* ''The Scheme'' (TV series), a BBC Scotland documentary series
* The Scheme (band), an English pop band
* ''The Schem ...
in this way. Today, the object system is a central part of Factor. Other important language features such as
tuple classes, combinator inlining,
macro
Macro (or MACRO) may refer to:
Science and technology
* Macroscopic, subjects visible to the eye
* Macro photography, a type of close-up photography
* Image macro, a picture with text superimposed
* Monopole, Astrophysics and Cosmic Ray Observat ...
s, user-defined
parsing words and the modern vocabulary system were only added in a piecemeal fashion as their utility became clear.
The
foreign function interface was present from very early versions to Factor, and an analogous system existed in JFactor. This was chosen over creating a plugin to the
C part of the implementation for each external
library that Factor should communicate with, and has the benefit of being more
declarative, faster to compile and easier to write.
The Java implementation initially consisted of just an
interpreter, but a compiler to
Java bytecode was later added. This compiler only worked on certain procedures. The Java version of Factor was replaced by a version written in C and Factor. Initially, this consisted of just an interpreter, but the interpreter was replaced by two compilers, used in different situations. Over time, the Factor implementation has grown significantly faster.
Description
Factor is a
dynamically typed,
functional
Functional may refer to:
* Movements in architecture:
** Functionalism (architecture)
** Form follows function
* Functional group, combination of atoms within molecules
* Medical conditions without currently visible organic basis:
** Functional sy ...
and
object-oriented programming language. Code is structured around small procedures, called words. In typical code, these are 1–3 lines long, and a procedure more than 7 lines long is very rare. Something that would idiomatically be expressed with one procedure in another programming language would be written as several words in Factor.
Each word takes a fixed number of arguments and has a fixed number of return values. Arguments to words are passed on a
data stack, using
reverse Polish notation. The stack is used just to organize calls to words, and not as a datastructure. The stack in Factor is used in a similar way to the stack in
Forth
Forth or FORTH may refer to:
Arts and entertainment
* ''forth'' magazine, an Internet magazine
* ''Forth'' (album), by The Verve, 2008
* ''Forth'', a 2011 album by Proto-Kaw
* Radio Forth, a group of independent local radio stations in Scotla ...
; for this, they are both considered
stack languages. For example, below is a snippet of code that prints out "hello world" to the current output stream:
"hello world" print
print
is a word in the
io
vocabulary that takes a string from the stack and returns nothing. It prints the string to the current output stream (by default, the terminal or the graphical listener).
The
factorial function
In mathematics, the factorial of a non-negative denoted is the product of all positive integers less than or equal The factorial also equals the product of n with the next smaller factorial:
\begin
n! &= n \times (n-1) \times (n-2) \t ...
can be implemented in Factor in the following way:
: factorial ( n -- n! ) dup 1 > [1,bproduct ">[1,b.html" ;"title="[1,b">[1,bproduct [ drop 1 ">,b.html"_;"title="[1,b">[1,b<_a>product_.html" ;"title="[1,b.html" ;"title="[1,b">[1,bproduct "> [1,bproduct [ drop 1 if ;
Not all data has to be passed around only with the stack. Lexical scoping">Lexically scoped
In computer programming, the scope of a name binding (an association of a name to an entity, such as a variable) is the part of a program where the name binding is valid; that is, where the name can be used to refer to the entity. In other parts o ...
variables are used to pass things between procedure calls without using the stack. For example, the current input and output streams are stored in dynamically scoped variables.
Factor emphasizes flexibility and the ability to extend the language.
There is a system for macros, as well as for arbitrary extension of Factor syntax. Factor's syntax is often extended to allow for new types of word definitions and new types of literal (computer science)">literal