Login

Literals

Literals

Anselm tries to be consistent in how literals are treated in a way that

Literals come in the following categories

  • Integer
  • Fractional
  • Real
  • Sequence
  • String

Sequences

Anselm uses square brackets [] for all "sequence-like" literals. This includes tuples, arrays, linked lists, vector, and more.

def
  x :: array(int) := Arr[1, 2, 3];
  y :: tuple(Int) := Tup[1, 2, 3];
exec
  # ...
end

Strings

Anselm string literals are very flexible. There are three different string delimiters, each of which have different properties:

  • Single quotes 'foo'
  • Double quotes "bar"
  • Back quotes `quux`

In addition, each of these literal forms can take a prefix.

Multiline Literals

Anselm does not have in-built support for multi-line string literals. I considered but rejected something like Zig's system, where each line of the literal is prefixed with delimiter that works along the same principles as a line comment, and then all consecutive lines are concatenated. Instead, the Anselm Basis library defines a special function ++ which takes care of this for you. E.g.

def
  multi :: String := "foo"    ++
                     "  bar"  ++
                     "  baz"  ++
                     "  quux"
exec
  # ...
end

The ++ operator concatenates two strings and joins them with a newline character in between. Admittedly, this is a bit more verbose, but in my opinion, the need for multi-line literals in practice is pretty rare. If you need to include large amounts of text, basis files can textually include files as strings. And for DSLs and what not, I think this is a better approach.

Custom Literals

Anselm also has extensive support for defining custom literals. These are a great a way to make the syntax flexible enough to support elaborate eDSLs without the need for macros.