After writing the first article on how to create your own operating system, someone told me that if I could do an article on how to create a programming language. At first I did not pay much attention, but now and in other ways I have learned much more about the creation of programming languages. So let's do a basic programming language, easily embeddable in other programs and that works with a virtual machine that we will also design. Today we have to make the most basic virtual machine.
You are probably wondering: A virtual machine? But isn't that very difficult and does it also slow down the programs? " In contrast, a simple virtual machine is very simple and relatively fast. I have chosen Rust as a language for the virtual machine. But what is it Rust?
Rust installs in Linux In a simple way. However, there is no official parcel. Users of Ubuntu you can add these two PPA: ppa: hansjorg / rust y ppa: cmrx64 / cargo, users of Arch can use AUR (cargo-git is the package that installs everything). The rest can use:
curl -s https://static.rust-lang.org/rustup.sh | sudo sh
How does a virtual machine work?
If you know how the assembler world works it is exactly the same, with the stack or the stack. If not, I'll explain it to you. Let's imagine the following code:
print 2 + 3
The computer does not understand what 2 + 3 means, nor does it know what order to follow. Computers work with batteries or stacks in which data is accumulated and continuously extracted. That code in our virtual machine should look something like this:
PUSH 2 PUSH 3 ADD PRINT
Basically we would put the 2 on the stack on top, the 3 as well. ADD would pull (i.e. remove it from the stack and get its value) the last 2 items on the stack and add the result to the top of the stack. PRINT would take the last item on the stack and use it to display it to us. Now let's do that in Rust.
We must first define a language for the byte code, we could use an existing one like the one in Java or CLR of .NET / Mono, but we are going to create a more basic one.
https://gist.github.com/a01de8904fd39a442c20
We use hexadecimal notation for each instruction. On high we have put # [deriving (FromPrimitive)], is a peculiarity of Rust and it will help us later to be able to compare the enumeration with bytes directly.
Now we must make a function that executes each of those instructions. For this we must read a byte and compare it with the instructions that we have in the enumeration. If you find any that exist, you must execute your action.
https://gist.github.com/8950ce212a2de2f397f9
We do that to read each byte individually and to execute them:
https://gist.github.com/12e24a1f0dd65e4cd65d
As you can see, we differentiate if we were given the PUSH command (our INTEGER command) before, the next byte will be completely taken to the stack. There we are using two functions that I have not taught you, self.pop () y self.push (), which are obviously in charge of handling the stack.
https://gist.github.com/54147f853a8a2b8c01d9
They are not very complex, but the pop function has error detection mechanisms. In fact, in Rust, if we remove those mechanisms it would give us a compilation error. Now we simply have to call in a program Perin (our virtual machine) and execute a bytecode.
https://gist.github.com/99b1ab461318b3a644d0
That bytecode can be read from a file, but here for simplicity I have stored it in a variable. If we execute it, it will give us the expected result:
Perin v0.1 Perin VM executes FlopFlip bytecode Starting PerinVM instance PerinVM v0.1.0 Integer value 5
All code is available at GitHub under the Apache License 2.0: https://github.com/AdrianArroyoCalle/perin. To compile they must have Job installed and put:
charge build && ./target/main
In the next chapter we will see more about our programming language.
Interesting curiosity, although it is not very useful in reality, it does not hurt to know.
It is great that you advertise Rust, it is a language that promises a lot, not only is it more secure than c ++, but (for now) clearer in its syntax.
As for photo, I would not consider java evolution XD.
And from fortran, I never used it, but I haven't heard many good things about it ...
I do, and it is especially useful in engineering even though Python is gaining ground.
Fortran is perhaps the other great language along with C. Even today in really critical questions will be one or the other.
And it would be debatable that Fortran is as an 'evolution' of C, when perhaps it should be the other way around, as C is newer, more modern and with more possibilities; although one is not detached from the other in the least.
Although the final positions are all debatable from some point of view.
+1 to Java
Let's see if I like this, I give something to programming but basic to see if I understand more.
What is the real purpose of creating a new programming language? It seems to me personally that it is a ploy to hide the source code.
Friend, what happened to the continuation of "your own operating system"? Don't leave it there please continue it.
Actually, you are a master and just these two songs have completely captivated my attention, but I would not want them to stay halfway.
I know that many of us think the same and we are waiting for the continuations and conclusions of these very interesting topics.
Very interesting, thank you very much. 🙂
I do not consider java a programming language, rather a command interpreter, since it is not compilable
[A programming language is a formal language designed to express processes that can be carried out by machines such as computers.]
For this reason, Java is a programming language. Even the Bash language (linux shell language) is a programming language in itself.
There are two types of languages:
- Compiled
- Interpreted
- Mixed (Virtual machines, the native libraries are compiled and the functional code is interpreted)
The interpreters are very useful when it comes to multiplatform and do not have a disastrous performance for that. Java, VB.NET, C ++ .NET, F #, C # are all mixed languages. The bash language, bat, PHP, Javascript and many more are interpreted languages.
If you do not consider Java as a language because it is interpreted (which it is not) you should not consider many other languages that are used to make programs. Moreover, by that rule of three you should not consider that there is any programming language except for the machine language itself.
And why not? Not even machine language could be considered a language since it is really just a set of commands "interpreted" by the processor.
Because effectively, ALL languages are nothing more than a set of commands that are interpreted by a processor.
You may like a language more or less (Java, in this case), seem more or less useful and powerful but to say that it is not a programming language because it is not compiled… it goes against all programming language definitions.
😐 I hope I didn't seem too rude
no quiet you only destroyed our lives
hahahahaha, perdoooon. it was not my intention xD
java is a programming language. Because you can develop an application and when you compile you make a .jar that is interpreted by the JVM. Then according to your python logic it is not interpreted either, but it compiles to different executables ...
Very good information
good information but I have a doubt, it will be possible to create a new programming language from scratch without having to rely on other analogies or software. I speak in the same way that other languages were made like java or HTML.
I would greatly appreciate your help on this question.