Sinisterly
Tutorial My Own Programming Lang? (1) - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: Coding (https://sinister.ly/Forum-Coding--71)
+--- Thread: Tutorial My Own Programming Lang? (1) (/Thread-Tutorial-My-Own-Programming-Lang-1)



My Own Programming Lang? (1) - m0dem - 03-17-2017

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!  Wink

Some Basic Theory.
An interpreter works like so:
  1. Raw source code is broken up into tokens.
  2. Tokens are structured into an Abstract Syntax Tree (AST).
  3. AST is executed (proper terminology: evaluated).
What would this look like with some pseudocode?
Let's start with the following raw code:
Code:
x = 1 y = 2 if (x > y):    max = x else:    max = y
After being run through the tokenizer:
Code:
["x", "=", "1", "\n", "y", "=", "2", "\n", "if", "(", "x", ">", "y", ")", ":", "\n", "max", "=", "x", "\n", "else", ":", "\n", "max", "=", "y", "\n"]
After the tokens are run through the parser and made into an AST:
Code:
[["=", "x", 1], ["=", "y", 2], ["if", [">", "x", "y"], ["=", "max", "x"], ["=", "max", "y"]]]
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.
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/2657268/whats-the-difference-between-compiled-and-interpreted-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:
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]
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 = y
Spoiler: Corresponding AST
[Image: KhIR8feoA.gif]


This concept may take a while a to sink in.  Stick with it!

https://en.wikipedia.org/wiki/Abstract_syntax_tree



RE: My Own Programming Lang? (1) - spirited_wolf™ - 03-17-2017

Nicely written sir, I hope it will help some new member out here. Smile


RE: My Own Programming Lang? (1) - mothered - 03-17-2017

This Is a very well defined tutorial.

Operators are quite powerful when used In the right context, and you've elaborated It exceptionally well.
Good work.


RE: My Own Programming Lang? (1) - Mr.Kurd - 03-17-2017

Thank mate, Keep it up Smile


RE: My Own Programming Lang? (1) - m0dem - 03-19-2017

Thanks for the replies guys. I should be writing part 2 shortly. There may be 5 parts in total, we'll see.
@spirited_wolf™ @mothered @Mr.Kurd


RE: My Own Programming Lang? (1) - mothered - 03-19-2017

(03-19-2017, 01:56 AM)m0dem Wrote: Thanks for the replies guys.  I should be writing part 2 shortly.  There may be 5 parts in total, we'll see.
@spirited_wolf™ @mothered @Mr.Kurd

Definitely so.

If you segregate each part In a simplified manner (as you've done here), that'll be great.
Again, well done with this.