Grammar and types
This chapter discusses Quackscript's basic grammar, variable declaration, data types and literals.
Basicsβ
In Quackscript each statement must end with a π¦
Commentsβ
A comment in quackscript start with //
until the end of the line.
Comments are not executed, they exist to annotate your code. It is good practice to explain complex parts of your code with comments.
// this is a comment
You can also write a multiline comment by starting with /*
and ending with */
/*
this is a multiline comment
*/
Declarationsβ
Quackscript supports two types of declarations.
quack
Declares a variable.
QUACK
Declares a read-only constant which can't be changed.
Declaration and initializationβ
All variables must be declared before they are used, otherwise an exception is thrown.
You can use quack
and QUACK
to declare block-scoped variables. (See variable scope below.)
To declare a variable you use the following syntax:
quack aπ¦
A variable can be initialized and declared on the same line using the following syntax:
quack a <- 'hello world'π¦
A constant must always be initialized when declared.
QUACK a <- 'hello world'π¦
Variable scopeβ
In quackscript variables belong to the global scope or the code block.
A code block is defined as a pair of {
}
quack a <- 'hello world'π¦
QUACK b <- () {
QUACK c <- 32π¦
a <- 'bye world'π¦
}π¦
On the example above. The variable a
is on the global scope, which means is accessible anywhere on this file. Variable c
is declared inside a code block which means it is only accessible inside such code block. Variables are deleted from memory after the block execution is finished so after the execution of the function b
the variable c
is no longer in memory and a
has been changed to 'bye world'
.
Data typesβ
Quackscript provides the following data types:
boolean
-true
andfalse
.nothing
- A data type which denotes the lack of a value. Non initialized variables will containnothing
and non returning functions will returnnothing
number
- An integer or floating point number. Eg.32
or32.5
text
- A sequence of characters. Eg.'Hello World'
function
- A function definitionobject
- [not implemented]list
- [not implemented] A sequence of same type elementsvector2
- [not implemented] A 2 dimensional vector containingx
andy
number
valuesvector3
- [not implemented] A 3 dimensional vector containingx
,y
andz
number
valuesdictionary
- [not implemented] A collection of key value pairs where each key is unique and of the same type
Variable typingβ
Every variable will have a type associated with it. You can explicitly define the type after the variable name or let quackscript infer the type for you.
QUACK a:text <- 'hello world'π¦
// this will generate the same typed variable as above
// quackscript infers the type from the initialization
QUACK b <- 'hello world'π¦
Quackscript is a strongly static typed language which means once a variable type is defined you can no longer assign a value of a different type
quack a:text <- 'hello world'π¦
// The following statement will throw an error
a <- 23π¦
A variable can be optional which allows it to be the typed value or nothing
. You can declare a variable as optional by adding a ?
after the variable identifier
quack couldBeNothing?:stringπ¦
// At this point 'couldBeNothing' is 'nothing'
couldBeNothing <- 'hello world'π¦
// At this point 'couldBeNothing' is 'hello world'