Citrine is a general-purpose
programming language
A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language.
The description of a programming ...
for
Cross-platform (multi-platform) operating systems. It focuses on readability and maintainability. Readability is achieved by syntactic and conceptual minimalism. The language is heavily inspired by
Smalltalk
Smalltalk is an object-oriented, dynamically typed reflective programming language. It was designed and created in part for educational use, specifically for constructionist learning, at the Learning Research Group (LRG) of Xerox PARC by Alan ...
and
Self
The self is an individual as the object of that individual’s own reflective consciousness. Since the ''self'' is a reference by a subject to the same subject, this reference is necessarily subjective. The sense of having a self—or ''selfhoo ...
but has some very distinctive features. Like Smalltalk, Citrine treats everything as an object and focuses on sending messages to these objects. However unlike Smalltalk, Citrine lacks the concept of a class. In this regard, Citrine is more like Self and
JavaScript
JavaScript (), often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of websites use JavaScript on the client side for webpage behavior, of ...
because it uses
prototypes
A prototype is an early sample, model, or release of a product built to test a concept or process. It is a term used in a variety of contexts, including semantics, design, electronics, and software programming. A prototype is generally used to ...
. The combination of Smalltalk like messages and prototypes is what makes Citrine unique.
As of the 0.7 version Citrine has focused on supporting native human languages instead of just English to help people reduce the number of bugs because of confusion and misunderstanding due to language barriers. As such Citrine 0.7 and higher feature a translator to translate between human languages.
Syntax
Citrine has a very limited syntax and it's very closely related to Smalltalk. Everything in Citrine is an ''object'', there are 5 literals:
*
*
*
*
*
The code block literal uses a ''pipe'' symbol to separate the parameters from the logic ', ', if there are no
parameters, the backslash should be used instead of '\'.
Citrine only supports full-line comments, comments start with a '#'.
A Citrine program is basically a sequence of messages sent to objects. For instance, to determine whether 5 is an even number, the message 'even?' is sent to the number 5.
5 even?
This is called a ''unary'' message because it takes no arguments. A ''binary'' message is always a single UTF-8 character; this differs from Smalltalk, where there is a fixed set of binary messages. Here is an example:
6 + 7.
Here a binary message '+' is sent to number 6, the argument of this binary message is '7', this will result in a new number object '13'. Assigning the outcome of this operation to a variable uses the assignment operator: :=.
total := money + debt.
Also note that each line in a Citrine program ends with a dot, just like in Smalltalk. Besides unary and binary messages, Citrine offers ''keyword messages'', which take arguments interspersed with the message itself just like Smalltalk and
Objective-C
Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. Originally developed by Brad Cox and Tom Love in the early 1980s, it was selected by NeXT for its NeXT ...
.
☞ x := Number between: 1 and: 5.
The code snippet above will return a boolean object ''True''.
Control flow
Just like Smalltalk, control flow in Citrine is implemented by strategic use of messages. For instance, writing a conditional statement requires sending a block of code to a boolean.
(money > price) true: .
Likewise, a for-loop is written as:
× 10.
To break out of a loop in Citrine one has to send the message 'break' to a boolean, this allows a conditional break from a loop without having to factor out the break conditions:
* 5.
Pipelines
Unlike Smalltalk, Citrine has no semi-colon to send a message to the original receiver. Instead Citrine has a comma token ',' used to chain keyword messages, this allows writing Unix-like ''
pipelines''. The following code uses a pipeline-like syntax to replace all the 'o' characters with zeroes, the resulting string would be something like: '1010101...'.
onesAndZeroes := '1o1o1o1o1o1' split: 'o', map: mapUp, join: '0'.
Prototypes
The biggest difference from Smalltalk is the use of prototypes. Citrine does not have a concept of a class, it only knows about objects. An object is created using the new message:
cat := Object new.
This object can be made to respond to messages by ordering the object to listen to events. This is kind of similar to
adding methods in languages like
Java
Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's mo ...
:
cat on: 'meow' do: .
As stated above, inheritance is based on prototypes. To derive an object from another object, the new message must be sent to the object to be extended:
☞ Animal := Object new.
Animal on: 'makeSound' do: .
☞ Cat := Animal new.
Cat on: 'makeSound' do: .
☞ Tom := Cat new.
Tom makeSound.
Unicode
Citrine uses UTF-8 unicode extensively, both objects and messages can consist of unicode symbols. All string length are calculated using UTF-8. Citrine distinguishes string length and size in bytes:
'text' length.
returns the length of the string in UTF-8 code points, while:
'text' bytes.
returns the number of bytes.
Scoping
Citrine uses
dynamic scoping
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 ...
instead of
lexical scoping
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 ...
. Thus, there is no need for
dependency injection or global variables, but it might be harder to reason about than lexical scope. This is similar in programming languages like
Emacs Lisp
Emacs Lisp is a dialect of the Lisp programming language used as a scripting language by Emacs (a text editor family most commonly associated with GNU Emacs and XEmacs). It is used for implementing most of the editing functionality built into Ema ...
and
BASIC
BASIC (Beginners' All-purpose Symbolic Instruction Code) is a family of general-purpose, high-level programming languages designed for ease of use. The original version was created by John G. Kemeny and Thomas E. Kurtz at Dartmouth College ...
. In code blocks the ''var'' keyword needs to be used to declare a local variable.
The following demonstration makes the Mailer object available in the module:
Application := .
See also
*
Smalltalk
Smalltalk is an object-oriented, dynamically typed reflective programming language. It was designed and created in part for educational use, specifically for constructionist learning, at the Learning Research Group (LRG) of Xerox PARC by Alan ...
programming language
References
demo in new programming languages communityAnnouncement on RedditTiobe Index
External links
* {{Official website, citrine-lang.org
source code Source code on GitHub
Smalltalk programming language family
Procedural programming languages