1
0
mirror of https://github.com/nushell/nushell.git synced 2025-07-12 20:35:18 +02:00

Add hex, octal, binary ()

This commit is contained in:
JT
2021-06-06 17:14:51 +12:00
committed by GitHub
parent 995dbd25b3
commit a2e6f5ebdb
2 changed files with 85 additions and 5 deletions
crates/nu-parser/src
tests/shell/pipeline/commands

@ -830,6 +830,56 @@ fn parse_table(
)
}
fn parse_int(lite_arg: &Spanned<String>) -> (SpannedExpression, Option<ParseError>) {
if lite_arg.item.starts_with("0x") {
if let Ok(v) = i64::from_str_radix(&lite_arg.item[2..], 16) {
(
SpannedExpression::new(Expression::integer(v), lite_arg.span),
None,
)
} else {
(
garbage(lite_arg.span),
Some(ParseError::mismatch("int", lite_arg.clone())),
)
}
} else if lite_arg.item.starts_with("0b") {
if let Ok(v) = i64::from_str_radix(&lite_arg.item[2..], 2) {
(
SpannedExpression::new(Expression::integer(v), lite_arg.span),
None,
)
} else {
(
garbage(lite_arg.span),
Some(ParseError::mismatch("int", lite_arg.clone())),
)
}
} else if lite_arg.item.starts_with("0o") {
if let Ok(v) = i64::from_str_radix(&lite_arg.item[2..], 8) {
(
SpannedExpression::new(Expression::integer(v), lite_arg.span),
None,
)
} else {
(
garbage(lite_arg.span),
Some(ParseError::mismatch("int", lite_arg.clone())),
)
}
} else if let Ok(x) = lite_arg.item.parse::<i64>() {
(
SpannedExpression::new(Expression::integer(x), lite_arg.span),
None,
)
} else {
(
garbage(lite_arg.span),
Some(ParseError::mismatch("int", lite_arg.clone())),
)
}
}
/// Parses the given argument using the shape as a guide for how to correctly parse the argument
fn parse_arg(
expected_type: SyntaxShape,
@ -850,11 +900,8 @@ fn parse_arg(
match expected_type {
SyntaxShape::Number => {
if let Ok(x) = lite_arg.item.parse::<i64>() {
(
SpannedExpression::new(Expression::integer(x), lite_arg.span),
None,
)
if let (x, None) = parse_int(lite_arg) {
(x, None)
} else if let Ok(x) = lite_arg.item.parse::<BigInt>() {
(
SpannedExpression::new(Expression::big_integer(x), lite_arg.span),

@ -529,6 +529,39 @@ fn block_params_override_correct() {
assert_eq!(actual.out, "[1,2,3]");
}
#[test]
fn hex_number() {
let actual = nu!(
cwd: ".",
r#"
0x10
"#
);
assert_eq!(actual.out, "16");
}
#[test]
fn binary_number() {
let actual = nu!(
cwd: ".",
r#"
0b10
"#
);
assert_eq!(actual.out, "2");
}
#[test]
fn octal_number() {
let actual = nu!(
cwd: ".",
r#"
0o10
"#
);
assert_eq!(actual.out, "8");
}
#[test]
fn run_dynamic_blocks() {
let actual = nu!(