To use this website fully, you first need to accept the use of cookies. By agreeing to the use of cookies you consent to the use of functional cookies. For more information read this page.

Official ZPE/YASS documentationAssignment and variables

Variables are normally declared with a $ token at the start of them, as with PHP, Perl, Bash and many other scripting languages, or as of ZPE 1.9.3/YASS 21 declared using the let keyword in which case they can be unbound from the $ token.

Variables should not be names of keywords, e.g. $is or $list are not valid names for variables.

YASS
$a = 43
$b = true
$c = false
$d = "Hello"
$e = $a || $b
$f = $c
  

As of version 1.5.3 the variable index syntax has been included, however, it should be avoided before version 1.6.3 since it was buggy.

YASS
$a = [43, 44, 45]
print($a[1])
    

Non-standard values

Version 1.5.0 added support for hexadecimal and octal values straight from the compiler. The compiler works these out and transforms them to their integer equivalents during compile time.

The following sample shows this:

YASS
$hex = 0x43
$octal = 0o33
        

Hexadecimal numbers start with a 0 (zero) followed by an x followed by an hexadecimal values.

Octal numbers start with a 0 (zero) followed by an o and then octal numbers.

These styles were borrowed from C.

Data types and TYPO

ZPE 1.6.7 adds support for typing through the TYPO typing system. TYPO is a runtime-based typing system designed for educational and learning use but also to add stricter programming practices to ZPE.

ZPE has 8 primitive types as of version 1.6.7 (prior to that it had several others, but most have since been combined into the object syntax such as the image type). Although there are 8 underlying primitive types there are only 6 different ways to declare them. The types and their declarative keywords are shown below:

The types are as follows:

Data type Keyword Example Purpose
Mixed - $x = "Hello" $x = 10 To hold a value of any data type and be dynamic in it's type.
String string string x = "Hello" To hold text or any character from the keboard.

Integer number number x = 10 To hold a numerical value
Real number x = 10.3
Boolean boolean boolean x = true To hold a true or false value
List list list x = [1, 2, 3, 4] To hold a list, array, map or ordered map
Map list x = [1 => 99, 2 => 98, 3 => 97]
Function function function x = function () print ("Hello") end function To hold a reference to a function or an actual anonymous function
Object object object x = {name : "Pete"} To hold an object value, which itself can contain other properties.

ZPE does not distinguish the difference between an integer value and a double value until assignment has taken place. The same goes for lists and maps, which both come under the List type but are represented differently in memory.

When setting the data type of a variable as of version 1.9.7 (Galashiels, July 2021), the declare keyword is used:

YASS
declare $a as string = "Hello"
declare $b as number = 10
declare $c as number = 10.5
declare $d as boolean = true
declare $e as list = ["Hello", "World"]
declare $f as list = ["Hello" => "World"]
declare $g as function = function() { print("Hello world") }
declare $h as object = {name : "Jamie"}
  

Further to this, ZPE 1.9.7 (Galashiels, July 2021) adds support for C-style type declarations:

YASS
string $a = "Hello"
number $b = 10
number $c = 10.5
boolean $d = true
list $e = ["Hello", "World"]
list $f = ["Hello" => "World"]
function $g = function() { print("Hello world") }
object $h = {name : "Jamie"}
  

ZPE 1.9.7 (Galashiels, July 2021) also permits the definition of types within a structure/class:

YASS
structure tester

private number val1 = 10
private declare val2 as number = 10
public function setter($v) this->val1 = $v end function public function getter() return this->val1 end function end structure function main() $x = new tester() print($x->getter()) $x->setter(15) print($x->getter()) end function

As with all built-in ZPE keywords, types and the declare and as keywords are all lowercase when using the case sensitive compiler.

Using the declare keyword, variables can also be unbound from the $ token but they must be specified with a data type whenever the declare keyword is used.

YASS
declare a as string = "Hello"
declare b as number = 10
declare c as number = 10.5
declare d as boolean = true
declare e as list = ["Hello", "World"]
declare f as list = ["Hello" => "World"]
declare g as function = function() { print("Hello world") }
declare h as object = {name : "Jamie"}
  

Inline assignment

Version 1.6.7 also added inline assignment which was originally thought to be a complicated procedure but ended up being relatively simple. This syntax allows declarations to take place within other expressions, e.g. an if statement:

YASS
$f = new SequentialFile()
$f->open_file("list.txt")

while (($check = $f->has_next()) == true)
  print($f->read_line())
end while
  

Notice that the assignment is bracketed! This is so important to prevent the compiler parsing it as a boolean assignment.

YASS
$x = get_input("Insert a value.")

if (($v = $x + 3) == 10)
  print($v)
end if
  

This type of assignment was added in ZPE 1.6.7 and improved over time.

Multiple assignments

Actually seen as an odd side effect of the compiler (and I'm not entirely sure which version added support for it as of yet), multiple assignment in ZPE/YASS is very easy:

YASS
$w = $x = $y = $z = 10
print($w)
print($x)
print($y)
print($z)
  

As mentioned above, I am not sure which version this was added in since it is a side-effect of some other compiler feature and was not intentional.

Using the let keyword in ZPE 1.9.3+/YASS 21+

Using the let keyword you can easily assign variables:

YASS
let $x = 10
print($x)
  

ZPE 1.9.3 (Hawick) brought one of the most desired features to ZPE/YASS, variable names that do not need preceeded by a $ sign.

It does this without using any late inference to determine variables but does it during compile time. It's important to note that the create_variable function creates variables that have the $ sign at the start of them since those variables cannot effectively be checked at compile time.

YASS
let x = 10
print(x)
  

This also works where variables can be assigned, e.g. in a for loop:

YASS
for (let x = 0 to 10)
  print(x)
end for
  
The let keyword can also be substituted with the var, making the syntax more like ES6 and JavaScript.

Using the be keyword

As of version 1.9.6 (St Boswells), it is now possible to declare a variable using the be keyword. This is a user friendly alternative way of saying set the value to.

YASS
let x be 10
  

Variable name tester

Use this form to test a variable name.

Variable name tester

Use this tool to test a variable name for validity.

Comments
jamiebalfour04:
2021-06-29 18:58:04
Finally, typing that works!
TYPO has been a major project in ZPE and after testing it out, I am happy to say with ZPE 1.9.7 (nightly) it finally works!
New comment
Reply
Code previewClose
Feedback 👍
Comments are sent via email to me.