diff --git a/.gitignore b/.gitignore index 5f969f8..453f6b7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +deps nim.cfg bin/ nimblemeta.json diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index f93225b..0000000 --- a/.gitmodules +++ /dev/null @@ -1,24 +0,0 @@ -[submodule "testes"] - path = testes - url = https://github.com/disruptek/testes - branch = master -[submodule "criterion"] - path = criterion - url = https://github.com/disruptek/criterion - branch = master -[submodule "grok"] - path = grok - url = https://github.com/disruptek/grok - branch = master -[submodule "packedjson"] - path = packedjson - url = https://github.com/Araq/packedjson - branch = master -[submodule "eminim"] - path = eminim - url = https://github.com/planetis-m/eminim - branch = master -[submodule "jsony"] - path = jsony - url = https://github.com/treeform/jsony - branch = master diff --git a/criterion b/criterion deleted file mode 160000 index 83a49e7..0000000 --- a/criterion +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 83a49e723df6b54fe2329ad941c2124df69e7bc6 diff --git a/eminim b/eminim deleted file mode 160000 index 86d93da..0000000 --- a/eminim +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 86d93daa6e4ee773838d968542e5d9614f011c36 diff --git a/grok b/grok deleted file mode 160000 index eb313f7..0000000 --- a/grok +++ /dev/null @@ -1 +0,0 @@ -Subproject commit eb313f7c1c655690cfdf90b19fe7386eccf93df0 diff --git a/jason.nim b/jason.nim index 9d192c8..3ae1a5a 100644 --- a/jason.nim +++ b/jason.nim @@ -193,7 +193,7 @@ proc jasonSquare(a: NimNode): NimNode = macro jason*[I, T](a: array[I, T]): Jason = ## Render any Nim array as a series of JSON values. # make sure the array ast has the form we expect - let typ = a.getTypeImpl + let typ = a.getType # support for nim-1.2 if typ.kind == nnkBracket: return jasonSquare a @@ -203,28 +203,59 @@ macro jason*[I, T](a: array[I, T]): Jason = else: # take a look at the range definition for the array let ranger = typ[1] - expectKind(ranger, nnkInfix) # infix - expectKind(ranger[0], nnkIdent) # ident".." - expectKind(ranger[1], nnkIntLit) # 0 - expectKind(ranger[2], nnkIntLit) # 10 - if $ranger[0] != "..": - error "unexpected infix range:\n" & treeRepr(ranger) + when false: + if ranger.kind == nnkBracketExpr: + echo treeRepr(ranger) + echo "get type1: ", treeRepr(ranger.getType) + echo "get inst1: ", treeRepr(ranger.getTypeInst) + echo "get impl1: ", treeRepr(ranger.getTypeImpl) + var ranger = ranger.getTypeInst + echo "get type2: ", treeRepr(ranger.getType) + echo "get inst2: ", treeRepr(ranger.getTypeInst) + echo "get impl2: ", treeRepr(ranger.getTypeImpl) # okay, let's do this thing let js = ident"jason" let s = genSym(nskVar, "jason") var list = newStmtList() + # we'll make adding strings to our accumulating string easier... template addString(x: typed): NimNode {.dirty.} = # also cast the argument to a string just for correctness - list.add newCall(newDotExpr(s, bindSym"add"), newCall(ident"string", x)) - # iterate over the array by index and add each item to the string + add list: + s.newDotExpr(bindSym"add").newCall: + ident"string".newCall x + + template composeIt(iter: typed; body: untyped) {.dirty.} = + var first = true + for it {.inject.} in iter: + #var it {.inject.} = it + if first: + first = false + else: + addString newLit"," # comma between each element + let index = body + # s.add jason(a[index]) + addString js.newCall(nnkBracketExpr.newTree(a, index)) + list.add newVarStmt(s, newLit"[") - for index in ranger[1].intVal .. ranger[2].intVal: - if index != 0: - addString newLit"," # comma between each element - # s.add jason(a[index]) - addString js.newCall(nnkBracketExpr.newTree(a, newLit index)) + + # iterate over the array by index and add each item to the string + case ranger.kind + of nnkBracketExpr, nnkInfix, nnkSym: + # type A = array[0..10, string] + # type A = array[foo..bar, string] + expectKind(ranger[1], nnkIntLit) # 0 + expectKind(ranger[2], nnkIntLit) # 10 + composeIt ranger[1].intVal .. ranger[2].intVal: + newLit it.int + of nnkEnumTy: + # type A = array[Enum, string] + composeIt 1 ..< ranger.len: + ranger[it] + else: + error "expected infix or enum:\n" & treeRepr(ranger) + addString newLit"]" list.add s # final value of the stmtlist is the string itself result = newCall(ident"Jason", list) diff --git a/jason.nimble b/jason.nimble index 32d5b81..cd49d28 100644 --- a/jason.nimble +++ b/jason.nimble @@ -4,14 +4,14 @@ description = "compile-time json" license = "MIT" when not defined(release): - requires "https://github.com/disruptek/testes >= 1.0.0 & < 2.0.0" + requires "https://github.com/disruptek/balls >= 2.0.0 & < 3.0.0" requires "https://github.com/disruptek/criterion < 1.0.0" task test, "run tests for ci": when defined(windows): - exec "testes.cmd" + exec "balls.cmd" else: - exec findExe"testes" + exec findExe"balls" task demo, "generate benchmarks": exec """demo docs/bench.svg "nim c --out=\$1 --panics:on --gc:arc --define:danger tests/bench.nim"""" diff --git a/jsony b/jsony deleted file mode 160000 index 80c9024..0000000 --- a/jsony +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 80c9024b91795042953fbe7b89af0a14991facaf diff --git a/packedjson b/packedjson deleted file mode 160000 index 5801329..0000000 --- a/packedjson +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 58013295996e8dd9cf9f95706e7470bc0e16ebeb diff --git a/testes b/testes deleted file mode 160000 index e6ddd95..0000000 --- a/testes +++ /dev/null @@ -1 +0,0 @@ -Subproject commit e6ddd9501b69e8e02ae88289dd6a47e10548263e diff --git a/tests/test.nim b/tests/test.nim index 63f0588..61e1f81 100644 --- a/tests/test.nim +++ b/tests/test.nim @@ -1,6 +1,6 @@ import std/options -import testes +import balls import jason type @@ -36,7 +36,7 @@ type func `==`(js: Jason or string; s: Jason or string): bool = system.`==`(js.string, s.string) -testes: +suite "sweet json": test "string": check jason"hello" == """"hello"""" @@ -164,3 +164,21 @@ testes: dj = $jason(d) check cj == """{"foo":3,"bar":4.0,"bif":0,"baz":true,"bin":["e","f","g","h"]}""" check dj == """{"foo":2,"bar":8.0,"bif":1,"boz":{"a":6,"b":7.0},"bin":["i","j","k","l"]}""" + + test "arrays with enum indices": + type + E = enum one, two, three, four + B = array[E, string] + let y: B = [one: "a", two: "b", three: "c", four: "d"] + check $jason(y) == """["a","b","c","d"]""" + + test "arrays with subset of enum indices": + when true: + skip"does not work yet; broken typed macro ast?" + else: + type + E = enum one, two, three, four + A = array[two..four, string] + let x: A = [two: "b", three: "c", four: "d"] + checkpoint $jason(x) + check $jason(x) == """["b","c","d"]"""