Expressions

The syntax gives names for branches and local variables. An expression combines those forms into a program written as data. The first item in a list is the operator. The other items are its arguments. You write 2 + 3 * 4 in this form:

edn17 B
["+" 2 ["*" 3 4]]
Result / 11 B
{:value 14}

An expression is an ordinary document. Stardust keeps it, examines it, and records each change to it. A native document keeps the expression without a text conversion. DUST gives a readable form of that document.

A string at the start of an array calls a known function or form. If the name does not resolve, the array constructs a list and evaluates each item. This lets an HTML tag such as h2 head node data while its children use expressions. Known forms still validate their arguments, and an explicit ?function call must resolve a local function. A nested fn parameter may shadow a name in an enclosing function or let; names within one parameter list must be unique.

edn237 B
{:#_ada {:name "Ada"
         :role "engineer"
         :age  36}
 :#_bob {:name   "Bob"
         :role   "engineer"
         :age    41
         :mentor {:#link "#_ada"}}
 :#_cy  {:name "Cy"
         :role "designer"
         :age  29}}

Variables

Every variable binding and reference uses ?name, including let names and fn parameters. A reference uses the inner declaration first. A reference without a declaration reads caller input. A plain word in value position is always text.

edn10 B
["+" ?x 1]
Result / 11 B
x
41
{:value 42}

With the caller variable x set to 41, this expression gives 42. An expression contains no inputs and no results. Each evaluation gives one value and the cost of the calculation.

An expression calculates its result from its variables. A standalone expression needs no database or external service.

True and false values

Only null and false are false. All other values are true. The number 0, the empty text, and the empty list are also true. The branch operators use this rule.

edn31 B
["if" 0 "zero-is-true" "never"]
Result / 23 B
{:value "zero-is-true"}

Exact numbers

An integer has 64 bits and keeps its exact value. A float has 64 bits in binary form. Stardust does not change the type of a value automatically. Each operator sets the type of its arguments. One argument with a known type therefore sets the type of the others.

  • [+ 9007199254740993 1] gives the exact value 9007199254740994.
  • [/ 6 3] gives 2. [/ 7 2] gives 3. [/ 6.0 3.0] gives 2.0.

An integer result outside the 64-bit range is an error. The value does not change to a negative number.

edn27 B
["+" 9223372036854775807 1]

Error

Runtime_Failure: Integer addition exceeds the signed 64-bit range.

This rule also applies to abs, incr and decr. A float argument gives a float. Use incrWrap or decrWrap to wrap an integer explicitly. They accept only integers.

edn32 B
["incrWrap" 9223372036854775807]
Result / 29 B
{:value -9223372036854775808}

Arithmetic operators do not change text into a number.

edn11 B
["+" "5" 1]

Error

Runtime_Failure: A number is required, but the value has type string.

An integer and a float are different types, also when they have the same value. [= 1 1.0] is false, and [!= 1 1.0] is true. This rule also applies to the items of lists and maps. An arithmetic operator or an ordered comparison with an integer and a float is an error. When both arguments are literal numbers, the error occurs when Stardust compiles the expression.

edn41 B
[">" 9007199254740993 9007199254740992.0]

Error

Compile_Failure: Ordered comparison of an integer and a float requires an explicit toInt or toFloat conversion.

Use toInt or toFloat to change one argument first.

edn23 B
["+" ["toFloat" 1] 1.0]
Result / 12 B
{:value 2.0}

Changing a value type

Use toInt, toFloat, toText, or toBool when an operator needs a different value type. Each operator accepts one argument.

toInt keeps an integer and changes a boolean to 1 or 0. It also reads signed decimal integer text.

edn15 B
["toInt" "-42"]
Result / 12 B
{:value -42}

A float loses its fraction toward zero. NaN, infinity, and values outside the signed 64-bit integer range give an error.

toFloat keeps a float, changes an integer to a float, and reads decimal or exponent text. The result can lose integer precision. A boolean gives an error.

edn17 B
["toFloat" "1e3"]
Result / 15 B
{:value 1000.0}

toText gives plain text for numbers and booleans. A float without a fraction gives no decimal point, so [toText 2.0] gives 2. It gives null as the text null, and gives lists and maps as JSON.

edn26 B
["toText" ["list" 1 true]]
Result / 19 B
{:value "[1,true]"}

toBool accepts a boolean, the exact text true or false, and the integer 1 or 0. Other values give an error.

These operators reject spaces around numeric text and values outside the applicable range. toInt rejects decimal text such as 4.0.

Use floor, ceil, or round when you must control how a float becomes an integer. Use format when you must control the text digits.

Cost

The native evaluation reports measured CPU time in 100-nanosecond ticks. The evaluation options use the same unit for the cost limit. Evaluating gives the details.

Types

Stardust calculates the type of the result and the type of each variable from their use. Each operator sets the types of its arguments and its result. Stardust changes no type automatically. One argument with a known type therefore sets the other types. A type that stays unknown becomes any.

Syntax gives the branch operators, the names that you set, the functions, and the literal values.

Run it

build · program evaluate.