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.
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 :)
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.
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!)
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.
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.
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.
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!
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?
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 !
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
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
Always viewed interpreters as beig indistinguishable from magic, this video goes a long way to demystifying the methods. Thank you
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.
@@talideon Excellent pointers for further reading, thank you
I like your solution for "automatic" operator prioritization of multiplication and adding. Great video!
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 :)
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!
That's very neat indeed. I love the use of a /single/ dictionary for all kinds of identifiers. That's quite cool !
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.
Hi David, yep, no # in string literal ;-) thanks for pointing this one out.
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!)
Thanks, Giacomo, I currently think in the same direction. A BASIC with Python's indentation style would be something. Cheers!
Man !! You are incredibly brilliant teacher ! I really love your videos. Many thanks 😊
You are very welcome. Glad you like my stuff ;-)
this tutorial was amazing, thanks a lot,
Wow.. brilliantly explained! Thank you sir!
For minimal systems of the past, I always loved FORTH as it barely even requires any interpretation.
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.
this complicated task was done so simply!
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.
Hi Krumpy, da finden wir ganz ähnliche Fragen spannend. Ich hoffe, mein Kanal hilft dir weiter!
Tiny C would be very interesting
This is a really good video! I'm currently making my own programming language called King in JavaScript, so this was very insightful :D
Terrific! Thanks for this video!
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.
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!
Thank you for this very informative walkthough
Oh, a new Slu4 video. :D
You sort of remind me of Bisqwit, if he went into more detail about what he was doing
I thought I was the only one who thought abt bisqwit hahaha
Logo looks like good learning language.
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?
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 !
Yes! A new video
heres the entire python interpreter rewritten in python in just 1 line
exec("code")
I love interpreters! BASIC is cool :)
Yes this is very informative and awesome, Slu4 Thanks. Tiny C would be nice to see. Cheers.
Bro has a degree on writing unreadable python code.
Ok ..where is code of this 168 lines of python code ..?
Its in the Minimal UART CPU repo (link in the description). Just click on "Min Language"...
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
Lua could be a good candidate! Or maybe micro-Lua (whatever it is... hahaha!)
Hm, you do not seem to be too good in following naming conventions :)
True, true, ... conventions are not my cup of tea ;-)
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.
you can just tell he intentionally formatted it terribly for the 168 lines title
There are probably alternatives with more spaces out there.
tiny C
Fusing your lexer/parser/evaluator is a terrible/unscalable way to write an interpreter.
He is not meant to follow the books. What he did is quite remarkable because of simplicity.
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