Skip to content

Instantly share code, notes, and snippets.

@sqbi-q
Created August 23, 2025 15:04
Show Gist options
  • Select an option

  • Save sqbi-q/e629db1d0fba6c032b062f43af8d8b65 to your computer and use it in GitHub Desktop.

Select an option

Save sqbi-q/e629db1d0fba6c032b062f43af8d8b65 to your computer and use it in GitHub Desktop.
Handle WebAssembly Text s-expr syntax in Lisp
;gnu clisp 2.49.60
;;;; readtable modifications
;(setq backslash-macro (get-macro-character #\\)) ; default macro under \ (NIL unfortunately)
(setq quotation-macro (get-macro-character #\")) ; default macro under "
(defun nop-backslash-macro (stream char) )
(defun wrap-quotation-macro (stream char)
(setq x (funcall quotation-macro stream char))
(concatenate 'string "\"" x "\""))
(defun set-backslash-macro (fun)
(set-macro-character #\\ fun))
(defun set-quotation-macro (fun)
(set-macro-character #\" fun)) ; "
;(defun enable-backslash-escape ()
; (set-backslash-macro backslash-macro))
(defun disable-backslash-escape ()
(set-backslash-macro #'nop-backslash-macro))
(defun enable-quotation-mark-wrap ()
(set-quotation-macro #'wrap-quotation-macro))
(defun disable-quotation-mark-wrap ()
(set-quotation-macro quotation-macro))
(defun reset-readtable ()
(setq *readtable* (copy-readtable nil)))
;;;;
(setq escape-print-wat nil)
(defun print-wat (wat)
(write wat :escape escape-print-wat))
(disable-backslash-escape)
(enable-quotation-mark-wrap)
(defun raw-wat () ; example sample from WASM-4 template
(setf (readtable-case *readtable*) :invert) ; scoped case-sensetive
`((data (i32.const 0x19a0) "\c3\81\24\24\00\24\99\c3")
(data (i32.const 0x19a8) "Hello from Wat!\00")
(data (i32.const 0x19b8) "Press X to blink\00")
(func (export "start")
(call $tone (i32.const 0xF0) (i32.const 0x2211) (i32.const 0x64) (i32.const 0)))
))
;(reset-readtable)
(print-wat (raw-wat))
;;; Output:
;((data (i32.const 0x19a0) "\c3\81\24\24\00\24\99\c3")
; (data (i32.const 0x19a8) "Hello from Wat!\00")
; (data (i32.const 0x19b8) "Press X to blink\00")
; (func (export "start")
; (call $tone (i32.const 0xf0) (i32.const 0x2211) (i32.const 0x64)
; (i32.const 0))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment