Тёмный

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

Slu4
Подписаться 7 тыс.
Просмотров 9 тыс.
50% 1

Опубликовано:

 

22 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 52   
@stupossibleify
@stupossibleify 2 года назад
Always viewed interpreters as beig indistinguishable from magic, this video goes a long way to demystifying the methods. Thank you
@talideon
@talideon 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
@StasPakhomov-wj1nn
@StasPakhomov-wj1nn Год назад
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 Год назад
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!
@glusiator
@glusiator 2 года назад
I like your solution for "automatic" operator prioritization of multiplication and adding. Great video!
@djc1402
@djc1402 2 года назад
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 года назад
Hi David, yep, no # in string literal ;-) thanks for pointing this one out.
@francoisdastardly4405
@francoisdastardly4405 2 года назад
Man !! You are incredibly brilliant teacher ! I really love your videos. Many thanks 😊
@slu467
@slu467 2 года назад
You are very welcome. Glad you like my stuff ;-)
@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 !
@giacomo.delazzari
@giacomo.delazzari 2 года назад
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 года назад
Thanks, Giacomo, I currently think in the same direction. A BASIC with Python's indentation style would be something. Cheers!
@edgeeffect
@edgeeffect 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.
@superkb172
@superkb172 10 месяцев назад
this tutorial was amazing, thanks a lot,
@BryanChance
@BryanChance Год назад
Wow.. brilliantly explained! Thank you sir!
@funwithalbi2425
@funwithalbi2425 2 года назад
this complicated task was done so simply!
@TheDarkelvenangel
@TheDarkelvenangel 2 года назад
Tiny C would be very interesting
@krumpy8259
@krumpy8259 2 года назад
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!
@eboatwright_
@eboatwright_ 2 года назад
This is a really good video! I'm currently making my own programming language called King in JavaScript, so this was very insightful :D
@kippie80
@kippie80 3 месяца назад
Logo looks like good learning language.
@talideon
@talideon 2 года назад
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 года назад
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
@massimogiussani4493
@massimogiussani4493 2 года назад
Terrific! Thanks for this video!
@colonelbarker
@colonelbarker 2 года назад
Oh, a new Slu4 video. :D
@gazehound
@gazehound 2 года назад
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
@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?
@alooyeg
@alooyeg 2 года назад
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.
@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 !
@fus3n
@fus3n 11 месяцев назад
Bro has a degree on writing unreadable python code.
@xyz2112zyx
@xyz2112zyx Год назад
Lua could be a good candidate! Or maybe micro-Lua (whatever it is... hahaha!)
@Borszczuk
@Borszczuk 2 года назад
Hm, you do not seem to be too good in following naming conventions :)
@slu467
@slu467 2 года назад
True, true, ... conventions are not my cup of tea ;-)
@jorgegomes83
@jorgegomes83 Год назад
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.
@zlatkovidlanovic6454
@zlatkovidlanovic6454 9 месяцев назад
Ok ..where is code of this 168 lines of python code ..?
@slu467
@slu467 8 месяцев назад
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
@hahayes7205
@hahayes7205 2 года назад
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 года назад
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 месяцев назад
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
Далее
Computer Language from Scratch #1 Introducing MIN
9:03
Minimal VGA Expansion Card - DIY Video RAM #5
8:58
Просмотров 5 тыс.
What’s your height?🩷🙀💚
00:59
Просмотров 4,3 млн
Кольцо Всевластия от Samsung
01:00
Просмотров 528 тыс.
So you want to write an interpreter?
40:39
Просмотров 134 тыс.
Compilers, How They Work, And Writing Them From Scratch
23:53
Computer Language from Scratch #2 MIN at Work
17:54
Просмотров 2,3 тыс.
Projects Every Programmer Should Try
16:58
Просмотров 487 тыс.
What is the Smallest Possible .EXE?
17:04
Просмотров 433 тыс.
Let's Create a Compiler (Pt.1)
1:11:03
Просмотров 538 тыс.
how NASA writes space-proof code
6:03
Просмотров 2,3 млн
What’s your height?🩷🙀💚
00:59
Просмотров 4,3 млн