Types in StackLang

StackLang is a statically typed language. This typing is enforced with latent typing. There are six basic types in StackLang:

  • Booleans: Booleans are either true or false.
  • Commands: Commands are either the name of a primitive function or the named of a defined function.
  • Numbers: StackLang numbers are arbitrary precision rational numbers. All StackLang numbers are displayed as fractions or as integers. However, numbers can be parsed as decimals or as fractions.
  • Strings: A StackLang string is an arbitrarily long string of ASCII characters.
  • Substacks: Substacks are a way to "group" stack elements together in StackLang. Stacks are similar to lists in Lisp, Scheme, and Racket - they can only be operated on from one end, and can be decomposed by getting the top and the rest of the substack.
  • Types: StackLang has a limited capability for reflection - it is able to get the type of an element and manipulate that information. A type is one of the six primitive types, or the special Any type.
Functions and commands check for the correct number and type of arguments before being evaluated, so any arity of type errors will be caught at the soonest time possible. When this happens, a TypeError is thrown.

Type Specialziation

A type specialziation is when there is additional information after the type in parenthesis. For example, a Substack(Number) is a substack of numbers. Only substacks may have a specialization. Substacks are specialized to indicate what type they can hold. In between the parenthesis should be another type (including further specialized types). For example, Substack(String) is a substack of strings. A Substack(Substack(Number)) is a substack of substacks of numbers - a 2d grid of numbers. An unspecialized Substack is a substack of any type.