Skip to content

03. The Basics

Tom Dodd edited this page Dec 8, 2023 · 10 revisions

DSSL scripts are read as a list of tokens, consisting of keywords, symbols, operators, numbers, characters, strings and names separated by comments and whitespace (spaces, tabs, and newlines). All fixed tokens are listed here. Each token that is read has an associated action which interacts with the internal data structures of DSSL: the stack and the hierarchy.

The stack is the global last in, first out store for elements, which consist of values and labels. In order to push to the stack, we could simply write a token, such as a string value:

"Hello, world!"

We could then print this string, which will also pop it from the stack:

"Hello, world!" println

At the end of this program, the stack is empty. Any elements still on the stack when the program ends are simply discarded.

We can define and modify variables using the def keyword and assignment operator =:

/msg "What's you name?" def
msg println
/msg "Hello " read "!" ~ ~ =
msg println

Here, we define a variable with the label /msg, and access the value using its associated identifier msg. The read keyword pushes a string from user input to the stack, and the concatenation operators ~ join the three separate strings together. Finally, we assign a new value to the variable msg before printing it again.

The hierarchy keeps track of all currently accessible variables and classes for the current scope of the program. An executed script will have a global scope at the root of the hierarchy, but variables and classes defined within a block, denoted by a matching pair of braces { and }, will only be accessible within that block and further internal blocks. Imported scripts will have their own hierarchies, the root of which becomes an accessible sub-hierarchy of the current scope once the import is complete.

For example, the following code would print inner_x, inner_y, inner_x, outer_y:

/x "outer_x" def
/y "outer_y" def
{
    /x "inner_x" =
    /y "inner_y" def
    x println
    y println
} exec
x println
y println

Here we see a key difference between the assignment operator = and the def keyword: the former modifies a pre-existing variable, while the latter makes a new shadowing definition in the current scope. Note that in DSSL, blocks have to be explicitly executed with the exec keyword, since they become elements on the stack.

An important detail is that the origin of the label determines the scope of the variable assignment or definition. Two labels with the same identifier are only equal if originating from the same scope. For example, the following code would print success:

/x
{
    "success" def
} exec
x println

Clone this wiki locally