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

Allow hex/octal/binary radix numeric literals #410

Merged
merged 4 commits into from
May 20, 2021
Merged
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
17 changes: 3 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions analyzer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ fe-parser = {path = "../parser", version = "^0.4.0-alpha"}
hex = "0.4"
ansi_term = "0.12.1"
num-bigint = "0.3.1"
num-traits = "0.2.14"
strum = { version = "0.20.0", features = ["derive"] }
lexical-core = "0.7.6"
vec1 = "1.8.0"

[dev-dependencies]
insta = "1.7.1"
rstest = "0.6.4"
rstest = "0.6.4"
56 changes: 18 additions & 38 deletions analyzer/src/namespace/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ use std::convert::TryFrom;

use crate::context::FunctionAttributes;
use num_bigint::BigInt;
use num_traits::ToPrimitive;
use strum::IntoStaticStr;
use vec1::Vec1;

pub fn u256_min() -> BigInt {
BigInt::from(0)
}

pub fn u256_max() -> BigInt {
BigInt::from(2).pow(256) - 1
}
Expand Down Expand Up @@ -281,45 +286,20 @@ impl Integer {
}

/// Returns `true` if `num` represents a number that fits the type
pub fn fits(&self, num: &str) -> bool {
use lexical_core::{parse, ErrorCode, FromLexical};
fn check_fit<T: FromLexical>(num: &str) -> bool {
if let Err(err) = parse::<T>(num.as_bytes()) {
match err.code {
ErrorCode::Overflow => false,
ErrorCode::Underflow => false,
// If we try to parse a negative value for an unsigned type
ErrorCode::InvalidDigit => false,
// We don't expect this but it would be tragic if we would map this to `false`
// incase it happens because it would mean we sweep a bug under the rug.
other => panic!("Unexpected ParseIntError: {:?}", other),
}
} else {
true
}
}

// We reject octal number literals.
if num.len() > 1 && num.starts_with('0') {
return false;
}

let radix = 10;
pub fn fits(&self, num: BigInt) -> bool {
match self {
Integer::U8 => check_fit::<u8>(num),
Integer::U16 => check_fit::<u16>(num),
Integer::U32 => check_fit::<u32>(num),
Integer::U64 => check_fit::<u64>(num),
Integer::U128 => check_fit::<u128>(num),
Integer::U256 => BigInt::parse_bytes(num.as_bytes(), radix)
.map_or(false, |val| val >= BigInt::from(0) && val <= u256_max()),
Integer::I8 => check_fit::<i8>(num),
Integer::I16 => check_fit::<i16>(num),
Integer::I32 => check_fit::<i32>(num),
Integer::I64 => check_fit::<i64>(num),
Integer::I128 => check_fit::<i128>(num),
Integer::I256 => BigInt::parse_bytes(num.as_bytes(), radix)
.map_or(false, |val| val >= i256_min() && val <= i256_max()),
Integer::U8 => num.to_u8().is_some(),
Integer::U16 => num.to_u16().is_some(),
Integer::U32 => num.to_u32().is_some(),
Integer::U64 => num.to_u64().is_some(),
Integer::U128 => num.to_u128().is_some(),
Integer::I8 => num.to_i8().is_some(),
Integer::I16 => num.to_i16().is_some(),
Integer::I32 => num.to_i32().is_some(),
Integer::I64 => num.to_i64().is_some(),
Integer::I128 => num.to_i128().is_some(),
Integer::U256 => num >= u256_min() && num <= u256_max(),
Integer::I256 => num >= i256_min() && num <= i256_max(),
}
}
}
Expand Down
25 changes: 20 additions & 5 deletions analyzer/src/traversal/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ use builtins::{
BlockField, ChainField, ContractTypeMethod, GlobalMethod, MsgField, Object, TxField,
ValueMethod,
};
use fe_common::numeric;
use fe_parser::ast as fe;
use fe_parser::node::Node;
use num_bigint::BigInt;
use std::convert::TryFrom;
use std::rc::Rc;
use std::str::FromStr;
Expand Down Expand Up @@ -237,6 +239,7 @@ fn expr_bool(exp: &Node<fe::Expr>) -> Result<ExpressionAttributes, SemanticError

fn expr_num(exp: &Node<fe::Expr>) -> Result<ExpressionAttributes, SemanticError> {
if let fe::Expr::Num(num) = &exp.kind {
let num = to_bigint(num);
validate_numeric_literal_fits_type(num, &Type::Base(U256))?;
return Ok(ExpressionAttributes::new(Type::Base(U256), Location::Value));
}
Expand Down Expand Up @@ -577,7 +580,7 @@ fn expr_call_type_constructor(
}
Type::Base(Base::Numeric(_)) => {
let num = validate_is_numeric_literal(&args.kind[0].kind)?;
validate_numeric_literal_fits_type(&num, &typ)?;
validate_numeric_literal_fits_type(num, &typ)?;
Ok(ExpressionAttributes::new(typ, Location::Value))
}
Type::Base(Base::Address) => {
Expand Down Expand Up @@ -941,21 +944,23 @@ fn validate_are_kw_args(args: &[Node<fe::CallArg>]) -> Result<(), SemanticError>
Ok(())
}

fn validate_is_numeric_literal(call_arg: &fe::CallArg) -> Result<String, SemanticError> {
fn validate_is_numeric_literal(call_arg: &fe::CallArg) -> Result<BigInt, SemanticError> {
let value = call_arg_value(call_arg);

if let fe::Expr::UnaryOperation { operand, op: _ } = &value.kind {
if let fe::Expr::Num(num) = &operand.kind {
return Ok(format!("-{}", num));
let num = to_bigint(num);
return Ok(-num);
}
} else if let fe::Expr::Num(num) = &value.kind {
return Ok(num.to_string());
let num = to_bigint(num);
return Ok(num);
}

Err(SemanticError::numeric_literal_expected())
}

fn validate_numeric_literal_fits_type(num: &str, typ: &Type) -> Result<(), SemanticError> {
fn validate_numeric_literal_fits_type(num: BigInt, typ: &Type) -> Result<(), SemanticError> {
if let Type::Base(Base::Numeric(integer)) = typ {
return if integer.fits(num) {
Ok(())
Expand Down Expand Up @@ -1062,3 +1067,13 @@ fn expr_bool_operation(

unreachable!()
}

/// Converts a input string to `BigInt`.
///
/// # Panics
/// Panics if `num` contains invalid digit.
fn to_bigint(num: &str) -> BigInt {
numeric::Literal::new(num)
.parse::<BigInt>()
.expect("the numeric literal contains a invalid digit")
}
1 change: 1 addition & 0 deletions common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ codespan-reporting = "0.11.1"
serde = { version = "1", features = ["derive"] }
ron = "0.5.1"
difference = "2.0"
num-traits = "0.2.14"
1 change: 1 addition & 0 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod diagnostics;
pub mod files;
pub mod numeric;
mod span;
pub mod utils;
pub use span::Span;
83 changes: 83 additions & 0 deletions common/src/numeric.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/// A type that represents the radix of a numeric literal.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Radix {
Hexadecimal,
Decimal,
Octal,
Binary,
}

impl Radix {
/// Returns number representation of the radix.
pub fn as_num(self) -> u32 {
match self {
Self::Hexadecimal => 16,
Self::Decimal => 10,
Self::Octal => 8,
Self::Binary => 2,
}
}
}

/// A helper type to interpret a numeric literal represented by string.
#[derive(Debug, Clone)]
pub struct Literal<'a> {
/// The number part of the string.
num: &'a str,
/// The radix of the literal.
radix: Radix,
/// The radix part of the string.
prefix: Option<&'a str>,
}

impl<'a> Literal<'a> {
pub fn new(src: &'a str) -> Self {
debug_assert!(!src.is_empty());
debug_assert_ne!(src.chars().next(), Some('-'));
let (radix, prefix) = if src.len() < 2 {
(Radix::Decimal, None)
} else {
match &src[0..2] {
"0x" | "0X" => (Radix::Hexadecimal, Some(&src[..2])),
"0o" | "0O" => (Radix::Octal, Some(&src[..2])),
"0b" | "0B" => (Radix::Binary, Some(&src[..2])),
_ => (Radix::Decimal, None),
}
};

Self {
num: &src[prefix.map_or(0, |pref| pref.len())..],
radix,
prefix,
}
}

/// Parse the numeric literal to `T`.
pub fn parse<T: num_traits::Num>(&self) -> Result<T, T::FromStrRadixErr> {
T::from_str_radix(&self.num, self.radix.as_num())
}

/// Returns radix of the numeric literal.
pub fn radix(&self) -> Radix {
self.radix
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_radix() {
assert_eq!(Literal::new("0XFF").radix(), Radix::Hexadecimal);
assert_eq!(Literal::new("0xFF").radix(), Radix::Hexadecimal);
assert_eq!(Literal::new("0O77").radix(), Radix::Octal);
assert_eq!(Literal::new("0o77").radix(), Radix::Octal);
assert_eq!(Literal::new("0B77").radix(), Radix::Binary);
assert_eq!(Literal::new("0b77").radix(), Radix::Binary);
assert_eq!(Literal::new("1").radix(), Radix::Decimal);

// Invalid radix is treated as `Decimal`.
assert_eq!(Literal::new("0D15").radix(), Radix::Decimal);
}
}
1 change: 1 addition & 0 deletions compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ solc = { git = "https://github.com/g-r-a-n-t/solc-rust", optional = true }
maplit = "1.0.2"
once_cell = "1.5.2"
vec1 = "1.8.0"
num-bigint = "0.3.1"

[dev-dependencies]
evm-runtime = "0.18"
Expand Down
10 changes: 10 additions & 0 deletions compiler/src/yul/mappers/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ use fe_analyzer::builtins;
use fe_analyzer::builtins::{ContractTypeMethod, GlobalMethod};
use fe_analyzer::context::{CallType, Context, Location};
use fe_analyzer::namespace::types::{Base, FeSized, FixedSize, Type};
use fe_common::numeric;
use fe_common::utils::keccak;
use fe_parser::ast as fe;
use fe_parser::node::Node;
use num_bigint::BigInt;
use std::convert::TryFrom;
use std::str::FromStr;
use yultsur::*;
Expand Down Expand Up @@ -296,6 +298,14 @@ fn expr_name(exp: &Node<fe::Expr>) -> yul::Expression {

fn expr_num(exp: &Node<fe::Expr>) -> yul::Expression {
if let fe::Expr::Num(num) = &exp.kind {
let literal = numeric::Literal::new(num);
let num = literal.parse::<BigInt>().expect("Invalid numeric literal");
let num = if matches!(literal.radix(), numeric::Radix::Decimal) {
format!("{}", num)
} else {
format!("{:#x}", num)
};

return literal_expression! {(num)};
}

Expand Down
1 change: 0 additions & 1 deletion compiler/tests/cases/compile_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ use rstest::rstest;
"numeric_capacity_mismatch/literal_too_small.fe",
NumericCapacityMismatch
),
case("numeric_capacity_mismatch/octal_number.fe", NumericCapacityMismatch),
case("numeric_capacity_mismatch/u128_neg.fe", NumericCapacityMismatch),
case("numeric_capacity_mismatch/u128_pos.fe", NumericCapacityMismatch),
case("numeric_capacity_mismatch/u16_neg.fe", NumericCapacityMismatch),
Expand Down
4 changes: 4 additions & 0 deletions compiler/tests/cases/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,10 @@ fn test_assert() {
case("return_bool_op_or.fe", &[bool_token(true), bool_token(false)], bool_token(true)),
case("return_bool_op_or.fe", &[bool_token(false), bool_token(true)], bool_token(true)),
case("return_bool_op_or.fe", &[bool_token(false), bool_token(false)], bool_token(false)),
// radix
case("radix_hex.fe", &[], uint_token(0xfe)),
case("radix_octal.fe", &[], uint_token(0o70)),
case("radix_binary.fe", &[], uint_token(0b10)),
)]
fn test_method_return(fixture_file: &str, input: &[ethabi::Token], expected: ethabi::Token) {
with_executor(&|mut executor| {
Expand Down

This file was deleted.

3 changes: 3 additions & 0 deletions compiler/tests/fixtures/features/radix_binary.fe
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
contract Foo:
pub def bar() -> u8:
return u8(0b10)
3 changes: 3 additions & 0 deletions compiler/tests/fixtures/features/radix_hex.fe
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
contract Foo:
pub def bar() -> u8:
return u8(0xfe)
3 changes: 3 additions & 0 deletions compiler/tests/fixtures/features/radix_octal.fe
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
contract Foo:
pub def bar() -> u8:
return u8(0o70)
9 changes: 9 additions & 0 deletions newsfragments/333.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Add support for hexadecimal/octal/binary numeric literals.

Example:

```
value_hex: u256 = 0xff
value_octal: u256 = 0o77
value_binary: u256 = 0b11
```
4 changes: 2 additions & 2 deletions parser/src/grammar/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ fn parse_expr_head(par: &mut Parser) -> ParseResult<Node<Expr>> {
use TokenKind::*;

match par.peek_or_err()? {
Name | Int | Hex | Text | True | False => {
Name | Int | Hex | Octal | Binary | Text | True | False => {
let tok = par.next()?;
Ok(atom(par, &tok))
}
Expand Down Expand Up @@ -323,7 +323,7 @@ fn atom(par: &mut Parser, tok: &Token) -> Node<Expr> {

let expr = match tok.kind {
Name => Expr::Name(tok.text.to_owned()),
Int | Hex => Expr::Num(tok.text.to_owned()),
Int | Hex | Octal | Binary => Expr::Num(tok.text.to_owned()),
True | False => Expr::Bool(tok.kind == True),
Text => {
if let Some(string) = unescape_string(tok.text) {
Expand Down
Loading