Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fasteval2"
version = "2.0.3"
version = "2.0.4"
authors = ["Pasha Podolsky <ppodolsky@me.com>", "Christopher Sebastian <christopher@likebike.com>"]
license = "MIT"
readme = "README.md"
Expand All @@ -18,8 +18,8 @@ debug = true
lto = true

[features]
default = ["alpha-keywords"]
default = ["alpha-keywords", "print-func"]
alpha-keywords = [] # Enable 'NaN', 'inf', 'and', 'or'
unsafe-vars = [] # tinyexpr-style pointer-based variables.
nightly = [] # Enable features that depend on Rust nightly.

print-func = [] # Enables the builtin print function
2 changes: 1 addition & 1 deletion examples/advanced-vars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn main() -> Result<(), fasteval2::Error> {
// The `args.get...` code is the same as:
// mydata[args[0] as usize]
// ...but it won't panic if either index is out-of-bounds.
"data" => args.get(0).and_then(|f| mydata.get(*f as usize).copied()),
"data" => args.first().and_then(|f| mydata.get(*f as usize).copied()),

// A wildcard to handle all undefined names:
_ => None,
Expand Down
2 changes: 1 addition & 1 deletion examples/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ fn repl() {
None => break,
};
let mut line = line.trim().to_string();
if line == "" {
if line.is_empty() {
continue;
}

Expand Down
104 changes: 53 additions & 51 deletions src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,21 @@ use crate::parser::{
BinaryOp::{
self, EAdd, EDiv, EExp, EMod, EMul, ESub, EAND, EEQ, EGT, EGTE, ELT, ELTE, ENE, EOR,
},
ExprPair, Expression, PrintFunc,
ExprPair, Expression,
StdFunc::{
self, EFunc, EFuncACos, EFuncACosH, EFuncASin, EFuncASinH, EFuncATan, EFuncATanH, EFuncAbs,
EFuncCeil, EFuncCos, EFuncCosH, EFuncE, EFuncFloor, EFuncInt, EFuncLog, EFuncMax, EFuncMin,
EFuncPi, EFuncRound, EFuncSign, EFuncSin, EFuncSinH, EFuncTan, EFuncTanH, EVar,
EFuncPi, EFuncRound, EFuncSign, EFuncSin, EFuncSinH, EFuncSqrt, EFuncTan, EFuncTanH, EVar,
},
UnaryOp::{self, ENeg, ENot, EParentheses, EPos},
Value,
};
use crate::slab::{CompileSlab, ParseSlab};
use crate::Error;

#[cfg(feature = "print-func")]
use crate::parser::PrintFunc;

/// `true` --> `1.0`, `false` --> `0.0`
#[macro_export]
macro_rules! bool_to_f64 {
Expand Down Expand Up @@ -167,7 +170,9 @@ pub enum Instruction {
IFuncASinH(InstructionI),
IFuncACosH(InstructionI),
IFuncATanH(InstructionI),
IFuncSqrt(InstructionI),

#[cfg(feature = "print-func")]
IPrintFunc(PrintFunc), // Not optimized (it would be pointless because of i/o bottleneck).
}
use crate::{eval_var, EvalNamespace};
Expand All @@ -176,10 +181,13 @@ use Instruction::IUnsafeVar;
use Instruction::{
IAdd, IConst, IExp, IFunc, IFuncACos, IFuncACosH, IFuncASin, IFuncASinH, IFuncATan, IFuncATanH,
IFuncAbs, IFuncCeil, IFuncCos, IFuncCosH, IFuncFloor, IFuncInt, IFuncLog, IFuncMax, IFuncMin,
IFuncRound, IFuncSign, IFuncSin, IFuncSinH, IFuncTan, IFuncTanH, IInv, IMod, IMul, INeg, INot,
IPrintFunc, IVar, IAND, IEQ, IGT, IGTE, ILT, ILTE, INE, IOR,
IFuncRound, IFuncSign, IFuncSin, IFuncSinH, IFuncSqrt, IFuncTan, IFuncTanH, IInv, IMod, IMul,
INeg, INot, IVar, IAND, IEQ, IGT, IGTE, ILT, ILTE, INE, IOR,
};

#[cfg(feature = "print-func")]
use Instruction::IPrintFunc;

impl Default for Instruction {
fn default() -> Self {
IConst(std::f64::NAN)
Expand Down Expand Up @@ -219,15 +227,12 @@ impl<'s> ExprSlice<'s> {
sl
}
fn split(&self, bop: BinaryOp, dst: &mut Vec<ExprSlice<'s>>) {
dst.push(ExprSlice::new(&self.first));
dst.push(ExprSlice::new(self.first));
for exprpair in self.pairs.iter() {
if exprpair.0 == bop {
dst.push(ExprSlice::new(&exprpair.1));
} else {
match dst.last_mut() {
Some(cur) => cur.pairs.push(exprpair),
None => (), // unreachable
}
} else if let Some(cur) = dst.last_mut() {
cur.pairs.push(exprpair)
}
}
}
Expand All @@ -237,16 +242,13 @@ impl<'s> ExprSlice<'s> {
xsdst: &mut Vec<ExprSlice<'s>>,
opdst: &mut Vec<&'s BinaryOp>,
) {
xsdst.push(ExprSlice::new(&self.first));
xsdst.push(ExprSlice::new(self.first));
for exprpair in self.pairs.iter() {
if search.contains(&exprpair.0) {
xsdst.push(ExprSlice::new(&exprpair.1));
opdst.push(&exprpair.0);
} else {
match xsdst.last_mut() {
Some(cur) => cur.pairs.push(exprpair),
None => (), // unreachable
}
} else if let Some(cur) = xsdst.last_mut() {
cur.pairs.push(exprpair)
}
}
}
Expand Down Expand Up @@ -321,13 +323,11 @@ fn compile_mul(instrs: Vec<Instruction>, cslab: &mut CompileSlab) -> Instruction
for instr in instrs {
if let IConst(c) = instr {
const_prod *= c; // Floats don't overflow.
} else if out_set {
out = IMul(cslab.push_instr(out), IC::I(cslab.push_instr(instr)));
} else {
if out_set {
out = IMul(cslab.push_instr(out), IC::I(cslab.push_instr(instr)));
} else {
out = instr;
out_set = true;
}
out = instr;
out_set = true;
}
}
if f64_ne!(const_prod, 1.0) {
Expand All @@ -346,13 +346,11 @@ fn compile_add(instrs: Vec<Instruction>, cslab: &mut CompileSlab) -> Instruction
for instr in instrs {
if let IConst(c) = instr {
const_sum += c; // Floats don't overflow.
} else if out_set {
out = IAdd(cslab.push_instr(out), IC::I(cslab.push_instr(instr)));
} else {
if out_set {
out = IAdd(cslab.push_instr(out), IC::I(cslab.push_instr(instr)));
} else {
out = instr;
out_set = true;
}
out = instr;
out_set = true;
}
}
if f64_ne!(const_sum, 0.0) {
Expand Down Expand Up @@ -519,17 +517,15 @@ impl Compiler for ExprSlice<'_> {
let instr = xs.compile(pslab, cslab, ns);
if out_set {
out = IOR(cslab.push_instr(out), instr_to_ic!(cslab, instr));
} else {
if let IConst(c) = instr {
if f64_ne!(c, 0.0) {
return instr;
}
// out = instr; // Skip this 0 value (mostly so I don't complicate my logic in 'if out_set' since I can assume that any set value is non-const).
// out_set = true;
} else {
out = instr;
out_set = true;
} else if let IConst(c) = instr {
if f64_ne!(c, 0.0) {
return instr;
}
// out = instr; // Skip this 0 value (mostly so I don't complicate my logic in 'if out_set' since I can assume that any set value is non-const).
// out_set = true;
} else {
out = instr;
out_set = true;
}
}
out
Expand Down Expand Up @@ -747,7 +743,7 @@ impl Compiler for Expression {
cslab: &mut CompileSlab,
ns: &mut impl EvalNamespace,
) -> Instruction {
let top = ExprSlice::from_expr(&self);
let top = ExprSlice::from_expr(self);
top.compile(pslab, cslab, ns)
}
}
Expand All @@ -763,6 +759,7 @@ impl Compiler for Value {
Value::EConstant(c) => IConst(*c),
Value::EUnaryOp(u) => u.compile(pslab, cslab, ns),
Value::EStdFunc(f) => f.compile(pslab, cslab, ns),
#[cfg(feature = "print-func")]
Value::EPrintFunc(pf) => IPrintFunc(pf.clone()),
}
}
Expand Down Expand Up @@ -827,6 +824,7 @@ impl Compiler for StdFunc {
}
if is_all_const {
let computed_value = eval_var!(ns, name, f64_args, unsafe {
#[allow(invalid_reference_casting)]
&mut *(&pslab.char_buf as *const _ as *mut _)
});
if let Ok(value) = computed_value {
Expand Down Expand Up @@ -953,13 +951,11 @@ impl Compiler for StdFunc {
const_min = f;
const_min_set = true;
}
} else if out_set {
out = IFuncMin(cslab.push_instr(out), IC::I(cslab.push_instr(instr)));
} else {
if out_set {
out = IFuncMin(cslab.push_instr(out), IC::I(cslab.push_instr(instr)));
} else {
out = instr;
out_set = true;
}
out = instr;
out_set = true;
}
}
if const_min_set {
Expand Down Expand Up @@ -1003,13 +999,11 @@ impl Compiler for StdFunc {
const_max = f;
const_max_set = true;
}
} else if out_set {
out = IFuncMax(cslab.push_instr(out), IC::I(cslab.push_instr(instr)));
} else {
if out_set {
out = IFuncMax(cslab.push_instr(out), IC::I(cslab.push_instr(instr)));
} else {
out = instr;
out_set = true;
}
out = instr;
out_set = true;
}
}
if const_max_set {
Expand Down Expand Up @@ -1123,6 +1117,14 @@ impl Compiler for StdFunc {
IFuncATanH(cslab.push_instr(instr))
}
}
EFuncSqrt(i) => {
let instr = get_expr!(pslab, i).compile(pslab, cslab, ns);
if let IConst(c) = instr {
IConst(c.sqrt())
} else {
IFuncSqrt(cslab.push_instr(instr))
}
}
}
}
}
Loading