Skip to content

Commit

Permalink
added power, error, and divide by zero
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSecondComing123 committed Feb 16, 2022
1 parent c907d0d commit eea4911
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 18 deletions.
37 changes: 20 additions & 17 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion codepage.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
codepage = ["+", "-", "×", "÷", "g", "¶"]
codepage = ["+", "-", "×", "÷", "ⁱ", "g", "¶"]
1 change: 1 addition & 0 deletions elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"-": [2, Sub],
"×": [2, Mul],
"÷": [2, TrueDiv],
"ⁱ": [2, Power],
"g": [0, lambda *x: g], # g (and other constants) must be a lambda because the interpreter tries to call it
"¶": [1, Print]
}
21 changes: 21 additions & 0 deletions errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import sys


class NoxanErrorBase:
"""
Base class for all Errorss.
"""

def __init__(self, mes=None):
self.mes = mes or ""

def raiserr(self):
errmessage = f"""Noxan {type(self).__name__} {{
{self.mes}
}}"""

sys.stderr.write(errmessage)


class InvalidSyntaxError(NoxanErrorBase):
pass
8 changes: 8 additions & 0 deletions functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ def Mul(a1, a2, ctx=None):

def TrueDiv(a1, a2, ctx=None):
"""Divides two numbers"""
if a2 == 0:
return float(f"{'-' if a1 < 0 else ''}Infinity")

return a1 / a2


Expand All @@ -51,3 +54,8 @@ def Print(a1, ctx=None):
ctx.printed = True

return a1


def Power(a1, a2, ctx=None):
"""Calculates a1 to the power of a2"""
return a1 ** a2

0 comments on commit eea4911

Please sign in to comment.