Skip to content

Instantly share code, notes, and snippets.

@callendorph
Last active October 12, 2022 02:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save callendorph/f69cdb54f24650be7a153a9bcd8a8286 to your computer and use it in GitHub Desktop.
Save callendorph/f69cdb54f24650be7a153a9bcd8a8286 to your computer and use it in GitHub Desktop.
assert check with bug in symbol map.
defpackage assert-check :
import core
import collections
import stz/core-macros
import macro-utils
defsyntax assert-check :
import (exp4, exp!) from core
; This should really be recursive - but
; I haven't gotten there yet.
defn find-arg-symbols (form) -> List<Symbol> :
match-syntax(form) :
($do ?f ?args ...) :
to-list $
for arg in unwrap-all(args) filter :
arg is Symbol
(_ ...) :
throw( Exception("Unsupported pattern within assert: Received %_" % [form]))
defn handleAssert (check-expr, msg) :
;println("check-expr = %_" % [check-expr])
;println("msg = %_" % [msg])
val argSymbs = find-arg-symbols(check-expr)
;println("Found Args: %~" % [argSymbs])
val format-string = to-string("ASSERT: %_ = %%~" % [check-expr])
val template = `(
if not check:
println(check-format % [ check ])
val msg = msg-content
if length(msg) > 0 :
println("MSG: %~" % [ msg-content ])
val symbTable = [ symbols{symbName => symb} ]
println("Symbols: %~" % [symbTable])
println("Fail Here"))
; I can't just pass the argSymbs List of symbols
; to the template because it gets evaluate at
; runtime. So I must extract this out into a
; mapping of "Name" -> "Symbol"
;
; To accomplist this - I must use the `nested`
; utility of the fill-template. Otherwise, the
; fill-template won't unpack the KVP's as s-expressions
; and this causes syntax errors.
defn extract-kvp (x:Symbol) :
[KeyValue(`symbName, name(x)) KeyValue(`symb, x)]
val symbTable = to-tuple(map(extract-kvp, argSymbs))
val replacements = [
`check => unwrap-token(check-expr)
`check-format => format-string
`msg-content => msg
`symbols => nested $ symbTable
]
fill-template(template, replacements)
defrule exp4 = (assert(?check-expr:#exp!)) :
val form = handleAssert(check-expr, "")
parse-syntax[core / #exp](form)
defrule exp4 = (assert(?check-expr:#exp!, ?msg:#exp!)) :
val form = handleAssert(check-expr, msg)
parse-syntax[core / #exp](form)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment