Login Register


Tutorial My Own Programming Lang? (1) filter_list
Author
Message
My Own Programming Lang? (1) #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!  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/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:
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
(This post was last modified: 03-17-2017, 03:16 AM by m0dem.)

[+] 2 users Like m0dem's post
Reply

RE: My Own Programming Lang? (1) #2
Nicely written sir, I hope it will help some new member out here. Smile
Think Less, Live more
Youtube | Twitter | Facebook | Instagram
Please Subscribe and follow me

Reply

RE: My Own Programming Lang? (1) #3
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.
[Image: AD83g1A.png]

Reply

RE: My Own Programming Lang? (1) #4
Thank mate, Keep it up Smile
(This post was last modified: 03-17-2017, 11:53 AM by Mr.Kurd.)
Die  But Don't Lie
“Oh Abu Dharr! Don’t look at the smallness of the sin but look at the one you disobeyed.” Prophet Muhammad (pbuh)
[Image: p_237m2jx1.png]
Click for Free VPN

Reply

RE: My Own Programming Lang? (1) #5
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
(This post was last modified: 03-19-2017, 02:19 AM by m0dem.)

[+] 1 user Likes m0dem's post
Reply

RE: My Own Programming Lang? (1) #6
(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.
[Image: AD83g1A.png]

[+] 1 user Likes mothered's post
Reply







Users browsing this thread: