From d6882feb157a1a93d23f378b4d32436bf8e015e3 Mon Sep 17 00:00:00 2001 From: tali Date: Fri, 19 Jan 2024 15:14:22 -0500 Subject: [PATCH] refactor string->sexp happens in S2 --- src/n2/dune | 2 +- src/n2/n2.ml | 67 +++++++++++++++++++++---------------------------- src/n2/n2.mli | 1 - src/s2/asset.ml | 8 ++++++ src/s2/dune | 3 ++- 5 files changed, 40 insertions(+), 41 deletions(-) diff --git a/src/n2/dune b/src/n2/dune index 7b864af..72441e0 100644 --- a/src/n2/dune +++ b/src/n2/dune @@ -5,4 +5,4 @@ adam s2 ohlog - sexplib)) + sexplib0)) diff --git a/src/n2/n2.ml b/src/n2/n2.ml index c53c82d..be897d2 100644 --- a/src/n2/n2.ml +++ b/src/n2/n2.ml @@ -1,5 +1,4 @@ open Adam -module Sexp = Sexplib0.Sexp include (val Ohlog.logs "N2") @@ -144,6 +143,9 @@ end *) module Scene = struct + module Sexp = Sexplib0.Sexp + open Sexplib0.Sexp_conv + include (val Ohlog.sublogs logger "Scene") type t = { @@ -156,36 +158,42 @@ module Scene = struct sprites : Sprite_graph.node list; } + let get t name = try Hashtbl.find t.objs name + with Not_found -> + Format.ksprintf failwith "no node %S in scene" name + + let transform obj = obj.transform + let parse_transform_arg ~(tf : mat2a) = function | Sexp.List [Atom "tx"; arg] -> - tf.a2 <- Sexplib.Conv.float_of_sexp arg + tf.a2 <- float_of_sexp arg | Sexp.List [Atom "ty"; arg] -> - tf.a5 <- Sexplib.Conv.float_of_sexp arg + tf.a5 <- float_of_sexp arg | Sexp.List [Atom "sx"; arg] -> - tf.a0 <- Sexplib.Conv.float_of_sexp arg + tf.a0 <- float_of_sexp arg | Sexp.List [Atom "sy"; arg] -> - tf.a4 <- Sexplib.Conv.float_of_sexp arg + tf.a4 <- float_of_sexp arg | sexp -> - Sexplib.Conv.of_sexp_error "bad argument to transform" sexp + of_sexp_error "bad argument to transform" sexp let parse_sprite_arg ~(rect : aabb) ~(fill : color) = function | Sexp.List [Atom "rect"; x0; x1; y0; y1] -> AABB.set rect - ~x0:(Sexplib.Conv.float_of_sexp x0) - ~y0:(Sexplib.Conv.float_of_sexp x1) - ~x1:(Sexplib.Conv.float_of_sexp y0) - ~y1:(Sexplib.Conv.float_of_sexp y1) + ~x0:(float_of_sexp x0) + ~y0:(float_of_sexp x1) + ~x1:(float_of_sexp y0) + ~y1:(float_of_sexp y1) | Sexp.List [Atom "fill"; rgb] -> Color.set_rgb24 fill (Sexplib.Conv.int_of_sexp rgb) | Sexp.List [Atom "fill"; r; g; b] -> begin - fill.r <- Sexplib.Conv.float_of_sexp r; - fill.g <- Sexplib.Conv.float_of_sexp g; - fill.b <- Sexplib.Conv.float_of_sexp b; + fill.r <- float_of_sexp r; + fill.g <- float_of_sexp g; + fill.b <- float_of_sexp b; end | sexp -> - Sexplib.Conv.of_sexp_error "bad argument to sprite" sexp + of_sexp_error "bad argument to sprite" sexp let parse_sprite ~sg ~tf = function | Sexp.List (Atom "sprite" :: args) -> @@ -194,7 +202,7 @@ module Scene = struct List.iter (parse_sprite_arg ~rect ~fill) args; Sprite_graph.add_rect sg ~tf ~bb:rect ~fill | sexp -> - Sexplib.Conv.of_sexp_error "invalid sprite" sexp + of_sexp_error "invalid sprite" sexp let rec parse_obj t ~tg ~sg ~parent = function | Sexp.List (Atom "object" :: args) -> @@ -208,7 +216,7 @@ module Scene = struct obj.name; obj | sexp -> - Sexplib.Conv.of_sexp_error "invalid object" sexp + of_sexp_error "invalid object" sexp and parse_obj_args t obj ~tg ~sg = function | [] -> { obj with sprites = List.rev obj.sprites } @@ -237,7 +245,7 @@ module Scene = struct trace (fun m -> m "parse_obj_args: end object"); parse_obj_args t obj args ~tg ~sg | sexp -> - Sexplib.Conv.of_sexp_error "bad argument to object" sexp + of_sexp_error "bad argument to object" sexp let of_sexp ~tg ~sg = function | Sexp.List [Atom "scene"; root] -> @@ -248,27 +256,10 @@ module Scene = struct t | sexp -> - Sexplib.Conv.of_sexp_error "invalid scene" sexp + of_sexp_error "invalid scene" sexp let load name ~tg ~sg = - let path = Format.sprintf "%s.scene" name in - match Sexplib.Sexp.of_string_conv - (S2.Asset.load_file path) - (of_sexp ~tg ~sg) - with - | `Result v -> - debug (fun m -> m "loaded scene %S" name); - v - | `Error (exn, _) -> - let msg = match exn with - | Failure msg -> msg - | exn -> Printexc.to_string exn - in - raise (S2.Asset.Error (path, "parse error: " ^ msg)) - - let get t name = - try Hashtbl.find t.objs name - with Not_found -> Format.ksprintf failwith "no node %S in scene" name - - let transform obj = obj.transform + S2.Asset.load_sexp_conv + (name ^ ".scene") + (of_sexp ~tg ~sg) end diff --git a/src/n2/n2.mli b/src/n2/n2.mli index 9af30bd..0123598 100644 --- a/src/n2/n2.mli +++ b/src/n2/n2.mli @@ -1,6 +1,5 @@ open Adam open S2 -module Sexp = Sexplib0.Sexp module Transform_graph : sig type t diff --git a/src/s2/asset.ml b/src/s2/asset.ml index e9a3980..d9cd273 100644 --- a/src/s2/asset.ml +++ b/src/s2/asset.ml @@ -23,3 +23,11 @@ let load_file path = | n -> read (i + n) in Bytes.sub_string buf 0 (read 0) + +let load_sexp_conv path of_sexp = + try + load_file path + |> Sexplib.Sexp.of_string + |> of_sexp + with Sexplib.Conv.Of_sexp_error (Failure msg, _) -> + raise (Error (path, "parse error: " ^ msg)) diff --git a/src/s2/dune b/src/s2/dune index e3bc01e..ef1b595 100644 --- a/src/s2/dune +++ b/src/s2/dune @@ -6,4 +6,5 @@ ohlog tsdl - tgls.tgl4)) + tgls.tgl4 + sexplib))