A Compiler For Our Own Programming Language // Full Guide

preview_player
Показать описание
Creating a programming language is a dream for many programmers. In this video I go over how you can create a simple compiler a for a programming language that we designed and interpreted in the previous video. This will help you get started creating and understanding how make and gain an intuition for your own compiler and programming language.

Make sure to let me know in the comments if you want to see this language being expanded. For the ability to do things such as assign variable or call functions.

Previous Video (Language + Interpreter):

Learning Resources:

Program Memory Layout (image):

Disclaimer:
The language (Our Little Language (.oll)) is by far not a complete language, and the interpreter only expects perfectly written code. Don't use this for production purposes, and only use it for the educational purpose of learning about creating and interpreting programming languages.

The Instruction in Our Little Language (.oll) are:
PUSH
POP
ADD
SUB
PRINT
READ
JUMP.EQ.0
JUMP.GT.0

_____________
_____________
READ
READ
SUB
JUMP.EQ.0 L1
PRINT "not equal"
HALT

L1:
PRINT "equal"
HALT
_____________

_____________
_____________
READ
JUMP.EQ.0 L1

LOOP:
PUSH 2
SUB
JUMP.EQ.0 L1
JUMP.GT.0 LOOP
PRINT "odd"
HALT

L1:
PRINT "even"
HALT
_____________

- Music -

- Time Codes -
0:00 - Intro
0:15 - Video Outline
0:25 - Compiler Overview
0:51 - Assembly Specifics
1:40 - Learning material
2:00 - Setting up the compiler files
2:13 - 1. Parser
4:20 - 2. Assembly Translation
12:40 - 3. Assembler (nasm)
12:50 - 4. Linker (gcc)
13:28 - ASM .data PRINT (printf)
16:50 - ASM .bss READ (scanf)
18:19 - Testing the compiler
18:39 - Outro

- Hashtags -
#programming
#coding
#interpreter
#programminglanguage
#python
#compiler

- Search Terms -
compiler design
making a programming language
making my own programming language
programming language interpreter
write a program
computer program
python tutorial for beginners
compiler design playlist
assembly language
x86_64 instruction set
Intel Syntax
Рекомендации по теме
Комментарии
Автор

Thanks for this guide! I followed along and customized this to my own needs, very simple and straight forward to follow

Love from Sweden

Overlycomplicatedswede
Автор

That was a great little process demystifier! Thanks for taking the time to share.

arnswine
Автор

Never thought I could look at some compiler building code...was always curious about it! Thanks!

mohithguptakorangi
Автор

Funny how you made a language 1000x faster than python in python! Great vid.

spider
Автор

They had to add the shadow space for debugging on x86-64 because most debuggers wouldn't correctly show parameters that were passed in registers. So you only need as much space as for the first four arguments, and only when they're passed. Which means if you don't pass any arguments to a function you don't need any, and if you only pass one you only need enough for one, et cetera. For main, since you're using gcc to link, you only need to account for two arguments. It should also be noted that you don't need shadow space if you pass arguments via the stack instead of registers, and you only need to worry about it if your code is being called externally, which main would be.

anon_y_mousse
Автор

Wow that was really cool.. can you also go through how assembler works??

vaisakh_km
Автор

Might also be interesting to take a look at ANTLR (java) to build compilers like the industry does. Really interesting topic and so many resources out there to understand all sides of a compiler (check out the book Engineering a Compiler). Having built my own compiler for my selfmade language for a project of mine I can definitely say there's so much to learn on this topic!

metteweisfelt
Автор

Cool thanks this and the intrepreter one gimme more ideas to experiment making more language.

noodle-eater
Автор

it would be cool to do this for a more complex grammar, rather than simple opcodes and instructions like PUSH, POP, READ, etc. take for instance c++, it compiles to an executable and there are lots of complicated things like classes, inheritance, etc.

QUASAR
Автор

That was very interesting stuff. Can't wait for more.

stupidPresident
Автор

You need more subs, these are quality vids!

reversev
Автор

Also at 1:22, did you mean to have “windows 10” and “13th gen intel
CPU” in that order? I think it would look better if they were swapped so that the columns would all line up.

reversev
Автор

Hey im getting this error, can u help me out

[CMD] Parsing
[CMD] Assembling
[CMD] Linking
program0.o: file not recognized: File format not recognized
collect2.exe: error: ld returned 1 exit status
[CMD] Running
'program0.exe' is not recognized as an internal or external command,
operable program or batch file.

sneakydark
Автор

Great Video ♥How can I make this compiler work for MacOS M1 Chip. What should I replace os.system(f"nasm -f elf64 {asm_filepath}") with?

dhairyashah
Автор

I was having trouble with the print function when combined with the JE function. The solution I eventually found with chatGPT was to modify it as follows:

elif opcode == "PRINT":
string_literal_index = program[ip]
ip += 1
out.write(f"; --- PRINT ---\n")
out.write(f"SUB rsp, 8\n") # added line
out.write(f"LEA rcx,
# out.write(f"XOR eax, eax\n") # removed
out.write(f"CALL printf\n")
out.write(f"ADD rsp, 8\n") # added

ChatGPT says "In the x86-64 ABI (Application Binary Interface), the stack must be aligned to a 16-byte boundary before making a function call." I don't know why this is the case but it seems to have fixed the problem. Do you have any ideas why I would've had to do this when you didn't have to do it? I'm also running windows on an intel cpu.

matta
Автор

I was strongly suggest, to make it in arm64 assembly + linux, because it is very easy.

undefined-none
Автор

Hey! at 13:31, I get a error in command prompt that says "
[CMD] Parsing
[CMD] Assembling
'nasm' is not recognized as an internal or external command,
operable program or batch file.
[CMD] Linking
gcc: error: program0.o: No such file or directory
gcc: fatal error: no input files
compilation terminated.
[CMD] Running
'program0.exe' is not recognized as an internal or external command,
operable program or batch file." Kindly help me in this regard.

SreenidhiRamprasath
Автор

Hey, what font do you use in your videos?

strawberrycake
Автор

For (win 11 +amd ryzen 7) (or) (win11 + i7 10thgen) how do i figure out what cmds to use
and is there anything else u need to know

the current error screen:
[CMD] Parsing
[CMD] Assembling
[CMD] Linking
program0.o: file not recognized: File format not recognized
collect2.exe: error: ld returned 1 exit status
[CMD] Running
'program0.exe' is not recognized as an internal or external command,
operable program or batch file.

sneakydark
Автор

we're stuck with the same error (device: win11 i7 10th gen) and don't know how to proceed. we have our project submission in 2 days. can you please help us out?

sneakydark