Just began my school project !

The goal is to write a bash interpreter, and of course I can't code this shit without having to talk about it.

I think everyone should know how interpreters works.

1/12 #100DaysOfCode
This thread will gives you my own vision and some resources to dive in details on how interpreters works.

So ... let's go. :)

2/12 #100DaysOfCode
If you're reading this, you're probably asking what is an interpreter ?

You probably used once at school when using python or just when typing commands in you terminal.

3/12 #100DaysOfCode
So, what is an interpreter ?

An interpreter is a program that execute a source code from a language A without having to compile it for a specific target.

It's pretty efficient for portable purposes, but the execution time is slower than a compiled program.

4/12 #100DaysOfCode
The subject of our project is pretty simple, we have to implement some basics commands, support pipes, redirections and signals.

Because the subject is pretty simple, we will simplify how interpreters works.

5/12 #100DaysOfCode
1) Lexer
The Lexer will transform every part of our source code into a Queue of Tokens.

So for example, the command :
$: echo "foo"

Will result into the following tokens :

Token("COMMAND", "echo")
Token("STRING", "foo")

6/12 #100DaysOfCode
2) Parser
The role of the Parser is to give a context to the Queue of Tokens. It'll throw an error if there is an inconsistent use of the language.

The command :
$: "foo" echo

Will throw an error.

7/12 #100DaysOfCode
3) Eval
The Evaluator will evaluate and execute every tokens and write the result into a Stream.

The command :
$: echo "foo"

Will result into :
$: foo

This is the main part of the project, because we need to manage a lot of things, like signals, processes etc ...

8/12
4) Output

The Output will manage the process of writing into a specific output by using a Stream.

A Stream is just a data-structure that describe a specific file-descriptor.

9/12 #100DaysOfCode
Now because we are writing bash interpreter, we want it to wait for new commands, so we need a new diagram.

10/12 #100DaysOfCode
Now I think we have seen all the parts of the interpreter, this is just MY vision of MY interpreter, it's not perfect but I think it worth talking about it.

11/12 #100DaysOfCode
I didn't mention some important parts, like operators precedence, grammar definition (BNF), etc ... but probably for a next thread. :)

Interpreters are very cool subject of studies, I encourage you to dive in details on how it works.

12/13 #100DaysOfCode
The main words is, keep coding, keep hacking, goodbye !

And as I promised, here is the following resources :

https://ruslanspivak.com/lsbasi-part1/ 

https://realpython.com/cpython-source-code-guide/
https://realpython.com/products/cpython-internals-book/

You can follow @el_nightboi.
Tip: mention @twtextapp on a Twitter thread with the keyword “unroll” to get a link to it.

Latest Threads Unrolled: