Discuss Scratch

Wes64
Scratcher
500+ posts

ZZ Documentation (programming language built inside of Scratch)

ZZ Language is a database language built inside of Scratch that executes code stored in scratch variables. It is able to represent many data types and can store and execute procedures.

What it has
  • Nested lists
  • Control flow (for, while, if, etc…)
  • Procedural abstraction
  • Variable declaration and storage
  • Big library of math, list stuff, and string stuff
  • A few glitches…

There are about 50 built-in broadcasts in the project, all prefaced with “zz_”, which control the built-in primitive functions. There are 4 sprites: 3 libraries, and the interface, which prints text on the screen. The language is not case sensitive. All code is written in prefix form.

I will do my best to document the functions, because it is very large and has unusual, somewhat inefficient syntax.

This post details the many basic functions that make the language, as well as literals and control flow.

Data Types
The type of an object is determined by special characters present within the string.

Numeric types
int - integer-valued number such as 42, 1.0, 10E4.

float - floating point (decimal) number such as 3.14, 10E-4, 0.1.

Sequence types
str - string delimited by apostrophe character, such as 'Hello world', '42', and the null string ''. May not contain other apostrophe characters.

list - list of objects separated by commas. Commas within other lists or strings are ignored. The list is delimited by braces.
Examples:
[1,2,3]
['a','b','c']
[[1,2,3],['a','b','c']]
[]
Commands
name - these are essentially pointers. It can contain any character, but cannot be a list, str, int, or float. There are no delimiters. Examples: x, temp, a5.

cmd - stores a piece of code delimited by curly braces. The commands are always of the form procedure-name arguments, where the arguments are separated by single spaces, and a single space separates the procedure-name and the first argument. Arguments may not contain space characters unless they are sequences or other cmd objects. The procedure-name may not contain spaces. You can connect many commands with semicolons, and commands may be nested. Examples: {add 4 3}, {sto x 5;sto y 6}, {sto x add 4 3}.

Keywords
Keywords are objects of type name that are reserved. Procedures and variables may not be assigned a name that is a keyword. Keywords return themselves.

Boolean values
true - the boolean value true.

false - the boolean value false.

Control
none - represents “no return value”. The none value is not pushed to the return stack when a procedure is evaluated, meaning that if none is returned, the value is deleted.

error - if this value is returned by a procedure, the entire execution is terminated and a “traceback” is printed to the terminal. This represents the case when invalid syntax, arguments of the incorrect type, or insufficient arguments are found.

Variables and procedures
Variables are stored in a scratch list, ordered sequentially. This allows lookup time to be O(logn) using binary search. Procedures are stored elsewhere.

Commands for variables only
sto varname varvalue - creates or overwrites the variable varname in the memory and assigns it varvalue. If varname is invalid (is a keyword or procedure already), error is raised, otherwise none is returned. Examples: sto temp 7, sto temp add 4 3, sto temp add 1 rcl temp.

rcl varname - returns the value associated with varname in the memory. If varname is not found, error is raised. Example: rcl temp.

del varname - deletes the variable associated with varname from the memory. Returns none.

ls - returns a list object of all variable names in the memory.

Commands for procedures only
proc procname args code - defines a new procedure with the name procname. args defines how many arguments the new procedure takes, and should be a positive integer. code is a cmd-type object that defines what the procedure does in relation to its arguments. Recursion is possible. This function returns none. Examples:
Square root:
proc sqrt 1 {pow arg 1 0.5}

Exclusive or:
proc xor 2 {and or arg 1 arg 2 not and arg 1 arg 2}

Factorial:
proc factorial 1 {if less arg 1 2 {return 1} {mul arg 1 factorial sub arg 1 1}}

arg n - returns the nth argument passed to a procedure. This function is only used within the code argument when a procedure is defined.

doc procname description - this is used to tell the help system what a procedure does. This is not necessary, but it is practical. The procname is the name of a procedure represented as a str type (with apostrophes), and is not a name. This function returns none.

help procname - returns a brief description of the function, as defined by the doc command. All built-in functions have been defined for the help system. procname is a string-type (with apostrophes) representation of the procedure name. Examples: help ‘factorial’, help ‘add’.

code procname - returns the code associated with the procedure given by the string procname as a cmd object.

kw - returns a list object of all hardcoded (programmed in scratch) procedure names. I will not be describing these procedures here because the help system is set up for all builtins.

pr - returns a list object of all user-defined procedure names in the memory. The ‘builtin library’ contains some primitive procedures, which are stored here as well.

Commands for both variables and procedures
clear - deletes all variables, procedures, and help documentation, and returns none. This is the only way to delete a procedure.

Control flow
Return
return value - returns the value entered. Because basic objects such as int, float, str, and cmd do not return themselves (they raise errors), you must use return to get the value. This is necessary only when you want a literal, so if you have used add 4 5, ir is not necessary to return. However, you must use return 9 if you aren't calling other functions.

Dynamic execution
exec cmd - cmd is a cmd-type object. This function will execute the code within the cmd and return none.
Examples: exec {sto temp 7}, exec {sto a 4;sto b 3;add rcl a rcl b}, exec entry 1, exec rcl temp.

if condition true-cmd false-cmd - if condition is equal to true, the true-cmd is executed. Otherwise, the false-cmd is executed. Both of these options should be cmd-type objects, but condition is not. Returns none. Examples: if true {return 2} {return 3}, if equal 3 3 {return 2} {return 3}, if less rcl x 4 {if less rcl y 4 {output ‘a’} {output ‘b’}} {output ‘c’}.

for name list command - each item in list is assigned to the name, and then the command is executed. Returns none. Examples: for x {output rcl x}, for x {for y {output rcl x;output rcl y}}. Nested for loops are known to be finicky and are being debugged.

Easy recursion - these are really just recursive procedures.
while condition-cmd do-cmd - a while loop. Executes the do-cmd until the condition-cmd returns true. Both of these are command objects. Examples: while {true} {output ‘Hello’}, sto i 0;while {less i 5} {output rcl i;sto i add 1 rcl i}.

loop n do-cmd - Executes the do-cmd n times. Example: loop 4 {output ‘a’}.

Interface
User interface
ans - returns the result of the last query the user entered.

entry n - returns the nth query the user entered as a cmd object.

get value - opens up a scratch list with value inside, so the user can copy/paste the value to other programs. Returns none.

input prompt - prompt should be a string object. The user is asked the prompt, and the entered value is returned.

Scratch interface
send - returns the value of the (send) variable in the scratch program.

Terminal interface
output ]value - writes the value on the terminal.

Other functions
More functions are described in the help system. Simply call kw or pr and choose a name, make sure you've put apostrophes on either side, and call help. Example: help ‘add’.

I will try to write about every function at some time in the future as well as post many more examples.

Comments are possible by making the first character of the line a “>”. This MUST be the first character.

Integration with Scratch
It is extremely simple to integrate with scratch.
set [QUERY v] to [your code here]
broadcast [QUERY v] and wait
the returned value is here
(return)

You can make a library from a list.
set [n v] to (1)
repeat (length of [library v])
set [QUERY v] to (item (n) of [library v])
broadcast [QUERY v] and wait
change [n v] by (1)
end

There are probably glitches, so report any code that misbehaves and I'll try to figure out what's going on.

Happy programming! I will answer any questions you might have.
Yoda3D
Scratcher
86 posts

ZZ Documentation (programming language built inside of Scratch)

Could you explain the syntax?
Wes64
Scratcher
500+ posts

ZZ Documentation (programming language built inside of Scratch)

Yoda3D wrote:

Could you explain the syntax?
the syntax is prefix notation, which means the operators go before the operands.

add 3 4 is addition of two numbers.

you can nest procedures like this: add 3 add 4 5, which is the same thing as add 3 9.

when you use the help function on ‘add’ it will say something like ‘add a b’. this is the syntax for that function - first is the name, ‘add’, then two arguments, ‘a’ and ‘b’. all of these are separated by spaces.

Last edited by Wes64 (Jan. 13, 2015 17:34:13)

MegaApuTurkUltra
Scratcher
1000+ posts

ZZ Documentation (programming language built inside of Scratch)

Cool stuff! It's kind of confusing but I'll try it out
drmcw
Scratcher
1000+ posts

ZZ Documentation (programming language built inside of Scratch)

Wes64 wrote:

add 3 add 4 5, which is the same thing as add 3 7.
.
I hope not

Last edited by drmcw (Jan. 13, 2015 10:02:44)

MegaApuTurkUltra
Scratcher
1000+ posts

ZZ Documentation (programming language built inside of Scratch)

Wes64 wrote:

you can nest procedures like this: add 3 add 4 5, which is the same thing as add 3 9.
ChocolatePi
Scratcher
1000+ posts

ZZ Documentation (programming language built inside of Scratch)

MegaApuTurkUltra wrote:

Wes64 wrote:

you can nest procedures like this: add 3 add 4 5, which is the same thing as add 3 9.
aputurk saves the day again!
Thepuzzlegame
Scratcher
1000+ posts

ZZ Documentation (programming language built inside of Scratch)

Very cool! Just curious, is ZZ an acronym for anything?
Wes64
Scratcher
500+ posts

ZZ Documentation (programming language built inside of Scratch)

Thepuzzlegame wrote:

Very cool! Just curious, is ZZ an acronym for anything?
its alphabetically last on the list of broadcasts.

i named all the built-in broadcasts “zz_” so they wouldn't clog up the messages list, and the name kind of stuck.
Thepuzzlegame
Scratcher
1000+ posts

ZZ Documentation (programming language built inside of Scratch)

Wes64 wrote:

Thepuzzlegame wrote:

Very cool! Just curious, is ZZ an acronym for anything?
its alphabetically last on the list of broadcasts.

i named all the built-in broadcasts “zz_” so they wouldn't clog up the messages list, and the name kind of stuck.
Neat
MegaApuTurkUltra
Scratcher
1000+ posts

ZZ Documentation (programming language built inside of Scratch)

I found 2 small bugs:

- Strings are not validated in the input command - http://i.gyazo.com/8fe7b528fd58e7b6994c6bf3b2ea13e9.png
- Double quote (which I use more than single quote for strings) isn't a recognized character - http://i.gyazo.com/76c54a3d21869f793d84ee5be4a7422a.png
Wes64
Scratcher
500+ posts

ZZ Documentation (programming language built inside of Scratch)

MegaApuTurkUltra wrote:

I found 2 small bugs:

- Strings are not validated in the input command - http://i.gyazo.com/8fe7b528fd58e7b6994c6bf3b2ea13e9.png
- Double quote (which I use more than single quote for strings) isn't a recognized character - http://i.gyazo.com/76c54a3d21869f793d84ee5be4a7422a.png
not glitches, the first one is just garbage in, garbage out type stuff. ther eis little to no input validation for many built-in functions, i merely wrote what was expected.
double quote is not recognized. this is again not a glitch, just how it is programmed.
MegaApuTurkUltra
Scratcher
1000+ posts

ZZ Documentation (programming language built inside of Scratch)

Wes64 wrote:

MegaApuTurkUltra wrote:

I found 2 small bugs:

- Strings are not validated in the input command - http://i.gyazo.com/8fe7b528fd58e7b6994c6bf3b2ea13e9.png
- Double quote (which I use more than single quote for strings) isn't a recognized character - http://i.gyazo.com/76c54a3d21869f793d84ee5be4a7422a.png
not glitches, the first one is just garbage in, garbage out type stuff. ther eis little to no input validation for many built-in functions, i merely wrote what was expected.
double quote is not recognized. this is again not a glitch, just how it is programmed.
Can you please add double quote? Or at least make it the default string symbol instead of single quote?

Also I think input validation should be there - I mean how do amateurs know they did something wrong (like me? I was confused for a sec when I tried it). I might remix your project to add validation.
Tropic
Scratcher
1000+ posts

ZZ Documentation (programming language built inside of Scratch)

MegaApuTurkUltra wrote:

Wes64 wrote:

MegaApuTurkUltra wrote:

I found 2 small bugs:

- Strings are not validated in the input command - http://i.gyazo.com/8fe7b528fd58e7b6994c6bf3b2ea13e9.png
- Double quote (which I use more than single quote for strings) isn't a recognized character - http://i.gyazo.com/76c54a3d21869f793d84ee5be4a7422a.png
not glitches, the first one is just garbage in, garbage out type stuff. ther eis little to no input validation for many built-in functions, i merely wrote what was expected.
double quote is not recognized. this is again not a glitch, just how it is programmed.
Can you please add double quote? Or at least make it the default string symbol instead of single quote?

Also I think input validation should be there - I mean how do amateurs know they did something wrong (like me? I was confused for a sec when I tried it). I might remix your project to add validation.
I agree about the double quotes
Wes64
Scratcher
500+ posts

ZZ Documentation (programming language built inside of Scratch)

the beauty of this language (i think) is that many of these validation things you want can be made as procedures.

for example, you want input validation for the output function: this procedure will do that.
proc outputv 1 {if equal type arg 1 str {output arg 1} {error}}

i am going to add double quote because it's a stupidly easy fix (literally just a few extra “or” blocks in the type function)
MasterOfArithmetic
Scratcher
32 posts

ZZ Documentation (programming language built inside of Scratch)

Doesn't using prefix notation make it impossible for order of operations to happen with math operators?
Example, multiply 3 add 4 3 is not the same as 3*4+3. multiply 3 add 4 3 would give you 21, when the real answer is 15. Does it sort the commands before executing?
Wes64
Scratcher
500+ posts

ZZ Documentation (programming language built inside of Scratch)

MasterOfArithmetic wrote:

Doesn't using prefix notation make it impossible for order of operations to happen with math operators?
Example, multiply 3 add 4 3 is not the same as 3*4+3. multiply 3 add 4 3 would give you 21, when the real answer is 15. Does it sort the commands before executing?
there is not order of operations. you must place the operations in the proper order when you write the command.

for example, 3 * (4 + 5) = 27. this is written mul 3 add 4 5.
however, (3 * 4) + 5 is 17. this is written add mul 3 4 5, or add 5 mul 3 4.
MrSherlockHolmes
Scratcher
500+ posts

ZZ Documentation (programming language built inside of Scratch)

Nice idea! I recently launched a programming language.
epicproductions36
Scratcher
21 posts

ZZ Documentation (programming language built inside of Scratch)

I thought it would function like actual Python…

Last edited by epicproductions36 (April 21, 2015 00:52:29)

PullJosh
Scratcher
1000+ posts

ZZ Documentation (programming language built inside of Scratch)

epicproductions36 wrote:

I thought it would function like actual Python…
Please don't bump old topics. ._.

Powered by DjangoBB