Skip to content

Expressions

An expression is a document of coeval: an envelope { type, value }. Its JSON form is the same as its value in memory. Each language that uses coeval writes the same idea as the same document (decision D12 of coeval).

Builder Document Meaning
lit(42) { type: "lit", value: 42 } at the root, 42 in an argument A literal value
lit(undefined), lit(NaN) { type: ["ext", "undefined"], value: null }, { type: ["ext", "number"], value: "NaN" } A value that JSON cannot write
ref("box", "width") { type: "ref", value: ["world", "box", "width"] } A path. The host finds its first segment
app("max", a, b) { type: ["ops", "max"], value: [a, b] } An application of a named op to the values of the arguments
record({ area, label }) { type: "record", value: { area, label } } An object

These rules apply:

  • A reference starts at a root. names holds the parameters of the enclosing fn. world holds the other names, which the host finds. In a store, the first segment is a node ID.
  • The root of an expression is always an envelope. In an argument, a field or a body, a value that is not an envelope is a literal. A literal with the shape of an envelope is a lit envelope.
  • A dot in the op name of app splits it into keys: app("a.b") is { type: ["ops", "a", "b"], value: [] }.
  • isExpr checks a full document with the static kit of the kernel. The viewer uses it on each edit.

This is the document of the formula map(items, fn(["x"], x * scale)):

{ "type": ["ops", "map"], "value": [
{ "type": "ref", "value": ["world", "items"] },
{ "type": "fn", "value": { "params": ["x"], "body": { "type": ["ops", "*"], "value": [
{ "type": "ref", "value": ["names", "x"] },
{ "type": "ref", "value": ["world", "scale"] }
] } } }
] }

The JSON form is exact, but it is long. The formula language is the text form of the same documents. formatExpr(expr) gives the formula of an expression, and parseExpr(text) gives the document of a formula, or the reason of a failure and its position.

Formula Builder
42, "text", [1, 2], {"a": 1}, null, undefined, NaN, -Infinity lit(...). JSON, and the values that JSON cannot write
self.cells.value ref("self", "cells", "value")
self.`my key`.0 ref("self", "my key", "0"). A segment that is not a name goes in backticks
max(a, 1) app("max", ref("a"), lit(1))
a * 2 + 1 app("+", app("*", ref("a"), lit(2)), lit(1))
`my op`(x) app("my op", ref("x"))

The ops * and / bind more tightly than + and -, and these bind more tightly than <, >, <= and >=. Each infix op with two arguments has the infix form, and the other ops have the form of a call. For each document that the parser makes, parseExpr(formatExpr(e)) gives e again, also after JSON text.

The kernel has constructs that the formula language cannot write, for example let. formatExpr writes … for such a part, and the parser refuses ….

Five names of the formula language are the constructs of the kernel (decision D13 of coeval). They evaluate only the arguments that they need, and an op with the same name has no effect.

Form Document Semantics
if(test, then, else?) { type: "if", value: { test, then, else } } Only the selected branch evaluates. Without else, the value is undefined
and(a, b, ...) { type: "and", value: [a, b] } Stops at the first false value, and gives the value that decided
or(a, b, ...) { type: "or", value: [a, b] } Stops at the first true value, and gives the value that decided
fn(["x"], body) { type: "fn", value: { params: ["x"], body } } A function. Its body reads the parameters as names references
record("a", a, "b", b) { type: "record", value: { a, b } } An object. A field whose value fails is not in the object

A form that its construct cannot hold stays an op application, for example a record with a computed field name. The documents of version 0.1.1 (["ops", "if"], ["ops", "and"] and ["ops", "or"]) still evaluate.

A function that fn makes is a closure of the kernel. An op, for example map, can apply it. When the body of the function fails, the op that applies it fails too.

render has no interpreter of its own. The machine of render is the formula kit of the kernel with the profile of render. The truth of a value is the truth of JavaScript, and the values are data. evaluate(expr, resolve, ops, issues?) compiles the expression one time for each op registry, then the plan evaluates it.

The resolver gives the value of each world path:

  • objectResolver(ctx) reads a plain object. It reads only own properties, thus ["constructor"] does not find a function of Object.prototype.
  • The node store is the host thing of the root world. The kernel gives it each path in one call, and the deref op of the store answers.

The kernel also finds the free references of an expression (refsOf): these are the reads of a node. The parameters of a fn are not free, thus they are not reads.

The evaluation does not throw. A failure gives none. When the caller gives an array, evaluate adds the issues of the error of the kernel. The first issue is the cause, and each op around the cause adds an arg-none issue.

Code Cause
unknown-op The registry has no op with this name
path-miss The resolver has no value at the path
arg-none An argument of the op gave none
op-threw The op threw an error. The message of the error is in the issue
bad-form A form has incorrect arguments, for example an if without then
bad-expr The document is not a valid expression, for example too deep. The message has the reason of the kernel

The standard ops throw a type error for a value of an incorrect type, for example + for a text. The issue then names the op and the reason.

Write a formula in the playground. Open the document to see the coeval document that the formula makes.

The expression playgroundEdit the formula. The result changes as you type.
some(9)
the document of the formula: (w + 1) * h
{
  "type": [
    "ops",
    "*"
  ],
  "value": [
    {
      "type": [
        "ops",
        "+"
      ],
      "value": [
        {
          "type": "ref",
          "value": [
            "world",
            "w"
          ]
        },
        1
      ]
    },
    {
      "type": "ref",
      "value": [
        "world",
        "h"
      ]
    }
  ]
}

standardOps of @mark1russell7/render-splay has these ops:

Category Ops
math +, -, *, /, max, min, <, >, <=, >=, abs
data get, if, not, concat, eq, typeof, map, props, array, str, toString
summaries keys, count, join, brief, formula, call
measure textWidth, textHeight (placeholders)

The eq op compares plain data structurally, like the change detection of the engine. The summary ops give the boundary of a value: brief(v) gives { a, b } for an object, [3 items] for an array, and the formula of an expression. call(f, ...args) applies a function, for example call(self.dehydrate) in a summary method.