Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The program takes inputs that are separated by space (or return in interactive m

### Stack pushes

Funcions that pushes something into the stack.
Functions that pushes something into the stack.

|Operation|Syntax|Description|
|---|---|---|
Expand All @@ -25,12 +25,13 @@ Funcions that pushes something into the stack.
|π|`pi`||
|Euler's number|`e`||

### System funcions
### System functions

|Operation|Syntax|Description|
|---|---|---|
|Quit|`q`, `quit`, `exit`|Quits the program|
|Print|`print`, `ls`|Prints the stack|
|Stack show|`ss`, `ts`|Toggles the option to print the full stack at the interactive prompt|

### Stack manipulation and memory

Expand All @@ -40,7 +41,8 @@ Funcions that pushes something into the stack.
|Roll down|`rld`, `roll`, `rolld`, `rolldown`|Rolls the stack down|
|Roll up|`rlu`, `rollu`, `rollup`|Rolls the stack up|
|Store|`sto`, `store`|Takes one argument that sets the storage location|
|Recall|`rcl`, `recall`|Pushes the numbes stored in a argument position|
|Recall|`rcl`, `recall`|Pushes the numbers stored in an argument position|
|Last X|`lastx`, `lx`|Pushes the value of the *x* buffer before the last calculation|

### Stack drop

Expand All @@ -50,23 +52,24 @@ Functions that pops off the *x* and *y* buffers and pushes the product based on
|---|---|---|
|Add|`a`, `+`, `add`, `plus`||
|Subtract|`s`, `-`, `sub`, `subtact`, `minus`|
|Muliply|`m`, `*`, `mul`, `multiply`, `times`||
|Multiply|`m`, `*`, `mul`, `multiply`, `times`||
|Divide|`d`, `/`, `:`, `div`, `divide`, `over`||
|Power|`p`, `^`, `**`, `pow`, `raised`, `expo`, `exponent`||
|Modulus|`mod`, `modulus`||
|Greatest common divider|`gcd`||
|Logarithm|`log`, `logarithm`|The *x* buffer is the base|
|Root|`r`, `root`|The *x* buffer is the root|
|Percent change|`pct`|Percent change from *x* to *y*: (y-x)/x|

### *x* buffer change

Funcions that takes what is stored in the *x* buffer and changes it. Trigonometric funcions returns number in radians.
Functions that takes what is stored in the *x* buffer and changes it. Trigonometric functions returns number in radians.

|Operation|Syntax|Description|
|---|---|---|
|Ceil|`cel`, `ceil`||
|Floor|`flr`,`floor`||
|Round|`rnd`, `round`|Rounds to nearest ingeger|
|Round|`rnd`, `round`|Rounds to nearest integer|
|Invert|`inv`, `inverse`, `invert`|1 / x|
|Absolute value|`abs`, `absolute`||
|Factorial|`fac`, `factorial`||
Expand Down
36 changes: 30 additions & 6 deletions rpn.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
import sys
import random

show_full_stack=False
showed_stack=False

class Stack():
def __init__(self):
self.list = []
self.lastx = float(0)
def push(self, item):
if len(self.list) < 1 and item == 0:
return
Expand All @@ -19,6 +23,7 @@ def pop(self):
return retval
def drop(self, func):
x = self.pop()
self.lastx = x
y = self.pop()
try:
self.push(func(x, y))
Expand All @@ -27,6 +32,7 @@ def drop(self, func):
self.push(x)
self.push(y)
def change(self, func):
self.lastx = self.list[0]
self.push(func(self.pop()))
def __getitem__(self, key):
if type(key) == slice: raise IndexError('No stack slicing')
Expand Down Expand Up @@ -81,6 +87,7 @@ def setdefault(self):
self.angle = 'deg'

def exe(self, raw):
global showed_stack, show_full_stack
ln = iter(raw.split(' '))
for cmd in ln:
if re.search('^-?(\d+\.?\d*|\.\d+)$', cmd):
Expand All @@ -105,6 +112,8 @@ def exe(self, raw):
self.stack.drop(lambda x, y: math.log(y, x))
elif cmd in ['r', 'root']:
self.stack.drop(lambda x, y: math.pow(y, 1/x))
elif cmd in ['pct']:
self.stack.drop(lambda x, y: (y-x)/x)

# change
elif cmd in ['cel', 'ceil']:
Expand Down Expand Up @@ -160,22 +169,31 @@ def exe(self, raw):
elif cmd in ['print', 'ls']:
for item in self.stack.gettable():
print(item[0] + ': ' + InteractiveMode().prettify(item[1]))
showed_stack=1
elif cmd in ['ss', 'ts']:
show_full_stack = not show_full_stack

# functions
elif cmd == ['swp', '<>', '><', 'swap']:
elif cmd in ['swp', '<>', '><', 'swap']:
self.stack['x'], self.stack['y'] = self.stack['y'], self.stack['x']
elif cmd in ['rld', 'roll', 'rolld', 'rolldown']:
self.stack.roll()
elif cmd in ['rlu', 'rollu', 'rollup']:
self.stack.roll(1)
elif cmd == ['sto', 'store']:
elif cmd in ['sto', 'store']:
self.regtable[next(ln)] = self.stack['x']
elif cmd == ['rcl', 'recall']:
elif cmd in ['rcl', 'recall']:
self.stack.push(self.regtable[next(ln)])
elif cmd in ['clr', 'clear', 'clean', 'erase']:
self.stack = Stack()

elif cmd == '':
elif cmd in ['dup']:
x=self.stack.pop()
self.stack.push(x)
self.stack.push(x)
elif cmd in ['lastx', 'lx']:
last=self.stack.lastx
self.stack.push(last)
elif cmd in '':
pass
else:

Expand All @@ -188,8 +206,14 @@ def __init__(self):
self.rpn = RPN()

def run(self):
global showed_stack, show_full_stack
while True:
print('\x1b[2m x: \x1b[0m' + self.prettify(self.rpn.stack['x']))
if not showed_stack:
if show_full_stack:
self.rpn.exe('ls')
else:
print('\x1b[2m x: \x1b[0m' + self.prettify(self.rpn.stack['x']))
showed_stack=False
try:
ret = self.rpn.exe(input(' > '))
except Exception as e:
Expand Down