📜 ⬆️ ⬇️

Io Language: Syntax


In continuation of a series of articles on Io, I would like to dwell separately on the syntax of the language.
In a nutshell, the Io syntax can be described as simple, but strange. Such an approach will surprise many, but, I hasten to assure, as in Lisp, the syntax is inconvenient only from the outside. From an aesthetic point of view, we can separately single out the fact that many programs on Io seem to be unnecessarily sparse, there is a lot of "extra" space left on the screen, this feeling does not leave me for quite a long time, I have not yet figured out how to completely get rid of of this dislocation of the brain, I just came up with some rules of code design for myself, subject to which the effect shrinks:


Here we go. Removal of the brain number 2.


Io syntax


In Io, there is no concept of “keyword”; any text in Io is an expression consisting of object names and messages. Thus, we do not have any “function, def, defun, define” or other syntactic garbage.
An object in Io is a basic container consisting of slots, a slot is a unit of storage for an object of something. Well, naturally so - slot. From here we will dance.
In Io, there are three assignment operations that are different in meaning:

So do not be scared,: = this is not at all a legacy of Pascal, be not remembered by night.
(Here, by the way, I will step aside a little more and answer the comments to the previous post: yes, assignment can be overloaded in Io, everything can be overloaded in Io)
')
Now a few words about the messages.
Messages are transmitted to objects and are recorded through a space, in a postfix way, and since everything is generally in an Io object, such glamorous programming techniques like chains are very famously rolled. For example, you can chop up such a squiggle:
 Lenin speak split reverse foreach (println)

The general message passing rules are simple as three kopecks: Object message. Everything.

The handler for each message lies in its own slot, so to speak, the name of the slot is the pattern that is triggered when a certain message is transmitted to the object by the slot holder. Creating slots is as easy as sending messages.
 SomeObject someSlot: = "Let's go to the numbers?"

Here we create someSlot slot of someObject object, which contains a string value.
(In this place, for the first time while writing this article, I felt myself in the role of Captain Obvious, hehe)
This way you can create properties of objects.
Object methods are created like this:
 SomeObject someMethod: = method (SomeObject someSlot print) 

Moreover, it is important to note that method is not a keyword, but the standard method of an Object (which means it is also possible, yeah ...)
The method method (my brain ...) returns an anonymous function that can be used at least right like that, at least assign a name to it and get a class method.
In addition to the method method (aaa !! 1), there is a method block, the only difference between them is that the block is lexically limited by the scope of the object from which it is called. (I will write a separate post about the message system, it is not trivial here and mega cool).
And now strange things begin.

Making special magic


The basic form of the method and block methods is:
 method (arg1, arg2, ..., argN, do message)

That is, the definition of the addition function of two numbers will look like this:
 add: = method (a, b, a + b)

As you can see, there is no forced return of the result here. It returns the result of the calculation of a do message, as in lisp, erlang, and in general in almost all languages ​​with powerful support for the functional paradigm. Although, in fairness, it should be noted that the return in Io is and works like a normal, normal return.
For convenience of recording, the do-part is usually transferred to the next line, like this:
 add: = method (a, b,
   a + b
 )

That is, it is possible to perceive the parentheses as operator ones, so as not to injure the psyche once again, although this is not so.

Conditional constructions


The form is:
 if (condition, true-expression, else-expression)

Also Io inherited the methods ifTrue, ifFalse, etc. from the Smalltalk dad.
You can write if in a more familiar form:
 if (condition) then (exp) else (exp)

But usually do not need (:

Operators


Operators in Io are nowhere more standard, no prefix notation for the Polish calculator to you, everything is usually nauseous:
 1 + 2
 1 + (2)
 1 + (2 * 4)
 etc ...


Strings


In Io, lines are most similar to lines (Captain Obvious, to the rescue!):
 s: = "Lenin was a mushroom. \ nAnd radio waves."

Long strings and strings with unshielded characters can be crammed into triple quotes:
 s: = "" "The fact is that being in one of the main temples of Mexico, I have long considered
 frescoes and found that most of the frescoes are dedicated to some event in the history of Mexico, 
 and which painfully reminded me of our October revolution. 
 The same emaciated people, armed with primitive tools, 
 which overthrow some governments - 
 it's all very similar, understand? "" "

The screen serves as the usual "backslash" "\". Non-printable characters are inherited from C "\ n, \ t, ...".

Comments


Io supports three forms of comments:
C style:
 / *
  Never compile it! 
  BadObject terribleMethod: = method (someAwfulLogic)
 * /

C ++ style inline:
 // OMFG!

And Unix style inline:
 # Old
 # School

The latter are mainly used to make shebangs (#! / Usr / bin / io).

Here, in fact, is the whole Io syntax, few which language can boast that its syntax description fits into one article.
Further according to plan an article about the object model Io.
Stay turned (-;

(From my blog )

Source: https://habr.com/ru/post/28167/


All Articles