Let's Write an Interpreter (in 168 Lines of Python)

Поділитися
Вставка
  • Опубліковано 27 жов 2024

КОМЕНТАРІ • 52

  • @stupossibleify
    @stupossibleify 2 роки тому +7

    Always viewed interpreters as beig indistinguishable from magic, this video goes a long way to demystifying the methods. Thank you

    • @talideon
      @talideon 2 роки тому +2

      A good place to start would be the interpreters for various esoteric programming languages. They're typically simpler than those for conventional ones, not least because they're purposely constrained in some way, either because they focus on some particular method of computational (Thue, Unlambda, SMETANA, &c., are good examples of this) or super minimalist (Unlambda, FALSE, Malbolge, &c., are good examples).
      Implementation strategies can vary from simply walking an abstract syntax tree generated from the parser output (Ruby did this for a long time) to generating bytecode for a simple custom VM. All that is actually really easy. Where it gets awkward is when you get down to things like optimisation: that's a real black art! It doesn't take much code to implement a Pascal compiler or interpreter as it's designed to be easy to parse, so building an interpreter for a cut down version of that would be a good exercise.

    • @stupossibleify
      @stupossibleify 2 роки тому

      @@talideon Excellent pointers for further reading, thank you

  • @glusiator
    @glusiator 2 роки тому +3

    I like your solution for "automatic" operator prioritization of multiplication and adding. Great video!

  • @StasPakhomov-wj1nn
    @StasPakhomov-wj1nn Рік тому +3

    I went through this line by line and re-wrote it locally in my language of choice, and it worked! I have to say, I've followed quite a few tutorials but none have been as magic as this one.
    I must warn people going through this in the future that he reminded us that he is building things in a simplified way, thus avoid OOP and other heavier utilizations. Thus, it is a little hard to follow as everything is one big jumbled implementation as opposed to it being broken up into SRP components.
    Amazing though nonetheless, I had a great time re-writing it locally to be in OOP as well :)

    • @slu467
      @slu467  Рік тому +1

      Thanks, Stas. Glad you found this helpful. I've made some follow-up stuff on interpreters and even released my own Python-like language 'Min'. Cheers!

  • @RelayComputer
    @RelayComputer 5 місяців тому

    That's very neat indeed. I love the use of a /single/ dictionary for all kinds of identifiers. That's quite cool !

  • @djc1402
    @djc1402 2 роки тому +3

    Thanks for another great video. It's a great introduction to people who think that writing interpreters and compilers is magic when it isn't all that hard to make a simple one. I learned this in my CS Degree in the Mid 80's and is called a recursive descent parser. There are other ways of doing it but this is probably the easiest. One small bug in your interpreter is that you can't have a # in a string literal e.g. "Hello # world" but it is easy to fix.

    • @slu467
      @slu467  2 роки тому +1

      Hi David, yep, no # in string literal ;-) thanks for pointing this one out.

  • @giacomo.delazzari
    @giacomo.delazzari 2 роки тому +10

    I think a stripped down Python would be interesting. BASIC is very very common on those kind of microcomputers, and a small subset of C has already been done. I would go for something Python-like just because it would be more original! Also, in the future it would be very very cool to build a compiler for the language, which outputs native assembly (if static typing is needed, the Python syntax for type annotations is also easier to parse than the C one!)

    • @slu467
      @slu467  2 роки тому +2

      Thanks, Giacomo, I currently think in the same direction. A BASIC with Python's indentation style would be something. Cheers!

  • @francoisdastardly4405
    @francoisdastardly4405 2 роки тому +2

    Man !! You are incredibly brilliant teacher ! I really love your videos. Many thanks 😊

    • @slu467
      @slu467  2 роки тому +1

      You are very welcome. Glad you like my stuff ;-)

  • @superkb172
    @superkb172 10 місяців тому +1

    this tutorial was amazing, thanks a lot,

  • @BryanChance
    @BryanChance Рік тому

    Wow.. brilliantly explained! Thank you sir!

  • @edgeeffect
    @edgeeffect 2 роки тому +2

    For minimal systems of the past, I always loved FORTH as it barely even requires any interpretation.

    • @slu467
      @slu467  2 роки тому

      There is already a FORTH running on the Minimal. See the Minimal Forum for more info: minimal-cpu-system.boards.net/
      It is being developed by a guy named slowcorners.

  • @funwithalbi2425
    @funwithalbi2425 2 роки тому

    this complicated task was done so simply!

  • @krumpy8259
    @krumpy8259 2 роки тому +1

    Ich bin froh dich entdeckt zu haben, super content. Ich bin auf der suche nach der Antwort auf die Frage, wie Computer überhaupt Programmiersprachen verstehen oder wie man eben von Anfang ein Computer bastelt, sodass etwas entsteht dass nach und nach Programmiersprachen versteht. Ich frag mich immer welche Vorüberlegung voran gehen muss, sodass dieses Zusammenspiel zwischen Hardware und software anfangs funktioniert und das überhaupt der weg geebnet werden kann bis da sowas wie ein compiler entsteht etc.

    • @slu467
      @slu467  2 роки тому

      Hi Krumpy, da finden wir ganz ähnliche Fragen spannend. Ich hoffe, mein Kanal hilft dir weiter!

  • @TheDarkelvenangel
    @TheDarkelvenangel 2 роки тому +4

    Tiny C would be very interesting

  • @eboatwright_
    @eboatwright_ 2 роки тому +1

    This is a really good video! I'm currently making my own programming language called King in JavaScript, so this was very insightful :D

  • @massimogiussani4493
    @massimogiussani4493 2 роки тому

    Terrific! Thanks for this video!

  • @talideon
    @talideon 2 роки тому +1

    6:30 - I'd recommend replacing 'act' with a small class that just contains a single field, and I'd call this 'State'. Why? Because it's less clumsy than passing around the one-element list and it leaves room for doing basic scope management, as it'll later give you somewhere to keep the interpreted program's globals and local variables for the currently executing subroutine. It's easy to do and really helps with complexity management. This is essentially what Lua's interpreter does, and it makes sense even with toy interpreters.

    • @slu467
      @slu467  2 роки тому +1

      Thanks, Keith, yes, you are right - some sort of 'state' can also do the trick and also allows to manage different reasons for a "bailout". I'd rather not use OOP here, since as I said, this piece has to be converted to very limited assembly very soon. Cheers!

  • @jimbailey3141
    @jimbailey3141 2 роки тому

    Thank you for this very informative walkthough

  • @colonelbarker
    @colonelbarker 2 роки тому +1

    Oh, a new Slu4 video. :D

  • @gazehound
    @gazehound 2 роки тому +3

    You sort of remind me of Bisqwit, if he went into more detail about what he was doing

    • @TerrexoDesign
      @TerrexoDesign Рік тому

      I thought I was the only one who thought abt bisqwit hahaha

  • @kippie80
    @kippie80 3 місяці тому

    Logo looks like good learning language.

  • @rednibcoding3412
    @rednibcoding3412 7 місяців тому

    I'm thinking: you could change the backend of Min to produce assembly code directly. So instead of calling the "do.."-functions, write and call the "emit..."-functions which then emit/produce assembly code.
    And since you already have written an assembler, this would close the cycle. What do you think about it?

  • @zlatkovidlanovic6454
    @zlatkovidlanovic6454 7 місяців тому

    what is a Main loop in MIN ?
    Is that func Program() with While loop where is
    while Next() != '\0': Block(act)
    and you wrote in video this ;
    while Look() != \0 ....so what is not Look() or Take() ..then u use Next() as endless loop?
    i am confused !

  • @alooyeg
    @alooyeg 2 роки тому +1

    Yes! A new video

  • @hamzacasdasdasd
    @hamzacasdasdasd Місяць тому

    heres the entire python interpreter rewritten in python in just 1 line
    exec("code")

  • @amnesie6615
    @amnesie6615 2 роки тому

    I love interpreters! BASIC is cool :)

  • @alessiocaffi5992
    @alessiocaffi5992 2 роки тому

    Yes this is very informative and awesome, Slu4 Thanks. Tiny C would be nice to see. Cheers.

  • @fus3n
    @fus3n Рік тому

    Bro has a degree on writing unreadable python code.

  • @zlatkovidlanovic6454
    @zlatkovidlanovic6454 9 місяців тому

    Ok ..where is code of this 168 lines of python code ..?

    • @slu467
      @slu467  9 місяців тому

      Its in the Minimal UART CPU repo (link in the description). Just click on "Min Language"...

    • @zlatkovidlanovic6454
      @zlatkovidlanovic6454 8 місяців тому

      thanks slu4 ...it is really interesting to see source code , very cool and easy to understand even i am not python programmer at all ...
      just to ask ...functions as such seems that are implemented because you have test code
      -------------------------------------
      def add_this(a, b, &c)
      r = a + b + c
      return r
      x = 100
      print add_this(1, 10, x)
      .-----------------------------------
      which looks to me like function..do i have right ?
      also which python version i need , i have Portable py 3 i think
      all best

  • @xyz2112zyx
    @xyz2112zyx Рік тому

    Lua could be a good candidate! Or maybe micro-Lua (whatever it is... hahaha!)

  • @Borszczuk
    @Borszczuk 2 роки тому +2

    Hm, you do not seem to be too good in following naming conventions :)

    • @slu467
      @slu467  2 роки тому +2

      True, true, ... conventions are not my cup of tea ;-)

  • @jorgegomes83
    @jorgegomes83 Рік тому +1

    2:35 You could have saved a few lines by using Python's string methods to check whether the char is a digit, a letter, and soon.

  • @hahayes7205
    @hahayes7205 2 роки тому +1

    you can just tell he intentionally formatted it terribly for the 168 lines title

    • @slu467
      @slu467  2 роки тому

      There are probably alternatives with more spaces out there.

  • @ayoodawg1104
    @ayoodawg1104 2 роки тому +1

    tiny C

  • @halfsourlizard9319
    @halfsourlizard9319 Рік тому

    Fusing your lexer/parser/evaluator is a terrible/unscalable way to write an interpreter.

    • @RelayComputer
      @RelayComputer 5 місяців тому

      He is not meant to follow the books. What he did is quite remarkable because of simplicity.

  • @zlatkovidlanovic6454
    @zlatkovidlanovic6454 8 місяців тому +1

    Hi slu4 ..it is me again...sorry if i bothering you
    i find int.py code and i load it into PyScripter - Portable_Python 3251
    code is executed and after RUN i get this:
    .................................................................................
    USAGE: int.py
    Exit code: 1
    .................................................................................
    so my question is :
    what i need to do to run your example demo.txt or test.txt ?
    thanks in advance