@@ -114,10 +114,11 @@ elements = {
":": (15, ("rangepre", 15), ("range", 15), ("rangepost", 15)),
"not": (10, ("not", 10)),
"!": (10, ("not", 10)),
"and": (5, None, ("and", 5)),
"&": (5, None, ("and", 5)),
+ "%": (5, None, ("only", 5), ("onlypost", 5)),
"or": (4, None, ("or", 4)),
"|": (4, None, ("or", 4)),
"+": (4, None, ("or", 4)),
",": (2, None, ("list", 2)),
")": (0, None, None),
@@ -150,11 +151,11 @@ def tokenize(program, lookup=None):
yield ('..', None, pos)
pos += 1 # skip ahead
elif c == '#' and program[pos:pos + 2] == '##': # look ahead carefully
yield ('##', None, pos)
pos += 1 # skip ahead
- elif c in "():,-|&+!~^": # handle simple operators
+ elif c in "():,-|&+!~^%": # handle simple operators
yield (c, None, pos)
elif (c in '"\'' or c == 'r' and
program[pos:pos + 2] in ("r'", 'r"')): # handle quoted strings
if c == 'r':
pos += 1
@@ -1920,10 +1921,12 @@ methods = {
"list": listset,
"func": func,
"ancestor": ancestorspec,
"parent": parentspec,
"parentpost": p1,
+ "only": only,
+ "onlypost": only,
}
def optimize(x, small):
if x is None:
return 0, x
@@ -1933,10 +1936,12 @@ def optimize(x, small):
smallbonus = .5
op = x[0]
if op == 'minus':
return optimize(('and', x[1], ('not', x[2])), small)
+ elif op == 'only':
+ return optimize(('func', ('symbol', 'only'), ('list', x[1], x[2])), small)
elif op == 'dagrangepre':
return optimize(('func', ('symbol', 'ancestors'), x[1]), small)
elif op == 'dagrangepost':
return optimize(('func', ('symbol', 'descendants'), x[1]), small)
elif op == 'rangepre':
@@ -436,10 +436,36 @@ Test empty set input
2
4
8
9
+Test '%' operator
+
+ $ log '9%'
+ 8
+ 9
+ $ log '9%5'
+ 2
+ 4
+ 8
+ 9
+ $ log '(7 + 9)%(5 + 2)'
+ 4
+ 6
+ 7
+ 8
+ 9
+
+Test the order of operations
+
+ $ log '7 + 9%5 + 2'
+ 7
+ 2
+ 4
+ 8
+ 9
+
Test explicit numeric revision
$ log 'rev(-1)'
$ log 'rev(0)'
0
$ log 'rev(9)'