This is a stack based language similar to Forth, but for Scratch. This is just a fun project I made in a day, but I might add to it later. Edit: I am adding to it later :P If you type a number, it will be added to the stack. If you type an operator like +, it will take the two last items off of the stack, replacing them with their sum. Multiple can be placed on the same line, separated by spaces (or newlines if you paste in the code) For example, "1 1 +" will instantly put 2 on the stack. These operators function the same as they do on Scratch: +, -, *, /, =, <, >, and, or, not, and % (which is mod) The word "dup" duplicates the last item on the stack. "over" adds the second last item to the stack again. "rotate" moves the third last item to the last item (E.g. "1 2 3 rotate" leaves 2 3 1 on the stack) "pop" removes the last item of the stack "if" will only run the code up to the next "then" if the last item on the stack is true. (E.g. "1 1 = if 17 then" will put 17 on the stack if 1 = 1, which it does) A loop can be constructed like this: "loop [condition] if [code to loop] while" It will repetitively run code to loop while the condition is true. Here is a loop that adds the numbers 0-10 to the stack: 0 loop dup 10 < if dup 1 + while Anything between the words "(" and ")" is ignored and is treated as a comment. Words must always be separated by spaces so ( this can be a comment but ) (this is not a comment) v0.1 NEW: macros! You can define a macro as so: "macro [macro name] [macro contents] #" (E.g. "macro avg + 2 / #" will add a macro that takes the average of two numbers. Then running "6 8 avg" will output 7) v1.2 NEW: strings! "This is a string! quotes and backslashes can be added by adding a backslash before: \", \\" Also added the letter and join operators from Scratch: "apple " "banana" join "apple" 1 letter Here are a bunch of useful macros: macro dup2 over over # macro pop2 pop pop # macro swap over rotate pop # macro nip dup rotate pop2 # macro true dup dup = # macro false dup dup > # macro ? dup if rotate then pop2 # macro min dup2 > ? # macro max dup2 < ? # macro length 1 loop dup2 letter "" = not if 1 + while nip 1 - # macro floor dup 1 % - # macro // / floor # macro sqrt 1 0 loop dup 8 < if rotate rotate dup2 / + 2 / rotate 1 + while rotate pop2 #
SORTH PROGRAMS: Here is a code that will calculate the square root of a number using Newtons method: 1 0 loop dup 10 < if rotate rotate over over / + 2 / rotate 1 + while rotate pop pop This one will find the prime factors of a number: 2 loop over over > if over over % if 1 + else dup rotate over / over rotate pop then while pop Here is one I wrote to approximate PI: ( newton iteration ) macro newton ( square guess -- square better_guess ) over over / + 2 / # 0 ( count ) 0.000005 ( y ) loop dup 1.000005 < if 1 over dup * - ( 1^2-y^2 ) ( sqrt ) dup 1 + 2 / ( initial guess ) newton newton newton newton newton newton 0 rotate pop pop ( count y x ) rotate + ( y count+x ) 0 rotate + ( count y+0 ) 0.00001 + ( y+=1/10000 ) while pop 4 * 0.00001 * This one just does it better: 3 2 loop dup 20000 < if dup 4 over dup 1 + dup 1 + * * / 4 rotate 2 + dup 1 + dup 1 + * * / - rotate + 4 rotate + while pop This one will run a simulation of rule 110: macro dup2 over over # macro pop2 pop pop # macro swap over rotate pop # macro nip dup rotate pop2 # macro SIZE 48 # macro RULE "01110110" # ( rule 110 ) ( generate initial state ) "" 1 loop dup SIZE < if swap " " join swap 1 + while pop "//" join macro get3b ( state idx -- val ) ( get the binary value of a group of three consecutive cells in a simulation state at a given idx ) dup2 letter "/" = 4 * rotate rotate 2 + dup2 letter "/" = 2 * rotate rotate 2 + letter "/" = + + # macro gen ( state -- state next ) "" 0 loop dup SIZE < if ( state next idx ) rotate swap dup2 2 * get3b ( next state idx val ) RULE swap 1 + letter if rotate "//" else rotate " " ( state idx next char ) then join swap 1 + ( state (next+char) (idx+1) ) while pop # 1 loop dup SIZE < if swap gen rotate 1 + while pop This one generates itself: macro p "macro p ""# macro j join # macro e ""# macro q ""# macro i over rotate pop j j # macro d q q j i # p q e q q j j j i q e e q j j j i q p d d d q j j i"# macro j join # macro e "\\"# macro q "\""# macro i over rotate pop j j # macro d q q j i # p q e q q j j j i q e e q j j j i q p d d d q j j i