Code · Modules | Program

Modules · Data · Code

Program's own code as data. Code models a running Program in two forms: the recursive Code.Language.Json.Value tree that mirrors the JSON command form every program is written in, and the serialised syntax tree under Code.Language.Program.Structure — modules, functions, records, unions, and the commands inside a body. Reader functions decode JSON into these structures, a stringifier serialises them back, and the Code.Parse helpers thread parsing state through a rule. Every value is reactive, so a decoded tree recomputes when its source text changes.

A JSON value: text, a number, a boolean, an array of values, or an object mapping names to values. Arrays and objects nest, so the type is recursive.

An argument in a function call: a reference to a value, an inline closure, or an inline constant.

An assignment (=) command: a named, typed local bound to a computed value.

The right-hand side of an assignment: a reference to a value, or a function call.

One command inside a function body: a comment, state, constant, assignment, call, or reverse.

A constant (#) command: a named, typed constant with its literal value.

A literal constant value: text, integer, real, boolean, or none.

The direction of an input or return: forward, reverse, or binding.

A named, typed field within a record or union type.

A function or native declaration: its name, generics, inputs, body, and optional return.

A function body: either a list of commands, or a map of per-platform native implementations.

A call to a function, with its name, type arguments, and arguments.

A function input: its name, type, and direction.

A module declaration: its name and its contents.

One member of a module: a comment, record type, union type, function, native, or native type.

A native type declaration: its name, type parameters, and per-platform implementations.

A single platform's native implementation: the platform name and its code.

A whole program: its ordered top-level contents.

One top-level program item: a comment, a module, or a run block.

A record or union type declaration: its name, generics, and members.

One entry in a record or union body: a comment, a field, or a header separator.

A function return: the returned variable name, type, and optional direction.

A reverse (~) command: a name, type, setter pairs, and a body.

A setter pair inside a reverse: an outer settable and the inner value written to it.

A state ($) command: a named, typed, optionally-lockable cell with an optional initial value.

The outcome of a parse rule: a success carrying a value, a no-match, or an error.

T — a generic type parameter.

A parse error, carrying a message.

A parse rule that did not match, with an optional explanation.

A successful parse: the parsed value and the index where it ended.

Decodes each element of a JSON array with a callback, gathering the results.

ElementType — a generic type parameter.

A callback that decodes one element (an optional JSON value) into an ElementType.

Returns Collection.Array(ElementType) .

Builds a JSON object value from a map of text values.

Returns Code.Language.Json.Value .

Reads a JSON value as an array, or an empty array.

Returns Collection.Array(Code.Language.Json.Value) .

Reads the element at an index of a JSON array, falling back to a default.

Returns Collection.Optional(Code.Language.Json.Value) .

The number of elements in a JSON array, or zero.

Returns Maths.Integer .

Reads a JSON value as a boolean, falling back to a default.

Returns Logic.Maybe .

Reads a JSON value as an integer, falling back to a default.

Reads a JSON value as a real number, falling back to a default.

Returns Maths.Real .

Reads a JSON value as an object, falling back to a default.

Returns Collection.Map(Code.Language.Json.Value) .

Reads a named field from a JSON object, falling back to a default.

Reads a JSON value as text, falling back to a default.

Reads a JSON value as an array, or nothing when it is not an array.

Returns Collection.Optional(Collection.Array(Code.Language.Json.Value)) .

Reads the element at an index of a JSON array, or nothing when out of range.

Reads a JSON value as an integer, or nothing when it is not a number.

Returns Collection.Optional(Maths.Integer) .

Reads the object of a JSON value, or nothing when it is not an object.

Returns Collection.Optional(Collection.Map(Code.Language.Json.Value)) .

Reads a named field from a JSON object, or nothing when absent.

Serialises an optional JSON value to JSON text, or empty text when absent.

Reads the text of a JSON value, or nothing when it is not text.

Returns Collection.Optional(Text) .

Parses JSON text into a JSON value, or nothing when the text is invalid.

Serialises a JSON value to its compact JSON text.

Builds a call argument from its JSON command form.

Returns Code.Language.Program.Structure.Argument .

Builds an assignment from its JSON command form.

Returns Code.Language.Program.Structure.Assignment .

Builds an assignment value from its JSON command form.

Returns Code.Language.Program.Structure.AssignmentValue .

Builds one body command from its JSON command form, dispatching on its tag.

Returns Code.Language.Program.Structure.BodyCommand .

Builds a constant declaration from its JSON command form.

Returns Code.Language.Program.Structure.Constant .

Builds a literal constant value from its type name and JSON command form.

Returns Code.Language.Program.Structure.ConstantValue .

Builds a direction from its JSON command form, defaulting to forward.

Returns Code.Language.Program.Structure.Direction .

Builds a field from its JSON command form.

Returns Code.Language.Program.Structure.Field .

Builds a function or native declaration from its JSON command form.

Returns Code.Language.Program.Structure.Function .

Builds a function body from its JSON command form, choosing commands or native implementations.

Returns Code.Language.Program.Structure.FunctionBody .

Builds a function call from its JSON command form, reading fields from a start slot.

Returns Code.Language.Program.Structure.FunctionCall .

Builds a function input from its JSON command form.

Returns Code.Language.Program.Structure.Input .

Builds a module from its JSON command form.

Returns Code.Language.Program.Structure.Module .

Builds one module member from its JSON command form, dispatching on its tag.

Returns Code.Language.Program.Structure.ModuleContent .

Builds a native type from its JSON command form.

Returns Code.Language.Program.Structure.NativeType .

Builds a per-platform implementation from its JSON command form.

Returns Code.Language.Program.Structure.PlatformImpl .

Builds a whole program from its JSON command form.

Returns Code.Language.Program.Structure.Program .

Builds one top-level program item from its JSON command form, dispatching on its tag.

Returns Code.Language.Program.Structure.ProgramContent .

Builds a record or union type from its JSON command form.

Returns Code.Language.Program.Structure.Record .

Builds one record body entry from its JSON command form, dispatching on its tag.

Returns Code.Language.Program.Structure.RecordBodyEntry .

Builds a function return from its JSON command form.

Returns Code.Language.Program.Structure.Return .

Builds a reverse command from its JSON command form.

Returns Code.Language.Program.Structure.Reverse .

Builds a setter pair from its JSON command form.

Returns Code.Language.Program.Structure.SetterPair .

Builds a state declaration from its JSON command form.

Returns Code.Language.Program.Structure.State .

Reads a JSON value as text, defaulting to empty.

Runs a callback with a named input value bound in context, returning its result.

A callback run while the input is bound; its result is returned.

Runs a callback with a named mutable value bound in context, returning its result.

A callback run while the mutable is bound; its result is returned.

The current parse index.

The value currently being parsed.

Sets up the parse context (value, start index, and error slot) then runs a callback.

A callback run with the parse state established; its result is returned.

Moves the parse index to a new position.

Recommended articles