flan/lib/form.ml
2026-09-10 14:40:34 +07:00

41 lines
1.3 KiB
OCaml

(** The reader's output: syntax, before any typing or macro expansion.
Deliberately dumb. [true], [false] and [nil] are ordinary symbols here and
are resolved later; the reader knows nothing about special forms. *)
type t = {
v : value;
loc : Loc.t;
}
and value =
| Sym of string (* foo rl/draw-fps .pos + *)
| Kw of string (* :space :else (leading : dropped) *)
| Int of int64 (* 42 -1 0xE6B800FF *)
| Float of float (* 0.05 *)
| Str of string (* "SAND" *)
| Byte of int (* \space \0 \( (0..255) *)
| List of t list (* (f x) *)
| Vec of t list (* [1 2 3] and every binding/type bracket *)
| Map of t list (* {:key v} in value position, {K V} in type position *)
let make v loc = { v; loc }
let rec to_string f =
let seq l = String.concat " " (List.map to_string l) in
match f.v with
| Sym s -> s
| Kw s -> ":" ^ s
| Int i -> Int64.to_string i
| Float x -> Printf.sprintf "%g" x
| Str s -> Printf.sprintf "%S" s
| Byte b ->
(match Char.chr b with
| ' ' -> "\\space"
| '\t' -> "\\tab"
| '\n' -> "\\newline"
| c -> Printf.sprintf "\\%c" c)
| List l -> "(" ^ seq l ^ ")"
| Vec l -> "[" ^ seq l ^ "]"
| Map l -> "{" ^ seq l ^ "}"