GameLib is a collection of libraries for creating applications in Cakelisp.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

277 lines
12 KiB

(set-cakelisp-option cakelisp-src-dir "Dependencies/cakelisp/src")
(add-cakelisp-search-directory "Dependencies/cakelisp/runtime")
(import &comptime-only "CHelpers.cake")
(import &comptime-only "STB.cake")
(use-stb-ds clone-stb-headers-dictionary)
;;
;; Hash table with string keys
;;
;; Automatically allocate each string key to a string arena
(def-c-function-alias strdict-use-key-arena sh_new_arena)
;; Automatically strdup and free string keys
(def-c-function-alias strdict-use-key-strdup sh_new_strdup)
(def-c-function-alias strdict-length shlenu)
;; (dict (* <your Key Value type>) &return size_t)
(def-c-function-alias strdict-free shfree)
;; (dict (* <your Key Value type>))
;; These are used for the following functions:
;; - strdict-ptr-or-default-at
;; - strdict-struct-at
;; - strdict-value-at
(def-c-function-alias strdict-set-not-found-default shdefaults)
;; (dict (* <your Key Value type>) item <your Key Value type>
;; &return <your Key Value type>)
(def-c-function-alias strdict-set-not-found-default-value shdefault)
;; (dict (* <your Key Value type>) value <your Value type>
;; &return <your Value type>)
;; Setting
(def-c-function-alias strdict-set-key-value shput)
;; (dict (* <your Key Value type>) key (* char) value <your Value type>
;; &return <your Value type>)
;; Requires a key field
(def-c-function-alias strdict-set-struct shputs)
;; (dict (* <your Key Value type>) item <your Key Value type> &return <your Key Value type>)
;; Getting
(def-c-function-alias strdict-ptr-at shgetp_null)
;; (dict (* <your Key Value type>) key (* (const char)) &return (* <your Key Value type>))
(def-c-function-alias strdict-ptr-or-default-at shgetp)
;; (dict (* <your Key Value type>) key (* (const char)) &return (* <your Key Value type>))
(def-c-function-alias strdict-index-at shgeti)
;; (dict (* <your Key Value type>) key (* (const char)) &return ptrdiff_t)
(def-c-function-alias strdict-struct-at shgets)
;; (dict (* <your Key Value type>) key (* (const char)) &return <your Key Value type>)
(def-c-function-alias strdict-value-at shget)
;; (dict (* <your Key Value type>) &return <your value type>)
(defmacro each-in-strdict (strdict any index-iterator-name symbol &rest body any)
(tokenize-push output
(c-for
(var (token-splice index-iterator-name) size_t 0)
;; We could hoist this out but it should be a quick op anyways
(< (token-splice index-iterator-name) (strdict-length (token-splice strdict)))
(incr (token-splice index-iterator-name))
(token-splice-rest body tokens)))
(return true))
(defmacro each-key-in-strdict (strdict any index-iterator-name symbol
key-name symbol &rest body any)
(tokenize-push output
(c-for
(var (token-splice index-iterator-name) size_t 0)
;; We could hoist this out but it should be a quick op anyways
(< (token-splice index-iterator-name) (strdict-length (token-splice strdict)))
(incr (token-splice index-iterator-name))
(var (token-splice key-name) (* char)
(field (at (token-splice index-name strdict)) key))
(token-splice-rest body tokens)))
(return true))
(defmacro each-item-in-strdict (strdict any index-name symbol
item-name symbol item-type any
&rest body any)
(tokenize-push output
(each-in-strdict (token-splice strdict) (token-splice index-name)
(var (token-splice item-name item-type) (at (token-splice index-name strdict)))
(token-splice-rest body tokens)))
(return true))
(defmacro each-item-addr-in-strdict (strdict any index-name symbol
item-name symbol ptr-to-item-type any
&rest body any)
(tokenize-push output
(each-in-strdict (token-splice strdict) (token-splice index-name)
(var (token-splice item-name ptr-to-item-type)
(addr (at (token-splice index-name strdict))))
(token-splice-rest body tokens)))
(return true))
;;
;; Hash table with arbitrary key
;;
(def-c-function-alias dict-length hmlenu)
;; (dict (* <your Key Value type>) &return size_t)
(def-c-function-alias dict-free hmfree)
;; (dict (* <your Key Value type>))
;; These are used for the following functions:
;; - dict-ptr-or-default-at
;; - dict-struct-at
;; - dict-value-at
(def-c-function-alias dict-set-not-found-default hmdefaults)
;; (dict (* <your Key Value type>) item <your Key Value type>
;; &return <your Key Value type>)
(def-c-function-alias dict-set-not-found-default-value hmdefault)
;; (dict (* <your Key Value type>) value <your Value type>
;; &return <your Value type>)
;; Setting
(def-c-function-alias dict-set-key-value hmput)
;; (dict (* <your Key Value type>) key <your Key type> value <your Value type>
;; &return <your Value type>)
;; Requires a key field
(def-c-function-alias dict-set-struct hmputs)
;; (dict (* <your Key Value type>) item <your Key Value type> &return <your Key Value type>)
;; Getting
(def-c-function-alias dict-ptr-at hmgetp_null)
;; (dict (* <your Key Value type>) key <your Key type> &return (* <your Key Value type>))
(def-c-function-alias dict-ptr-or-default-at hmgetp)
;; (dict (* <your Key Value type>) key <your Key type> &return (* <your Key Value type>))
(def-c-function-alias dict-index-at hmgeti)
;; (dict (* <your Key Value type>) key <your Key type> &return ptrdiff_t)
(def-c-function-alias dict-struct-at hmgets)
;; (dict (* <your Key Value type>) key <your Key type> &return <your Key Value type>)
(def-c-function-alias dict-value-at hmget)
;; (dict (* <your Key Value type>) &return <your value type>)
(defmacro each-key-in-dict (dict any index-iterator-name symbol
key-name symbol key-type symbol &rest body any)
(tokenize-push output
(c-for
(var (token-splice index-iterator-name) size_t 0)
;; We could hoist this out but it should be a quick op anyways
(< (token-splice index-iterator-name) (dict-length (token-splice dict)))
(incr (token-splice index-iterator-name))
(var (token-splice key-name) (token-splice key-type)
(field (at (token-splice index-name dict)) key))
(token-splice-rest body tokens)))
(return true))
(defmacro each-item-in-dict (dict any index-name symbol
item-name symbol item-type any
&rest body any)
(tokenize-push output
(each-in-dict (token-splice dict) (token-splice index-name)
(var (token-splice item-name item-type) (at (token-splice index-name dict)))
(token-splice-rest body tokens)))
(return true))
(defmacro each-item-addr-in-dict (dict any index-name symbol
item-name symbol ptr-to-item-type any
&rest body any)
(tokenize-push output
(each-in-dict (token-splice dict) (token-splice index-name)
(var (token-splice item-name ptr-to-item-type)
(addr (at (token-splice index-name dict))))
(token-splice-rest body tokens)))
(return true))
;;
;; Tests
;;
(comptime-cond
('auto-test
(c-import "stdio.h")
(defun test--dictionary (&return int)
(scope ;; Simple key value entries
(defstruct dictionary-entry
key (* char)
value (* (const char)))
(var my-dictionary (* dictionary-entry) null)
(strdict-use-key-arena my-dictionary)
(strdict-set-key-value my-dictionary "cakelisp" "a programming language")
(strdict-set-key-value my-dictionary "gamelib" "a helpful library")
(var keys ([] (* (const char))) (array "cakelisp" "gamelib"))
(each-in-array keys i
(var returned-entry-copy (* (const char)) (strdict-value-at my-dictionary (at i keys)))
(unless returned-entry-copy
(fprintf stderr "Expected to get string back. Length: %ld\n"
(strdict-length my-dictionary))
(return 1))
(fprintf stderr "what is %s? %s\n" (at i keys) returned-entry-copy)
(var entry-index ptrdiff_t (strdict-index-at 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) (strdict-ptr-or-default-at 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)) (strdict-length my-dictionary))
(return 1))
(fprintf stderr "what is %s (%p)? %s\n"
(path returned-entry > key) (path returned-entry > key)
(path returned-entry > value)))
(strdict-free my-dictionary))
(scope ;; Arbitrary fields dictionary
(defstruct complex-dictionary-entry
key (* char)
thing-category (* (const char))
thing-description (* (const char)))
(var my-dictionary (* complex-dictionary-entry) null)
(strdict-use-key-arena my-dictionary)
(var items ([] complex-dictionary-entry)
(array
(array (strdup "cakelisp") "programming language" "cakelisp is a programming language")
(array (strdup "gamelib") "library" "gamelib is a library for making games")))
(each-in-array items i
(strdict-set-struct my-dictionary (at i items)))
(unless (= (array-size items) (strdict-length my-dictionary))
(fprintf stderr "Expected size %d, got %d"
(type-cast (array-size items) int)
(type-cast (strdict-length my-dictionary) int))
(return 1))
(each-in-array items i
(var returned-entry (* complex-dictionary-entry)
(strdict-ptr-or-default-at my-dictionary (field (at i items) key)))
(unless (and returned-entry
(path returned-entry > key)
(path returned-entry > thing-category)
(path returned-entry > thing-description))
(fprintf stderr "Expected to get valid entry back, got %p (array[-1] = %p). Length: %ld\n"
returned-entry (addr (at -1 my-dictionary)) (strdict-length my-dictionary))
(return 1))
(fprintf stderr "What is %s? (%s) %s\n"
(path returned-entry > key)
(path returned-entry > thing-category)
(path returned-entry > thing-description)))
(each-item-addr-in-strdict my-dictionary i entry (* complex-dictionary-entry)
(fprintf stderr "[%d] %s = %s\n" (type-cast i int)
(path entry > key)
(path entry > thing-description)))
(scope
(var returned-entry (* complex-dictionary-entry)
(strdict-ptr-or-default-at my-dictionary "bad value"))
(fprintf stderr "Expected to get default (not found) entry back, got %p (array[-1] = %p).
Length: %ld\n"
returned-entry (addr (at -1 my-dictionary)) (strdict-length my-dictionary))
(unless (= returned-entry (addr (at -1 my-dictionary)))
(return 1)))
(scope
(var returned-entry (* complex-dictionary-entry)
(strdict-ptr-at my-dictionary "bad value"))
(fprintf stderr "Expected to get null back, got %p (array[-1] = %p). Length: %ld\n"
returned-entry (addr (at -1 my-dictionary)) (strdict-length my-dictionary))
(when returned-entry
(return 1)))
(each-in-array items i
(free (type-cast (field (at i items) key) (* void))))
(strdict-free my-dictionary))
(return 0))))