diff --git a/crates/oq3_semantics/tests/from_string_tests.rs b/crates/oq3_semantics/tests/from_string_tests.rs index f97ecfa..72d4b93 100644 --- a/crates/oq3_semantics/tests/from_string_tests.rs +++ b/crates/oq3_semantics/tests/from_string_tests.rs @@ -521,6 +521,78 @@ fn test_from_string_neg_spc_lit_float() { assert_eq!(expr, "-1.23"); } +#[test] +fn test_from_string_hex_literal() { + let code = r##" +0xFF; +"##; + let (program, errors, _symbol_table) = parse_string(code); + assert!(errors.is_empty()); + assert_eq!(program.len(), 1); + let expr = literal_value(&program[0]).unwrap(); + assert_eq!(expr, "255"); +} + +#[test] +fn test_from_string_hex_literal_capital() { + let code = r##" +0XFF; +"##; + let (program, errors, _symbol_table) = parse_string(code); + assert!(errors.is_empty()); + assert_eq!(program.len(), 1); + let expr = literal_value(&program[0]).unwrap(); + assert_eq!(expr, "255"); +} + +#[test] +fn test_from_string_octal_literal() { + let code = r##" +0o77; +"##; + let (program, errors, _symbol_table) = parse_string(code); + assert!(errors.is_empty()); + assert_eq!(program.len(), 1); + let expr = literal_value(&program[0]).unwrap(); + assert_eq!(expr, "63"); +} + +#[test] +fn test_from_string_octal_literal_capital() { + let code = r##" +0O77; +"##; + let (program, errors, _symbol_table) = parse_string(code); + assert!(errors.is_empty()); + assert_eq!(program.len(), 1); + let expr = literal_value(&program[0]).unwrap(); + assert_eq!(expr, "63"); +} + +#[test] +fn test_from_string_binary_literal() { + let code = r##" +0b11; +"##; + let (program, errors, _symbol_table) = parse_string(code); + assert!(errors.is_empty()); + assert_eq!(program.len(), 1); + let expr = literal_value(&program[0]).unwrap(); + assert_eq!(expr, "3"); +} + +#[test] +fn test_from_string_binary_literal_capital() { + let code = r##" +0B11; +"##; + let (program, errors, _symbol_table) = parse_string(code); + assert!(errors.is_empty()); + assert_eq!(program.len(), 1); + let expr = literal_value(&program[0]).unwrap(); + assert_eq!(expr, "3"); +} + // PR #91 #[test] fn test_from_string_bin_expr_no_spc() { diff --git a/crates/oq3_syntax/src/ast/token_ext.rs b/crates/oq3_syntax/src/ast/token_ext.rs index 3f62d1f..4c8ab4c 100644 --- a/crates/oq3_syntax/src/ast/token_ext.rs +++ b/crates/oq3_syntax/src/ast/token_ext.rs @@ -259,9 +259,9 @@ impl ast::BitString { impl ast::IntNumber { pub fn radix(&self) -> Radix { match self.text().get(..2).unwrap_or_default() { - "0b" => Radix::Binary, - "0o" => Radix::Octal, - "0x" => Radix::Hexadecimal, + "0b" | "0B" => Radix::Binary, + "0o" | "0O" => Radix::Octal, + "0x" | "0X" => Radix::Hexadecimal, _ => Radix::Decimal, } }