This tutorial teaches how to evaluate an expression .
Making the Custom Block
This implementation does not include support for functions or unary negation. This means if you want to calculate -5 * 10
you will have to rephrase it as (0-5) * 10
Variables needed
You will need to create these Variables :
You will also need to create these Lists :
Шаблон:Note
And a list called operators with these items:
-
+
/
*
Full block
This Project has the script for easy backpacking.
define evaluate expression set i to 1 delete all of tokens repeat length of expression set letter to letter i of expression set last token to item length of tokens of tokens if 123456.7890 contains letter then if last token / 1 = last token or last token = . then replace item length of tokens of tokens with join last token letter else add letter to tokens end else if ()+-/* contains letter then add letter to tokens end end end delete all of stack delete all of queue set i to 1 repeat length of tokens set token to item i of tokens if token / 1 = token then add token to queue end if operators contains token then repeat until item 1 of stack = ( or length of stack = 0 or item # of item 1 of stack in operators < item # of token in operators add item 1 of stack to queue delete 1 of stack end insert token at 1 of stack end if token = ( then insert token at 1 of stack end if token = ) then repeat until item 1 of stack = ( or length of stack = 0 add item 1 of stack to queue delete 1 of stack end if item 1 of stack = ( then delete 1 of stack end end change i by 1 end repeat until length of stack = 0 add item 1 of stack to queue delete 1 of stack end repeat until length of queue = 0 set token to item 1 of queue delete 1 of queue if operators contains token then if token = + then insert item 2 of stack + item 1 of stack at 1 of stack end if token = - then insert item 2 of stack - item 1 of stack at 1 of stack end if token = / then insert item 2 of stack / item 1 of stack at 1 of stack end if token = * then insert item 2 of stack * item 1 of stack at 1 of stack end delete 2 of stack delete 2 of stack else insert token at 1 of stack end end set result to item 1 of stack
Explanation
The problem of evaluating an expression can be broken down into 3 steps:
Tokenizing the expression (turning the input into a list of symbols and numbers)
Converting the tokens to postfix format (where the operator goes after the 2 operands)
Evaluating the postfix expression with a stack machine (calculating the answer)
Tokenizing the expression
The expression must first be converted into a list. Every number must have all its digits in the same list element.
This script will tokenize an expression:
set i to 1 delete all of tokens repeat length of expression set letter to letter i of expression set last token to item length of tokens of tokens if 123456.7890 contains letter then if last token / 1 = last token or last token = . then replace item length of tokens of tokens with join last token letter else add letter to tokens end else if ()+-/* contains letter then add letter to tokens end end
Converting the tokens to postfix format
Next the Shunting yard algorithm is used to convert the tokens from infix (A op B) to postfix (A B op). This is done because it's a lot less code to calculate a postfix expression than an infix one.
This script will run the shunting yard algorithm on the tokens list:
Шаблон:Note
delete all of stack delete all of queue set i to 1 repeat length of tokens set token to item i of tokens if token / 1 = token then add token to queue end if operators contains token then repeat until item 1 of stack = ( or length of stack = 0 or item # of item 1 of stack in operators < item # of token in operators add item 1 of stack to queue delete 1 of stack end insert token at 1 of stack end if token = ( then insert token at 1 of stack end if token = ) then repeat until item 1 of stack = ( or length of stack = 0 add item 1 of stack to queue delete 1 of stack end if item 1 of stack = ( then delete 1 of stack end end change i by 1 end repeat until length of stack = 0 add item 1 of stack to queue delete 1 of stack end
Calculating the result with a stack machine
This script will calculate the result of the postfix expression:
repeat until length of queue = 0 set token to item 1 of queue delete 1 of queue if operators contains token then if token = + then insert item 2 of stack + item 1 of stack at 1 of stack end if token = - then insert item 2 of stack - item 1 of stack at 1 of stack end if token = / then insert item 2 of stack / item 1 of stack at 1 of stack end if token = * then insert item 2 of stack * item 1 of stack at 1 of stack end delete 2 of stack delete 2 of stack else insert token at 1 of stack end end set result to item 1 of stack
Using the Custom Block
when clicked evaluate 5 + 5 say return
If it worked, your sprite should say 10.