In this tutorial, we will build together a simple stack-based language called “lifo” (for “Last In First Out”) so you can learn how this kind of programming language works by building one.
A stack-based language is often used as an intermediate language. One of the most popular uses of a stack-based language nowadays is the set of EVM opcodes used on Ethereum and all the blockchains.
LIFO is written in Rust. For the lexer, we will use an existing crate called “logos” that will help us focus on the language.
The stack works on the “last in first out” principle (hence the name of our programming language): you can only use the value (or values, depending on the instruction) that are directly at the top of the stack.
The instructions available in the set are generally of two kinds:
The enum takes a few attributes.
The invalid_token function is used as a callback.
You pass the stack to the add function that will perform the required checks and changes.
This is why you can now implement an instruction to retrieve an element in a vector by its index.
But you can create your own features and improve Lifo!