term rewriting improvements

This commit is contained in:
Araq 2012-09-08 15:43:21 +02:00
commit b64eeeb430
18 changed files with 284 additions and 144 deletions

View file

@ -0,0 +1,26 @@
discard """
output: "21"
"""
import macros
type
TMat = object
dummy: int
proc `*`(a, b: TMat): TMat = nil
proc `+`(a, b: TMat): TMat = nil
proc `-`(a, b: TMat): TMat = nil
proc `$`(a: TMat): string = result = $a.dummy
macro optOps{ (`+`|`-`|`*`) *| a }(a: varargs[TMat]): expr =
result = newIntLitNode(21)
#macro optPlus{ `+` * a }(a: varargs[TMat]): expr =
# result = newIntLitNode(21)
var x, y, z: TMat
echo x + y * z - x
#echo x + y + z

View file

@ -0,0 +1,10 @@
discard """
output: "4"
"""
# test that an endless recursion is avoided:
template optLen{len(x)}(x: expr): expr = len(x)
var s = "lala"
echo len(s)

View file

@ -10,7 +10,7 @@ proc `&&`(s: varargs[string]): string =
for i in 1..len(s)-1: result.add s[i]
inc calls
template optConc{ `&&` * a }(a: expr): expr = &&a
template optConc{ `&&` * a }(a: varargs[string]): expr = &&a
let space = " "
echo "my" && (space & "awe" && "some " ) && "concat"

View file

@ -4,8 +4,8 @@ discard """
"""
template optZero{x+x}(x: int): int = x*3
template andthen{x*3}(x: int): int = x*4
template optSubstr1{x = substr(x, a, b)}(x: string, a, b: int) = setlen(x, b+1)
template andthen{`*`(x,3)}(x: int): int = x*4
template optSubstr1{x = substr(x, 0, b)}(x: string, b: int) = setlen(x, b+1)
template disallowIf{
if cond: action

View file

@ -1,7 +1,7 @@
discard """
file: "ttypenoval.nim"
line: 38
errormsg: "type mismatch: got (typedesc{int}) but expected 'int'"
errormsg: "type mismatch: got (typedesc[int]) but expected 'int'"
"""
# A min-heap.

View file

@ -4,7 +4,7 @@ hel'''
"""
template optZero{x+x}(x: int): int = x*3
template andthen{x*3}(x: int): int = x*4
template andthen{`*`(x,3)}(x: int): int = x*4
template optSubstr1{x = substr(x, a, b)}(x: string, a, b: int) = setlen(x, b+1)
var y = 12