diff --git a/bbscript/stdlib/math.py b/bbscript/stdlib/math.py index 0d74d8b..67184f7 100644 --- a/bbscript/stdlib/math.py +++ b/bbscript/stdlib/math.py @@ -2,16 +2,30 @@ from ..meta import bbmeta from ..errors import InvalidOperation from .utils import flt, cint +from jsonschema import validate -@bbmeta( - description="Calculates a mathematical operation given the operator and two values.", - inputs=[ - dict(name="op", type="str", options=["+", "-", "*", "/", "%", "MOD", "WRAP"]), - dict(name="left", type=dict(union=["float", "int"], match="right")), - dict(name="right", type=dict(union=["float", "int"], match="left")) - ], - outputs=dict(name="result", type=dict(union=["float", "int"], match=["left", "right"])) -) + +schema = { + "$schema": "https://json-schema.org/draft/2020-12/schema", + "$id": "https://example.com/product.schema.json", + "title": "Math", + "type": "object", + "properties": { + "op": { + "description": "Math Operation to perform", + "type": "string", + }, + "left": { + "description": "left value of the operation", + "type": "number" + }, + "right": { + "description": "left value of the operation", + "type": "number" + } + }, + "required": [ "op", "left", "right" ] +} def cmd_math(ctx, op: str, left, right): """ To perform math operations. @@ -27,28 +41,38 @@ def cmd_math(ctx, op: str, left, right): value of the left and right parameter with respect to the op. """ - type_fn = flt - if isinstance(left, int): - type_fn = cint - if isinstance(right, int): - type_fn = cint - - left = type_fn(left) - right = type_fn(right) - - if op == "+": - return left + right - elif op == "-": - return left - right - elif op == "*": - return left * right - elif op == "/": - return left / right - elif op == "%": - return left * (right / 100) - elif op == "MOD": - return math.fmod(left, right) - elif op == "WRAP": - return left % right - - raise InvalidOperation("op must be one of +, -, *, /, %, MOD") \ No newline at end of file + try: + math_json = { + "op": op, + "left": left, + "right": right, + } + validate(instance=math_json, schema=schema) + type_fn = flt + if isinstance(left, int): + type_fn = cint + left = type_fn(left) + if isinstance(right, int): + type_fn = cint + right = type_fn(right) + + + if op == "+": + return left + right + elif op == "-": + return left - right + elif op == "*": + return left * right + elif op == "/": + return left / right + elif op == "%": + return left * (right / 100) + elif op == "MOD": + return math.fmod(left, right) + elif op == "WRAP": + return left % right + + raise InvalidOperation("op must be one of +, -, *, /, %, MOD") + + except Exception as err: + raise ValueError(err) diff --git a/requirements.txt b/requirements.txt index f21bb73..077963e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,29 @@ +alabaster==0.7.12 +attrs==21.2.0 +Babel==2.9.1 +certifi==2021.5.30 +chardet==4.0.0 click==8.0.1 +docutils==0.17.1 +idna==2.10 +imagesize==1.2.0 importlib-metadata==4.3.1 +Jinja2==3.0.1 +json-cfg==0.4.2 +jsonschema==3.2.0 +kwonly-args==1.0.10 +MarkupSafe==2.0.1 +Pygments==2.9.0 +pyrsistent==0.17.3 +pytz==2021.1 +requests==2.25.1 +six==1.16.0 +snowballstemmer==2.1.0 +Sphinx==1.6.6 +sphinxcontrib-serializinghtml==1.1.5 +sphinxcontrib-websupport==1.2.4 termcolor==1.1.0 typing-extensions==3.10.0.0 ujson==4.0.2 -zipp==3.4.1 +urllib3==1.26.5 +zipp==3.4.1 \ No newline at end of file