further improvements for term rewriting macros
This commit is contained in:
parent
b4cd119800
commit
6753d36854
8 changed files with 74 additions and 28 deletions
|
|
@ -23,9 +23,6 @@ type
|
||||||
PPatternContext = var TPatternContext
|
PPatternContext = var TPatternContext
|
||||||
|
|
||||||
proc matches(c: PPatternContext, p, n: PNode): bool
|
proc matches(c: PPatternContext, p, n: PNode): bool
|
||||||
proc checkConstraints(c: PPatternContext, p, n: PNode): bool =
|
|
||||||
# XXX create a new mapping here? --> need use cases
|
|
||||||
result = matches(c, p, n)
|
|
||||||
|
|
||||||
proc canonKind(n: PNode): TNodeKind =
|
proc canonKind(n: PNode): TNodeKind =
|
||||||
## nodekind canonilization for pattern matching
|
## nodekind canonilization for pattern matching
|
||||||
|
|
@ -85,34 +82,28 @@ proc bindOrCheck(c: PPatternContext, param: PSym, n: PNode): bool =
|
||||||
if pp != nil:
|
if pp != nil:
|
||||||
# check if we got the same pattern (already unified):
|
# check if we got the same pattern (already unified):
|
||||||
result = sameTrees(pp, n) #matches(c, pp, n)
|
result = sameTrees(pp, n) #matches(c, pp, n)
|
||||||
elif checkTypes(c, param, n) and
|
elif n.kind == nkArgList or checkTypes(c, param, n):
|
||||||
(param.ast == nil or checkConstraints(c, param.ast, n)):
|
|
||||||
IdNodeTablePutLazy(c.mapping, param, n)
|
IdNodeTablePutLazy(c.mapping, param, n)
|
||||||
result = true
|
result = true
|
||||||
|
|
||||||
proc matchStar(c: PPatternContext, p, n: PNode): bool =
|
proc matchStar(c: PPatternContext, p, n: PNode): bool =
|
||||||
# match ``op*param``
|
# match ``op*param``
|
||||||
# this is quite hard:
|
|
||||||
# match against: f(a, ..., f(b, c, f(...)))
|
|
||||||
# we have different semantics if there is a choice as left operand:
|
|
||||||
|
|
||||||
proc matchStarAux(c: PPatternContext, op, n, arglist: PNode) =
|
proc matchStarAux(c: PPatternContext, op, n, arglist: PNode) =
|
||||||
if n.kind in nkCallKinds and matches(c, op, n.sons[0]):
|
if n.kind in nkCallKinds and matches(c, op, n.sons[0]):
|
||||||
for i in 1..sonsLen(n)-1: matchStarAux(c, op, n.sons[i], arglist)
|
for i in 1..sonsLen(n)-1:
|
||||||
|
matchStarAux(c, op, n[i], arglist)
|
||||||
|
elif n.kind == nkHiddenStdConv and n.sons[1].kind == nkBracket:
|
||||||
|
let n = n.sons[1]
|
||||||
|
for i in 0.. <n.len: matchStarAux(c, op, n[i], arglist)
|
||||||
else:
|
else:
|
||||||
add(arglist, n)
|
add(arglist, n)
|
||||||
|
|
||||||
if n.kind notin nkCallKinds: return false
|
if n.kind notin nkCallKinds: return false
|
||||||
if p.sons[0].kind != nkPattern:
|
if matches(c, p.sons[1], n.sons[0]):
|
||||||
if matches(c, p.sons[0], n.sons[0]):
|
|
||||||
var arglist = newNodeI(nkArgList, n.info)
|
var arglist = newNodeI(nkArgList, n.info)
|
||||||
arglist.typ = p.sons[1].sym.typ
|
matchStarAux(c, p.sons[1], n, arglist)
|
||||||
matchStarAux(c, p.sons[0], n, arglist)
|
result = bindOrCheck(c, p.sons[2].sym, arglist)
|
||||||
result = bindOrCheck(c, p.sons[1].sym, arglist)
|
|
||||||
else:
|
|
||||||
# well it matches somehow ...
|
|
||||||
if matches(c, p.sons[0], n.sons[0]):
|
|
||||||
result = bindOrCheck(c, p.sons[1].sym, n)
|
|
||||||
|
|
||||||
proc matches(c: PPatternContext, p, n: PNode): bool =
|
proc matches(c: PPatternContext, p, n: PNode): bool =
|
||||||
# hidden conversions (?)
|
# hidden conversions (?)
|
||||||
|
|
|
||||||
|
|
@ -432,12 +432,12 @@ proc TypeToString(typ: PType, prefer: TPreferedDesc = preferName): string =
|
||||||
add(result, ']')
|
add(result, ']')
|
||||||
of tyTypeDesc:
|
of tyTypeDesc:
|
||||||
if t.sons.len == 0: result = "typedesc"
|
if t.sons.len == 0: result = "typedesc"
|
||||||
else: result = "typedesc{" & constraintsToStr(t) & "}"
|
else: result = "typedesc[" & constraintsToStr(t) & "]"
|
||||||
of tyTypeClass:
|
of tyTypeClass:
|
||||||
result = constraintsToStr(t)
|
result = constraintsToStr(t)
|
||||||
of tyExpr:
|
of tyExpr:
|
||||||
if t.sons.len == 0: result = "expr"
|
if t.sons.len == 0: result = "expr"
|
||||||
else: result = "expr{" & constraintsToStr(t) & "}"
|
else: result = "expr[" & constraintsToStr(t) & "]"
|
||||||
of tyArray:
|
of tyArray:
|
||||||
if t.sons[0].kind == tyRange:
|
if t.sons[0].kind == tyRange:
|
||||||
result = "array[" & rangeToStr(t.sons[0].n) & ", " &
|
result = "array[" & rangeToStr(t.sons[0].n) & ", " &
|
||||||
|
|
|
||||||
9
tests/patterns/targlist.nim
Normal file
9
tests/patterns/targlist.nim
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
discard """
|
||||||
|
output: "12false3ha"
|
||||||
|
"""
|
||||||
|
|
||||||
|
proc f(x: varargs[string, `$`]) = nil
|
||||||
|
template optF{f(X)}(x: varargs[expr]) =
|
||||||
|
writeln(stdout, x)
|
||||||
|
|
||||||
|
f 1, 2, false, 3, "ha"
|
||||||
9
tests/patterns/tor.nim
Normal file
9
tests/patterns/tor.nim
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
discard """
|
||||||
|
output: "110"
|
||||||
|
"""
|
||||||
|
|
||||||
|
template arithOps: expr = (`+` | `-` | `*`)
|
||||||
|
template testOr{ (arithOps{f})(a, b) }(a, b, f: expr): expr = f(a+1, b)
|
||||||
|
|
||||||
|
let xx = 10
|
||||||
|
echo 10*xx
|
||||||
19
tests/patterns/tstar.nim
Normal file
19
tests/patterns/tstar.nim
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
discard """
|
||||||
|
output: "my awesome concat"
|
||||||
|
"""
|
||||||
|
|
||||||
|
var
|
||||||
|
calls = 0
|
||||||
|
|
||||||
|
proc `&&`(s: varargs[string]): string =
|
||||||
|
result = s[0]
|
||||||
|
for i in 1..len(s)-1: result.add s[i]
|
||||||
|
inc calls
|
||||||
|
|
||||||
|
template optConc{ `&&` * a }(a: expr): expr = &&a
|
||||||
|
|
||||||
|
let space = " "
|
||||||
|
echo "my" && (space & "awe" && "some " ) && "concat"
|
||||||
|
|
||||||
|
# check that it's been properly optimized:
|
||||||
|
doAssert calls == 1
|
||||||
18
tests/patterns/tstmtlist.nim
Normal file
18
tests/patterns/tstmtlist.nim
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
discard """
|
||||||
|
output: '''0
|
||||||
|
|12|34
|
||||||
|
'''
|
||||||
|
"""
|
||||||
|
|
||||||
|
template optWrite{
|
||||||
|
write(stdout, x)
|
||||||
|
write(stdout, y)
|
||||||
|
}(x, y: string) =
|
||||||
|
write(stdout, "|", x, y, "|")
|
||||||
|
|
||||||
|
if true:
|
||||||
|
echo "0"
|
||||||
|
write stdout, "1"
|
||||||
|
write stdout, "2"
|
||||||
|
write stdout, "3"
|
||||||
|
echo "4"
|
||||||
|
|
@ -5,7 +5,7 @@ discard """
|
||||||
|
|
||||||
import strutils
|
import strutils
|
||||||
|
|
||||||
proc foo(s: expr{string}): string =
|
proc foo(s: expr[string]): string =
|
||||||
static: echo s
|
static: echo s
|
||||||
|
|
||||||
const R = s.toUpper
|
const R = s.toUpper
|
||||||
|
|
|
||||||
|
|
@ -5,18 +5,18 @@ type
|
||||||
x: T
|
x: T
|
||||||
y: U
|
y: U
|
||||||
|
|
||||||
proc foo(T: typedesc{float}, a: expr): string =
|
proc foo(T: typedesc[float], a: expr): string =
|
||||||
result = "float " & $(a.len > 5)
|
result = "float " & $(a.len > 5)
|
||||||
|
|
||||||
proc foo(T: typedesc{TFoo}, a: int): string =
|
proc foo(T: typedesc[TFoo], a: int): string =
|
||||||
result = "TFoo " & $(a)
|
result = "TFoo " & $(a)
|
||||||
|
|
||||||
proc foo(T: typedesc{int or bool}): string =
|
proc foo(T: typedesc[int or bool]): string =
|
||||||
var a: T
|
var a: T
|
||||||
a = 10
|
a = 10
|
||||||
result = "int or bool " & ($a)
|
result = "int or bool " & ($a)
|
||||||
|
|
||||||
template foo(T: typedesc{seq}): expr = "seq"
|
template foo(T: typedesc[seq]): expr = "seq"
|
||||||
|
|
||||||
test "types can be used as proc params":
|
test "types can be used as proc params":
|
||||||
check foo(TFoo[int, float], 1000) == "TFoo 1000"
|
check foo(TFoo[int, float], 1000) == "TFoo 1000"
|
||||||
|
|
@ -31,5 +31,5 @@ test "types can be used as proc params":
|
||||||
check foo(seq[TFoo[bool, string]]) == "seq"
|
check foo(seq[TFoo[bool, string]]) == "seq"
|
||||||
|
|
||||||
when false:
|
when false:
|
||||||
proc foo(T: typedesc{seq}, s: T) = nil
|
proc foo(T: typedesc[seq], s: T) = nil
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue