Ported Lahiru's tokneizer code over. Appears to work fine. Also did some house keeping.

This commit is contained in:
2020-04-07 23:54:55 -05:00
parent d0e36ff2ce
commit c760dc04b5
4 changed files with 153 additions and 85 deletions
+58 -40
View File
@@ -1,46 +1,64 @@
class Token:
def __init__(self, value, type):
self.value = value
self.type = type
class CToken:
def __init__(self, value, type):
self.value = value
self.type = type
class TokenType:
add = 0
subtract = 1
multiply = 2
divide = 3
power = 4
variable = 5
constant = 6
exp_start = 7
exp_end = 8
unknown = 9
add = 0
subtract = 1
multiply = 2
divide = 3
power = 4
variable = 5
constant = 6
exp_start = 7
exp_end = 8
unknown = 9
OPERATOR_VERBS = {
0 : "Adding",
1 : "Subtracting",
2 : "Multiplying",
3 : "Dividing",
4 : "Raising"
}
OPERATOR_VERBS = {
0 : "Adding",
1 : "Subtracting",
2 : "Multiplying",
3 : "Dividing",
4 : "Raising"
}
TOKEN_NAMES = {
0 : "Addition",
1 : "Subtraction",
2 : "Multiplication",
3 : "Division",
4 : "Power",
5 : "Variable",
6 : "Constant",
7 : "Expression Start",
8 : "Expression End",
9 : "Unknown"
}
OPERATORS = {
'+' : add,
'-' : subtract,
'*' : multiply,
'^' : power,
'/' : divide
}
OPERATORS = {
'+' : add,
'-' : subtract,
'*' : multiply,
'^' : power,
'/' : divide
}
def get_operator_verb(self, token):
if not token in self.OPERATOR_VERBS:
raise ValueError("An operator verb could not be found.", "token")
return self.OPERATOR_VERBS[token]
@staticmethod
def get_operator_verb(token):
if not token in TokenType.OPERATOR_VERBS:
raise ValueError("An operator verb could not be found.", "token")
return TokenType.OPERATOR_VERBS[token]
def get_operator(self, character):
if not character in self.OPERATORS:
raise ValueError(f"Invalid operator '{character}' could not be found in the operators list.", "character")
return self.OPERATORS[character]
#t = TokenType()
#t1 = Token("$", TokenType.Unknown)
#print(t.GetOperator(t1.value))
@staticmethod
def get_operator(character):
"""
Accepts an individual character and returns either the math operator TokenType or unknown.
"""
if not character in TokenType.OPERATORS:
return TokenType.unknown
return TokenType.OPERATORS[character]
@staticmethod
def get_operator_name(tokenType):
return TokenType.TOKEN_NAMES[tokenType]