My Own Programming Lang? (1) 03-17-2017, 01:00 AM
#1
What is This?
You've always wanted to know how to make your own programming language, right? Â Right.
This series will teach you the basic theory behind an interpreted programming language and how you can create one for yourself.
We will be using pseudocode and Python for demonstrations. Â #PythonMasterRace
Enjoy! Â If you learned something, be sure to leave a Like! Â
Some Basic Theory.
An interpreter works like so:
Let's start with the following raw code:
After being run through the tokenizer:
After the tokens are run through the parser and made into an AST:
And then that is executed...
If you're wondering why the operators (=, +, -, >, <, ...) come before the data, it's because computers are dumb. Â A computer can't understand 1 + 2, but it can understand add(1, 2).
Explanations and Definitions.
You've always wanted to know how to make your own programming language, right? Â Right.
This series will teach you the basic theory behind an interpreted programming language and how you can create one for yourself.
We will be using pseudocode and Python for demonstrations. Â #PythonMasterRace
Enjoy! Â If you learned something, be sure to leave a Like! Â

Some Basic Theory.
An interpreter works like so:
- Raw source code is broken up into tokens.
- Tokens are structured into an Abstract Syntax Tree (AST).
- AST is executed (proper terminology: evaluated).
Let's start with the following raw code:
Code:
x = 1
y = 2
if (x > y):
  max = x
else:
  max = yCode:
["x", "=", "1", "\n", "y", "=", "2", "\n", "if", "(", "x", ">", "y", ")", ":", "\n", "max", "=", "x", "\n", "else", ":", "\n", "max", "=", "y", "\n"]Code:
[["=", "x", 1], ["=", "y", 2], ["if", [">", "x", "y"], ["=", "max", "x"], ["=", "max", "y"]]]If you're wondering why the operators (=, +, -, >, <, ...) come before the data, it's because computers are dumb. Â A computer can't understand 1 + 2, but it can understand add(1, 2).
Explanations and Definitions.
Spoiler: Interpreter vs. Compiler?
An interpreter runs through your source code, parsing and executing the source each and every time you want to run your script.
A compiler converts the source code into native machine code that can be executed by the hardware.
For example, you can use gcc to compile your C source code into an .EXE that can be run by (theoretically) all computers.
On the other side, if you write a Python script, you will need the Python interpreter installed in order to execute the code.
http://stackoverflow.com/questions/26572...d-language
A compiler converts the source code into native machine code that can be executed by the hardware.
For example, you can use gcc to compile your C source code into an .EXE that can be run by (theoretically) all computers.
On the other side, if you write a Python script, you will need the Python interpreter installed in order to execute the code.
http://stackoverflow.com/questions/26572...d-language
Spoiler: Machine Code?
Machine code is the lowest level programming language. Â It is the only thing your hardware can understand, so all other code somehow or other ends up being executed as machine code.
Quote:Cognitive science professor Douglas Hofstadter has compared machine code to genetic code, saying that "Looking at a program written in machine language is vaguely comparable to looking at a DNA molecule atom by atom."https://en.wikipedia.org/wiki/Machine_code
Spoiler: What are Tokens?
Tokens are just like words in a sentence.
print is a token in:
print is a token in:
Code:
x = 10
print(x)Spoiler: Abstract Syntax Tree?
An Abstract Syntax Tree (also known as an AST) is a computer-friendly structure that a parser creates from source code.
It sounds complex, but it is really quite intuitive.
Take a moment to look at this very simple AST and how it connects to the psuedocode below.
![[Image: KhqLwVPUs.png]](https://cdn.pbrd.co/images/KhqLwVPUs.png)
What do you think the AST will look like for this snippet of code?
![[Image: KhIR8feoA.gif]](https://cdn.pbrd.co/images/KhIR8feoA.gif)
This concept may take a while a to sink in. Â Stick with it!
https://en.wikipedia.org/wiki/Abstract_syntax_tree
It sounds complex, but it is really quite intuitive.
Take a moment to look at this very simple AST and how it connects to the psuedocode below.
![[Image: KhqLwVPUs.png]](https://cdn.pbrd.co/images/KhqLwVPUs.png)
Code:
x = 1
y = 2
3 * (x + y)What do you think the AST will look like for this snippet of code?
Code:
if (x > y):
  max = x
else:
  max = ySpoiler: Corresponding AST
![[Image: KhIR8feoA.gif]](https://cdn.pbrd.co/images/KhIR8feoA.gif)
This concept may take a while a to sink in. Â Stick with it!
https://en.wikipedia.org/wiki/Abstract_syntax_tree
(This post was last modified: 03-17-2017, 03:16 AM by m0dem.)





![[+]](https://sinister.ly/images/modern/collapse_collapsed.png)













![[Image: p_237m2jx1.png]](http://c.top4top.net/p_237m2jx1.png)