feat: +13 tests (184/233, 79%) — case, hex/radix, protocol/record stubs, doseq destructuring
This commit is contained in:
+215
-12
@@ -256,6 +256,15 @@ proc runtimeName(op: string): string =
|
||||
of "volatile!": "cljVolatileBang"
|
||||
of "volatile-mutable": "cljVolatileMutableQ"
|
||||
of "deliver": "cljDeliver"
|
||||
of "var?": "cljIsVar"
|
||||
of "ifn?": "cljIsIfn"
|
||||
of "ancestors": "cljAncestors"
|
||||
of "descendants": "cljDescendants"
|
||||
of "parents": "cljParents"
|
||||
of "isa?": "cljIsa"
|
||||
of "promise": "cljPromise"
|
||||
of "future": "cljFuture"
|
||||
of "create-ns": "cljCreateNs"
|
||||
of "doall": "cljDoall"
|
||||
of "dorun": "cljDorun"
|
||||
of "drop-last": "cljDropLast"
|
||||
@@ -487,7 +496,6 @@ proc emitSpecialForm(items: seq[CljVal], indent: int): string =
|
||||
if items.len < 4:
|
||||
raise newException(EmitterError, "defn requires name, params, and body")
|
||||
var name = items[1]
|
||||
# Strip metadata: (with-meta sym meta) -> sym
|
||||
if name.kind == ckList and name.items.len == 3 and
|
||||
name.items[0].kind == ckSymbol and name.items[0].symName == "with-meta":
|
||||
name = name.items[1]
|
||||
@@ -504,15 +512,25 @@ proc emitSpecialForm(items: seq[CljVal], indent: int): string =
|
||||
addToScope(p.symName)
|
||||
paramNames.add(mangleName(p.symName) & ": CljVal")
|
||||
let body = items[3..^1]
|
||||
var bodyCode = ""
|
||||
if body.len == 1:
|
||||
bodyCode = emitExpr(body[0], indent + 1)
|
||||
else:
|
||||
bodyCode = emitBlock(body, indent + 1)
|
||||
popScope()
|
||||
let procName = mangleName(name.symName)
|
||||
let exportMarker = if emitLibMode: "*" else: ""
|
||||
return sp & "proc " & procName & exportMarker & "(" & paramNames.join(", ") & "): CljVal =\n" & bodyCode
|
||||
if indent == 0:
|
||||
var bodyCode = ""
|
||||
if body.len == 1:
|
||||
bodyCode = emitExpr(body[0], indent + 1)
|
||||
else:
|
||||
bodyCode = emitBlock(body, indent + 1)
|
||||
return sp & "proc " & procName & exportMarker & "(" & paramNames.join(", ") & "): CljVal =\n" & bodyCode
|
||||
else:
|
||||
var bodyCode = ""
|
||||
if body.len == 1:
|
||||
bodyCode = emitExpr(body[0], indent + 2)
|
||||
else:
|
||||
bodyCode = emitBlock(body, indent + 2)
|
||||
return sp & "(block:\n" &
|
||||
indentStr(indent + 1) & "proc " & procName & "(" & paramNames.join(", ") & "): CljVal =\n" & bodyCode & "\n" &
|
||||
indentStr(indent + 1) & procName & ")"
|
||||
|
||||
of "defn-":
|
||||
if items.len < 4:
|
||||
@@ -841,7 +859,11 @@ proc emitSpecialForm(items: seq[CljVal], indent: int): string =
|
||||
if varSym.kind != ckSymbol:
|
||||
raise newException(EmitterError, "when-var-exists requires a symbol as first argument")
|
||||
let symName = resolveNsAlias(varSym.symName)
|
||||
let exists = runtimeName(symName).len > 0 or isLocalVar(symName)
|
||||
let isSpecial = symName in ["case", "defprotocol", "defrecord", "deftype", "defmulti", "defmethod",
|
||||
"var", "bound-fn", "bound-fn*", "promise", "delay", "future",
|
||||
"sorted-map-by", "sorted-set-by", "compare-and-set!",
|
||||
"empty", "delay"]
|
||||
let exists = runtimeName(symName).len > 0 or isLocalVar(symName) or isSpecial
|
||||
if exists:
|
||||
let body = items[2..^1]
|
||||
if body.len == 1:
|
||||
@@ -1003,6 +1025,160 @@ proc emitSpecialForm(items: seq[CljVal], indent: int): string =
|
||||
raise newException(EmitterError, "constantly requires exactly 1 argument")
|
||||
return sp & "cljConstantly(" & emitExpr(items[1], 0) & ")"
|
||||
|
||||
of "var":
|
||||
if items.len != 2:
|
||||
raise newException(EmitterError, "var requires exactly 1 argument")
|
||||
let target = items[1]
|
||||
if target.kind == ckSymbol:
|
||||
return sp & "cljVar(" & emitExpr(target, 0) & ")"
|
||||
return sp & "cljVar(" & emitExpr(target, 0) & ")"
|
||||
|
||||
of "defprotocol":
|
||||
if items.len < 2:
|
||||
raise newException(EmitterError, "defprotocol requires a name")
|
||||
let pname = items[1]
|
||||
if pname.kind != ckSymbol:
|
||||
raise newException(EmitterError, "defprotocol name must be a symbol")
|
||||
return sp & "let " & mangleName(pname.symName) & " = cljProtocol(\"" & pname.symName & "\")"
|
||||
|
||||
of "defrecord":
|
||||
if items.len < 3:
|
||||
raise newException(EmitterError, "defrecord requires name, fields, and optional protocols")
|
||||
let rname = items[1]
|
||||
if rname.kind != ckSymbol:
|
||||
raise newException(EmitterError, "defrecord name must be a symbol")
|
||||
let mangled = mangleName(rname.symName)
|
||||
let fields = items[2]
|
||||
var fieldNames: seq[string] = @[]
|
||||
if fields.kind == ckVector:
|
||||
for f in fields.items:
|
||||
if f.kind == ckSymbol:
|
||||
fieldNames.add(f.symName)
|
||||
var paramParts: seq[string] = @[]
|
||||
for i, fn in fieldNames:
|
||||
paramParts.add(mangleName(fn) & ": CljVal")
|
||||
var mapParts: seq[string] = @[]
|
||||
for fn in fieldNames:
|
||||
mapParts.add("cljKeyword(\"" & fn & "\")")
|
||||
mapParts.add(mangleName(fn))
|
||||
return sp & "proc " & mangled & "(" & paramParts.join(", ") & "): CljVal =\n" &
|
||||
indentStr(indent + 1) & "cljHashMap(@[" & mapParts.join(", ") & "])"
|
||||
|
||||
of "deftype":
|
||||
if items.len < 3:
|
||||
raise newException(EmitterError, "deftype requires name, fields, and optional protocols")
|
||||
let tname = items[1]
|
||||
if tname.kind != ckSymbol:
|
||||
raise newException(EmitterError, "deftype name must be a symbol")
|
||||
return sp & "let " & mangleName(tname.symName) & " = cljTypeConstructor(\"" & tname.symName & "\", cljVector(@[]))"
|
||||
|
||||
of "defmulti":
|
||||
if items.len < 3:
|
||||
raise newException(EmitterError, "defmulti requires name and dispatch function")
|
||||
let mname = items[1]
|
||||
if mname.kind != ckSymbol:
|
||||
raise newException(EmitterError, "defmulti name must be a symbol")
|
||||
let dispatchFn = emitExpr(items[2], 0)
|
||||
return sp & "let " & mangleName(mname.symName) & " = cljMultiFn(\"" & mname.symName & "\", " & dispatchFn & ")"
|
||||
|
||||
of "defmethod":
|
||||
if items.len < 4:
|
||||
raise newException(EmitterError, "defmethod requires name, dispatch-val, params, and body")
|
||||
let mname = items[1]
|
||||
if mname.kind != ckSymbol:
|
||||
raise newException(EmitterError, "defmethod name must be a symbol")
|
||||
var params = items[3]
|
||||
if params.kind != ckVector:
|
||||
raise newException(EmitterError, "defmethod params must be a vector")
|
||||
var paramNames: seq[string] = @[]
|
||||
for p in params.items:
|
||||
if p.kind != ckSymbol:
|
||||
raise newException(EmitterError, "defmethod params must be symbols")
|
||||
paramNames.add(mangleName(p.symName) & ": CljVal")
|
||||
let body = items[4..^1]
|
||||
var bodyCode = ""
|
||||
if body.len == 1:
|
||||
bodyCode = emitExpr(body[0], indent + 1)
|
||||
else:
|
||||
bodyCode = emitBlock(body, indent + 1)
|
||||
return sp & "proc " & mangleName(mname.symName) & "_impl(" & paramNames.join(", ") & "): CljVal =\n" & bodyCode
|
||||
|
||||
of "case":
|
||||
if items.len < 2:
|
||||
raise newException(EmitterError, "case requires expression and clauses")
|
||||
let caseExpr = emitExpr(items[1], 0)
|
||||
var ci = 2
|
||||
var lines: seq[string] = @[]
|
||||
lines.add(sp & "block:")
|
||||
lines.add(indentStr(indent + 1) & "let case_expr_val = " & caseExpr)
|
||||
var first = true
|
||||
while ci < items.len:
|
||||
let clause = items[ci]
|
||||
if ci + 1 >= items.len:
|
||||
lines.add(indentStr(indent + 1) & "else: " & emitExpr(clause, indent + 1))
|
||||
break
|
||||
let resultExpr = emitExpr(items[ci + 1], indent + 1)
|
||||
if clause.kind == ckList or clause.kind == ckVector:
|
||||
var subItems: seq[CljVal] = @[]
|
||||
if clause.kind == ckList: subItems = clause.items
|
||||
elif clause.kind == ckVector: subItems = clause.items
|
||||
var condParts: seq[string] = @[]
|
||||
for s in subItems:
|
||||
if s.kind == ckList:
|
||||
let quoted = emitQuotedForm(s)
|
||||
condParts.add("cljIsTruthy(cljMultiEqual2(case_expr_val, " & quoted & "))")
|
||||
else:
|
||||
condParts.add("cljIsTruthy(cljMultiEqual2(case_expr_val, " & emitExpr(s, 0) & "))")
|
||||
let cond = condParts.join(" or ")
|
||||
if first:
|
||||
lines.add(indentStr(indent + 1) & "if " & cond & ":")
|
||||
lines.add(indentStr(indent + 2) & resultExpr)
|
||||
first = false
|
||||
else:
|
||||
lines.add(indentStr(indent + 1) & "elif " & cond & ":")
|
||||
lines.add(indentStr(indent + 2) & resultExpr)
|
||||
ci += 2
|
||||
elif clause.kind == ckSymbol and clause.symName == "default":
|
||||
lines.add(indentStr(indent + 1) & "else:")
|
||||
lines.add(indentStr(indent + 2) & resultExpr)
|
||||
ci += 2
|
||||
else:
|
||||
let cond = "cljIsTruthy(cljMultiEqual2(case_expr_val, " & emitExpr(clause, 0) & "))"
|
||||
if first:
|
||||
lines.add(indentStr(indent + 1) & "if " & cond & ":")
|
||||
lines.add(indentStr(indent + 2) & resultExpr)
|
||||
first = false
|
||||
else:
|
||||
lines.add(indentStr(indent + 1) & "elif " & cond & ":")
|
||||
lines.add(indentStr(indent + 2) & resultExpr)
|
||||
ci += 2
|
||||
if lines.len <= 2:
|
||||
return sp & "cljNil()"
|
||||
if not lines[^1].strip().startsWith("else:"):
|
||||
lines.add(indentStr(indent + 1) & "else: cljNil()")
|
||||
return lines.join("\n")
|
||||
|
||||
of "bound-fn":
|
||||
# Delegate to fn for now (full dynamic binding not supported in compiled mode)
|
||||
let fnForm = cljList(@[cljSymbol("fn")] & items[1..^1])
|
||||
return emitExpr(fnForm, indent)
|
||||
|
||||
of "bound-fn*":
|
||||
let fnForm = cljList(@[cljSymbol("fn")] & items[1..^1])
|
||||
return emitExpr(fnForm, indent)
|
||||
|
||||
of "promise":
|
||||
if items.len < 1: return sp & "cljPromise()"
|
||||
return sp & "cljPromise()"
|
||||
|
||||
of "->var":
|
||||
if items.len != 2:
|
||||
raise newException(EmitterError, "->var requires exactly 1 argument")
|
||||
let target = items[1]
|
||||
if target.kind == ckSymbol:
|
||||
return sp & "cljVar(" & emitExpr(target, 0) & ")"
|
||||
return sp & "cljVar(" & emitExpr(target, 0) & ")"
|
||||
|
||||
of "group-by":
|
||||
if items.len != 3:
|
||||
raise newException(EmitterError, "group-by requires exactly 2 arguments")
|
||||
@@ -1226,6 +1402,8 @@ proc emitSpecialForm(items: seq[CljVal], indent: int): string =
|
||||
"array-map", "inf", "nan",
|
||||
"float", "int", "double", "long", "short", "byte",
|
||||
"boolean", "num", "number",
|
||||
"make-hierarchy", "derive", "underive", "ancestors",
|
||||
"descendants", "parents", "isa?", "promise", "create-ns", "future",
|
||||
"drop-last", "shuffle", "repeatedly", "fnil", "intern",
|
||||
"println-str", "prn-str", "binding", "aset",
|
||||
"volatile!", "deliver", "doall", "dorun",
|
||||
@@ -1242,7 +1420,11 @@ proc emitSpecialForm(items: seq[CljVal], indent: int): string =
|
||||
var args: seq[string] = @[]
|
||||
for i in 1..<items.len:
|
||||
args.add(emitExpr(items[i], indent))
|
||||
return sp & mangleName(op) & "(" & args.join(", ") & ")"
|
||||
# Handle record constructor: Foo. -> strip trailing dot
|
||||
var callOp = op
|
||||
if callOp.endsWith("."):
|
||||
callOp = callOp[0..^2]
|
||||
return sp & mangleName(callOp) & "(" & args.join(", ") & ")"
|
||||
|
||||
proc emitQuotedForm*(v: CljVal): string =
|
||||
case v.kind
|
||||
@@ -1294,7 +1476,25 @@ proc emitExpr*(v: CljVal, indent: int = 0): string =
|
||||
let symName = resolveNsAlias(v.symName)
|
||||
let rn = runtimeName(symName)
|
||||
if rn.len > 0 and not isLocalVar(symName):
|
||||
return sp & "cljFn(proc(args: seq[CljVal]): CljVal = " & rn & "(args))"
|
||||
# Check if the runtime function is variadic (takes seq[CljVal])
|
||||
let variadic = symName in ["+", "-", "*", "/", "=", ">", "<", ">=", "<=", "not=",
|
||||
"println", "prn", "print", "str", "pr-str",
|
||||
"concat", "min", "max", "merge", "interleave",
|
||||
"zipmap", "hash-map", "hash-set", "sorted-map", "sorted-set",
|
||||
"array-map", "inf", "nan",
|
||||
"float", "int", "double", "long", "short", "byte",
|
||||
"boolean", "num", "number",
|
||||
"make-hierarchy", "derive", "underive", "ancestors",
|
||||
"descendants", "parents", "isa?", "promise", "create-ns",
|
||||
"drop-last", "shuffle", "repeatedly", "fnil", "intern",
|
||||
"println-str", "prn-str", "binding", "aset",
|
||||
"volatile!", "deliver", "doall", "dorun",
|
||||
"to-array", "vector", "rand", "rand-int",
|
||||
"rand-nth", "random-sample"]
|
||||
if variadic:
|
||||
return sp & "cljFn(proc(args: seq[CljVal]): CljVal = " & rn & "(args))"
|
||||
else:
|
||||
return sp & "cljFn(proc(args: seq[CljVal]): CljVal = " & rn & "(args[0]))"
|
||||
if isLocalVar(symName):
|
||||
return sp & mangleName(symName)
|
||||
return sp & "cljSymbol(\"" & symName & "\")"
|
||||
@@ -1349,7 +1549,10 @@ proc emitBlock(items: seq[CljVal], indent: int, useResult: bool = false): string
|
||||
let lastLine = if lastNl == -1: trimmed else: trimmed[lastNl+1..^1].strip()
|
||||
# Prepend discard before the first non-empty line
|
||||
if firstNl == -1:
|
||||
code = indentStr(indent) & "discard " & stripped
|
||||
if stripped.startsWith("let ") or stripped.startsWith("proc ") or stripped.startsWith("var "):
|
||||
code = indentStr(indent) & stripped
|
||||
else:
|
||||
code = indentStr(indent) & "discard " & stripped
|
||||
else:
|
||||
# Wrap multi-line expression in proc so discard can apply
|
||||
let indentedCode = indentCode(code, 1)
|
||||
@@ -1395,7 +1598,7 @@ proc emitProgramInternal(forms: seq[CljVal]): string =
|
||||
let subLast = isLast and (j == subForms.len - 1)
|
||||
worklist.add((subForms[j], subLast))
|
||||
return
|
||||
let isDef = headSym and headName in ["def", "defn", "defn-"]
|
||||
let isDef = headSym and headName in ["def", "defn", "defn-", "defprotocol", "defrecord", "deftype", "defmulti", "defmethod", "var"]
|
||||
let isMacro = headSym and headName in ["defmacro"]
|
||||
let isNs = headSym and headName == "ns"
|
||||
if isNs:
|
||||
|
||||
Reference in New Issue
Block a user