Skip to content

Commit 93f155e

Browse files
authored
fix identifiers (#43)
1 parent ce39ca7 commit 93f155e

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/circuit.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,26 @@ pub struct Identifier {
6565
impl Identifier {
6666
/// Creates a new identifier with the given name
6767
pub fn new(name: String) -> Self {
68+
if name.is_empty() {
69+
panic!("Identifier name cannot be empty");
70+
}
71+
6872
if let Some(root) = name.strip_prefix('\\') {
6973
return Identifier {
7074
name: root.to_string(),
7175
id_type: IdentifierType::Escaped,
7276
};
7377
}
7478

79+
// Check if first char is a digit
80+
let esc_chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
81+
if esc_chars.contains(&name.chars().next().unwrap()) {
82+
return Identifier {
83+
name,
84+
id_type: IdentifierType::Escaped,
85+
};
86+
}
87+
7588
// Certainly not an exhaustive list.
7689
// TODO(matth2k): Implement a true isEscaped()
7790
let esc_chars = [' ', '\\', '(', ')', ',', '+', '-', '$', '\'', '~'];

tests/identifiers.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,13 @@ fn concat_simple_w_escaped() {
2525
assert_eq!(Identifier::new("\\id0_id1$".to_string()), id2);
2626
assert!(id2.is_escaped());
2727
}
28+
29+
#[test]
30+
fn aig_id() {
31+
let id0 = Identifier::new("1".to_string());
32+
assert!(id0.is_escaped());
33+
let id1 = Identifier::new("inv".to_string());
34+
let id2 = id0 + id1;
35+
assert_eq!(Identifier::new("\\1_inv".to_string()), id2);
36+
assert!(id2.is_escaped());
37+
}

0 commit comments

Comments
 (0)