The formatting on this page has been broken because the blocks are not finished. If you can help fix the formatting, please do! (Date?)

This is how to make a custom block for evaluating mathematics expressions.

## Making the Custom Block

First, you will need the following variables:

• TokenizeIter
• TokenizeCollect
• return

and the following lists:

• Tokens
• symbols
• outputQueue
• operatorStack
• stack

Then make this script:

 Note: This script can be difficult to create for new Scratchers.
```define evaluate (expr)
set [TokenizeIter v] to [0] // Basic Tokenization Algorithm
set [TokenizeCollect v] to []
delete (all v) of [Tokens v]
repeat (length of (expr))
change [TokenizeIter v] by (1)
if <[symbols v] contains (letter (TokenizeIter) of (expr))> then
if <(TokenizeCollect) = []> then
set [TokenizeColllect v] to []
else
set [TokenizeCollect v] to []
end
add (letter (TokenizeIter) of (expr)) to [Tokens v]
else
set [TokenizeCollect v] to (join (TokenizeCollect)(letter (TokenizeIter) of (expr))
end
end
if <not <(TokenizeCollect) = []>> then
end
delete (all v) of [outputQueue v] // Shunting yard algorithm
delete (all v) of [operatorStack v]
repeat until <(length of [Tokens v]) < [1]>
if <((item (1 v) of [Tokens v]) * (1)) = (item (1 v) of [Tokens v])> then
add (item (1 v) of [Tokens v]) to [outputQueue v]
delete (1 v) of [Tokens v]
end
end
if <<<(item (1 v) of [Tokens v]) = [+]> or <(item (1 v) of [Tokens v]) = [-]>> or <<(item (1 v) of [Tokens  v]) = [*]> or <(item (1 v) of [Tokens v]) = [/]>>> then
if <<(item (1 v) of [Tokens v]) = [+]> or <(item (1 v) of [Tokens v]) = [-]>> then
repeat until <<(length of [operatorStack v]) = [0]> or <(item (last v) of [operatorStack v])= [(]>>
add (item (last v) of [operatorStack v]) to [outputQueue v]
delete (last v) of [operatorStack v]
end
end
if <<(item (1 v) of [Tokens v]) = [*]> or <(item (1 v) of [Tokens v]) = [/]>> then
repeat until <<<(item (last v) of [operatorStack v]) = [+]> or <(item (last v) of [Tokens v]) = [-]>> or <<(length of [operatorStack v]) = [0]> or <(item (last v) of [operatorStack v]) = [)]>>>
add (item (last v) of [operatorStack v]) to [outputQueue v]
delete (last v) of [operatorStack v]
end
end
add (item (1 v) of [Tokens v]) to [operatorStack v]
delete (1 v) of [Tokens v]
else
if <(item (1 v) of [Tokens v]) = [(]> then
add (item (1 v) of [Tokens v]) to [operatorStack v]
delete (1 v) of [Tokens v]
else
repeat until <(item (last v) of [operatorStack v]) = [(]>
add (item (last v) of [operatorStack v]) to [outputQueue v]
delete (last v) of [operatorStack v]
end
delete (last v) of [operatorStack v]
delete (1 v) of [Tokens v]
end
end
repeat until <(length of [operatorStack v]) = [0]>
add (item (last v) of [operatorStack v]) to [outputQueue v]
delete (last v) of [operatorStack v]
end
delete (all v) of [stack v]
repeat until <(length of [outputQueue v]) = [0]> //Stack Machine
if <(item (1 v) of [outputQueue v]) = [+]> then
add ((item ((length of [stack v]) - [1]) of [stack v]) + (item (last v) of [stack v])) to [stack v]
delete ((length of [stack v]) - [1]) of [stack v]
end
else
if <(item (1 v) of [outputQueue v]) = [-]> then
add ((item ((length of [stack v]) - [1]) of [stack v]) - (item (last v) of [stack v])) to [stack v]
delete ((length of [stack v]) - [1]) of [stack v]
end
else
if <(item (1 v) of [outputQueue v]) = [*]> then
add ((item ((length of [stack v]) - [1]) of [stack v]) * (item (last v) of [stack v])) to [stack v]
delete ((length of [stack v]) - [1]) of [stack v]
end
else
if <(item (1 v) of [outputQueue v]) = [/]> then
add ((item ((length of [stack v]) - [1]) of [stack v]) / (item (last v) of [stack v])) to [stack v]
delete ((length of [stack v]) - [1]) of [stack v]
end
else
add (item (1 v) of [outputQueue v]) to [stack v]
end
delete (1 v) of [outputQueue v]
end
set [return v] to (item (1 v) of [stack v]) //Return Value
```

## Using the Custom Block

Make this script to test it:

```when green flag clicked
evaluate [1 * 2] :: custom
say (return)
```

If it worked, the sprite should say 2.