Skip to content

Commit

Permalink
0.1.1, do notation with names
Browse files Browse the repository at this point in the history
  • Loading branch information
metagn committed Jan 14, 2021
1 parent 542a315 commit 1b414db
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion applicates.nimble
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Package

version = "0.1.0"
version = "0.1.1"
author = "hlaaftana"
description = "\"pointers\" to cached AST that instantiate routines when called"
license = "MIT"
Expand Down
18 changes: 16 additions & 2 deletions src/applicates.nim
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ macro applicate*(params, body): untyped =
appl.apply()

var c = 0
when false: # runnableExamples does not handle do: well
when false: # runnableExamples does not handle do: correctly, this works otherwise
double(applicate do:
double(applicate do:
double(applicate do: inc c)))
Expand Down Expand Up @@ -250,11 +250,25 @@ macro applicate*(body): untyped =

# const incr = applicate do (x: int) -> int: x + 1
# doAssert incr.apply(x) == 4

# named:

# applicate named do (a, b; c: int):
# let a = b(c)
# named.apply(upperA, char, 65)
# doAssert upperA == 'A'
if body.kind == nnkDo:
if body[3][0].kind == nnkEmpty: body[3][0] = ident"untyped"
let templ = newNimNode(nnkTemplateDef, body)
for s in body:
templ.add(s)
if templ[3][0].kind == nnkEmpty: templ[3][0] = ident"untyped"
result = getAst(makeTypedApplicate(templ))
elif body.kind in {nnkCall, nnkCommand} and body.len == 2 and body[1].kind == nnkDo:
let templ = newNimNode(nnkTemplateDef, body[1])
for s in body[1]:
templ.add(s)
if templ[3][0].kind == nnkEmpty: templ[3][0] = ident"untyped"
templ[0] = body[0]
result = getAst(makeTypedApplicate(templ))
else:
let args = newPar()
Expand Down
9 changes: 9 additions & 0 deletions tests/test_basic.nim
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ test "applicate macro and apply":

foo.apply(5, FooType)
check x.field == 5

const incr = applicate do (x: int) -> int: x + 1
doAssert incr.apply(x.field) == 6

applicate named do (a, b; c: int):
let a = b(c)

named.apply(upperA, char, 65)
doAssert upperA == 'A'

test "operators":
check (x !=> x[^1]) | "abc" == 'c'
Expand Down

0 comments on commit 1b414db

Please sign in to comment.