Auto-translation used

Article 3: Three masters for one language. How a computer understands runes.

This is the third part of our series on the creation of a new Kazakh programming language. In previous articles, we talked about his philosophy and created a runic alphabet for him.

So we have a concept and we have an alphabet full of deep images. But how can you make the soulless silicon of a computer understand that Λ is not just an icon, but an unshakable Mountain-a Constant? How can I teach him to read our runes?

This post is about how we created the "brain" of our language. About the three "masters" who breathe life into our code.

In computer science they are called Lexer, Parser and Interpreter, but in our philosophy they got their names: Tanusha, Kurastyrusha and Ike Asyrusha.

Imagine that our code is just a continuous stream of characters. The computer doesn't see any runes or names in it. The task of the first master, "Tanusha" (the Discerner), is to peer into this stream and recognize familiar images.

He doesn't try to understand the meaning of the sentence. It only identifies individual "words" — tokens or tokens.

  • When he sees it, he says, "It's a Rune."_A variable."
  • When he sees the san, he says, "This is the ID (name)."
  • When he sees:, he says, "This is the Operator_Assignments".
  • When he sees 10, he says, "This is an integer literal."

For example, a simple line of code:

— □ a : 10

"Tanusha" turns into a meaningful sequence of tokens:

[Runa_Var] [Runa_Type_Int] [Identifier: "a"] [Op_Assign: ":"] [IntegerLiteral: 10]

After its operation, instead of a chaos of symbols, we have a clear list of recognized elements. But how they are connected to each other, the system still does not know.

This is where the second master, "Kurastyrushy" (the Collector or Architect) comes into play. He takes a stream of tokens from Tanusha and checks if they match the grammar of our language.

He is the guardian of the laws.

He looks at the sequence [Runa_Var] [Runa_Type_Int] [Identifier] [Op_Assign] [IntegerLiteral] and says: "Yes, this corresponds to the rule "Variable declaration". That's right." If the order had been violated, Kurastyrushi would have reported an error.

The main result of his work is "Oh Bayteregi" (The Tree of Thought). In science, this is called an Abstract Syntax Tree (AST). It turns a flat list of tokens into a hierarchical, tree-like structure that reflects the logic of the program.

For the code

— □ c : a * (b + 2)

The Tree of Thought will look something like this, accurately reflecting the priority of operations (first addition in parentheses, then multiplication):

(Node: Variable Declaration, Name: "c")
|
|-- (Value: Binary Operation Node, Operator: '*')
|
|-- Left side: (Node: Variable Access, Name: "a")
|
|-- Right side: (Binary Operation Node, Operator: '+')
|
|-- Left part: (Node: Variable Access, Name: "b")
|-- Right part: (Node: Number, Value: 2)

Now we have not just words, but the perfect logical blueprint for our program.

We have a blueprint, but the house hasn't been built yet. Here the last master appears — "Ike Asyrusha" (Performer or Incarnator). His task is to take the "Tree of Thought" and bring it to life.

It traverses the Tree node by node and executes commands:

  • After seeing the node of the exchange declaration, it allocates a cell in the computer's memory.
  • After seeing the Binary Operation node, it performs a mathematical operation.
  • When he sees the Output node (), he gives the command to display the characters on the screen.
It is he who produces the final, visible result of our program.

These three masters—the Recognizer, the Architect, and the Embodier— are our compiler. It's an eternal cycle: to recognize symbols, to build a structure out of them, to execute this structure.

Now that you know how our language "thinks", in the next article we will finally show it in action. We will write our first full-fledged program using runic syntax, and see how it passes through the hands of all three masters to produce the final result.

Comments 0

Login to leave a comment