The Programming Language DINO: Patterns Next Previous Contents

6. Patterns

Patterns have a form of expressions. They are used to match an expression and decompose it. Pattern can have form a vector value, a table value, a function call, an identifier, or a special wildcard symbol "_". Anything else in the pattern is treated as an expression. The simplest way is to treat a pattern, e.g. identfier, as an expression is to put it into paretheses.

           Pattern = Expr
           Expr = "_" | "..."

A separate wildcard matches any value. Identifier also matches any value but additionally the matched value is assigned to the pattern variable denoted by the identifier. An expression in a pattern matches any value equal to the the expression value.

A vector pattern matches a vector value. All vector pattern elements should match all subsequent elements of the vector value. If a vector pattern element has a form of another pattern than it matches the corresponding vector value element iff the element pattern matches the corresponding element value of the vector value.

A vector pattern element can also have a form expr ":" pattern. The value of the expression before ":" is converted into an integer value by default. If the value after the conversion is not integer, the exception optype is generated. The value can be zero or negative. In this case matching the pattern element is always successful and this matching does not correspond any element of the matched vector value. Othewise, the pattern after ":" should match the corresponding elements in the vector value. The exact number of the elements is defined by the expression value before ":". The matched element values should be equal unless the pattern after ":" is a wildcard "_".

The last element of the vector pattern can be "...". It matches the rest elements in the matched vector value. The following are examples of vector patterns (a, b, and c are identifies of pattern variables):

           [a, b]   // a and b are pattern variables
           [(a), b] // value of earlier declared a is used
           [a, ...]
           [2, [a, b], 3 : c, 4 : _]

A table pattern matches a table value. All table pattern elements should match all elements of the table value. If a table pattern element has a form expr, then the table value should have an element with a key given by the expression.

A table pattern element can also have a form expr ":" pattern. In this case the table value also should have an element with a key given by the expression. Additionally the element value with given key should match the pattern after ":".

The last element of the table pattern can be "...". It matches the rest elements in the matched table value. The following are examples of table patterns (a, b, and c are identifies of the pattern variables):

           tab ["k1", "k2"]
           tab ["k1" : _, ...]
           tab ["k" @ 1, [a, b], "k2" : c, ...]

An object pattern has a form of function/class call. An object pattern matches a class/function instance. The function/class should be a subtype of class/function given by the value of the expression in the pattern before "(". The parameters in the pattern should match all corresponding instance parameter values. The number of the values is taken from function/class given by the expression in the pattern before "(", not from function class of the instance. Remember that the arguments corresponding to "..." in function/class parameter definitions become one array value.

The last parameter of the object pattern can be "...". It matches the rest arguments of the corresponding object. The following are examples of object patterns (a, b, and c are identifies of the pattern variables):

           leaf (10)
           node (_, a)
           node (node (a, b), leaf (c))

Symbols "_" and "..." can be used only in patterns - they can not be used in expressions.


Next Previous Contents