candidate f64 support
This commit is contained in:
parent
0b9865b24c
commit
01666cefb9
@ -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};
|
use std::ops::{Add, Sub, Mul, Div};
|
||||||
use std::cmp::{Ord, Eq};
|
use std::cmp::{PartialOrd, PartialEq};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum EvalAltResult {
|
pub enum EvalAltResult {
|
||||||
@ -913,6 +913,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) => {
|
||||||
@ -1329,12 +1330,12 @@ impl Engine {
|
|||||||
fn sub<T: Sub>(x: T, y: T) -> <T as Sub>::Output { x - y }
|
fn sub<T: Sub>(x: T, y: T) -> <T as Sub>::Output { x - y }
|
||||||
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 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 concat(x: String, y: String) -> String { x + &y }
|
fn concat(x: String, y: String) -> String { x + &y }
|
||||||
@ -1344,12 +1345,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);
|
||||||
|
@ -99,6 +99,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),
|
||||||
@ -114,6 +115,7 @@ pub enum Expr {
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Token {
|
pub enum Token {
|
||||||
IntConst(i64),
|
IntConst(i64),
|
||||||
|
FloatConst(f64),
|
||||||
Identifier(String),
|
Identifier(String),
|
||||||
CharConst(char),
|
CharConst(char),
|
||||||
StringConst(String),
|
StringConst(String),
|
||||||
@ -306,7 +308,6 @@ impl<'a> Iterator for 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),
|
||||||
@ -317,7 +318,18 @@ impl<'a> Iterator for 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 => {
|
||||||
|
match out.starts_with("f") {
|
||||||
|
false => return Some(Token::Identifier(x.to_string())),
|
||||||
|
true => {
|
||||||
|
if let Ok(f) = (&out[1..]).to_owned().replace("_", ".").parse::<f64>() {
|
||||||
|
return Some(Token::FloatConst(f));
|
||||||
|
} else {
|
||||||
|
return Some(Token::Identifier(x.to_string()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
'"' => {
|
'"' => {
|
||||||
@ -549,6 +561,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),
|
||||||
|
69
src/tests.rs
69
src/tests.rs
@ -401,3 +401,72 @@ fn test_array_with_structs() {
|
|||||||
assert!(false);
|
assert!(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_float() {
|
||||||
|
let mut engine = Engine::new();
|
||||||
|
|
||||||
|
if let Ok(result) = engine.eval::<bool>("let x = f0_0; let y = f1_0; x < y") {
|
||||||
|
assert!(result);
|
||||||
|
} else {
|
||||||
|
assert!(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Ok(result) = engine.eval::<bool>("let x = f0_0; let y = f1_0; x > y") {
|
||||||
|
assert!(!result);
|
||||||
|
} else {
|
||||||
|
assert!(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Ok(result) = engine.eval::<f64>("let x = f9_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 = f10_1001; ts.x") {
|
||||||
|
assert_eq!(result, 10.1001);
|
||||||
|
} else {
|
||||||
|
assert!(false);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user