Guide to variables and data types in Sncode

**variables&data types **- Guide to variables and data types in Sncode.

Variables are composed of a value of a specific type located in memory and a human-readable name that points to it.

And variables can store data of different types, and different data types can do different things.

Sncode has 8 built-in data types for variables:

The first step is to name your variable. Names can contains either letters `[a-zA-Z]`

, numbers `[0-9]`

or `_`

but cannot begin with a number:

A__ = 1; // valid _a4 = 1; // valid 4b_ = 1; // invalid

A value is then assigned to a variable by using the `=`

operator.

In Sncode, the type of a variable is **dynamic**. You don't need to declare variables before using them because their type is defined by the type of the value that is assigned to it:

a; // initially undefined a = 5; // now an integer a = "Hello"; // and finally a string

An array is a sequence of items of any types. An array has an initial length that can be increased but never reduced.

Arrays are defined like this:

a = [ 1, 2.1, true, "abc", null, undefined];

Items are accessed by their index (starting at 0):

a[3]; // returns "abc"

array can also be get with a range operator:

a[start:end];

start (starting from zero) and end with the last element that you want in the array.

start and end can also be negative which compute the index from the end of the array.

If end is greater than the length of the array, you will get the last entry in the arry without any error.

For example:

a = [ 'p', 'i', 'e', 'r', 'r', 'r', 'e'];

a[1]; // return i

a[1:2]; // return ["i"]

a[1:]; // return ["i","e","r","r","e"]

a[:1]; // return ["p"]

a[1:-1]; // return ["i","e","r","r"]

a[-2:-1]; // return ["r"]

a[1:200]; // return ["i","e","r","r","e"]

a[1:200]; // return ["i","e","r","r","e"]

A string is also an array:

s = "string";

s[1];// return t

s[1:2];// return t

s[1:-1];// return trin

s[-2:-1];// return n

More on strings, please see string

An associative array, map, symbol table, or dictionary is an abstract data type composed of a collection of [(key, value)] pairs, such that each possible key appears just once in the collection:

a = { "x" : 1, "y" : 2 };

Keys can contain strings, variables or function calls (see examples below).

Once declared, items of an associative array can be accessed in two ways:

a["x"]; a["x"] = 1; // or a.x; a.x = 1;

**Note that associative array in Sncode also called CONTEXT is NOT ordered since it's an implementation of hash table.**

A bool is a binary variable that can take one of those two values: `true`

or `false`

.

a = true;

Represents a real number. In mathematics, a real number is a value that represents a quantity along a continuous line.

The real numbers include all the rational numbers, such as the integer −5 and the fraction 4/3, and all the irrational numbers such as √2 (1.41421356…, the square root of two, an irrational algebraic number) and π (3.14159265…, a transcendental number).

The internal representation of this type is similar to a double in the C programming language.

a = 2.4;

An integer (from the Latin integer meaning "whole") is a number that can be written without a fractional component. For example, 21, 4, 0, and −2048 are integers, while 9.75, 5½, and √2 are not.

The set of integers consists of zero (0), the natural numbers (1, 2, 3, ...), also called whole numbers or counting numbers, and their additive inverses (the negative integers, i.e. −1, −2, −3, ...).

There are 3 ways to declare an integer:

a = 56; a = 0x56; // Hexadeciaml number a = 0x101010; // Binary Number

A variable set to `null`

means that it is defined but holds no value at all.

A string is a sequence of character implemented as an array. To represent a series of characters as a string, you have to enclose them in either **single** or **double** quotes.

Double quotes replace special characters (`\n`

, `\\`

, `\a`

, `\b`

, `\v`

, `\f`

, `\t`

, `\r`

) by their corresponding value whereas single quotes handle them literally as they appear. In other words, The strings that are enclosed in double-quotes will be interpreted/evaluated by the complier.

Characters | Description |
---|---|

`\n` |
Linefeed |

`\\` |
Backslash |

`\a` |
Alert |

`\b` |
Backspace |

`\v` |
Vertical tab |

`\f` |
Form feed |

`\t` |
Horizontal tab |

`\r` |
Carriage return |

For instance:

a = "abc\ndef"; a;

will output:

abc def

Whereas,

a = 'abc\ndef'; a;

will output:

abc\ndef

range operator can also be used in string:

a ="pierre";

a[1]; // return i

a[1:2]; // return "i"

a[1:]; // return "ierre"

a[:1]; // return p

a[1:-1]; // return "ierr"

a[-2:-1]; // return "r"

a[1:200]; // return "ierre"

a[1:200]; // return "ierre"

The type of any variable that has not yet been assigned a value to.

A variable can be use as a reference for another variable in an assignement as in

a = "varname";

&a = "value";

In this case, value will be put in the variable varname. This is available starting at version 5.85 of Sncode.

Using a variable as a key:

x = "laplante"; a = { x : 1, "y" : 3 };

Using a function as a key:

function f() return "x"; endf a = { f() : 1, "y" : 3 };

Written by Pierre Laplante, <laplante@sednove.com> and Jean-Georges Guenard, <jg@sednove.com>

Edit© 2023 extenso Inc. All rights reserved.