Merge pull request #48 from rustysec/integer_literals

Integer literals
This commit is contained in:
Lukáš Hozda [magnusi] 2017-11-01 22:12:33 +01:00 committed by GitHub
commit 580b7c7d69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 101 additions and 0 deletions

View File

@ -372,6 +372,7 @@ impl<'a> TokenIterator<'a> {
match c { match c {
'0'...'9' => { '0'...'9' => {
let mut result = Vec::new(); let mut result = Vec::new();
let mut radix_base: Option<u32> = None;
result.push(c); result.push(c);
while let Some(&nxt) = self.char_stream.peek() { while let Some(&nxt) = self.char_stream.peek() {
@ -393,9 +394,58 @@ impl<'a> TokenIterator<'a> {
} }
} }
} }
'x' | 'X' => {
result.push(nxt);
self.char_stream.next();
while let Some(&nxt_hex) = self.char_stream.peek() {
match nxt_hex {
'0'...'9' | 'a'...'f' | 'A'...'F' => {
result.push(nxt_hex);
self.char_stream.next();
}
_ => break, _ => break,
} }
} }
radix_base = Some(16);
}
'o' | 'O' => {
result.push(nxt);
self.char_stream.next();
while let Some(&nxt_oct) = self.char_stream.peek() {
match nxt_oct {
'0'...'8' => {
result.push(nxt_oct);
self.char_stream.next();
}
_ => break,
}
}
radix_base = Some(8);
}
'b' | 'B' => {
result.push(nxt);
self.char_stream.next();
while let Some(&nxt_bin) = self.char_stream.peek() {
match nxt_bin {
'0' | '1' | '_' => {
result.push(nxt_bin);
self.char_stream.next();
}
_ => break,
}
}
radix_base = Some(2);
}
_ => break,
}
}
if let Some(radix) = radix_base {
let out: String = result.iter().cloned().skip(2).filter(|c| c != &'_').collect();
if let Ok(val) = i64::from_str_radix(&out, radix) {
return Some(Token::IntConst(val));
}
}
let out: String = result.iter().cloned().collect(); let out: String = result.iter().cloned().collect();

View File

@ -690,6 +690,23 @@ fn test_right_shift_equals() {
} }
} }
#[test]
fn test_hex_literal() {
let mut engine = Engine::new();
if let Ok(result) = engine.eval::<i64>("let x = 0xf; x") {
assert_eq!(result, 15);
} else {
assert!(false);
}
if let Ok(result) = engine.eval::<i64>("let x = 0xff; x") {
assert_eq!(result, 255);
} else {
assert!(false);
}
}
#[test] #[test]
fn test_modulo_equals() { fn test_modulo_equals() {
let mut engine = Engine::new(); let mut engine = Engine::new();
@ -701,6 +718,40 @@ fn test_modulo_equals() {
} }
} }
#[test]
fn test_octal_literal() {
let mut engine = Engine::new();
if let Ok(result) = engine.eval::<i64>("let x = 0o77; x") {
assert_eq!(result, 63);
} else {
assert!(false)
}
if let Ok(result) = engine.eval::<i64>("let x = 0o1234; x") {
assert_eq!(result, 668);
} else {
assert!(false);
}
}
#[test]
fn test_binary_literal() {
let mut engine = Engine::new();
if let Ok(result) = engine.eval::<i64>("let x = 0b1111; x") {
assert_eq!(result, 15);
} else {
assert!(false);
}
if let Ok(result) = engine.eval::<i64>("let x = 0b0011_1100_1010_0101; x") {
assert_eq!(result, 15525);
} else {
assert!(false);
}
}
#[test] #[test]
fn test_binary_ops() { fn test_binary_ops() {
let mut engine = Engine::new(); let mut engine = Engine::new();