Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
SkymanOne committed Mar 2, 2024
1 parent bbcae69 commit 9a0aa64
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 44 deletions.
37 changes: 7 additions & 30 deletions crates/semantics/src/expression/literals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use crate::{
types::{report_type_mismatch, ExpectedType},
};

use super::dynamic_to_concrete_type;

/// Resolve bool to an expression.
///
/// # Errors
Expand All @@ -32,12 +34,7 @@ pub fn resolve_bool(
}
},
ExpectedType::Dynamic(tys) => {
let mut allowed_tys = tys.iter().filter(|ty| matches!(ty, TypeVariant::Bool));
let expected = if let Some(ty) = allowed_tys.next() {
ExpectedType::Concrete(ty.clone())
} else {
ExpectedType::Concrete(TypeVariant::Bool)
};
let expected = dynamic_to_concrete_type(tys, &[TypeVariant::Bool]);
resolve_bool(value, loc, contract, expected)
}
ExpectedType::Empty => {
Expand Down Expand Up @@ -72,12 +69,7 @@ pub fn resolve_char(
}
},
ExpectedType::Dynamic(tys) => {
let mut allowed_tys = tys.iter().filter(|ty| matches!(ty, TypeVariant::Char));
let expected = if let Some(ty) = allowed_tys.next() {
ExpectedType::Concrete(ty.clone())
} else {
ExpectedType::Concrete(TypeVariant::Char)
};
let expected = dynamic_to_concrete_type(tys, &[TypeVariant::Char]);
resolve_char(value, loc, contract, expected)
}
ExpectedType::Empty => {
Expand Down Expand Up @@ -113,12 +105,7 @@ pub fn resolve_string(
}
},
ExpectedType::Dynamic(tys) => {
let mut allowed_tys = tys.iter().filter(|ty| matches!(ty, TypeVariant::String));
let expected = if let Some(ty) = allowed_tys.next() {
ExpectedType::Concrete(ty.clone())
} else {
ExpectedType::Concrete(TypeVariant::String)
};
let expected = dynamic_to_concrete_type(tys, &[TypeVariant::String]);
resolve_string(value, loc, contract, expected)
}
ExpectedType::Empty => {
Expand Down Expand Up @@ -162,12 +149,7 @@ pub fn resolve_hex(
}
},
ExpectedType::Dynamic(tys) => {
let mut allowed_tys = tys.iter().filter(|ty| matches!(ty, TypeVariant::Hex));
let expected = if let Some(ty) = allowed_tys.next() {
ExpectedType::Concrete(ty.clone())
} else {
ExpectedType::Concrete(TypeVariant::Hex)
};
let expected = dynamic_to_concrete_type(tys, &[TypeVariant::Hex]);
resolve_hex(value, loc, contract, expected)
}
ExpectedType::Empty => {
Expand Down Expand Up @@ -206,12 +188,7 @@ pub fn resolve_address(
}
},
ExpectedType::Dynamic(tys) => {
let mut allowed_tys = tys.iter().filter(|ty| matches!(ty, TypeVariant::Address));
let expected = if let Some(ty) = allowed_tys.next() {
ExpectedType::Concrete(ty.clone())
} else {
ExpectedType::Concrete(TypeVariant::Address)
};
let expected = dynamic_to_concrete_type(tys, &[TypeVariant::Address]);
resolve_address(value, loc, contract, expected)
}
ExpectedType::Empty => {
Expand Down
11 changes: 11 additions & 0 deletions crates/semantics/src/expression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,14 @@ pub fn expression(
parsed_ast::Expression::List(_) => todo!(),
}
}

/// Derives a concrete expected type from the list of supported types.
/// The first element is used as a fallback option in case `tys` is empty.
pub fn dynamic_to_concrete_type(tys: &[TypeVariant], allowed: &[TypeVariant]) -> ExpectedType {
let mut allowed_tys = tys.iter().filter(|ty| allowed.contains(ty));
if let Some(ty) = allowed_tys.next() {
ExpectedType::Concrete(ty.clone())
} else {
ExpectedType::Concrete(allowed[0].clone())
}
}
18 changes: 4 additions & 14 deletions crates/semantics/src/expression/nums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use crate::{
types::{report_type_mismatch, ExpectedType},
};

use super::dynamic_to_concrete_type;

/// Resolve signed and unsigned integers.
///
/// # Errors
Expand Down Expand Up @@ -52,14 +54,7 @@ pub fn resolve_integer(
// use first type to resolve in the list of possible types,
// otherwise we resolve to signed int
// The latter can happen when we have var declaration without the type annotation.
let mut allowed_tys = tys
.iter()
.filter(|ty| matches!(ty, TypeVariant::Int | TypeVariant::Uint));
let expected = if let Some(ty) = allowed_tys.next() {
ExpectedType::Concrete(ty.clone())
} else {
ExpectedType::Concrete(TypeVariant::Int)
};
let expected = dynamic_to_concrete_type(tys, &[TypeVariant::Int, TypeVariant::Uint]);
resolve_integer(number_str, loc, contract, expected)
}
ExpectedType::Empty => {
Expand Down Expand Up @@ -108,12 +103,7 @@ pub fn resolve_float(
}
},
ExpectedType::Dynamic(tys) => {
let mut allowed_tys = tys.iter().filter(|ty| matches!(ty, TypeVariant::Float));
let expected = if let Some(ty) = allowed_tys.next() {
ExpectedType::Concrete(ty.clone())
} else {
ExpectedType::Concrete(TypeVariant::Float)
};
let expected = dynamic_to_concrete_type(tys, &[TypeVariant::Float]);
resolve_float(number_str, loc, contract, expected)
}
ExpectedType::Empty => {
Expand Down

0 comments on commit 9a0aa64

Please sign in to comment.