Browse Source

Fix indentation, add ignore annotation, null strs

* Only write/copy strings if not empty
* Add support for backslash string endings
windows-imgui
Macoy Madson 2 months ago
parent
commit
6c33357b62
1 changed files with 68 additions and 50 deletions
  1. +68
    -50
      src/Introspection.cake

+ 68
- 50
src/Introspection.cake View File

@ -99,52 +99,63 @@
(return false))
(each-token-argument-in tokens start-members end-token-index i
(var name-token (* (const Token))
(addr (at i tokens)))
(set i (getNextArgument tokens i end-token-index))
(var type-token-index int i)
(var type-token (* (const Token))
(addr (at i tokens)))
(var possible-annotation-index int (getNextArgument tokens i end-token-index))
(var annotations-token (* (const Token))
(addr (at possible-annotation-index tokens)))
(when (= TokenType_OpenParen (path annotations-token > type))
;; Process annotation
;; (NoteAtToken (deref annotations-token) "detected annotation")
;; Absorb the argument as part of this field
(set i possible-annotation-index))
(scope ;; Output field metadata
(var name-to-string-token Token (deref name-token))
(set (field name-to-string-token type) TokenType_String)
(var serialize-type Token (deref type-token))
(set (field serialize-type type) TokenType_Symbol)
(var determined-type-string (* (const char)) "serialization-type-unknown")
(var possible-string-serialization-type (* (const char)) null)
(cond
((std-str-equals (path type-token > contents) "int")
(set determined-type-string "serialization-type-int"))
((std-str-equals (path type-token > contents) "float")
(set determined-type-string "serialization-type-float"))
((std-str-equals (path type-token > contents) "bool")
(set determined-type-string "serialization-type-bool"))
((std-str-equals (path type-token > contents) "char")
(set determined-type-string "serialization-type-char"))
((is-type-string tokens type-token-index (addr possible-string-serialization-type))
(set determined-type-string possible-string-serialization-type)))
(set (field serialize-type contents) determined-type-string)
(tokenize-push (deref fields-metadata)
(array (token-splice-addr name-to-string-token) (token-splice-addr serialize-type)
(offsetof (type (token-splice struct-name)) (token-splice name-token))
null null)))
;; Output regular struct fields
(PushBackTokenExpression (deref processed-arguments) name-token)
(PushBackTokenExpression (deref processed-arguments) type-token))
(var name-token (* (const Token))
(addr (at i tokens)))
(set i (getNextArgument tokens i end-token-index))
(var type-token-index int i)
(var type-token (* (const Token))
(addr (at i tokens)))
(var ignore-field bool false)
(var possible-annotation-index int (getNextArgument tokens i end-token-index))
(var annotations-token (* (const Token))
(addr (at possible-annotation-index tokens)))
(when (= TokenType_OpenParen (path annotations-token > type))
;; Process annotation
(var end-annotation-index int (FindCloseParenTokenIndex tokens possible-annotation-index))
(each-token-argument-in tokens (+ 1 possible-annotation-index) end-annotation-index annotation-arg
(var annotation-token (* (const Token)) (addr (at annotation-arg tokens)))
(when (= TokenType_Symbol (path annotation-token > type))
(cond
((std-str-equals (path annotation-token > contents) "ignore")
(set ignore-field true)))))
;; (when (ignore-field))
;; Absorb the argument as part of this field
(set i possible-annotation-index))
(unless ignore-field ;; Output field metadata
(var name-to-string-token Token (deref name-token))
(set (field name-to-string-token type) TokenType_String)
(var serialize-type Token (deref type-token))
(set (field serialize-type type) TokenType_Symbol)
(var determined-type-string (* (const char)) "serialization-type-unknown")
(var possible-string-serialization-type (* (const char)) null)
(cond
((std-str-equals (path type-token > contents) "int")
(set determined-type-string "serialization-type-int"))
((std-str-equals (path type-token > contents) "float")
(set determined-type-string "serialization-type-float"))
((std-str-equals (path type-token > contents) "bool")
(set determined-type-string "serialization-type-bool"))
((std-str-equals (path type-token > contents) "char")
(set determined-type-string "serialization-type-char"))
((is-type-string tokens type-token-index (addr possible-string-serialization-type))
(set determined-type-string possible-string-serialization-type))
(true
(ErrorAtToken (deref type-token) "cannot generate metadata for unknown type")
(return false)))
(set (field serialize-type contents) determined-type-string)
(tokenize-push (deref fields-metadata)
(array (token-splice-addr name-to-string-token) (token-splice-addr serialize-type)
(offsetof (type (token-splice struct-name)) (token-splice name-token))
null null)))
;; Output regular struct fields
(PushBackTokenExpression (deref processed-arguments) name-token)
(PushBackTokenExpression (deref processed-arguments) type-token))
(var metadata-name Token (deref struct-name))
(scope
@ -191,6 +202,7 @@
(forward-declare (struct MetadataField)
(struct MetadataStruct))
;; TODO Hook these up
(def-function-signature serialization-read-func (metadata (* metadata-field) data (* void) output (* FILE)))
(def-function-signature serialization-write-func (metadata (* metadata-field) data (* void) output (* FILE)))
@ -212,8 +224,10 @@
name (* (const char)) (string)
other-name ([] 32 char)
yet-another-name (* char)
optional-name (* char)
value int
decimal float
bad ([] 3 float) (ignore)
truthy bool
charry char)
@ -258,12 +272,12 @@
(var str-write (* (const char))
(offset-pointer-to-type struct-to-write (path field > offset) (* (const char))))
;; TODO: This will require quoting any '"' in the string (etc.)
(fprintf out-file " \"%s\"" str-write))
(fprintf out-file " \"%s\"" (? str-write str-write "")))
((= (path field > type) serialization-type-string)
(var str-write (* (* (const char)))
(offset-pointer-to-type struct-to-write (path field > offset) (* (* (const char)))))
;; TODO: This will require quoting any '"' in the string (etc.)
(fprintf out-file " \"%s\"" (deref str-write)))
(fprintf out-file " \"%s\"" (? (deref str-write) (deref str-write) "")))
(true
(fprintf out-file " <unknown>"))))
(fprintf out-file ")\n"))
@ -305,6 +319,9 @@
;; TODO Validate size
(strcpy str-write in-string))
((= (path field > type) serialization-type-string)
;; Don't allocate for empty strings
(unless (and in-string (at 0 in-string))
(return true))
(var str-write (* (* (const char)))
(offset-pointer-to-type struct-out (path field > offset) (* (* (const char)))))
(var string-length size_t (strlen in-string))
@ -409,7 +426,8 @@
((= state state-reading-member-value-string)
(when (and (= '\"' (deref current-char))
;; TODO: Bug here if you want a string ending in backslash: "\\"
(!= '\\' (deref (- current-char 1))))
(!= '\\' (deref (- current-char 1)))
(!= '\\' (deref (- current-char 2))))
(set (deref current-symbol-write) 0) ;; Terminate string
(fprintf stderr "found value \"%s\"\n" current-symbol-buffer)
(unless (set-metadata-field-from-string current-field
@ -490,7 +508,7 @@
"<stdlib.h>") ;; free()
(defun test--serialization (&return int)
(var a my-struct (array "Test struct" "Other name" (strdup "Another name")
(var a my-struct (array "Test struct" "Other name" (strdup "Another name") null
42 -0.33f false 'a'))
(write-serializable-struct-plaintext (addr my-struct--metadata) (addr a) stderr)


Loading…
Cancel
Save