-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.py
180 lines (122 loc) · 3.77 KB
/
decode.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
from __future__ import annotations
from enum import Enum, auto
from dataclasses import dataclass
import ascii_helpers
class TranslateException(Exception):
pass
class WrongIndicatorException(TranslateException):
pass
class UnhandledIndicatorException(TranslateException):
pass
class Indicator(Enum):
WRONG_INDICATOR = auto()
BOOLEAN_TRUE = "T"
BOOLEAN_FALSE = "F"
INTEGER = "I"
STRING = "S"
UNARY = "U"
BINARY = "B"
IF = "?"
LAMBDA = "L"
VARIABLE = "v"
@dataclass
class Message:
indicator: Indicator
token_body: str
@classmethod
def from_string(cls, token: str) -> Message:
return Message(indicator=Indicator(token[0]), token_body=token[1:])
@dataclass
class Token:
pass
@dataclass
class ValueToken(Token):
value: str | int | bool
class UnaryOperator(Enum):
NEGATE = "-"
NOT = "!"
STRING_TO_INT = "#"
INT_TO_STRING = "$"
@dataclass
class UnaryOperatorToken(Token):
operator: UnaryOperator
class BinaryOperator(Enum):
ADD = "+"
SUBTRACT = "-"
MULTIPLY = "*"
DIVIDE = "/"
MODULO = "%"
EQUAL = "="
LESS_THAN = "<"
GREATER_THAN = ">"
AND = "&"
OR = "|"
STRING_CONCAT = "."
TAKE_FIRST = "T"
DROP_FIRST = "D"
APPLY = "$"
@dataclass
class BinaryOperatorToken(Token):
operator: BinaryOperator
@dataclass
class IfToken(Token):
pass
@dataclass
class LambdaToken(Token):
variable: int
@dataclass
class VariableToken(Token):
variable: int
def parse_integer(msg: Message) -> ValueToken:
if msg.indicator != Indicator.INTEGER:
raise WrongIndicatorException(f"Wrong indicator {msg}")
return ValueToken(value=ascii_helpers.decode_integer(msg.token_body))
def parse_string(msg: Message) -> ValueToken:
if msg.indicator != Indicator.STRING:
raise WrongIndicatorException(f"Wrong indicator {msg}")
return ValueToken(value=ascii_helpers.decode_string(msg.token_body))
def parse_unary(msg: Message) -> UnaryOperatorToken:
if msg.indicator != Indicator.UNARY:
raise WrongIndicatorException(f"Wrong indicator {msg}")
operator = UnaryOperator(msg.token_body[0])
return UnaryOperatorToken(operator=operator)
def parse_binary(msg: Message) -> BinaryOperatorToken:
if msg.indicator != Indicator.BINARY:
raise WrongIndicatorException(f"Wrong indicator {msg}")
operator = BinaryOperator(msg.token_body[0])
return BinaryOperatorToken(operator=operator)
def parse_lambda(msg: Message) -> LambdaToken:
if msg.indicator != Indicator.LAMBDA:
raise WrongIndicatorException(f"Wrong indicator {msg}")
return LambdaToken(variable=ascii_helpers.decode_integer(msg.token_body))
def parse_variable(msg: Message) -> VariableToken:
if msg.indicator != Indicator.VARIABLE:
raise WrongIndicatorException(f"Wrong indicator {msg}")
return VariableToken(variable=ascii_helpers.decode_integer(msg.token_body))
def parse_token(token: str) -> Token:
msg = Message.from_string(token)
match msg.indicator:
case Indicator.UNARY:
return parse_unary(msg)
case Indicator.BINARY:
return parse_binary(msg)
case Indicator.BOOLEAN_FALSE:
return ValueToken(value=False)
case Indicator.BOOLEAN_TRUE:
return ValueToken(value=True)
case Indicator.INTEGER:
return parse_integer(msg)
case Indicator.STRING:
return parse_string(msg)
case Indicator.IF:
return IfToken()
case Indicator.LAMBDA:
return parse_lambda(msg)
case Indicator.VARIABLE:
return parse_variable(msg)
case _:
raise UnhandledIndicatorException(f"{msg}")
def main():
pass
if __name__ == "__main__":
main()