Browse Source

Tweak Emacs config, add helper macros

* Add set-fields and if-open-file-scoped for convenience
c-port
Macoy Madson 1 year ago
parent
commit
4e0dcb1b99
  1. 33
      runtime/CHelpers.cake
  2. 17
      runtime/FileUtilities.cake
  3. 29
      test/CppHelpersTest.cake
  4. 10
      tools/cakelisp.el

33
runtime/CHelpers.cake

@ -451,3 +451,36 @@
(addr alias)))
(return evaluated-success))
;;
;; Uncategorized
;;
;; Make it easier to specify fields by name:
;;(set-fields my-struct
;; member-a 43
;; member-b (* 3 4)
;; (member-c field-a) 3)
(defmacro set-fields (output-struct any &rest set-fields (index any))
(var end-invocation-index int (FindCloseParenTokenIndex tokens startTokenIndex))
(var field-name-token (addr (const Token)) null)
(each-token-argument-in tokens set-fields end-invocation-index current-index
(unless field-name-token
(set field-name-token (addr (at current-index tokens)))
(continue))
(var set-value-token (addr (const Token)) (addr (at current-index tokens)))
(if (= (path field-name-token > type) TokenType_OpenParen)
(scope ;; Support nested fields via parens
(tokenize-push output
(set (field (token-splice output-struct)
(token-splice-rest (+ field-name-token 1) tokens))
(token-splice set-value-token))))
(scope
(tokenize-push output
(set (field (token-splice output-struct) (token-splice field-name-token))
(token-splice set-value-token)))))
(set field-name-token null))
(when field-name-token
(ErrorAtToken (deref field-name-token) "Expected value for this field")
(return false))
(return true))

17
runtime/FileUtilities.cake

@ -176,6 +176,23 @@
(fread (addr (token-splice item)) (sizeof (token-splice item)) 1 (token-splice in-file)))
(return true))
;; Automatically closes the file when exited
;; Note that the on-failure-block is intentionally not optional to encourage error handling
(defmacro if-open-file-scoped (filename any flags string file-pointer-name symbol
on-success-block array on-failure-block array)
(tokenize-push output
(scope
(var (token-splice file-pointer-name) (addr FILE)
(fopen (token-splice filename) (token-splice flags)))
(if (token-splice file-pointer-name)
(scope
;; We need defer in case the user returns out of this block etc.
(defer (fclose (token-splice file-pointer-name)))
(token-splice on-success-block))
(scope
(token-splice on-failure-block)))))
(return true))
(ignore ;; For reference
(defun-local test--load-save ()
(scope

29
test/CppHelpersTest.cake

@ -1,5 +1,5 @@
(add-cakelisp-search-directory "runtime")
(import "CHelpers.cake")
(import "CHelpers.cake" "FileUtilities.cake")
(defenum my-enum
my-enum-a
@ -18,6 +18,33 @@
(defun main (&return int)
(var state my-enum my-enum-a)
;; Add test some regular C helpers while we are at it
(defstruct nested
a int
b int)
(defstruct my-struct
a int
b int
c nested)
(var thing my-struct (array 0))
(set-fields thing
a 42
b 64
(c a) 92
(c b) 72)
(if-open-file-scoped "fail.txt" "rb" fail-file
(scope
(fprintf stderr "error: I didn't expect to succeed, but I did\n")
(return 1))
(fprintf stderr "I expect to fail, and I did\n"))
(var filename (addr (const char)) "CppHelpersTest.cake")
(if-open-file-scoped filename "rb" succeed-file
(fprintf stderr "successfully opened %s\n" filename)
(scope
(fprintf stderr "error: Failed to open %s\n" filename)
(return 1)))
(return 0))
(set-cakelisp-option executable-output "test/CppHelpers")

10
tools/cakelisp.el

@ -48,14 +48,20 @@
(put 'defer 'lisp-indent-function 0)
(put 'set-fields 'lisp-indent-function 1)
(put 'var-cast-to 'lisp-indent-function 2)
(put 'if-open-file-scoped 'lisp-indent-function 3)
;; Keywords
;; "(def[a-zA-Z0-9-]*" all define keywords
;; Configuration, build stuff, etc.
(font-lock-add-keywords nil '(("(\\(add-build-config-label\\|add-build-options\\|add-build-options-global\\|add-c-build-dependency\\|add-c-search-directory-global\\|add-c-search-directory-module\\|add-cakelisp-search-directory\\|add-compile-time-hook\\|add-compile-time-hook-module\\|add-compiler-link-options\\|add-cpp-build-dependency\\|add-library-dependency\\|add-library-runtime-search-directory\\|add-library-search-directory\\|add-linker-options\\|add-static-link-objects\\|set-cakelisp-option\\|set-module-option\\|c-import\\|c-preprocessor-define\\|c-preprocessor-define-global\\|comptime-cond\\|comptime-define-symbol\\|comptime-error\\|import\\|rename-builtin\\|export\\|export-and-evaluate\\|splice-point\\|token-splice\\|token-splice-addr\\|token-splice-array\\|token-splice-rest\\|tokenize-push\\)[ )\n]"
(font-lock-add-keywords nil '(("(\\(add-build-config-label\\|add-build-options\\|add-build-options-global\\|add-c-build-dependency\\|add-c-search-directory-global\\|add-c-search-directory-module\\|add-cakelisp-search-directory\\|add-compile-time-hook\\|add-compile-time-hook-module\\|add-compiler-link-options\\|add-cpp-build-dependency\\|add-library-dependency\\|add-library-runtime-search-directory\\|add-library-search-directory\\|add-linker-options\\|add-static-link-objects\\|set-cakelisp-option\\|set-module-option\\|c-import\\|c-preprocessor-define\\|c-preprocessor-define-global\\|comptime-cond\\|comptime-define-symbol\\|comptime-error\\|import\\|rename-builtin\\|export\\|export-and-evaluate\\|splice-point\\|token-splice-rest\\|token-splice\\|token-splice-addr\\|token-splice-array\\|token-splice-rest\\|tokenize-push\\)[ )\n]"
1 font-lock-builtin-face)))
(font-lock-add-keywords nil '(("\\b\\(true\\|false\\|null\\)\\b"
;; This doesn't quite work, because two keywords in a row will not be highlighted. Example: (index any)
(font-lock-add-keywords nil '(("[^-]\\b\\(false\\|true\\|string\\|any\\|index\\|arg-index\\|symbol\\|array\\|null\\)\\b[^-]"
1 font-lock-builtin-face)))
(font-lock-add-keywords nil '(("(\\(addr\\|ref\\|template\\|and\\|array\\|at\\|bit-shift-<<\\|bit-shift->>\\|bit-and\\|bit-ones-complement\\|bit-or\\|bit-xor\\|call\\|call-on\\|call-on-ptr\\|decr\\|def-function-signature\\|def-function-signature-global\\|def-type-alias\\|def-type-alias-global\\|defgenerator\\|defmacro\\|defstruct\\|defstruct-local\\|defun\\|defun-comptime\\|defun-local\\|defun-nodecl\\|delete\\|delete-array\\|deref\\|eq\\|field\\|in\\|incr\\|mod\\|neq\\|new\\|new-array\\|not\\|nth\\|or\\|path\\|scope\\|defer\\|set\\|type\\|type-cast\\|var\\|var-global\\|var-static\\)[ )\n]"

Loading…
Cancel
Save