-
Notifications
You must be signed in to change notification settings - Fork 3
BBScript: BBScript 2: Variables & Logic
Variables can be created and assigned in BBScript, with their scope being limited to the post the script is defined in. Variables are expected to only contain alphabets (a-z or A-Z) or underscore (_) in their name, and are case-sensitive. BBScript has no dynamic variable naming, and expect all variables to be a literal without any starting/ending quotes. This means foo is the variable foo.
BBScript 2 has a unique resolution for undeclared variables, where if a literal being referenced is not an existing variable, it will be treated as string. For example, if foo is not yet defined, it will be treated as the string "foo".
To create a variable, = operator is used. In the following snippet, the variable foo is assigned the value "bar".
(= foo "bar")
(print foo)
At print, it will log the string "bar" into the console.
Sets a value to the variable name.
(= varName value)
varName
: The name of the variable to set the value to or create. This should be single string or literal conforming to the restrictions of only alphabets or underscores (a-zA-Z_).
: This can be a string or resolve to a string, but it is better for clarity to make this a literal.
value : The value to be assigned to the variable. This can be anything.
Prints the value of the arguments in the console. The console can be accessed by right clicking on the page, and clicking on the console tab.
(print arg ...)
arguments unlimited : The list of values to print to the console.
Increment the value of a variable by 1. Operation is done inplace.
(++ varName)
varName : The variable to increment the value of. The variable must refer to a number value.
Decrement the value of a variable by 1. Operation is done inplace.
(-- varName)
varName : The variable to decrement the value of. The variable must refer to a number value.
Performs a conditional check and executes the then or else block.
(if (conditional) (then) (else))
conditional : The conditional block to be tested. Should resolve to a boolean, but will use Javascript truthy-falsy values.
(then) : Sub-block to execute if conditional is true.
(else) : Sub-block to execute if conditional is false.
Return : This function can return the value of the then/else block depending on the conditional and will pass the value to the parent. By this way, if statements can be chained inside other functions.
Returns true or false if the first argument is loosely equal to the second argument.
(== val1 val2)
arguments
: The two values to compare against each other. Performs loose evaluation, i.e. number 1 is equal to string "1".
Return : Returns true or false based on conditional.
Returns true or false if the first argument is not loosely equal to the second argument.
(!= val1 val2)
arguments : The two values to compare against each other.
Return : Returns true or false based on conditional.
Returns true if all arguments evaluate to true, false otherwise. Performs quick fallout if any argument resolves to false and prevents following arguments from being evaluated.
(and arg1 arg2 ...)
arguments unlimited : Arguments that should be true or false. Should resolve to a boolean, but Javascript truthy-falsy is also valid.
Return : Returns true if all arguments resolve to true. False otherwise.
Returns true if any argument evaluates to true, false otherwise. Performs quick fallout if any argument resolves to true, and prevents following arguments from being evaluated.
(or arg1 arg2 ...)
arguments unlimited : Arguments that should be true or false. Should resolve to a boolean, but Javascript truthy-falsy is also valid.
Return : Returns true if any argument resolves to true. False otherwise.
Returns true or false if the first argument is loosely greater than to the second argument.
(> val1 val2)
arguments
: The two values to compare against each other. Performs loose evaluation, i.e. number 2 is greater than string "1".
Return : Returns true or false based on conditional.
Returns true or false if the first argument is loosely greater than or equal to the second argument.
(>= val1 val2)
arguments
: The two values to compare against each other. Performs loose evaluation, i.e. number 1 is greater than or equal to string "1".
Return : Returns true or false based on conditional.
Returns true or false if the first argument is loosely less than to the second argument.
(< val1 val2)
arguments
: The two values to compare against each other. Performs loose evaluation, i.e. number 2 is less than string "1".
Return : Returns true or false based on conditional.
Returns true or false if the first argument is loosely less than or equal to the second argument.
(<= val1 val2)
arguments
: The two values to compare against each other. Performs loose evaluation, i.e. number 1 is less than or equal to string "1".
Return : Returns true or false based on conditional.
Ends the execution of the script.
(stop)
Delays the execution of a provided script by a given amount of seconds. Does not block the rest of the script from executing.
(setTimeout duration (function))
duration : The amount of time to wait before executing the given function in seconds. Must resolve to a number.
(function) : A script to execute at the end of the timeout.
Returns
: Returns a handle id that can be used to pre-maturely stop the timer. See clearTimeout.
Prematurely end the timer of a setTimeout function and prevent execution of associated script.
(clearTimeout id)
id
: The handle id value that the setTimeout returned.
Example: The following script sets a timeout of 1 second. At the end of 1 second, it will print "hello world". However, there is a clearTimeout of the handle that immediately stops the timer and prevents the print.
(= handle (setTimeout 1 (print "hello world")))
(clearTimeout handle)
Continuously the execution of a provided script every given amount of seconds. Does not block the rest of the script from executing.
(setInterval duration (function))
duration : The amount of time to wait between each execution of the given function in seconds. Must resolve to a number.
(function)
: A script to execute every duration seconds.
Returns
: Returns a handle id that can be used to stop the cycle. See clearInterval.
Stops the cycle of the setInterval function and prevent any further execution of associated script.
(clearInterval id)
id
: The handle id value that the setInterval returned.
Example: The following script sets a interval of 1 second. Every 1 second, it will print "hello world". However, there is a clearInterval of the handle that immediately stops the cycle and prevents the print.
(= handle (setInterval 1 (print "hello world")))
(clearInterval handle)
Returns the current time in milliseconds.
(time)
Return : Returns the number of milliseconds since the epoch. Resolve to a number
Combines multiple script functions as a single function block. This is often used in if, setTimeout, setInterval, and each.
(group (script1) (script2))
scripts unlimited : The scripts to be bundled together as one single function
Returns : Returns the value of the last script if applicable
Example: Is often used to chain commands together in if statements. In the following example, if 1 equals 1, it will print "hello" and "test"
(if (== 1 1) (group
(print "hello")
(print "test")
)
)
As BBScript 2 ignores whitespace, it is better for clarity to separate group scripts into individual lines.
Performs a given function on each element of an array. This acts like a for-of loop in Javascript.
(each array (function) token)
array : The array of elements to perform the function on.
(function) : The function to apply the logic on a given element. The element can be referenced using the token provided.
token optional
: The token to use to reference the element. Must resolve to a string.
: Default - by default, uses _
Example:
(= sum 0)
(each [1 2 3] (group
(print _)
(= sum (+ sum _))
))
(print sum)
Will print:
1
2
3
6
See also BBScript 2 Arrays