Discuss Scratch

lillyfern_games
Scratcher
8 posts

Python engine in scratch

I'm currently making a python engine in scratch. So far it can print hello world. I'm wondering if anyone knows how to make it interpret any words using the print (“…”) function. Is this possible in scratch? I'm using lists for the code. Has anyone done this sort of thing before?

Last edited by lillyfern_games (May 3, 2020 23:52:20)

ElsieBreeze
Scratcher
100+ posts

Python engine in scratch

You'll need to write a Lexer, Parser and Interpreter inside Scratch for this to work.

This may be of some use: https://ruslanspivak.com/lsbasi-part1/
marcelzietek2006
Scratcher
500+ posts

Python engine in scratch

Well this would be just for you!
https://scratch.mit.edu/projects/387534263/
TheAspiringHacker
Scratcher
100+ posts

Python engine in scratch

Hi! I saw this post last night when your project could only handle “print(”Hello world!“)” and was going to respond, but I was busy with homework. I see that you've updated your project to print any arbitrary string. That's a good first step.

I was going to say something like:

Consider the fact that there are infinite programs. If you can handle print(“Hello world!”), I can give you print(“Hello world!0”). If you can handle print(“Hello world!0”), I can give you print(“Hello world!00”). And that's not just limited to statements about printing a string that contains “Hello world!”

Right now, when you parse a command, you require the argument of print to be inside double quotes. What if I wanted to do

x = "Hello world!"
print(x)

or even

x = 1
y = 1
print(x + y)
?

The big idea about programming and programming languages is the recursive nature of programs. Do you notice that in Scratch, scripts are comprised of smaller scripts? Scratch programs can be arbitrarily large, so in order to evaluate them, you must appeal to recursion and write a function that calls itself on the sub-programs.

In the 2.0 era, on my old account, I made a programming language that handles arbitrarily large programs with recursion: https://scratch.mit.edu/projects/103551203/ However, I found it unmaintainable because Scratch didn't provide me with enough abstractions, and I don't know if it still works in 3.0 (I don't make any guarantees).

I've been interested in programming language theory for a long time now and can talk a lot about it. I will direct you to the following resources:
If you don't know one of these languages, pick one and learn it. There is an educational value in learning different programming languages, especially ones that are different from what you may know. If you're most fluent in Scratch, learning one of these languages will give you a bigger idea about what programming is about, and some of the concepts you learn will even help you implement your own languages. (e.g. Java uses a virtual machine. What is that? Can you use a virtual machine for your own language, perhaps? What are the stack and heap? What is garbage collection?)

Last edited by TheAspiringHacker (May 4, 2020 12:15:56)

lillyfern_games
Scratcher
8 posts

Python engine in scratch

TheAspiringHacker wrote:

Hi! I saw this post last night when your project could only handle “print(”Hello world!“)” and was going to respond, but I was busy with homework. I see that you've updated your project to print any arbitrary string. That's a good first step.

I was going to say something like:

Consider the fact that there are infinite programs. If you can handle print(“Hello world!”), I can give you print(“Hello world!0”). If you can handle print(“Hello world!0”), I can give you print(“Hello world!00”). And that's not just limited to statements about printing a string that contains “Hello world!”

Right now, when you parse a command, you require the argument of print to be inside double quotes. What if I wanted to do

x = "Hello world!"
print(x)

or even

x = 1
y = 1
print(x + y)
?

The big idea about programming and programming languages is the recursive nature of programs. Do you notice that in Scratch, scripts are comprised of smaller scripts? Scratch programs can be arbitrarily large, so in order to evaluate them, you must appeal to recursion and write a function that calls itself on the sub-programs.

In the 2.0 era, on my old account, I made a programming language that handles arbitrarily large programs with recursion: https://scratch.mit.edu/projects/103551203/ However, I found it unmaintainable because Scratch didn't provide me with enough abstractions, and I don't know if it still works in 3.0 (I don't make any guarantees).

I've been interested in programming language theory for a long time now and can talk a lot about it. I will direct you to the following resources:
If you don't know one of these languages, pick one and learn it. There is an educational value in learning different programming languages, especially ones that are different from what you may know. If you're most fluent in Scratch, learning one of these languages will give you a bigger idea about what programming is about, and some of the concepts you learn will even help you implement your own languages. (e.g. Java uses a virtual machine. What is that? Can you use a virtual machine for your own language, perhaps? What are the stack and heap? What is garbage collection?)
Thanks! I'm pretty familiar with Java, my dad is a software developer and he uses Java, Java script and python. I'll look into those suggestions. Thanks for the help and feedback!
christo
Scratcher
5 posts

Python engine in scratch

Wow! Have you written any other programming language before? If so, what did you write it in? If something other than scratch then you might gain a lot of value from doing this first. If scratch is your first programming language then I think you can make a programming language in it as long as you start with the simplest thing that could possibly work and then step up towards Python gradually.
lillyfern_games
Scratcher
8 posts

Python engine in scratch

christo wrote:

Wow! Have you written any other programming language before? If so, what did you write it in? If something other than scratch then you might gain a lot of value from doing this first. If scratch is your first programming language then I think you can make a programming language in it as long as you start with the simplest thing that could possibly work and then step up towards Python gradually.
I've worked in JavaScript and python before. Python is my first programming language, as my dad uses it (he is a software developer)
badatprogrammingibe
Scratcher
500+ posts

Python engine in scratch

I recommend starting with a slightly less ambitious programming language, such as implementing lisp.
The structure of lisp programs is very basic, however it is still a similar structure to most programming languages (most likely including Python), and it should give you some ideas for implementing programming languages.
Lisp has also been implemented a few times already in scratch (see https://scratch.mit.edu/studios/5404836/ ) so you can use those projects for reference should you need help.

After implementing a simpler language such as lisp, you should get the hang of parsing and ASTs and all that jazz, and it would be less difficult to program a full python engine in scratch.

(You could also try implementing an even simpler language such as a subset of Forth or brainf***, however this wouldn't really reflect the structure of a python interpreter.)
ZZC12345
Scratcher
500+ posts

Python engine in scratch

Sorry to bump this topic.

I've been looking into also trying to implement Python in Scratch, though it wouldn't be complete and looks pretty hard.
I'm not going to even write the parser at all - I'm thinking about implementing the Python Virtual Machine (PVM, docs here). Any tips? It's hard to implement basic data types. I've been reading several articles about the Python bytecode.

Powered by DjangoBB