Skip to content

Commit

Permalink
Merge pull request #16 from aceto/regex
Browse files Browse the repository at this point in the history
Regex
  • Loading branch information
L3viathan committed May 22, 2017
2 parents f4faf19 + 63e9273 commit a3ba7e4
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 27 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,17 @@ depends on the previous command.
- ![#c5f015](https://placehold.it/15/c5f015/000000?text=+) ``: Explode a
string: Pop a string, and push all of its characters in reverse (such that the
top stack element will be the first character).
- ![#f0e788](https://placehold.it/15/c5f015/000000?text=+) `-`: Split a string
- ![#f0e788](https://placehold.it/15/f0e788/000000?text=+) `-`: Split a string
on whitespace.
- ![#f0e788](https://placehold.it/15/c5f015/000000?text=+) `:`: Split a string
- ![#f0e788](https://placehold.it/15/f0e788/000000?text=+) `:`: Split a string
on another string (`['foo,bar,bat', ',']``['bat', 'bar', 'foo']`).
- ![#f03c15](https://placehold.it/15/f03c15/000000?text=+) `/`: Push the number
of regex matches of the first popped value in the second.
- ![#f03c15](https://placehold.it/15/f03c15/000000?text=+) `%`: Push the third
popped value, but with all instances of the regex in the second popped value
replaced with the first popped value.
- ![#f0e788](https://placehold.it/15/f0e788/000000?text=+) `a`: Push all
matching strings of the first popped element in the second popped element.

### Casting
- ![#1589F0](https://placehold.it/15/1589F0/000000?text=+) `i`: Pop a value,
Expand Down
61 changes: 36 additions & 25 deletions aceto.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ def run(self):
def push(self, thing):
self.stacks[self.sid].append(thing)

def pushiter(self, iterable):
for element in iterable:
self.push(element)

def pop(self):
try:
x = self.stacks[self.sid][-1]
Expand Down Expand Up @@ -229,8 +233,7 @@ def _minus__split1(self, cmd) -> '-':
except TypeError:
raise CodeException(f"Can't subtract {x!r} from {y!r}")
else:
for element in reversed(x.split()):
self.push(element)
self.pushiter(reversed(x.split()))
self.move()

def _times(self, cmd) -> '*':
Expand All @@ -242,24 +245,31 @@ def _times(self, cmd) -> '*':
except TypeError:
raise CodeException(f"Can't multiply {x!r} with {y!r}")

def _mod(self, cmd) -> '%':
def _mod__re_replace(self, cmd) -> '%':
x = self.pop()
y = self.pop()
try:
self.push(y%x)
self.move()
except TypeError:
raise CodeException(f"Can't get modulo of {y!r} and {x!r}")
if isinstance(x, Number):
try:
self.push(y%x)
except TypeError:
raise CodeException(f"Can't get modulo of {y!r} and {x!r}")
else:
z = self.pop()
self.push(re.sub(y, z, x))
self.move()

def _div(self, cmd) -> '/':
def _div__re_matches(self, cmd) -> '/':
x = self.pop()
y = self.pop()
try:
self.push(y//x)
except ZeroDivisionError:
raise CodeException("Zero division")
except TypeError:
raise CodeException(f"Can't idivide {y!r} by {x!r}")
if isinstance(x, Number):
try:
self.push(y//x)
except ZeroDivisionError:
raise CodeException("Zero division")
except TypeError:
raise CodeException(f"Can't idivide {y!r} by {x!r}")
else:
self.push(len(re.findall(y, x)))
self.move()

def _floatdiv__split2(self, cmd) -> ':':
Expand All @@ -274,8 +284,7 @@ def _floatdiv__split2(self, cmd) -> ':':
raise CodeException(f"Can't fdivide {y!r} by {x!r}")
else:
y = self.pop()
for element in reversed(y.split(x)):
self.push(element)
self.pushiter(reversed(y.split(x)))
self.move()

def _equals(self, cmd) -> '=':
Expand Down Expand Up @@ -516,10 +525,14 @@ def _invert(self, cmd) -> '~':

def _bitwise_negate(self, cmd) -> 'a':
x = self.pop()
try:
self.push(~x)
except:
raise CodeException(f"Don't know how to invert {x!r}")
if isinstance(x, Number):
try:
self.push(~x)
except:
raise CodeException(f"Don't know how to invert {x!r}")
else:
y = self.pop()
self.pushiter(reversed(re.findall(y, x)))
self.move()

def _restart(self, cmd) -> 'O':
Expand Down Expand Up @@ -648,17 +661,15 @@ def _range_down(self, cmd) -> 'z':
if not isinstance(val, int) or val == 0:
raise CodeException("Can only construct range with nonzero integer")
step = -1 if x>0 else 1
for element in range(x, 0, step):
self.push(element)
self.pushiter(range(x, 0, step))
self.move()

def _range_up(self, cmd) -> 'Z':
val = self.pop()
if not isinstance(val, int) or val == 0:
raise CodeException("Can only construct range with nonzero integer")
step = 1 if x>0 else -1
for element in range(sign, x+sign, sign):
self.push(element)
self.pushiter(range(sign, x+sign, sign)
self.move()

def _order_up(self, cmd) -> 'G':
Expand Down

0 comments on commit a3ba7e4

Please sign in to comment.