Add support for character constants

This commit is contained in:
jonathandturner
2016-04-13 18:40:06 -07:00
parent 3364191781
commit b9ea072d6a
7 changed files with 180 additions and 17 deletions

View File

@@ -636,6 +636,7 @@ impl Engine {
match *expr {
Expr::IntConst(i) => Ok(Box::new(i)),
Expr::StringConst(ref s) => Ok(Box::new(s.clone())),
Expr::CharConst(ref c) => Ok(Box::new(c.clone())),
Expr::Identifier(ref id) => {
for &mut (ref name, ref mut val) in &mut scope.iter_mut().rev() {
if *id == *name {
@@ -1029,6 +1030,30 @@ fn test_number_literal() {
}
}
#[test]
fn test_chars() {
let mut engine = Engine::new();
if let Ok(result) = engine.eval::<char>("'y'") {
assert_eq!(result, 'y');
}
else {
assert!(false);
}
if let Ok(result) = engine.eval::<char>("'\\u2764'") {
assert_eq!(result, '❤');
}
else {
assert!(false);
}
match engine.eval::<char>("''") {
Err(_) => (),
_ => assert!(false)
}
}
#[test]
fn test_ops() {
let mut engine = Engine::new();