fixes 7980
This commit is contained in:
parent
bf394ed1a1
commit
e03b3bdde7
2 changed files with 56 additions and 21 deletions
|
|
@ -36,27 +36,35 @@ proc applyPatterns(c: PContext, n: PNode): PNode =
|
||||||
# we apply the last pattern first, so that pattern overriding is possible;
|
# we apply the last pattern first, so that pattern overriding is possible;
|
||||||
# however the resulting AST would better not trigger the old rule then
|
# however the resulting AST would better not trigger the old rule then
|
||||||
# anymore ;-)
|
# anymore ;-)
|
||||||
for i in countdown(c.patterns.len-1, 0):
|
if c.patterns.len > 0:
|
||||||
let pattern = c.patterns[i]
|
|
||||||
if not isNil(pattern):
|
# temporary disable converters
|
||||||
let x = applyRule(c, pattern, result)
|
var ctx_converters: TSymSeq
|
||||||
if not isNil(x):
|
shallowCopy(ctx_converters, c.converters)
|
||||||
assert x.kind in {nkStmtList, nkCall}
|
c.converters = @[]
|
||||||
# better be safe than sorry, so check evalTemplateCounter too:
|
defer: shallowCopy(c.converters, ctx_converters)
|
||||||
inc(evalTemplateCounter)
|
|
||||||
if evalTemplateCounter > evalTemplateLimit:
|
for i in countdown(c.patterns.len-1, 0):
|
||||||
globalError(c.config, n.info, "template instantiation too nested")
|
let pattern = c.patterns[i]
|
||||||
# deactivate this pattern:
|
if not isNil(pattern):
|
||||||
c.patterns[i] = nil
|
let x = applyRule(c, pattern, result)
|
||||||
if x.kind == nkStmtList:
|
if not isNil(x):
|
||||||
assert x.len == 3
|
assert x.kind in {nkStmtList, nkCall}
|
||||||
x.sons[1] = evalPattern(c, x.sons[1], result)
|
# better be safe than sorry, so check evalTemplateCounter too:
|
||||||
result = flattenStmts(x)
|
inc(evalTemplateCounter)
|
||||||
else:
|
if evalTemplateCounter > evalTemplateLimit:
|
||||||
result = evalPattern(c, x, result)
|
globalError(c.config, n.info, "template instantiation too nested")
|
||||||
dec(evalTemplateCounter)
|
# deactivate this pattern:
|
||||||
# activate this pattern again:
|
c.patterns[i] = nil
|
||||||
c.patterns[i] = pattern
|
if x.kind == nkStmtList:
|
||||||
|
assert x.len == 3
|
||||||
|
x.sons[1] = evalPattern(c, x.sons[1], result)
|
||||||
|
result = flattenStmts(x)
|
||||||
|
else:
|
||||||
|
result = evalPattern(c, x, result)
|
||||||
|
dec(evalTemplateCounter)
|
||||||
|
# activate this pattern again:
|
||||||
|
c.patterns[i] = pattern
|
||||||
|
|
||||||
proc hlo(c: PContext, n: PNode): PNode =
|
proc hlo(c: PContext, n: PNode): PNode =
|
||||||
inc(c.hloLoopDetector)
|
inc(c.hloLoopDetector)
|
||||||
|
|
|
||||||
27
tests/template/tpattern_with_converter.nim
Normal file
27
tests/template/tpattern_with_converter.nim
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
discard """
|
||||||
|
output: 10.0
|
||||||
|
"""
|
||||||
|
|
||||||
|
type
|
||||||
|
MyFloat = object
|
||||||
|
val: float
|
||||||
|
|
||||||
|
converter to_myfloat*(x: float): MyFloat {.inline.} =
|
||||||
|
MyFloat(val: x)
|
||||||
|
|
||||||
|
proc `+`(x1, x2: MyFloat): MyFloat =
|
||||||
|
MyFloat(val: x1.val + x2.val)
|
||||||
|
|
||||||
|
proc `*`(x1, x2: MyFloat): MyFloat =
|
||||||
|
MyFloat(val: x1.val * x2.val)
|
||||||
|
|
||||||
|
template optMul{`*`(a, 2.0)}(a: MyFloat): MyFloat =
|
||||||
|
a + a
|
||||||
|
|
||||||
|
func floatMyFloat(x: MyFloat): MyFloat =
|
||||||
|
result = x * 2.0
|
||||||
|
|
||||||
|
func floatDouble(x: float): float =
|
||||||
|
result = x * 2.0
|
||||||
|
|
||||||
|
echo floatDouble(5)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue