Reformat evaluator #2 (exceptions)

This commit is contained in:
Bartłomiej Pluta
2019-07-04 18:09:20 +02:00
parent 34a0eda199
commit 23e0f3f33e
4 changed files with 23 additions and 12 deletions

View File

@@ -35,4 +35,5 @@ def parseNote(input, parent):
input.ahead()
return NoteLiteralNode(Note(notePitch, octave, duration, dot), parent, token.pos)
return None

9
smnp/error/note.py Normal file
View File

@@ -0,0 +1,9 @@
from smnp.error.base import SmnpException
class NoteException(SmnpException):
def __init__(self, msg):
super().__init__(msg)
def _title(self):
return "Note Error"

View File

@@ -1,6 +1,6 @@
from enum import Enum
from smnp.error.syntax import SyntaxException
from smnp.error.note import NoteException
class NotePitch(Enum):
@@ -39,16 +39,12 @@ class NotePitch(Enum):
def __repr__(self):
return self.__str__()
#@staticmethod
#def checkInterval(a, b):
#return a.value - b.value
@staticmethod
def toPitch(string): #TODO: token zamiast stringa, żeby można było wziąć pos
def toPitch(string):
try:
return stringToPitch[string.lower()]
except KeyError as e:
raise SyntaxException(f"Note '{string}' does not exist") #TODO jakis inny exception
raise NoteException(f"Note '{string}' does not exist")
stringToPitch = {
'c': NotePitch.C,

View File

@@ -1,5 +1,6 @@
from smnp.ast.node.identifier import IdentifierNode
from smnp.ast.node.program import Program
from smnp.error.base import SmnpException
from smnp.error.runtime import RuntimeException
from smnp.runtime.evaluators.list import evaluateList
from smnp.runtime.tools import flatListNode
@@ -27,9 +28,13 @@ def evaluateFunctionDefinition(definition, environment):
def evaluateFunctionCall(functionCall, environment):
try:
functionName = functionCall.identifier.identifier
arguments = evaluateList(functionCall.arguments, environment).value
return environment.invokeFunction(functionName, arguments)
except SmnpException as e:
e.pos = functionCall.pos
raise e
# def evaluateFunctionCall(functionCall, environment):