Discuss Scratch
- Discussion Forums
- » Show and Tell
- » TextScratch - A programming language in Scratch.
- Greg8128
-
Scratcher
500+ posts
TextScratch - A programming language in Scratch.
Another piece of useful advice: before you implement the language compiler, implement some sort of virtual machine code for it to compile to. I recommend a stack machine because they are relatively easy to write compilers for. Stack machines don't need to worry about assigning registers and can use a relatively small instruction set.This tells me I don't know anything about how programming languages work. I really don't get what you mean.
Don't worry, everyone has to start somewhere.
Code in the format that your computer runs is known as machine code. Machine code is designed to be simple and to run fast: it generally consists of a few simple pre-defined instructions which are executed sequentially. A program that converts programming language code to machine code is called a compiler.
One type of computer is a register machine, which stores numbers in variables known as registers. Register machines can do simple operations and save the result in a simple variable known as a register.
Let's consider a simple register machine with the following instructions
MOV A, n sets register A equal to the number n (n is NOT a register)
ADD A, B, C adds the numbers in registers A and B, and stores the result in register C
MUL A, B, C multiplies the numbers in registers A and B and stores the result in register C
Say that we need to compute 4 * 5 + 6 * 3. In register machine code, this could be:
MOV A, 4
MOV B, 5
MUL A, B, A
MOV B, 6
MOV C, 3
MUL B, C, B
ADD A, B, A
This format is looks strange, but it is great for computers because the computer doesn't need to figure out order of operations. It only has to run the code in sequential order, which is very easy to do and very fast.
The problem with register machines, however, is that the compiler needs to remember which register is storing what. For example, if we had used registers A and B again to compute 6 * 3, we would have overwritten our computed value for 4 * 5. Additionally, if we wanted to compute (4 * 5 + 6 * 3) as part of a more complex expression, say (4*5 + 6 *3) + (8 + 10 *2), then we would have to calculate the rest without ever
A solution to this problem is the stack machine model.
Stack machines use a different model of computation. A stack machine stores numbers in a list known as a stack. Instructions for stack machines can remove (pop) one or more numbers from the “top” of the stack (the end of the list), do calculations with them, and push (add) the result to the stack.
Let's revisit our example: what is 4 * 5 + 6 * 3? Let's see how this could be computed with a stack machine that has the following three commands:
PUSH n pushes the number n to the stack
ADD pops two numbers from the top of the stack, adds them, and pushes the result
MUL pops two numbers from the top of the stack, multiplies them, and pushes the result
To compute 4 * 5 + 6 * 3, we can do:
PUSH 4
PUSH 5
MUL
PUSH 6
PUSH 3
MUL
ADD
The advantage of the stack machine model is that it is easier to generate code for; if you need to compute the sum of two expressions, you can compute the expressions one after another, and then put an “ADD” opcode at the end. There's no need to worry about which registers are currently being used. Another advantage of the stack machine model is that the code is more compact; notice how the instructions have fewer inputs. This is an advantage on Scratch, where a list can only store 200,000 elements. (200,000 seems like a lot, but for substantially large projects it could be a problem)
I used the stack machine model with a few modifications for CRIS. However, I didn't do everything the “right” way and performance suffers a bit as a result. Right now I'm working on a project to do a better implementation of stack machine code.
- mybearworld
-
Scratcher
1000+ posts
TextScratch - A programming language in Scratch.
Thank you very much for that, I'll think of using that.
Let me test if I understand that code.
Looks like I misunderstood something here…
Do you know what I did wrong? I'd like not to code my program to do this…
Let me test if I understand that code.
PUSH 4 # stack: 4
PUSH 5 # stack: 4, 5
MUL # stack: 4*5=20
PUSH 6 # stack: 20, 6
PUSH 3 # stack: 20, 6, 3
MUL # stack: 20*6*3=360
ADD #stack: 360=360
Do you know what I did wrong? I'd like not to code my program to do this…
- Greg8128
-
Scratcher
500+ posts
TextScratch - A programming language in Scratch.
Thank you very much for that, I'll think of using that.You did everything correctly until the very end. MUL and ADD only multiply the last two numbers on the stack, not everything on the stack.
Let me test if I understand that code.Looks like I misunderstood something here…PUSH 4 # stack: 4
PUSH 5 # stack: 4, 5
MUL # stack: 4*5=20
PUSH 6 # stack: 20, 6
PUSH 3 # stack: 20, 6, 3
MUL # stack: 20*6*3=360
ADD #stack: 360=360
Do you know what I did wrong? I'd like not to code my program to do this…
It would work like this:
PUSH 4 # stack: 4
PUSH 5 # stack: 4, 5
MUL # stack: 4*5=20
PUSH 6 # stack: 20, 6
PUSH 3 # stack: 20, 6, 3
MUL # stack: 20, 6*6=18
ADD #stack: 10 + 18 = 38
This model can be extended beyond arithmetic. For example. to be able to store and retrieve variables, you could make a list called “memory”, add a bunch of blank slots, then add the following instructions:
READ pops one number (n) from the top of the stack and pushes the nth item in memory
WRITE first pops one number (n) from the top of the stack, then pops (v) from the top of the stack. It then replaces the nth item in memory with v.
If you have arithmetic operators, this is all you need in order to work with memory.
Last edited by Greg8128 (Jan. 13, 2021 09:37:14)
- mybearworld
-
Scratcher
1000+ posts
TextScratch - A programming language in Scratch.
I guess 3+4+5 would be
, then?
Or did I misunderstand that?
PUSH 3
PUSH 4
PUSH 5
ADD
ADD
This model can be extended beyond arithmetic. For example. to be able to store and retrieve variables, you could make a list called “memory”, add a bunch of blank slots, then add the following instructions:Like this..?
READ pops one number (n) from the top of the stack and pushes the nth item in memory
WRITE first pops one number (n) from the top of the stack, then pops (v) from the top of the stack. It then replaces the nth item in memory with v.
READ 0
PUSH hello
WRITE 0
- gosoccerboy5
-
Scratcher
1000+ posts
TextScratch - A programming language in Scratch.
Wow, I still couldn't catch that. I'll try re-reading it a couple times
Last edited by gosoccerboy5 (Jan. 13, 2021 14:31:52)
- mybearworld
-
Scratcher
1000+ posts
TextScratch - A programming language in Scratch.
Wow, I still couldn't catch that. I'll try re-reading it a couple timesSame here… but I think I got it.
- Greg8128
-
Scratcher
500+ posts
TextScratch - A programming language in Scratch.
I guess 3+4+5 would beYep! That would be correct, then?PUSH 3
PUSH 4
PUSH 5
ADD
ADD
Having the address be a fixed argument works if it is known ahead of time. However, you often want to read from a calculated address, so there should be an instruction that obtains the address from the stack.This model can be extended beyond arithmetic. For example. to be able to store and retrieve variables, you could make a list called “memory”, add a bunch of blank slots, then add the following instructions:Like this..?
READ pops one number (n) from the top of the stack and pushes the nth item in memory
WRITE first pops one number (n) from the top of the stack, then pops (v) from the top of the stack. It then replaces the nth item in memory with v.READ 0Or did I misunderstand that?PUSH hello
WRITE 0
Say that the 1st item in memory is a variable “x”, and you want to read the (x+3)th item in memory. Then you could do
PUSH 1 # 1
READ # x
PUSH 3 # x, 3
ADD # (x+3)
READ # (item (x+3) of memory)
Likewise, if you want to replace the (x+3)th item in memory with “100”, you could do
PUSH 100 #100
PUSH 1 # 100, 1
READ # 100, x
PUSH 3 # 100, x, 3
ADD # 100, (x+3)
WRITE #
One more thing:
This is an “assembly” representation of machine code. It has the same general structure as machine code, but with a few differences:
1. The instructions have an easy-to-understand name instead of a number. Using numbers for actual machine code is slightly better, in part because it makes it easier to set up an efficient set of nested if-else statements to find which instruction to run. (Sending a message depending on a variable is another idea, but it is slower for some reason).
2. The fixed arguments (like the “1” in “PUSH 1”) are on the same line as the instruction. In Scratch machine code, it is more efficient to have the fixed argument be on the line after the instruction, because you don't have to run a loop to split each instruction into the instruction name and its arguments.
Last edited by Greg8128 (Jan. 13, 2021 22:13:48)
- mybearworld
-
Scratcher
1000+ posts
TextScratch - A programming language in Scratch.
Makes sense. My “memory” could simply be a list with empty slots, a lot of them. (So the stack).
I've got to make it assign a variable with a number so the code actually remembers what variable is what number in memory.
I've got to make it assign a variable with a number so the code actually remembers what variable is what number in memory.
- Greg8128
-
Scratcher
500+ posts
TextScratch - A programming language in Scratch.
Makes sense. My “memory” could simply be a list with empty slots, a lot of them. (So the stack).
I've got to make it assign a variable with a number so the code actually remembers what variable is what number in memory.
This works for global variables. But say that you want to have local variables for functions. Then it gets a bit more complicated. In order to do it, languages like C use a “call stack”: https://en.wikipedia.org/wiki/Call_stack.
- mybearworld
-
Scratcher
1000+ posts
TextScratch - A programming language in Scratch.
can I helpI'm not sure how.
- Chewyblockguy
-
Scratcher
100+ posts
TextScratch - A programming language in Scratch.
I can do graphic design some code and I have a github and repl.it acountcan I helpI'm not sure how.
- samq64
-
Scratcher
1000+ posts
TextScratch - A programming language in Scratch.
Very cool! I was going to do something slimier except you code as if you were writing a script with the scratchblocks tag in the forums. I haven't made much progress though.
- Greg8128
-
Scratcher
500+ posts
TextScratch - A programming language in Scratch.
You should try to complete TextScratch imo, it looks promising
- gosoccerboy5
-
Scratcher
1000+ posts
TextScratch - A programming language in Scratch.
You should try to complete TextScratch imo, it looks promisingYeah, I agree. And who knows more about creating a language in Scratch than @Greg8128?

- gosoccerboy5
-
Scratcher
1000+ posts
TextScratch - A programming language in Scratch.
Bump. Keep working, no rest breaks!
- mybearworld
-
Scratcher
1000+ posts
TextScratch - A programming language in Scratch.
no rest breaks!No rest bumps!
- notwait
-
Scratcher
100+ posts
TextScratch - A programming language in Scratch.
i will not let you rest-break!no rest breaks!No rest bumps!
- mybearworld
-
Scratcher
1000+ posts
TextScratch - A programming language in Scratch.
i will let myself rest-break!i will not let you rest-break!no rest breaks!No rest bumps!
- notwait
-
Scratcher
100+ posts
TextScratch - A programming language in Scratch.
1 < 2i will let myself rest-break!i will not let you rest-break!no rest breaks!No rest bumps!
no




