This repository has been archived by the owner on Aug 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTokens.py
87 lines (66 loc) · 2.85 KB
/
Tokens.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import aLEXis as alexis
import math
class NumberToken(alexis.Token):
identifiers = "1234567890.,"
def __init__(self, lineNum, columnNum, truePosition, data):
super().__init__(lineNum, columnNum, truePosition, data)
class OperatorToken(alexis.Token):
identifiers = "+-/*^"
def __init__(self, lineNum, columnNum, truePosition, data):
super().__init__(lineNum, columnNum, truePosition, data)
def Solve(self):
selfPosition = self.parent.index(self)
if selfPosition == len(self.parent)-1 or selfPosition <= 0:
return False
else:
left = self.parent[selfPosition-1]
right = self.parent[selfPosition+1]
if self.data == '+':
Simplified = NumberToken(self.line, self.column, self.truePosition, float(left.data)+float(right.data))
elif self.data == "/":
Simplified = NumberToken(self.line, self.column, self.truePosition, float(left.data)/float(right.data))
elif self.data == "*":
Simplified = NumberToken(self.line, self.column, self.truePosition, float(left.data)*float(right.data))
elif self.data == "-":
Simplified = NumberToken(self.line, self.column, self.truePosition, float(left.data)-float(right.data))
elif self.data == "^":
Simplified = NumberToken(self.line, self.column, self.truePosition, float(left.data)**float(right.data))
Simplified.parent = self.parent
self.parent[selfPosition-1] = Simplified
del self.parent[selfPosition+1]
del self.parent[selfPosition]
if not isinstance(self.parent, list):
self.parent.recast()
class SeperatorToken(alexis.Token):
identifiers = ","
def __init__(self, lineNum, columnNum, truePosition, data):
super().__init__(lineNum, columnNum, truePosition, data)
class KeywordToken(alexis.Token):
def __init__(self, lineNum, columnNum, truePosition, data):
super().__init__(lineNum, columnNum, truePosition, data)
def Solve(self):
selfPosition = self.parent.index(self)
if selfPosition == len(self.parent)-1:
return False
else:
right = self.parent[selfPosition+1]
if hasattr(math, self.data.lower()):
Knu = getattr(math, self.data.lower())( *right.packageNumbers() )
Simplified = NumberToken(self.line, self.column, self.truePosition, Knu)
Simplified.parent = self.parent
self.parent[selfPosition] = Simplified
del self.parent[selfPosition+1]
if not isinstance(self.parent, list):
self.parent.recast()
@classmethod
def isValidCharacter(cls, char):
return char.lower() in "abcdefghijklmnpqrstuvwxyz" # This makes it so this *has* to be the last token in the registry
class ComparisonToken(alexis.Token):
identifiers = "=><"
def __init__(self, lineNum, columnNum, truePosition, data):
super().__init__(lineNum, columnNum, truePosition, data)
class GroupingToken(alexis.Token):
identifiers = "()"
OnlyOne = True
def __init__(self, lineNum, columnNum, truePosition, data):
super().__init__(lineNum, columnNum, truePosition, data)