Factor (programming language): Difference between revisions

Content deleted Content added
LittleDan (talk | contribs)
No edit summary
m Bot: http → https
 
(221 intermediate revisions by more than 100 users not shown)
Line 1:
{{Short description|Stack-oriented programming language}}
{{Primary sources|date=July 2019}}
{{Infobox programming language
|name = Factor
|logo = [[Factor_iconFile:NewFactorLogo.png|200px]]
|paradigm = [[multi-paradigm programming language|multi-paradigm]]: [[concatenative programming language|concatenative]] ([[stack-oriented programming|stack-based]]), [[functional programming|functional]], [[object-oriented programming|object-oriented]]
|paradigm = stack-based
|year = [[2003]]
|developer = [[Slava Pestov]]
|latest_release_version = 0.88100
|latest_release_date = [[February{{start 28]], [[2007]]date|2024|9|11}}
|typing = [[strong typing|strong]], [[dynamic typing|dynamic]]
|influenced_by = [[Joy (programming language)|Joy]], [[Forth (programming language)|Forth]], [[Lisp (programming language)|Lisp]], [[Self (programming language)|Self]]
|influenced =
|operating_system = [[Microsoft Windows|Windows]], [[Mac OSmacOS]], [[Linux]], others
|license = [[BSD license]]
|website = [http://factorcode.org/ factorcode.org]
}}
 
'''Factor''' is a [[stack-oriented programming language]] 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 [[compiler optimization|optimizing compiler]] and an [[Integrated development environment|interactive development environment]]. The Factor distribution includes a large [[standard library]].
'''Factor''' is a dynamically typed [[concatenative programming language|concatenative]] [[programming language]] whose design and implementation is led by [[Slava Pestov]]. Factor's main influences are [[Joy programming language|Joy]], [[Forth (programming language)|Forth]], [[Lisp programming language|Lisp]] and [[Self programming language|Self]].
 
==History==
Like other concatenative languages, Factor has a [[reverse Polish notation|postfix]] syntax, meaning that you write the arguments of a function before the function name. As an example, [[Hello world]] in Factor is
Slava Pestov created Factor in 2003 as a [[scripting language]] for a [[video game]].<ref>{{cite web | url = https://factorcode.org/slava/ | first = Slava | last = Pestov | title = Slava Pestov's corner of the web}}</ref> The initial implementation, now referred to as JFactor, was implemented in [[Java (programming language)|Java]] and ran on the [[Java Virtual Machine]]. Though the early language resembled modern Factor superficially in terms of [[syntax (programming languages)|syntax]], the modern language is very different in practical terms and the current implementation is much faster.
"Hello world" print
 
The language has changed significantly over time. Originally, Factor programs centered on manipulating Java objects with Java's [[reflection (computer science)|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]] where the programmer could define their own [[class (computer science)|class]]es, and early versions of native Factor were the same; the language was similar to [[Scheme (programming language)|Scheme]] in this way. Today, the object system is a central part of Factor. Other important language features such as [[tuple]] classes, combinator inlining, [[macro (computer science)|macro]]s, user-defined [[parsing]] words and the modern vocabulary system were only added in a piecemeal fashion as their utility became clear.
Factor is dynamically [[datatype|typed]], and a unique [[Object-oriented programming|object]] system accompanies it. In Factor, there is a small group of base types, and users and the standard library can make their own classes using tuples and other mechanisms. There is no [[Inheritance (computer science)|inheritance]], but there is [[Delegation (programming)|delegation]] as in Self. Additionally, there are other ways to make [[Class (computer science)|class]]es other than types or tuples; Factor supports [[predicate class]]es and union classes. Factor's built-in compound datatypes include fixed and variable length [[array|vector]]s and [[hash table|hashtables]]. The language also supports [[floating point]], and arbitrary precision integers. Linked lists, complex numbers and fractions are implemented in the standard library.
 
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 (programming language)|C]] part of the implementation for each external [[library (computing)|library]] that Factor should communicate with, and has the benefit of being more [[declarative programming|declarative]], faster to compile and easier to write.
Factor was originally only interpreted, but it can now also be compiled. The compiler is written entirely in Factor, and it does not output standalone executables but rather merely a faster image.
 
The Java implementation initially consisted of just an [[interpreter (computing)|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.<ref>{{cite web | url = https://concatenative.org/wiki/view/Factor/Implementation%20history | title = Concatenative.org wiki: Factor/Implementation History}}</ref>
When using the stack system does not suffice, dynamic [[scope (programming)|scoping]] is a supported alternative. Factor has a growing library which supports [[continuation]]s, an [[Web server|HTTP server]] and accompanying web framework, an [[OpenGL]] binding, a [[GUI]] library, an [[XML]] parser, and several other utilities.
 
==Description==
One of Factor's main goals is to be useful for interactive and [[test-driven development]], which is why Factor is, at its core, a safe version of Forth. Factor is dynamically typed, but the compiler assesses the stack depth of words (functions).
Factor is a [[dynamically typed]], [[functional programming|functional]] and [[object-oriented programming|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.<ref name="dls">{{cite journal |last1=Pestov |first1=Sviatoslav |last2=Ehrenberg |first2=Daniel |year=2010 |title=Factor: a dynamic stack-based programming language |journal=ACM SIGPLAN Notices |volume=45 |issue=12 |pages=43–58 |publisher=ACM |doi=10.1145/1899661.1869637 }}</ref>
 
Each word takes a fixed number of arguments and has a fixed number of return values. Arguments to words are passed on a [[stack (data structure)|data stack]], using [[reverse Polish notation]]. The stack is used just to organize calls to words, and not as a data structure. The stack in Factor is used in a similar way to the stack in [[Forth (programming language)|Forth]]; for this, they are both considered [[stack-oriented programming language|stack languages]]. For example, below is a snippet of code that prints out "hello world" to the current output stream:
So far, both [[Java (programming language)|Java]] and [[C (programming language)|C]] implementations have been constructed. The Java implementation is deprecated and no longer maintained. The native runtime of the C version is continually shrinking as an increasing proportion of Factor is self-hosted. However, there will likely always be a portion of Factor written in C.
"Hellohello world" print
<code>print</code> is a word in the <code>io</code> 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).<ref name = "dls" />
 
The [[factorial|factorial function]] <math>n!</math> can be implemented in Factor in the following way:
Though Factor does not adhere to an external standard the way C does, the language is heavily documented.
<syntaxhighlight lang="factor">
: factorial ( n -- n! ) dup 1 > [ [1..b] product ] [ drop 1 ] if ;
</syntaxhighlight>
 
Not all data has to be passed around only with the stack. [[Lexical scoping|Lexically scoped]] local variables let one store and access [[temporary variable|temporaries]] used within a procedure. [[Dynamic scoping|Dynamically scoped]] 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.<ref name="dls" />
As of February 2007, the current version of Factor is .88. A 1.0 release is planned within the next two years.
 
Factor emphasizes flexibility and the ability to extend the language.<ref name = "dls" /> 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]]s for data structures. It is also used in the [[XML]] library to provide literal syntax for generating XML. For example, the following word takes a string and produces an XML document object which is an HTML document emphasizing the string:
<syntaxhighlight lang="factor">
: make-html ( string -- xml )
dup
<XML
<html>
<head><title><-></title></head>
<body><h1><-></h1></body>
</html>
XML> ;
</syntaxhighlight>
The word <code>dup</code> duplicates the top item on the stack. The <code>&lt;-></code> stands for filling in that part of the XML document with an item from the stack.
 
==Implementation and libraries==
Factor includes a large standard library, written entirely in the language. These include
* A cross-platform GUI toolkit, built on top of [[OpenGL]] and various windowing systems, used for the development environment.<ref>{{cite web | url = https://docs.factorcode.org/content/article-ui.html | title = Factor documentation: UI framework | first = Slava | last = Pestov}}</ref>
* Bindings to several database libraries, including [[PostgreSQL]] and [[SQLite]].<ref>{{cite web | url = https://docs.factorcode.org/content/article-db.html | title = Factor documentation: Database library | first = Doug | last = Coleman}}</ref>
* An [[HTTP]] server and client, with the Furnace web framework.<ref>{{cite web | url = https://docs.factorcode.org/content/article-http.server.html | title = Factor documentation: HTTP server | first = Slava | last = Pestov}}</ref>
* Efficient homogeneous arrays of integers, floats and C structs.<ref>{{cite web | url = https://docs.factorcode.org/content/article-specialized-arrays.html | title = Factor documentation: Specialized arrays | first = Slava | last = Pestov}}</ref>
* A library implementing regular expressions, generating machine code to do the matching.<ref>{{cite web | url = https://docs.factorcode.org/content/article-regexp.html | title = Factor documentation: Regular expressions | first1 = Doug | last1 = Coleman | first2 = Daniel | last2 = Ehrenberg}}</ref>
 
A [[foreign function interface]] is built into Factor, allowing for communication with [[C (programming language)|C]], [[Objective-C]] and [[Fortran]] programs. There is also support for executing and communicating with shaders written in [[GLSL]].<ref name="dls"/><ref>{{cite web | first = Slava | last = Pestov | url = https://factor-language.blogspot.com/2010/07/overhauling-factors-c-library-interface.html | title = Overhauling Factor's C library interface| date = 28 July 2010 }}</ref>
 
Factor is implemented in Factor and [[C++]]. It was originally bootstrapped from an earlier Java implementation. Today, the parser and the optimizing compiler are written in the language. Certain basic parts of the language are implemented in C++ such as the garbage collector and certain primitives.
 
Factor uses an [[system image|image]]-based model, analogous to many [[Smalltalk]] implementations, where compiled code and data are stored in an image.<ref>{{cite web | first = Slava | last = Pestov | url = https://factor-language.blogspot.com/2010/01/factors-bootstrap-process-explained.html | title = Factor's bootstrap process explained| date = 10 January 2010 }}</ref> To compile a program, the program is loaded into an image and the image is saved. A special tool assists in the process of creating a minimal image to run a particular program, packaging the result into something that can be deployed as a standalone application.<ref name="dls"/><ref>{{cite web | first = Slava | last = Pestov | url = https://factor-language.blogspot.com/2008/07/on-shaking-trees.html | title = On shaking trees| date = 5 July 2008 }}</ref>
 
The Factor compiler implements many advanced optimizations and has been used as a target for research in new optimization techniques.<ref name="dls"/><ref>{{cite web|first=Daniel |last=Ehrenberg |title=Closure elimination as constant propagation |url=http://factorcode.org/littledan/abstract.pdf |year=2010 |url-status=dead |archive-url=https://web.archive.org/web/20110726044425/http://factorcode.org/littledan/abstract.pdf |archive-date=2011-07-26 }}</ref>
 
==References==
{{Reflist}}
 
==External links==
*[{{Official website|http://factorcode.org/ Factor website]}}
*{{cite video |people=Slava Pestov |date=October 27, 2008 |title=Factor: An Extensible Interactive Language |url=https://www.youtube.com/watch?v=f_0QlhYlS8g |archive-url=https://ghostarchive.org/varchive/youtube/20211222/f_0QlhYlS8g |archive-date=2021-12-22 |url-status=live|format=flv |medium=Tech talk |publisher=[[Google]]}}{{cbignore}}
*[https://lists.sourceforge.net/lists/listinfo/factor-talk Factor mailing list]
*{{cite video |people=Zed Shaw |date=2008 |title=The ACL is Dead|url=http://vimeo.com/2723800 |format=flv |medium=CUSEC 2008 |publisher=CUSEC}} – a presentation written in Factor which mentions and praises Factor
*[http://www.ircbrowse.com/cdates.html?channel=concatenative Logs of #concatenative] on [[freenode]], an [[IRC]] channel which mainly discusses Factor
*[http://factorcode.org/responder/help/ Factor documentation]
 
{{DEFAULTSORT:Factor (Programming Language)}}
[[Category:Programming languages]]
[[Category:Concatenative programming languages]]
[[Category:Function-level languages]]
[[Category:Stack-oriented programming languages]]
[[Category:Programming languages created in 2003]]
 
[[Category:Extensible syntax programming languages]]
[[de:Factor]]
[[Category:2003 software]]
[[sv:Factor]]
[[Category:High-level programming languages]]
[[Category:Software using the BSD license]]