From 022440b53fc367b046b065975f75bea52e1bed5c Mon Sep 17 00:00:00 2001 From: L3viathan Date: Wed, 10 May 2017 19:14:15 +0200 Subject: [PATCH] New commands: - `Y` shuffles the current stack. - `y` pushes the sign of a popped element (1, 0, or -1) - `g` sorts the top two elements (ascending) - `G` sorts the top two elements (descending) - `a` does bitwise NOT on the top element. This fills up all printable ascii. --- README.md | 13 ++++++++++++- aceto.py | 39 +++++++++++++++++++++++++++++++++++---- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 660500c..634637a 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Aceto -This is Aceto v1.5.0 +This is Aceto v1.6.0 Aceto is a simple stack language that is based on a 2D Hilbert curve grid. The name is a reference to Aceto Balsamico (balsamic vinegar), and to @@ -93,6 +93,8 @@ depends on the previous command. insert it at the bottom of the stack. - ![#1589F0](https://placehold.it/15/1589F0/000000?text=+) `Q`: Remove an item from the bottom of the stack and push it. +- ![#1589F0](https://placehold.it/15/1589F0/000000?text=+) `Y`: Shuffle the + current stack. - ![#1589F0](https://placehold.it/15/1589F0/000000?text=+) `X`: Exit the interpreter abruptly. - ![#f03c15](https://placehold.it/15/f03c15/000000?text=+) `x`: Pop a value and @@ -158,6 +160,9 @@ depends on the previous command. negation of a popped value. - ![#1589F0](https://placehold.it/15/1589F0/000000?text=+) `~`: Invert the popped element and push it. Will also negate booleans and reverse strings. +- ![#1589F0](https://placehold.it/15/1589F0/000000?text=+) `y`: Push the sign of + a popped element (1 for positive numbers, -1 for negative numbers, 0 + otherwise). - ![#f03c15](https://placehold.it/15/f03c15/000000?text=+) `A`: Take two elements a and b from the stack and put the result of `a&b` (bitwise AND) on the stack. @@ -167,6 +172,8 @@ depends on the previous command. - ![#f03c15](https://placehold.it/15/f03c15/000000?text=+) `H`: Take two elements a and b from the stack and put the result of `a^b` (bitwise XOR) on the stack. +- ![#1589F0](https://placehold.it/15/1589F0/000000?text=+) `a`: Push the result + of bitwise NOT of the popped element on the stack. - ![#f03c15](https://placehold.it/15/f03c15/000000?text=+) `J`: Join the top two elements as a string. @@ -237,3 +244,7 @@ depends on the previous command. push an increasing range on the stack: A popped `5` will push `1`, `2`, `3`, `4`, `5`. Also works with negative numbers, in which case it will count down from `-1`. +- ![#1589F0](https://placehold.it/15/1589F0/000000?text=+) `g`: Sort the top two + elements of the stack (ascending) +- ![#1589F0](https://placehold.it/15/1589F0/000000?text=+) `G`: Sort the top two + elements of the stack (descending) diff --git a/aceto.py b/aceto.py index a0045a5..08b21e7 100644 --- a/aceto.py +++ b/aceto.py @@ -19,7 +19,7 @@ from math import ceil, log2 from numbers import Number from collections import defaultdict -from random import choice, random +from random import choice, random, shuffle from math import e, pi from docopt import docopt from hilbert_curve import hilbert @@ -487,6 +487,14 @@ def _invert(self, cmd) -> '~': raise CodeException(f"Don't know how to invert {x!r}") self.move() + 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}") + self.move() + def _restart(self, cmd) -> 'O': if self.dir==1: self.x, self.y = 0, 0 @@ -590,19 +598,19 @@ def _less_or_equal(self, cmd) -> 'w': self.push(y<=x) self.move() - def _and(self, cmd) -> 'A': + def _bitwise_and(self, cmd) -> 'A': x = self.pop() y = self.pop() self.push(y&x) self.move() - def _or(self, cmd) -> 'V': + def _bitwise_or(self, cmd) -> 'V': x = self.pop() y = self.pop() self.push(y|x) self.move() - def _xor(self, cmd) -> 'H': + def _bitwise_xor(self, cmd) -> 'H': x = self.pop() y = self.pop() self.push(y^x) @@ -626,6 +634,29 @@ def _range_up(self, cmd) -> 'Z': self.push(element) self.move() + def _order_up(self, cmd) -> 'G': + x = [self.pop(), self.pop()] + x.sort() + self.push(x.pop()) + self.push(x.pop()) + self.move() + + def _order_down(self, cmd) -> 'g': + x = [self.pop(), self.pop()] + x.sort(reverse=True) + self.push(x.pop()) + self.push(x.pop()) + self.move() + + def _shuffle(self, cmd) -> 'Y': + shuffle(self.stacks[self.sid]) + self.move() + + def _sign(self, cmd) -> 'y': + x = self.pop() + self.push(1 if x>0 else -1 if x<0 else 0) + self.move() + def getch(): fd = sys.stdin.fileno()