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

Refactor types #29

Merged
merged 6 commits into from
Apr 17, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
move FloatType to types.py
t81lal committed Apr 15, 2024
commit bcdf1ada2f8a83991f9cb7ce2380ce5041876dcd
7 changes: 1 addition & 6 deletions src/solidity_parser/ast/ast2builder.py
Original file line number Diff line number Diff line change
@@ -14,11 +14,6 @@
from solidity_parser import errors


# This is not a real type, hence why it's not defined in the AST2 nodes file, but the solidity
# compiler allows for
@dataclass
class FloatType(soltypes.Type):
value: float


T = typing.TypeVar('T')
@@ -269,7 +264,7 @@ def get_expr_type(self, expr: solnodes1.Expr | soltypes.Type, allow_multiple=Fal
# This can happen because compiletime floats are allowed in solidity for expressions
# that are evaluated at compiletime. E.g. instead of 0.01 ether which would give full_value
# an integer value, we could have 0.01 * 1 ether as a binary expr
return FloatType(full_value)
return soltypes.FloatType(full_value)
full_value = int(full_value)
else:
full_value = value
3 changes: 2 additions & 1 deletion src/solidity_parser/ast/solnodes2.py
Original file line number Diff line number Diff line change
@@ -103,6 +103,7 @@ def code_str(self):
def __str__(self):
return f'super({str(self.declarer.x.name)})'


@nodebase.NodeDataclass
class VisibilityModifier(Modifier):
kind: solnodes1.VisibilityModifierKind
@@ -1318,4 +1319,4 @@ class UnprocessedCode(Stmt):
| soltypes.AnyType | soltypes.MappingType | soltypes.StringType | soltypes.AddressType
| soltypes.FixedLengthArrayType | soltypes.ByteType | soltypes.MetaTypeType
| soltypes.TupleType | soltypes.PreciseIntType | soltypes.PreciseIntType
| soltypes.BuiltinType | ResolvedUserType | SuperType)
| soltypes.BuiltinType | ResolvedUserType | SuperType | soltypes.FloatType)
29 changes: 29 additions & 0 deletions src/solidity_parser/ast/types.py
Original file line number Diff line number Diff line change
@@ -75,6 +75,10 @@ def is_literal_type(self) -> bool:
"""
return False

def is_float(self) -> bool:
""" Check whether this type is a compile time float"""
return False

def is_void(self) -> bool:
""" Check if the type represents a void return type. This isn't part of Solidity directly but is represented
when a function doesn't define any return types
@@ -94,6 +98,31 @@ def code_str(self):
pass


@NodeDataclass
class FloatType(Type):
"""
This is not a real type in valid Solidity code but the Solidity compiler allows compile time expression evaluation
of floats
"""

value: float
""" Since the value is always known at compile time, we have it here """

def is_float(self) -> bool:
return True

def __str__(self):
return 'float'

def code_str(self):
return str(self)

def can_implicitly_cast_from(self, actual_type: 'Type') -> bool:
if super().can_implicitly_cast_from(actual_type):
return True
return actual_type.is_float()


@NodeDataclass
class VoidType(Type):
def is_void(self) -> bool: