Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fp #34

Merged
merged 11 commits into from
May 21, 2019
Merged

Fp #34

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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