mirror of
https://gitlab.com/niansa/pilang.git
synced 2025-03-06 20:48:26 +01:00
81 lines
2.1 KiB
Python
81 lines
2.1 KiB
Python
import errors
|
|
import rtypes
|
|
|
|
|
|
class math:
|
|
def __init__(self, environ):
|
|
self.commands = {
|
|
"add": self.cmd_add,
|
|
"sub": self.cmd_sub,
|
|
"mul": self.cmd_mul,
|
|
"div": self.cmd_div
|
|
}
|
|
|
|
def processor(self, command, args):
|
|
try:
|
|
return self.commands[command](args)
|
|
except KeyError:
|
|
return errors.nochsuchcmd
|
|
|
|
def cmd_add(self, args):
|
|
try:
|
|
if args[0] == "asint":
|
|
asint = True
|
|
args = args[1:]
|
|
else:
|
|
asint = False
|
|
result = 0
|
|
for arg in args:
|
|
result += float(arg)
|
|
if asint: result = int(result)
|
|
return result
|
|
except ValueError:
|
|
return errors.badarguments
|
|
|
|
def cmd_sub(self, args):
|
|
try:
|
|
if args[0] == "asint":
|
|
asint = True
|
|
args = args[1:]
|
|
else:
|
|
asint = False
|
|
result = float(args[0])
|
|
for arg in args[1:]:
|
|
result -= float(arg)
|
|
if asint: result = int(result)
|
|
return result
|
|
except ValueError:
|
|
return errors.badarguments
|
|
|
|
def cmd_mul(self, args):
|
|
try:
|
|
if args[0] == "asint":
|
|
asint = True
|
|
args = args[1:]
|
|
else:
|
|
asint = False
|
|
result = 1
|
|
for arg in args:
|
|
result *= float(arg)
|
|
if asint: result = int(result)
|
|
return result
|
|
except ValueError:
|
|
return errors.badarguments
|
|
|
|
def cmd_div(self, args):
|
|
try:
|
|
if args[0] == "asint":
|
|
asint = True
|
|
args = args[1:]
|
|
else:
|
|
asint = False
|
|
result = float(args[0])
|
|
for arg in args[1:]:
|
|
result /= float(arg)
|
|
if asint: result = int(result)
|
|
return result
|
|
except ValueError:
|
|
return errors.badarguments
|
|
|
|
def adder(registerer):
|
|
registerer(math)
|