Browse Source

Add characters-read-out for Introspection reading

This allows reading multiple structs from the same file.
zig-compilation
Macoy Madson 2 years ago
parent
commit
ec66ebd9f0
  1. 6
      src/Dictionary.cake
  2. 2
      src/DynamicArray.cake
  3. 27
      src/Introspection.cake

6
src/Dictionary.cake

@ -364,7 +364,7 @@
(memset new-element 0 (path (path field > field-type-metadata) > struct-size))
(unless (read-introspect-struct-s-expr (path field > field-type-metadata)
new-element
argument-start string-allocate)
argument-start string-allocate null)
(fprintf stderr "error: failed to read element into dictionary: ")
(print-string-range argument-start argument-end true)
(free-introspect-struct-fields (path field > field-type-metadata)
@ -611,7 +611,7 @@
(fclose in-file)
(unless (read-introspect-struct-s-expr my-dict--metadata (addr read-struct)
file-contents malloc)
file-contents malloc null)
(free-introspect-struct-fields
my-dict--metadata (addr read-struct) free)
(free (type-cast file-contents (* void)))
@ -658,7 +658,7 @@
(fclose in-file)
(unless (read-introspect-struct-s-expr my-strdict--metadata (addr read-struct)
file-contents malloc)
file-contents malloc null)
(free-introspect-struct-fields
my-strdict--metadata (addr read-struct) free)
(free (type-cast file-contents (* void)))

2
src/DynamicArray.cake

@ -299,7 +299,7 @@
(memset current-element 0 (path (path field > field-type-metadata) > struct-size))
(unless (read-introspect-struct-s-expr (path field > field-type-metadata)
current-element
argument-start string-allocate)
argument-start string-allocate null)
(fprintf stderr "error: failed to read element at %d: " current-index)
(print-string-range argument-start argument-end true)
(dynarray-free (deref dynarray-ptr-read))

27
src/Introspection.cake

@ -893,7 +893,11 @@
(return (read-introspect-struct-s-expr (path field > field-type-metadata)
substruct-out
in-string
string-allocate)))
string-allocate
;; Why can this be null? Because in a valid S-expression
;; file, our parent should be able to find the number of
;; characters read via scanning and counting parentheses
null)))
(true
(fprintf stderr "error: do not know how to parse field '%s' with type %d from string \"%s\"\n"
@ -975,11 +979,16 @@
(set (deref end-out) (+ 1 current-char))
(break))))))))
;; characters-read-out signals the end of this struct, which enables many potentially different
;; structs to be in the same file, one after another. null is okay if you don't care
(defun read-introspect-struct-s-expr (struct-metadata (* (const metadata-struct))
struct-out (* void)
in-string (* (const char))
string-allocate allocate-string-function
characters-read-out (* (unsigned int))
&return bool)
(when characters-read-out
(set (deref characters-read-out) 0))
;; Find the start of the struct; validates we are parsing the struct we expect via checking the
;; type name after the first opening paren
(var struct-member-start (* (const char)) null)
@ -1021,6 +1030,7 @@
(set state state-read-member-name)
(var argument-start (* (const char)) null)
(var argument-end (* (const char)) null)
(var last-valid-argument-end (* (const char)) null)
(var current-char (* (const char)) struct-member-start)
(s-expr-get-next-argument-start-end current-char (addr argument-start) (addr argument-end))
(while (and argument-start argument-end)
@ -1028,6 +1038,8 @@
;; (print-string-range argument-start argument-end false)
;; (fprintf stderr "'\n")
(set last-valid-argument-end argument-end)
(cond
((= state state-read-member-name)
(unless (= (deref argument-start) ':')
@ -1115,6 +1127,11 @@
(return false))))
(set state state-read-member-name)))
(s-expr-get-next-argument-start-end argument-end (addr argument-start) (addr argument-end)))
(when characters-read-out
(set (deref characters-read-out)
;; Add one to eat the final closing paren
(+ 1 (type-cast (- last-valid-argument-end in-string)
(unsigned int)))))
(return true))
(defun free-introspect-struct-fields (struct-metadata (* (const metadata-struct))
@ -1339,12 +1356,16 @@
(unless in-file (return false))
(var file-contents (* (const char)) (read-file-into-memory in-file))
(fclose in-file)
(var num-characters-read (unsigned int) 0)
(unless (read-introspect-struct-s-expr struct-metadata read-struct
file-contents malloc)
file-contents malloc (addr num-characters-read))
(free (type-cast file-contents (* void)))
(free read-struct)
(return false))
;; (fprintf stderr "Read %d characters. Last char: %c Rest: \"%s\"\n"
;; num-characters-read (deref (+ file-contents num-characters-read))
;; (+ file-contents num-characters-read))
(free (type-cast file-contents (* void))))
(fprintf stderr "Read-in struct:\n")
@ -1503,7 +1524,7 @@
(fclose in-file)
(fprintf stderr "note: I expect there to be an error to read struct from Errors.cakedata -- ")
(when (read-introspect-struct-s-expr my-struct--metadata (addr read-struct)
file-contents malloc)
file-contents malloc null)
(fprintf stderr "error: expected to fail to read struct from Errors.cakedata\n")
(free-introspect-struct-fields
my-struct--metadata (addr read-struct) free)

Loading…
Cancel
Save