Skip to content

Commit

Permalink
Merge pull request #34 from leonardt/fp
Browse files Browse the repository at this point in the history
Fp
  • Loading branch information
leonardt authored May 21, 2019
2 parents ababa30 + 7bdec24 commit 3197920
Show file tree
Hide file tree
Showing 9 changed files with 907 additions and 14 deletions.
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
language: python
addons:
apt:
packages:
- libgmp-dev
- libmpfr-dev
- libmpc-dev
python:
- '3.6'
install:
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
[![Build Status](https://travis-ci.org/leonardt/hwtypes.svg?branch=master)](https://travis-ci.org/leonardt/hwtypes)


# Install
## Debian
```
apt install libgmp-dev libmpfr-dev libmpc-dev
pip install hwtypes
```
## OSX
```
brew install gmp mpfr libmpc
pip install hwtypes
```
2 changes: 2 additions & 0 deletions hwtypes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
from .adt import *
from .smt_bit_vector import *
from .z3_bit_vector import *
from .fp_vector_abc import *
from .fp_vector import *
28 changes: 16 additions & 12 deletions hwtypes/bit_vector_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
#BitVector(val, None) from BitVector(val)
_MISSING = object()
class AbstractBitVectorMeta(ABCMeta):
# BitVectorType, size : BitVectorType[size]
_class_cache = weakref.WeakValueDictionary()

# BitVectorType : UnsizedBitVectorType, size
_class_info = weakref.WeakKeyDictionary()

def __call__(cls, value=_MISSING, size=_MISSING, *args, **kwargs):
Expand Down Expand Up @@ -44,8 +47,7 @@ def __call__(cls, value=_MISSING, size=_MISSING, *args, **kwargs):
else:
warnings.warn('DEPRECATION WARNING: Use of BitVectorT(value, size) is deprecated')

return AbstractBitVectorMeta.__call__(cls[size], value)

return type(cls).__call__(cls[size], value)


def __new__(mcs, name, bases, namespace, **kwargs):
Expand All @@ -59,15 +61,17 @@ def __new__(mcs, name, bases, namespace, **kwargs):

t = super().__new__(mcs, name, bases, namespace, **kwargs)
if size is None:
AbstractBitVectorMeta._class_info[t] = t, size
mcs._class_info[t] = t, size
else:
AbstractBitVectorMeta._class_info[t] = None, size
mcs._class_info[t] = None, size

return t


def __getitem__(cls, idx : int) -> 'AbstractBitVectorMeta':
mcs = type(cls)
try:
return AbstractBitVector._class_cache[cls, idx]
return mcs._class_cache[cls, idx]
except KeyError:
pass

Expand All @@ -80,30 +84,30 @@ def __getitem__(cls, idx : int) -> 'AbstractBitVectorMeta':
raise TypeError('{} is already sized'.format(cls))

bases = [cls]
bases.extend(b[idx] for b in cls.__bases__ if isinstance(b, AbstractBitVectorMeta))
bases.extend(b[idx] for b in cls.__bases__ if isinstance(b, mcs))
bases = tuple(bases)
class_name = '{}[{}]'.format(cls.__name__, idx)
t = type(cls)(class_name, bases, {})
t = mcs(class_name, bases, {})
t.__module__ = cls.__module__
AbstractBitVectorMeta._class_cache[cls, idx] = t
AbstractBitVectorMeta._class_info[t] = cls, idx
mcs._class_cache[cls, idx] = t
mcs._class_info[t] = cls, idx
return t

@property
def unsized_t(cls) -> 'AbstractBitVectorMeta':
t = AbstractBitVectorMeta._class_info[cls][0]
t = type(cls)._class_info[cls][0]
if t is not None:
return t
else:
raise AttributeError('type {} has no unsized_t'.format(cls))

@property
def size(cls) -> int:
return AbstractBitVectorMeta._class_info[cls][1]
return type(cls)._class_info[cls][1]

@property
def is_sized(cls) -> bool:
return AbstractBitVectorMeta._class_info[cls][1] is not None
return type(cls)._class_info[cls][1] is not None

class AbstractBit(metaclass=ABCMeta):
@staticmethod
Expand Down
Loading

0 comments on commit 3197920

Please sign in to comment.