tur/datum
quoted forms as runtime data.
(quote (+ 1 2)), or '(+ 1 2), is a Datum under --enable=quote-data: (DList [(DSym +) (DInt 1) (DInt 2)]). A symbol inside a quoted list is a DSym, a literal is the matching leaf, a parenthesised form is a DList and a bracketed one a DVec. A quoted bare symbol is still a Sym, and a quoted literal is the literal itself, as in Lisp.
Under the quote-data experiment the module is loaded on first use: when any file the program reads -- the entry, a loaded file, an imported module -- quotes a list or defines a macro that expands to one. A program that never quotes a list does not load it, so its names stay free. It loads stdlib/list-typed.tur for (List Datum).
Since: quote-list-data-plan
Datum
(defdata Datum :copy (DSym Sym) (DInt int) (DFloat float) (DStr cstr) (DBool bool) (DList (List Datum)) (DVec (List Datum)))
one quoted form: a symbol, a literal, or a list of data.
Variants:
(DSym s) -- a symbol, as the interned `Sym` (DInt n) -- an integer literal (DFloat x) -- a float literal (DStr s) -- a string literal (DBool b) -- `true` or `false` (DList xs) -- a parenthesised form `(a b c)` (DVec xs) -- a bracketed form `[a b c]`
(match '(+ 1 2) (DList xs) (list-count xs) _ 0) ; => 3
Since: quote-list-data-plan
datum-cat-
(datum-cat- [a :cstr b :cstr] :cstr)
concatenate two cstrs into a fresh buffer.
datum-str=?-
(datum-str=?- [a :cstr b :cstr] :bool)
byte equality of two cstrs.
datum-int-str-
(datum-int-str- [v :int] :cstr)
an int in decimal.
datum-float-str-
(datum-float-str- [v :float] :cstr)
a float that reads back as a float (3.0, not 3).
datum-quote-str-
(datum-quote-str- [s :cstr] :cstr)
a string literal with its quotes and escapes.
datum-items-str-
(datum-items-str- [xs :(List Datum) acc :cstr] :cstr)
the elements of a list datum after the first, each preceded by a space.
datum-seq-str-
(datum-seq-str- [xs :(List Datum)] :cstr)
the elements of a list datum, space-separated.
datum->str
(datum->str [d :Datum] :cstr)
render a datum the way it would be written in source.
| d | the datum to render |
A fresh cstr: (+ 1 2) for '(+ 1 2), [a "b"] for '[a "b"]. A
string leaf is quoted and escaped; a float keeps its decimal point.
(println (datum->str '(+ 1 (f 2.5 "s")))) ; prints (+ 1 (f 2.5 "s"))
Since: quote-list-data-plan
datum-list=?-
(datum-list=?- [xs :(List Datum) ys :(List Datum)] :bool)
element-wise equality of two lists of data.
datum=?
(datum=? [a :Datum b :Datum] :bool)
structural equality of two data.
| a | the first datum | |
| b | the second datum |
true when both are the same kind of leaf with equal values, or both
lists (or both vectors) of pairwise-equal data. '(1) is not '[1],
and (DInt 1) is not (DFloat 1.0).
(datum=? '(a 1) '(a 1)) ; => true (datum=? '(a 1) '[a 1]) ; => false
Since: quote-list-data-plan
Eq[Datum]
(definstance Eq [Datum])
structural equality, as datum=?.
Show[Datum]
(definstance Show [Datum])
the source rendering, as datum->str.
datum-sym?
(datum-sym? [d :Datum] :bool)
is the datum a symbol?
| d | the datum to inspect |
true for a DSym.
(datum-sym? (datum-nth '(+ 1 2) 0)) ; => true
Since: quote-list-data-plan
datum-int?
(datum-int? [d :Datum] :bool)
is the datum an integer?
| d | the datum to inspect |
true for a DInt.
(datum-int? (datum-nth '(+ 1 2) 1)) ; => true
Since: quote-list-data-plan
datum-float?
(datum-float? [d :Datum] :bool)
is the datum a float?
| d | the datum to inspect |
true for a DFloat.
(datum-float? (datum-nth '(x 7.1) 1)) ; => true
Since: quote-list-data-plan
datum-str?
(datum-str? [d :Datum] :bool)
is the datum a string?
| d | the datum to inspect |
true for a DStr.
(datum-str? (datum-nth '(say "hi") 1)) ; => true
Since: quote-list-data-plan
datum-bool?
(datum-bool? [d :Datum] :bool)
is the datum a boolean?
| d | the datum to inspect |
true for a DBool.
(datum-bool? (datum-nth '(if true 1 2) 1)) ; => true
Since: quote-list-data-plan
datum-list?
(datum-list? [d :Datum] :bool)
is the datum a parenthesised list?
| d | the datum to inspect |
true for a DList; false for a DVec.
(datum-list? '(a b)) ; => true (datum-list? '[a b]) ; => false
Since: quote-list-data-plan
datum-vec?
(datum-vec? [d :Datum] :bool)
is the datum a bracketed vector?
| d | the datum to inspect |
true for a DVec.
(datum-vec? '[a b]) ; => true
Since: quote-list-data-plan
datum-no-items-
(datum-no-items- :(List Datum))
the empty (List Datum), for a quoted () or [].
datum-items
(datum-items [d :Datum] :(List Datum))
the elements of a list or vector datum.
| d | the datum to take apart |
The (List Datum) of a DList or DVec; the empty list for a leaf.
(list-count (datum-items '(+ 1 2))) ; => 3
Since: quote-list-data-plan
datum-length
(datum-length [d :Datum] :int)
the number of elements of a list or vector datum.
| d | the datum to measure |
The element count of a DList or DVec; 0 for a leaf.
(datum-length '(+ 1 2)) ; => 3
Since: quote-list-data-plan
datum-nth-loop-
(datum-nth-loop- [xs :(List Datum) i :int] :Datum)
the i-th element of a list of data.
datum-nth
(datum-nth [d :Datum i :int] :Datum)
the i-th element of a list or vector datum.
| d | a DList or DVec | |
| i | the zero-based index |
The element at i. Panics when i is out of range or d is a leaf.
(datum->str (datum-nth '(+ 1 2) 0)) ; => "+"
Since: quote-list-data-plan