Skip to content
This repository has been archived by the owner on Jun 7, 2020. It is now read-only.

Commit

Permalink
Add support for hexadecimal coded floats
Browse files Browse the repository at this point in the history
  • Loading branch information
thejoeejoee committed Nov 25, 2017
1 parent 2cf0644 commit 501b006
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
12 changes: 11 additions & 1 deletion ifj2017/interpreter/operand.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@
TYPE_RE = re.compile(r'^(?P<type>int|string|bool|float)$', re.IGNORECASE)
LABEL_RE = re.compile(r'^{}$'.format(_IDENTIFIER_RE_PART), re.IGNORECASE)

float_ = float


def float(value):
# sorry, Python builtin
try:
return float_(value)
except ValueError:
return float_.fromhex(value)


class TypeOperand(IntEnum):
VARIABLE = 1
Expand Down Expand Up @@ -97,4 +107,4 @@ def _resolve_type(self, type_match):
def __str__(self):
return 'Operand({})'.format(
self.value or self.name or self.label
)
)
4 changes: 2 additions & 2 deletions ifj2017/interpreter/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,12 @@ def read(self, to, type_):
self.set_value(to, int(''.join(loaded)))
except ValueError:
self.set_value(to, 0)
elif type_.data_type == Operand.CONSTANT_MAPPING_REVERSE.get(float):
elif type_.data_type == Operand.CONSTANT_MAPPING_REVERSE.get(Operand.CONSTANT_MAPPING.get('float')):
float_re = re.compile(r'^(\d+\.\d+)|(\d+[Ee][+-]?\d+)')
match = float_re.match(input_)
assert match
try:
self.set_value(to, float(match.group(0)))
self.set_value(to, Operand.CONSTANT_MAPPING.get('float')(match.group(0)))
except ValueError:
self.set_value(to, .0)
elif type_.data_type == Operand.CONSTANT_MAPPING_REVERSE.get(bool):
Expand Down

0 comments on commit 501b006

Please sign in to comment.