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

Support prefixes 0X, 0O, 0B for hex, octal, binary literals #232

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ A reminder: A front end is not of much use unless you have a back end. Examples
can be found in [./crates/oq3_semantics/examples/semdemo.rs](./crates/oq3_semantics/examples/semdemo.rs).

```shell
shell> export QASM3_PATH=./crates/semantics/examples/qasm/
shell> export QASM3_PATH=./crates/oq3_semantics/examples/qasm/
shell> cargo run --example semdemo -- semantic scratch1.qasm
```

Expand Down
72 changes: 72 additions & 0 deletions crates/oq3_semantics/tests/from_string_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
6 changes: 3 additions & 3 deletions crates/oq3_syntax/src/ast/token_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down
Loading