Merge pull request #38 from rustysec/float

candidate f64 support
This commit is contained in:
Lukáš Hozda [magnusi] 2017-10-30 16:37:49 +01:00 committed by GitHub
commit 04f0b70b4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 106 additions and 19 deletions

View File

@ -8,7 +8,7 @@ use parser::{lex, parse, Expr, Stmt, FnDef};
use fn_register::FnRegister; use fn_register::FnRegister;
use std::ops::{Add, Sub, Mul, Div, Neg}; use std::ops::{Add, Sub, Mul, Div, Neg};
use std::cmp::{Ord, Eq}; use std::cmp::{PartialOrd, PartialEq};
#[derive(Debug)] #[derive(Debug)]
pub enum EvalAltResult { pub enum EvalAltResult {
@ -948,6 +948,7 @@ impl Engine {
fn eval_expr(&self, scope: &mut Scope, expr: &Expr) -> Result<Box<Any>, EvalAltResult> { fn eval_expr(&self, scope: &mut Scope, expr: &Expr) -> Result<Box<Any>, EvalAltResult> {
match *expr { match *expr {
Expr::IntConst(i) => Ok(Box::new(i)), Expr::IntConst(i) => Ok(Box::new(i)),
Expr::FloatConst(i) => Ok(Box::new(i)),
Expr::StringConst(ref s) => Ok(Box::new(s.clone())), Expr::StringConst(ref s) => Ok(Box::new(s.clone())),
Expr::CharConst(ref c) => Ok(Box::new(*c)), Expr::CharConst(ref c) => Ok(Box::new(*c)),
Expr::Identifier(ref id) => { Expr::Identifier(ref id) => {
@ -1400,12 +1401,12 @@ impl Engine {
fn mul<T: Mul>(x: T, y: T) -> <T as Mul>::Output { x * y } fn mul<T: Mul>(x: T, y: T) -> <T as Mul>::Output { x * y }
fn div<T: Div>(x: T, y: T) -> <T as Div>::Output { x / y } fn div<T: Div>(x: T, y: T) -> <T as Div>::Output { x / y }
fn neg<T: Neg>(x: T) -> <T as Neg>::Output { -x } fn neg<T: Neg>(x: T) -> <T as Neg>::Output { -x }
fn lt<T: Ord>(x: T, y: T) -> bool { x < y } fn lt<T: PartialOrd>(x: T, y: T) -> bool { x < y }
fn lte<T: Ord>(x: T, y: T) -> bool { x <= y } fn lte<T: PartialOrd>(x: T, y: T) -> bool { x <= y }
fn gt<T: Ord>(x: T, y: T) -> bool { x > y } fn gt<T: PartialOrd>(x: T, y: T) -> bool { x > y }
fn gte<T: Ord>(x: T, y: T) -> bool { x >= y } fn gte<T: PartialOrd>(x: T, y: T) -> bool { x >= y }
fn eq<T: Eq>(x: T, y: T) -> bool { x == y } fn eq<T: PartialEq>(x: T, y: T) -> bool { x == y }
fn ne<T: Eq>(x: T, y: T) -> bool { x != y } fn ne<T: PartialEq>(x: T, y: T) -> bool { x != y }
fn and(x: bool, y: bool) -> bool { x && y } fn and(x: bool, y: bool) -> bool { x && y }
fn or(x: bool, y: bool) -> bool { x || y } fn or(x: bool, y: bool) -> bool { x || y }
fn not(x: bool) -> bool { !x } fn not(x: bool) -> bool { !x }
@ -1416,12 +1417,12 @@ impl Engine {
reg_op!(engine, "*", mul, i32, i64, u32, u64, f32, f64); reg_op!(engine, "*", mul, i32, i64, u32, u64, f32, f64);
reg_op!(engine, "/", div, i32, i64, u32, u64, f32, f64); reg_op!(engine, "/", div, i32, i64, u32, u64, f32, f64);
reg_cmp!(engine, "<", lt, i32, i64, u32, u64, String); reg_cmp!(engine, "<", lt, i32, i64, u32, u64, String, f64);
reg_cmp!(engine, "<=", lte, i32, i64, u32, u64, String); reg_cmp!(engine, "<=", lte, i32, i64, u32, u64, String, f64);
reg_cmp!(engine, ">", gt, i32, i64, u32, u64, String); reg_cmp!(engine, ">", gt, i32, i64, u32, u64, String, f64);
reg_cmp!(engine, ">=", gte, i32, i64, u32, u64, String); reg_cmp!(engine, ">=", gte, i32, i64, u32, u64, String, f64);
reg_cmp!(engine, "==", eq, i32, i64, u32, u64, bool, String); reg_cmp!(engine, "==", eq, i32, i64, u32, u64, bool, String, f64);
reg_cmp!(engine, "!=", ne, i32, i64, u32, u64, bool, String); reg_cmp!(engine, "!=", ne, i32, i64, u32, u64, bool, String, f64);
reg_op!(engine, "||", or, bool); reg_op!(engine, "||", or, bool);
reg_op!(engine, "&&", and, bool); reg_op!(engine, "&&", and, bool);

View File

@ -98,6 +98,7 @@ pub enum Stmt {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum Expr { pub enum Expr {
IntConst(i64), IntConst(i64),
FloatConst(f64),
Identifier(String), Identifier(String),
CharConst(char), CharConst(char),
StringConst(String), StringConst(String),
@ -113,6 +114,7 @@ pub enum Expr {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum Token { pub enum Token {
IntConst(i64), IntConst(i64),
FloatConst(f64),
Identifier(String), Identifier(String),
CharConst(char), CharConst(char),
StringConst(String), StringConst(String),
@ -366,6 +368,19 @@ impl<'a> TokenIterator<'a> {
result.push(nxt); result.push(nxt);
self.char_stream.next(); self.char_stream.next();
} }
'.' => {
result.push(nxt);
self.char_stream.next();
while let Some(&nxt_float) = self.char_stream.peek() {
match nxt_float {
'0'...'9' => {
result.push(nxt_float);
self.char_stream.next();
}
_ => break,
}
}
}
_ => break, _ => break,
} }
} }
@ -374,6 +389,8 @@ impl<'a> TokenIterator<'a> {
if let Ok(val) = out.parse::<i64>() { if let Ok(val) = out.parse::<i64>() {
return Some(Token::IntConst(val)); return Some(Token::IntConst(val));
} else if let Ok(val) = out.parse::<f64>() {
return Some(Token::FloatConst(val));
} }
return Some(Token::LexErr(LexError::MalformedNumber)); return Some(Token::LexErr(LexError::MalformedNumber));
} }
@ -392,7 +409,6 @@ impl<'a> TokenIterator<'a> {
} }
let out: String = result.iter().cloned().collect(); let out: String = result.iter().cloned().collect();
match out.as_ref() { match out.as_ref() {
"true" => return Some(Token::True), "true" => return Some(Token::True),
"false" => return Some(Token::False), "false" => return Some(Token::False),
@ -404,7 +420,7 @@ impl<'a> TokenIterator<'a> {
"break" => return Some(Token::Break), "break" => return Some(Token::Break),
"return" => return Some(Token::Return), "return" => return Some(Token::Return),
"fn" => return Some(Token::Fn), "fn" => return Some(Token::Fn),
x => return Some(Token::Identifier(x.to_string())) x => return Some(Token::Identifier(x.to_string())),
} }
} }
'"' => { '"' => {
@ -698,6 +714,7 @@ fn parse_primary<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<Expr, Pa
if let Some(token) = input.next() { if let Some(token) = input.next() {
match token { match token {
Token::IntConst(ref x) => Ok(Expr::IntConst(*x)), Token::IntConst(ref x) => Ok(Expr::IntConst(*x)),
Token::FloatConst(ref x) => Ok(Expr::FloatConst(*x)),
Token::StringConst(ref s) => Ok(Expr::StringConst(s.clone())), Token::StringConst(ref s) => Ok(Expr::StringConst(s.clone())),
Token::CharConst(ref c) => Ok(Expr::CharConst(*c)), Token::CharConst(ref c) => Ok(Expr::CharConst(*c)),
Token::Identifier(ref s) => parse_ident_expr(s.clone(), input), Token::Identifier(ref s) => parse_ident_expr(s.clone(), input),

View File

@ -402,6 +402,75 @@ fn test_array_with_structs() {
} }
} }
#[test]
fn test_float() {
let mut engine = Engine::new();
if let Ok(result) = engine.eval::<bool>("let x = 0.0; let y = 1.0; x < y") {
assert!(result);
} else {
assert!(false);
}
if let Ok(result) = engine.eval::<bool>("let x = 0.0; let y = 1.0; x > y") {
assert!(!result);
} else {
assert!(false);
}
if let Ok(result) = engine.eval::<f64>("let x = 9.9999; x") {
assert_eq!(result, 9.9999);
} else {
assert!(false);
}
}
#[test]
fn struct_with_float() {
#[derive(Clone)]
struct TestStruct {
x: f64,
}
impl TestStruct {
fn update(&mut self) {
self.x += 5.789_f64;
}
fn get_x(&mut self) -> f64 {
self.x
}
fn set_x(&mut self, new_x: f64) {
self.x = new_x;
}
fn new() -> TestStruct {
TestStruct { x: 1.0 }
}
}
let mut engine = Engine::new();
engine.register_type::<TestStruct>();
engine.register_get_set("x", TestStruct::get_x, TestStruct::set_x);
engine.register_fn("update", TestStruct::update);
engine.register_fn("new_ts", TestStruct::new);
if let Ok(result) = engine.eval::<f64>("let ts = new_ts(); ts.update(); ts.x") {
assert_eq!(result, 6.789);
} else {
assert!(false);
}
if let Ok(result) = engine.eval::<f64>("let ts = new_ts(); ts.x = 10.1001; ts.x") {
assert_eq!(result, 10.1001);
} else {
assert!(false);
}
}
#[test] #[test]
fn test_comments() { fn test_comments() {
let mut engine = Engine::new(); let mut engine = Engine::new();