Browse Source

Fix Dictionary, rename dynstring function

windows-imgui
Macoy Madson 9 months ago
parent
commit
8fd524899b
  1. 29
      src/Dictionary.cake
  2. 15
      src/DynamicArray.cake

29
src/Dictionary.cake

@ -15,9 +15,6 @@
value (* (const char)))
(var my-dictionary (* dictionary-entry) null)
(sh_new_arena my-dictionary)
;; (when (shgetp_null my-dictionary "Test")
;; (fprintf stderr "Expected not to get entry back\n")
;; (return 1))
;; TODO Memory leak
;; (var my-entry dictionary-entry (array (strdup "cakelisp") "a programming language"))
;; (shputs my-dictionary my-entry)
@ -30,14 +27,24 @@
(unless returned-entry-copy
(fprintf stderr "Expected to get string back. Length: %ld\n" (shlen my-dictionary))
(return 1))
(fprintf stderr "what is %s? %s\n" (at i keys) returned-entry-copy))
(fprintf stderr "what is %s? %s\n" (at i keys) returned-entry-copy)
(var entry-index ptrdiff_t (shgeti my-dictionary (at i keys)))
(unless (>= entry-index 0)
(fprintf stderr "Expected to get entry index back\n")
(return 1))
(fprintf stderr "Element is at %d (%p). Key = %s\n" (type-cast entry-index int)
(addr (at entry-index my-dictionary))
(field (at entry-index my-dictionary) key))
(var returned-entry (* dictionary-entry) (shgetp my-dictionary (at i keys)))
(unless (and returned-entry (path returned-entry > key) (path returned-entry > value))
(fprintf stderr "Expected to get valid entry back, got %p (array[-1] = %p). Length: %ld\n"
returned-entry (addr (at -1 my-dictionary)) (shlen my-dictionary))
(return 1))
(fprintf stderr "what is %s (%p)? %s\n"
(path returned-entry > key) (path returned-entry > key)
(path returned-entry > value)))
;; (var returned-entry (* dictionary-entry) (shgetp_null my-dictionary "cakelisp"))
;; (unless (and returned-entry (path returned-entry > key) (path returned-entry > value))
;; (fprintf stderr "Expected to get valid entry back. Length: %ld\n" (shlen my-dictionary))
;; (return 1))
;; (fprintf stderr "what is %s? %s\n"
;; (path returned-entry > key)
;; (path returned-entry > value))
(shfree my-dictionary)
(return 0))))

15
src/DynamicArray.cake

@ -89,8 +89,8 @@
;; using strcat may be faster if it is optimized. I won't worry about it for now
(each-char-in-string-const append-str current-char
;; Will resize if necessary
(dynarray-append (deref str) (deref current-char)))
;; Will resize if necessary
(dynarray-append (deref str) (deref current-char)))
;; Add the null terminator
(dynarray-append (deref str) 0)
@ -100,8 +100,9 @@
(defun dynstring-strlen (str dynstring &return size_t)
(return (- (dynarray-length str) 1)))
(defmacro make-dynstring-f (var-name symbol format-string string
&rest format-arguments any)
;; A very quick way to get a heap-allocated formatted string (quick for the programmer)
(defmacro dynstring-create-f (var-name symbol format-string string
&rest format-arguments any)
(tokenize-push output
(var (token-splice var-name) dynstring null)
(dynstring-printf (addr (token-splice var-name))
@ -163,9 +164,9 @@
(dynstring-concat (addr my-string) "Test")
(print-dynstring-details my-string)
(make-dynstring-f my-quick-string "The answer is %d" 42)
(dynstring-create-f my-quick-string "The answer is %d" 42)
(print-dynstring-details my-quick-string)
(dynarray-free my-string)
(dynarray-free my-quick-string)
(dynarray-free my-string)
(dynarray-free my-quick-string)
(return 0))))

Loading…
Cancel
Save