A Compiler For Our Own Programming Language // Full Guide

แชร์
ฝัง
  • เผยแพร่เมื่อ 17 ธ.ค. 2024

ความคิดเห็น • 102

  • @Overlycomplicatedswede
    @Overlycomplicatedswede 3 หลายเดือนก่อน +6

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

    • @bvdlio
      @bvdlio  3 หลายเดือนก่อน

      That is great to hear!

  • @arnswine
    @arnswine ปีที่แล้ว +31

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

    • @bvdlio
      @bvdlio  ปีที่แล้ว +2

      That is great to hear! I appreciate your kind words.

  • @mohithguptakorangi1766
    @mohithguptakorangi1766 ปีที่แล้ว +18

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

  • @spider3515
    @spider3515 ปีที่แล้ว +13

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

    • @bvdlio
      @bvdlio  ปีที่แล้ว +4

      Glad you liked it!

    • @rohithbhandari7836
      @rohithbhandari7836 8 หลายเดือนก่อน

      @spider3515 still the python file has to run

    • @SDZeeAy
      @SDZeeAy 7 หลายเดือนก่อน +3

      ​@@rohithbhandari7836 The compiling speed doesn't matter, what matters is the speed of the program itself

    • @dk4kja8
      @dk4kja8 2 หลายเดือนก่อน +3

      @@SDZeeAy rust users be like:

  • @hehe-bm5gc
    @hehe-bm5gc ปีที่แล้ว +3

    you're seriously under-rated

    • @bvdlio
      @bvdlio  ปีที่แล้ว

      Thank you!

    • @AlexTsaava
      @AlexTsaava หลายเดือนก่อน

      agree

  • @reversev9778
    @reversev9778 ปีที่แล้ว +4

    You need more subs, these are quality vids!

    • @bvdlio
      @bvdlio  ปีที่แล้ว

      Thank you :)

  • @anon_y_mousse
    @anon_y_mousse ปีที่แล้ว +3

    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.

  • @noodle-eater
    @noodle-eater ปีที่แล้ว +2

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

    • @bvdlio
      @bvdlio  ปีที่แล้ว

      Work on the "Not a Little Language" interpreter has started 🤙

  • @OneAndOnlyMe
    @OneAndOnlyMe หลายเดือนก่อน

    Very well explained!

  • @vaisakh_km
    @vaisakh_km ปีที่แล้ว +4

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

  • @metteweisfelt1679
    @metteweisfelt1679 ปีที่แล้ว +4

    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!

    • @bvdlio
      @bvdlio  ปีที่แล้ว +1

      Great idea! I am planning to do that at some point. The playlist from Immo Landwerth in the description is a really in-depth on industry (microsoft) compiler design series.
      I think it would be nice to then compile a c-like language, instead of the current stack based language.
      Thanks for you comment and suggestion :)

  • @QUASAR098
    @QUASAR098 ปีที่แล้ว +17

    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.

    • @bvdlio
      @bvdlio  ปีที่แล้ว +7

      I can really recommend the sources in the description if you are interested in learning about those things right now :) Especially this one: th-cam.com/play/PLRAdsfhKI4OWNOSfS7EUu5GRAVmze1t2y.html

    • @bvdlio
      @bvdlio  ปีที่แล้ว +5

      Work on the "Not a Little Language" interpreter has started 🤙

    • @ClicheKHFan
      @ClicheKHFan 7 หลายเดือนก่อน +3

      I would highly recommend the book “crafting interpreters” it’s available for free in website format and features a more complex language with lots of detail. There are also lots of playlists of people working through the book on TH-cam.

  • @mayelespino
    @mayelespino หลายเดือนก่อน

    I loved it! Thanks!

  • @GeoffJop
    @GeoffJop หลายเดือนก่อน +1

    I have a problem when writing the code in the video. Around 16:05 where the print function is supposed to run, I tried running the code, it would print linking and running, it will stop for a while, and it prints nothing. It does not print "Hello, World." Nothing appears.
    Is there any problem with my NASM configurations?

  • @SreenidhiRamprasath
    @SreenidhiRamprasath ปีที่แล้ว +1

    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.

    • @bvdlio
      @bvdlio  ปีที่แล้ว +1

      'nasm' is not recognized as an internal or external
      This means you need to install nasm :) Make sure to add it to your environment variables.

    • @SreenidhiRamprasath
      @SreenidhiRamprasath ปีที่แล้ว

      @@bvdlio Hey! tried that...now I am getting an error in the linking part dude
      [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.

    • @assemblycat
      @assemblycat ปีที่แล้ว

      @@SreenidhiRamprasath
      either it doesnt exist
      or
      use .\program0.exe

    • @TacticalAllRounder
      @TacticalAllRounder 5 หลายเดือนก่อน

      ​@@SreenidhiRamprasathyou need to be on linux or WSL

    • @Heavy_Lvy
      @Heavy_Lvy 2 หลายเดือนก่อน

      @@SreenidhiRamprasath did u forgot nasm -f win64? it needs a format to be linkable if ur on linux nasm -f elf64

  • @reversev9778
    @reversev9778 ปีที่แล้ว +2

    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.

    • @bvdlio
      @bvdlio  ปีที่แล้ว +2

      Good suggestion! It would have been a better organization of the columns. Thanks :)

  • @matta5749
    @matta5749 10 หลายเดือนก่อน +1

    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 ---
    ")
    out.write(f"SUB rsp, 8
    ") # added line
    out.write(f"LEA rcx, string_literal_{string_literal_index}
    ")
    # out.write(f"XOR eax, eax
    ") # removed
    out.write(f"CALL printf
    ")
    out.write(f"ADD rsp, 8
    ") # 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.

    • @bvdlio
      @bvdlio  10 หลายเดือนก่อน +1

      Interesting. I don't know the exact reason why it would be like that on your system. But I know that the "XOR eax, eax" was part of the calling convention for my system.
      So most likely your system just expects slightly different assembly (calling conventions).
      I also knowticed later that I am using the elf64 format, which is actually a Linux format, so it was surprising to see that I had no issues. Perhaps it has to do with that... Idk. Maybe mine ran using WSL somehow :p
      Glad you managed to fix it on your end, Great job!

  • @AustinBlass-v2u
    @AustinBlass-v2u หลายเดือนก่อน +1

    They should do one of these for Java or C, that would be very useful

    • @bvdlio
      @bvdlio  หลายเดือนก่อน

      Hey, nice idea!
      Which on of these do you mean?
      Would you like to see the interpreter written in Java or C.
      or.
      Would you like to see an interpreter written to interpret Java or C?

  • @sneakydark7631
    @sneakydark7631 ปีที่แล้ว +2

    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.

    • @bvdlio
      @bvdlio  ปีที่แล้ว +2

      Yea sure. Can you give me a link to the repository containing your code?
      I'll make sure to add a link to my code at the end of the day. So you can compare them aswell :)

    • @sneakydark7631
      @sneakydark7631 ปีที่แล้ว +1

      @@bvdlio VasPar1211/Proj1

  • @strawberrycake8253
    @strawberrycake8253 8 หลายเดือนก่อน +2

    Hey, what font do you use in your videos?

    • @bvdlio
      @bvdlio  8 หลายเดือนก่อน +1

      I'll look it up for you :)

    • @bvdlio
      @bvdlio  7 หลายเดือนก่อน +2

      The name of the font is: Baloo 2
      I use it inside of: Figma

  • @0xumarkhatab
    @0xumarkhatab ปีที่แล้ว +2

    please make more

    • @bvdlio
      @bvdlio  ปีที่แล้ว +1

      Work on the "Not a Little Language" interpreter has started 🤙

  • @dhairyashah4387
    @dhairyashah4387 7 หลายเดือนก่อน +1

    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?

    • @bvdlio
      @bvdlio  7 หลายเดือนก่อน +1

      Thanks!
      Unfortunately, as far as I know, Apple uses the ARM instruction set. Meaning, that it requires a different assembly language.
      You would need to look at the equivalent arm instructions to the x86 ones in the video and replace them.
      So unfortunately it's not as simple as changing the command arguments.
      Hope it helps :)

    • @dhairyashah4387
      @dhairyashah4387 7 หลายเดือนก่อน

      Got it! Thanks for replying 🙂

  • @Heavy_Lvy
    @Heavy_Lvy 2 หลายเดือนก่อน

    Do you have git repo for this?

  • @sneakydark7631
    @sneakydark7631 ปีที่แล้ว

    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.

    • @bvdlio
      @bvdlio  ปีที่แล้ว

      Use the intel CPU.
      You can find nasm formatting arguments by using Google. Like:
      "nasm formatting arguments"
      "nasm win 11 formats"
      etc.
      So the error is the same as you shared before, so my previous reply is what I would suggest you do, in order to resolve the problem.
      If you are sure you have the correct nasm command, try finding out if the linker is being called with the correct arguments.
      Good luck

  • @undefined-none
    @undefined-none 4 หลายเดือนก่อน

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

  • @sneakydark7631
    @sneakydark7631 ปีที่แล้ว

    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?

    • @bvdlio
      @bvdlio  ปีที่แล้ว +1

      I've replied to your previous comments. Please try keep our conversation going in just one comment thread. (so please reply to those, and don't create a new comment thread each time. Your comments are very similar each time, so i dont know if you are reading my response. Thanks.)
      I will gladly help over the next 2 days to help you guys complete your project :)
      Keep in mind though, if your assignment is to make a simple compiler, that if your project is the code you shared in the repository (VasPar1211/Proj1). I am fine with you using it, but your school will consider it copy-paste/plagiarism.

    • @upasanal7663
      @upasanal7663 ปีที่แล้ว +1

      @@bvdlio we're sorry we created different comment threads. we are a team of 3 and yes we did read all of your responses and went through nasm formatting arguments, win 11 formats and all those. but we're still not able to rectify the error.
      and yeah we thought of working on the project further by adding more syntaxes for our own language but we thought of executing yours first and are stuck.

    • @bvdlio
      @bvdlio  ปีที่แล้ว +1

      @@upasanal7663
      No problem.
      Since I can't reproduce the error on my end, here are some more suggestions:
      1. Try using the following format with nasm: win64
      so: nasm -f win64 {asm_filepath}
      2. Remove (since they are not used):
      extern printf
      extern scanf
      It can be that the external functions are not properly exposed, and the linker can not find them, after they were added to the .o file.
      Possibly the same goes for 'extern ExitProcess', but that one is necessary to properly exit. However you can try remove it see if at least linking will work.
      3. File Extensions: Ensure that the file extensions are correctly used in your commands. For example, check if the output file from NASM actually matches the input file expected by GCC for linking.
      4. Path Issues: Verify that the paths used in the script are correct and the files exist at those paths. Relative paths can sometimes cause issues depending on where the script is run from.
      5. Permissions and Environment: Make sure you have the necessary permissions to execute these commands and that your environment is set up correctly for running NASM and GCC. Try using name and gcc on some something that you know should work.
      6. Use Docker: Try using a docker container, to create the exact environment that you want, or try WSL2.
      Hope that gets you somewhere :)
      Good luck!

    • @upasanal7663
      @upasanal7663 ปีที่แล้ว

      @@bvdlio thank you so much!! but its still the same😭😭
      we tried win64 win32 elf64 elf32 but none of them are working, tried by removing the external functions, all the files are in the same path, set up the environment variables, but none of them are working for us. the object file is not being created and thus linking and running are not working.
      but we really appreciate your effort, thanks!!

    • @bvdlio
      @bvdlio  ปีที่แล้ว

      Thats too bad to hear.
      It is strange that you mention that the object file is not being created. Since the first printed error: "program0.o: file not recognized: File format not recognized collect2.exe: error: ld returned 1 exit status"
      Seems to suggest that the file exists, but is not readable or in the wrong format.
      If it is the case that the object file is not created, then it must the that its an issue with nasm, or even the steps before.
      There are dubug methods for nasm which can give you more insight on what is going wrong, check out this stackoverflow thread or similar directions: stackoverflow.com/questions/27747556/how-can-i-include-debug-information-with-nasm
      A final suggestion to maybe get something running, would be to look for an online asm "interpreter/compiler", so you can use the output .asm file that you generate and check it in there, and perhaps get it running in there.
      Like this one: www.mycompiler.io/new/asm-x86_64
      Best of luck!

  • @akhilphilnat
    @akhilphilnat 2 หลายเดือนก่อน +2

    Vs code theme please?

    • @bvdlio
      @bvdlio  2 หลายเดือนก่อน +1

      PawelBorkar.jellyfish

  • @Axtriel
    @Axtriel ปีที่แล้ว

    im not an expert but how can i declare variables?

    • @bvdlio
      @bvdlio  ปีที่แล้ว

      It's a very simple stack based language, that doesn't support variables. Just pushing and popping from a stack, and operations on things on the stack.
      In the near future there will be a video on how to create an interpreter for higher level language. With variables, ifs, loops, etc.

  • @sneakydark7631
    @sneakydark7631 ปีที่แล้ว

    VasPar1211/Proj1
    can u check this kind sir

    • @bvdlio
      @bvdlio  ปีที่แล้ว

      So I ran your code, and it works fine on my machine.
      So most likely, your assembly step (with nasm) is assembling to a format (elf64) that is not meant for your system. In the video, I state some of the requirements, like being on windows 10 64 bit, intel cpu, etc.
      If that is not identical to your setup, nasm will assemble the program to a object file (.o file) in a way that your system does not support, resulting in: "program0.o: file not recognized: File format not recognized."
      Try figure out what nasm commands are suited for your system.
      Good Luck!

    • @sneakydark7631
      @sneakydark7631 ปีที่แล้ว

      @@bvdlio Ill check and let yk thanks a lot

    • @treudden
      @treudden 9 หลายเดือนก่อน

      Run gcc -v and check your target architecture. Since this project uses the x64 architecture you need to make sure that your GCC install includes it

  • @oneflower6686
    @oneflower6686 ปีที่แล้ว

    test two READ equal failed when compile program1.oll, ok with PUSH

    • @bvdlio
      @bvdlio  ปีที่แล้ว

      You mean when you run it yourself? in the video? or where? what is 'test two'? are you on windows? do you link the c libraries?
      If you want me to fix or help with something, i need a little more info :)

    • @oneflower6686
      @oneflower6686 ปีที่แล้ว

      @@bvdlio I mean when I compare two same numbers from READ, it jump to "not equal". Thanks

  • @mario-rosell
    @mario-rosell 6 หลายเดือนก่อน

    now im making a programming lenguage, C+ (the opposite of C++)

  • @justcurious1940
    @justcurious1940 9 วันที่ผ่านมา

    Python makes it EZPZ lemon squeezy.

  • @vivekshah-ky2on
    @vivekshah-ky2on 5 หลายเดือนก่อน

    can u share this code?

  • @snorman1911
    @snorman1911 6 หลายเดือนก่อน

    Isn't this more an assembler than a compiler?

    • @Nylspider
      @Nylspider 4 หลายเดือนก่อน +1

      Not quite :3 An assembler is a program that takes assembly code and converts it to machine code. This program takes .oll code and converts it to assembly code, so it compiles the code to assembly. Hope this makes sense :3

  • @uuu12343
    @uuu12343 ปีที่แล้ว

    Hold on, you need to use a compiler...to create a compiler?

    • @bvdlio
      @bvdlio  ปีที่แล้ว +2

      Unless we write our compiler in assembly, we need a compiler or interpret to compiler/interpret our own compiler to use it.
      If you are talking about gcc. gcc started out as a compiler, but these days it is a collection of tools. In our case we use it as the linker.
      It is indeed a funny cycle of using compilers on compilers. I recommend tsoding dailys Porth playlist in the description. He eventually makes his language self hosted. Meaning he wrote the compiler for his own programming language using his own programming language. It's great fun :)

    • @andrewdunbar828
      @andrewdunbar828 8 หลายเดือนก่อน

      You don't *need* to. You can use an assembler but it will be a lot harder. You can hand-assemble by outputting the binary bytes directly but it will be a lot harder again. As a bare minimum you need something that can edit a text files and something that can create a binary executable file. In the beforetimes you could write the machine code directly into RAM and execute it but modern operating systems wouldn't allow that for security reasons. You could of course use an old operating system or old computer from the beforetimes.
      I would definitely watch such a video, but these days almost nobody other than maybe malware researches would go lower level than as assembler and even then almost never.

  • @NamerunaTheFool
    @NamerunaTheFool หลายเดือนก่อน

    a compiler written in a interpreter...isn't this counter intuitive?

    • @bvdlio
      @bvdlio  หลายเดือนก่อน +2

      I would argue no, since the output (compiled program) is the same no matter what language the compiler is written in. Be that interpreted or compiled.
      It does not affect the compiled program/it's execution speed. (since the compiler is not responsible for running the programming).
      Personally I just chose Python for educational purposes, since most people understand python.

    • @NamerunaTheFool
      @NamerunaTheFool หลายเดือนก่อน

      ​@@bvdliook make sense but still curious whether it does effect the compile speed. Say if you have an IDE written in python vs C. Which would compile faster? Also when pressing run to test code, a compiler written in C would run faster than python?
      I'm curious. This needs to be tested.

    • @bvdlio
      @bvdlio  หลายเดือนก่อน +1

      @@NamerunaTheFool !00%
      The compile speed is for sure slowing using python/interpreted languages. (most of the time)

  • @РодионЧаускин
    @РодионЧаускин 2 หลายเดือนก่อน

    White Christopher Thomas Margaret Jackson Kimberly

  • @JoshuaRose-hm3xq
    @JoshuaRose-hm3xq 3 หลายเดือนก่อน

    Ok but why python?

    • @bvdlio
      @bvdlio  3 หลายเดือนก่อน

      It's just a choice-it could have been any language. Python is handy for this video since it's beginner-friendly and widely known. It's best to gain an understanding in a programming language-agnostic way.

    • @JoshuaRose-hm3xq
      @JoshuaRose-hm3xq 3 หลายเดือนก่อน

      @@bvdlio thats fair enough. python is a good choice if ur aiming for a tutorial-based approach for a general audience

  • @山田ちゃん
    @山田ちゃん 6 หลายเดือนก่อน +1

    Eww python

    • @bvdlio
      @bvdlio  6 หลายเดือนก่อน

      🤮

  • @UnderScore3
    @UnderScore3 5 หลายเดือนก่อน

    I'm getting this error:
    [CMD] Parsing
    [CMD] Assembling
    program3.asm:27: error: symbol `L2' not defined
    program3.asm:30: error: symbol `L0' not defined
    program3.asm:43: error: symbol `L1' not defined
    program3.asm:46: error: symbol `L0' not defined
    [CMD] Linking
    C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find program3.o: No such file or directory
    collect2.exe: error: ld returned 1 exit status
    [CMD] Running
    'program3.exe' is not recognized as an internal or external command,
    operable program or batch file.

  • @om.mallick02
    @om.mallick02 7 หลายเดือนก่อน

    Hi there.
    I treid to recreate your project, and am getting the following errors. Can you please help me with this?
    [CMD] Parsing
    [CMD] Assembling
    [CMD] Linking
    /usr/bin/ld: program0.o: warning: relocation against `ExitProcess' in read-only section `.text'
    /usr/bin/ld: program0.o: relocation R_X86_64_PC32 against symbol `printf@@GLIBC_2.2.5' can not be used when making a PIE object; recompile with -fPIE
    /usr/bin/ld: final link failed: bad value
    collect2: error: ld returned 1 exit status
    [CMD] Running
    sh: 1: program0.exe: not found

    • @bvdlio
      @bvdlio  7 หลายเดือนก่อน

      Did you try re-compile with -fPIE like the error suggests? What output do you get then?
      It also seems to suggest you have the ExitProcess imported/used in the wrong section of your assembly.

    • @om.mallick02
      @om.mallick02 7 หลายเดือนก่อน

      @@bvdlio I did, but I was getting some other error. And I am not sure how I imported it wrong, as another friend of mine suggested it might be the case, but I used the same code as you and have an intel cpu.

  • @partehthecoder5219
    @partehthecoder5219 5 หลายเดือนก่อน

    Thank you for making this tutorial, you are the best for this!!

    • @bvdlio
      @bvdlio  5 หลายเดือนก่อน

      Glad you like it!