1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
//! [TinyExpr](https://github.com/kondrak/tinyexpr-rs) is a tiny recursive descent expression
//! parser, compiler, and evaluation engine for math expressions.
//! This is a work in progress port of [TinyExpr](https://github.com/codeplea/tinyexpr) to Rust.
//!
//! Current release only supports built-in system functions (trigonometry, algebraic operations, constants, etc.).
//! See the `tests` module for more examples.
//!
//!# Quick Start
//!
//!```
//!extern crate tinyexpr;
//!
//!fn main()
//!{
//!    // parse the expression and fetch result
//!    let r = tinyexpr::interp("2+2*2").unwrap();
//!
//!    // should print "6"
//!    println!("{:?}", r);
//!}
//!```
#[macro_use]
extern crate bitflags;
pub mod error;
use error::Result;
use std::f64::consts;
use std::str::FromStr;

#[doc(hidden)]
bitflags! {
    #[doc(hidden)]
    pub flags ExprType: u64 {
        const TE_VARIABLE  = 0,
        const TE_CONSTANT  = 1,
        const TE_FUNCTION0 = 8,
        const TE_FUNCTION1 = 9,
        const TE_FUNCTION2 = 10,
        const TE_FUNCTION3 = 11,
        const TE_FUNCTION4 = 12,
        const TE_FUNCTION5 = 13,
        const TE_FUNCTION6 = 14,
        const TE_FUNCTION7 = 15,
        const TE_CLOSURE0  = 16,
        const TE_CLOSURE1  = 17,
        const TE_CLOSURE2  = 18,
        const TE_CLOSURE3  = 19,
        const TE_CLOSURE4  = 20,
        const TE_CLOSURE5  = 21,
        const TE_CLOSURE6  = 22,
        const TE_CLOSURE7  = 23,
        const TE_FLAG_PURE = 32,
        const TOK_NULL     = 24,
        const TOK_ERROR    = 25,
        const TOK_END      = 26,
        const TOK_SEP      = 27,
        const TOK_OPEN     = 28,
        const TOK_CLOSE    = 29,
        const TOK_NUMBER   = 30,
        const TOK_VARIABLE = 31,
        const TOK_INFIX    = 32,
        const T_MASK       = 0x0000001F
    }
}

macro_rules! type_mask {
    ($x:expr) => ($x & T_MASK)
}

macro_rules! is_pure {
    ($x:expr) => (($x & TE_FLAG_PURE).bits() != 0)
}

macro_rules! is_function {
    ($x:expr) => (($x & TE_FUNCTION0).bits() != 0)
}

macro_rules! is_closure {
    ($x:expr) => (($x & TE_CLOSURE0).bits() != 0)
}

macro_rules! arity {
    ($x:expr) => (if($x & (TE_FUNCTION0 | TE_CLOSURE0)).bits() != 0 { $x.bits() & 0x00000007 } else { 0 })
}

// todo: introduce a Function struct to accomodate different arg numbers and ret values?
const FUNCTIONS: [&'static str; 21] = ["abs", "acos", "asin", "atan", "atan2", "ceil", "cos",
                                       "cosh", "e", "exp", "floor", "ln", "log", "log10",
                                       "pi", "pow", "sin", "sinh", "sqrt", "tan", "tanh" ];
const FUNCTION_TYPES: [(fn(f64, f64) -> f64, ExprType); 21] = [ (abs,  TE_FUNCTION1), (acos,  TE_FUNCTION1), (asin, TE_FUNCTION1),
                                                                (atan, TE_FUNCTION1), (atan2, TE_FUNCTION2), (ceil, TE_FUNCTION1),
                                                                (cos,  TE_FUNCTION1), (cosh,  TE_FUNCTION1), (e,    TE_FUNCTION0),
                                                                (exp,  TE_FUNCTION1), (floor, TE_FUNCTION1), (ln,   TE_FUNCTION1),
                                                                (log,  TE_FUNCTION1), (log10, TE_FUNCTION1), (pi,   TE_FUNCTION0),
                                                                (pow,  TE_FUNCTION2), (sin,   TE_FUNCTION1), (sinh, TE_FUNCTION1),
                                                                (sqrt, TE_FUNCTION1), (tan,   TE_FUNCTION1), (tanh, TE_FUNCTION1)];

fn dummy(_: f64, _: f64) -> f64 { panic!("called dummy!") } // todo
fn   add(a: f64, b: f64) -> f64 { a + b }
fn   sub(a: f64, b: f64) -> f64 { a - b }
fn   mul(a: f64, b: f64) -> f64 { a * b }
fn   div(a: f64, b: f64) -> f64 { a / b }
fn  fmod(a: f64, b: f64) -> f64 { a % b }
fn   neg(a: f64, _: f64) -> f64 { -a }
fn comma(_: f64, b: f64) -> f64 {  b }
// todo: this is added so that it works with current fptr... - need more types! no extra unused params!
fn   abs(a: f64, _: f64) -> f64 { a.abs()    }
fn  acos(a: f64, _: f64) -> f64 { a.acos()   }
fn  asin(a: f64, _: f64) -> f64 { a.asin()   }
fn  atan(a: f64, _: f64) -> f64 { a.atan()   }
fn atan2(a: f64, b: f64) -> f64 { a.atan2(b) }
fn  ceil(a: f64, _: f64) -> f64 { a.ceil()   }
fn   cos(a: f64, _: f64) -> f64 { a.cos()    }
fn  cosh(a: f64, _: f64) -> f64 { a.cosh()   }
fn     e(_: f64, _: f64) -> f64 { consts::E  }
fn   exp(a: f64, _: f64) -> f64 { a.exp()    }
fn floor(a: f64, _: f64) -> f64 { a.floor()  }
fn    ln(a: f64, _: f64) -> f64 { a.ln()     }
fn   log(a: f64, _: f64) -> f64 { a.log10()  } // todo ?
fn log10(a: f64, _: f64) -> f64 { a.log10()  }
fn    pi(_: f64, _: f64) -> f64 { consts::PI }
fn   pow(a: f64, b: f64) -> f64 { a.powf(b)  }
fn   sin(a: f64, _: f64) -> f64 { a.sin()    }
fn  sinh(a: f64, _: f64) -> f64 { a.sinh()   }
fn  sqrt(a: f64, _: f64) -> f64 { a.sqrt()   }
fn   tan(a: f64, _: f64) -> f64 { a.tan()    }
fn  tanh(a: f64, _: f64) -> f64 { a.tanh()   }

#[doc(hidden)]
#[derive(Debug)]
pub struct Expr {
    pub e_type: ExprType,
    pub value:  f64,
    pub bound:  i8, // todo: Variable?
    pub function:   fn(f64, f64) -> f64,
    pub parameters: Vec<Expr> // todo: should this be Option<>? Also, Expr&?
}

impl Expr {
    fn new() -> Expr {
        Expr {
            e_type: TOK_NULL,
            value:  0.0,
            bound:  0,
            function:   dummy,
            parameters: Vec::<Expr>::new()
        }
    }
}

impl Clone for Expr {
    fn clone(&self) -> Expr {
        Expr {
            e_type: self.e_type,
            value:  self.value,
            bound:  self.bound,
            function:   self.function,
            parameters: self.parameters.clone()
        }
    }
}


#[doc(hidden)]
#[derive(Debug)]
pub struct Variable {
    pub name:     String,
    pub address:  i8, // todo: this will have to go - handle variables? (no void*)
    pub function: fn(f64, f64) -> f64,
    pub v_type:   ExprType,
    pub context:  Vec<Expr>,
}

impl Variable {
    fn new(name: &str, v_type: ExprType) -> Variable {
        Variable {
            name:     String::from(name),
            address:  0,
            function: dummy,
            v_type:   v_type,
            context:  Vec::<Expr>::new(),
        }
    }
}

impl Clone for Variable {
    fn clone(&self) -> Variable {
        Variable {
            name:     self.name.clone(),
            address:  self.address,
            function: self.function,
            v_type:   self.v_type,
            context:  self.context.clone()
        }
    }
}

#[derive(Debug)]
struct State {
    pub next:   String,
    pub s_type: ExprType,
    pub n_idx:  usize,
    pub value:  f64,
    pub bound:  i8,
    pub function: fn(f64, f64) -> f64,
    pub context:  Vec<Expr>,
    pub lookup:   Vec<Variable>,
}

impl State {
    fn new(expression: &str) -> State {
        State {
            next:   String::from(expression),
            s_type: TOK_NULL,
            n_idx:  0,
            value:  0.0,
            bound:  0,
            function: mul,
            context:  Vec::<Expr>::new(),
            lookup:   Vec::<Variable>::new()
        }
    }
}

// todo
fn new_expr(e_type: ExprType, params: Option<Vec<Expr>>) -> Expr {
    let arity = arity!(e_type);
    let mut ret = Expr::new();
    // just create a new expression with new type based on old expression, no weird memcpy mumbo jumbo
    ret.e_type = e_type;
    ret.bound = 0;
    if let Some(params) = params {
        ret.parameters = params;
    }

    ret
}

fn find_lookup(s: &State, txt: &str) -> Option<Variable> {
    for var in &s.lookup {
        if &(*var.name) == txt {
            return Some((*var).clone());
        }
    }
    
    None
}

fn find_builtin(txt: &str) -> Option<Variable> {
    if let Ok(idx) = FUNCTIONS.binary_search(&txt) {
        let mut v = Variable::new(txt, FUNCTION_TYPES[idx].1 | TE_FLAG_PURE);
        v.function = FUNCTION_TYPES[idx].0;
        return Some(v);
    }

    None
}

fn next_token(s: &mut State) -> Result<String> {
    s.s_type = TOK_NULL;
    
    while s.s_type == TOK_NULL {
        if s.n_idx == s.next.len() {
            s.s_type = TOK_END;
            break;
        }

        let next_char = s.next.as_bytes()[s.n_idx] as char;
        // try reading a number
        if (next_char >= '0' && next_char <= '9') || next_char == '.' {
            let mut num_str = String::new();
            let mut c = next_char;

            // extract the number part to separate string which we then convert to f64
            while s.n_idx < s.next.len() && (c >= '0' && c <= '9') || c == '.' {
                num_str.push(c);
                s.n_idx += 1;
                if s.n_idx < s.next.len() {
                    c = s.next.as_bytes()[s.n_idx] as char;
                }
            }
            s.value  = try!(f64::from_str(&num_str));
            s.s_type = TOK_NUMBER;
        } else {
            // look for a variable or builting function call
            if next_char >= 'a' && next_char <= 'z' {
                let mut txt_str = String::new();
                let mut c = next_char;

                while s.n_idx < s.next.len() && (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') {
                    txt_str.push(c);
                    s.n_idx += 1;
                    if s.n_idx < s.next.len() {
                        c = s.next.as_bytes()[s.n_idx] as char;
                    }
                }

                let mut var = find_lookup(&s, &txt_str);
                if let None = var {
                   var = find_builtin(&txt_str);
                }

                if let Some(v) = var {
                    match type_mask!(v.v_type) {
                        TE_VARIABLE => { s.s_type = TOK_VARIABLE; s.bound = v.address; },
                        TE_CLOSURE0 => s.context = v.context,
                        TE_CLOSURE1 => s.context = v.context,
                        TE_CLOSURE2 => s.context = v.context,
                        TE_CLOSURE3 => s.context = v.context,
                        TE_CLOSURE4 => s.context = v.context,
                        TE_CLOSURE5 => s.context = v.context,
                        TE_CLOSURE6 => s.context = v.context,
                        TE_CLOSURE7 => s.context = v.context,
                        TE_FUNCTION0 => { s.s_type = v.v_type; s.function = v.function; },
                        TE_FUNCTION1 => { s.s_type = v.v_type; s.function = v.function; },
                        TE_FUNCTION2 => { s.s_type = v.v_type; s.function = v.function; },
                        TE_FUNCTION3 => { s.s_type = v.v_type; s.function = v.function; },
                        TE_FUNCTION4 => { s.s_type = v.v_type; s.function = v.function; },
                        TE_FUNCTION5 => { s.s_type = v.v_type; s.function = v.function; },
                        TE_FUNCTION6 => { s.s_type = v.v_type; s.function = v.function; },
                        TE_FUNCTION7 => { s.s_type = v.v_type; s.function = v.function; },
                        _ => {}
                    }
                }
                else {
                    s.s_type = TOK_ERROR;
                }
            } else {
                // look for an operator or special character
                match s.next.as_bytes()[s.n_idx] as char {
                    '+' => { s.s_type = TOK_INFIX; s.function = add; },
                    '-' => { s.s_type = TOK_INFIX; s.function = sub; },
                    '*' => { s.s_type = TOK_INFIX; s.function = mul; },
                    '/' => { s.s_type = TOK_INFIX; s.function = div; },
                    '^' => { s.s_type = TOK_INFIX; s.function = pow; },
                    '%' => { s.s_type = TOK_INFIX; s.function = fmod; },
                    '(' =>  s.s_type = TOK_OPEN,
                    ')' =>  s.s_type = TOK_CLOSE,
                    ',' =>  s.s_type = TOK_SEP,
                    ' ' | '\t' | '\n' |'\r' => {},
                      _ => s.s_type = TOK_ERROR
                }
                s.n_idx += 1;
            }
        }
    }

    Ok(String::new())
}

fn base(s: &mut State) -> Result<Expr> {
    let mut ret: Expr;

    match type_mask!(s.s_type) {
        TOK_NUMBER => {
            ret = new_expr(TE_CONSTANT, None);
            ret.value = s.value;
            try!(next_token(s));
        },
        TOK_VARIABLE => {
            ret = new_expr(TE_VARIABLE, None);
            ret.bound = s.bound;
            try!(next_token(s));
        },
        TE_FUNCTION0 | TE_CLOSURE0 => {
            ret = new_expr(s.s_type, None);
            ret.function = s.function;
            // todo: set parameters
            /*if is_closure!(s.s_type) {
                ret.parameters[0] = s.context[0].clone();
            }*/
            try!(next_token(s));
            // todo: set parameters
        },
        TE_FUNCTION1 | TE_CLOSURE1 => {
            ret = new_expr(s.s_type, None);
            ret.function = s.function;
            // todo: set parameters
            try!(next_token(s));
            ret.parameters.push(try!(power(s)));
            // todo: set parameters
        },
        TE_FUNCTION2 | TE_CLOSURE2  | TE_FUNCTION3 |
        TE_CLOSURE3  | TE_FUNCTION4 | TE_CLOSURE4  |
        TE_FUNCTION5 | TE_CLOSURE5  | TE_FUNCTION6 |
        TE_CLOSURE6  | TE_FUNCTION7 | TE_CLOSURE7 => {
            let arity = arity!(s.s_type);

            ret = new_expr(s.s_type, None);
            ret.function = s.function;
            // todo: set parameters
            try!(next_token(s));

            if s.s_type != TOK_OPEN {
                s.s_type = TOK_ERROR;
            } else {
                let mut idx = 0;
                for i in 0..arity {
                    try!(next_token(s));
                    ret.parameters.push(try!(expr(s)));
                    if s.s_type != TOK_SEP {
                        break;
                    }
                    idx += 1;
                }
                if s.s_type != TOK_CLOSE || (idx != arity-1) {
                    s.s_type = TOK_ERROR;
                } else {
                    try!(next_token(s));
                }
            }
        },
        TOK_OPEN => {
            try!(next_token(s));
            ret = try!(list(s));
            if s.s_type != TOK_CLOSE {
                s.s_type = TOK_ERROR;
            } else {
                try!(next_token(s));
            }
        }
        _ => {
            // todo: better error? Use NaN?
            ret = new_expr(TE_VARIABLE, None);
            s.s_type = TOK_ERROR;
            ret.value = 0.0;
        }
    }

    Ok(ret)
}

fn power(s: &mut State) -> Result<Expr> {
    let mut sign = 1;

    while s.s_type == TOK_INFIX && (s.function == add || s.function == sub) {
        if s.function == sub { sign = -sign; }
        try!(next_token(s));
    }

    let mut ret: Expr;

    if sign == 1 {
        ret = try!(base(s));
    } else {
        ret = new_expr(TE_FUNCTION1 | TE_FLAG_PURE, Some(vec![try!(base(s)).clone()]));
        ret.function = neg;
    }

    Ok(ret)
}

// todo: ifdef TE_POW_FROM_RIGHT
fn factor(s: &mut State) -> Result<Expr> {
    let mut ret = try!(power(s));

    // todo: check functions here
    while s.s_type == TOK_INFIX && s.function == pow {
        let f = s.function;
        try!(next_token(s));
        ret = new_expr(TE_FUNCTION2 | TE_FLAG_PURE, Some(vec![ret.clone(), try!(power(s)).clone()]));
        ret.function = f;
    }
    
    Ok(ret)
}

fn term(s: &mut State) -> Result<Expr> {
    let mut ret = try!(factor(s));

    while s.s_type == TOK_INFIX && (s.function == mul || s.function == div || s.function == fmod) {
        let f = s.function;
        try!(next_token(s));
        ret = new_expr(TE_FUNCTION2 | TE_FLAG_PURE, Some(vec![ret.clone(), try!(factor(s)).clone()]));
        ret.function = f;
    }
    
    Ok(ret)
}

fn expr(s: &mut State) -> Result<Expr> {
    let mut ret = try!(term(s));

    while s.s_type == TOK_INFIX && (s.function == add || s.function == sub) {
        let f = s.function;
        try!(next_token(s));
        ret = new_expr(TE_FUNCTION2 | TE_FLAG_PURE, Some(vec![ret.clone(), try!(term(s)).clone()]));
        ret.function = f;
    }
    
    Ok(ret)
}

fn list(s: &mut State) -> Result<Expr> {
    let mut ret = try!(expr(s));

    while s.s_type == TOK_SEP {
        try!(next_token(s));
        ret = new_expr(TE_FUNCTION2 | TE_FLAG_PURE, Some(vec![ret.clone(), try!(expr(s)).clone()]));
        ret.function = comma;
    }
    
    Ok(ret)
}

fn optimize(n: &mut Expr) {
    // evaluates as much as possible
    if n.e_type == TE_CONSTANT { return; }
    if n.e_type == TE_VARIABLE { return; }

    if (n.e_type & TE_FLAG_PURE).bits() != 0  {
        let mut known = 1;
        let arity = arity!(n.e_type);
        
        for i in 0..arity {
            // todo: optimize parameters
        }

        if known != 0 {
            n.value = eval(&n);
            n.e_type = TE_CONSTANT;
        }
    }
}

fn compile(expression: &str, variables: Option<Vec<Variable>> ) -> Result<Option<Expr>> {
    let mut s = State::new(expression);
    if let Some(vars) = variables {
        s.lookup = vars;
    }

    arity!(s.s_type);
    try!(next_token(&mut s));
    let mut root = try!(list(&mut s));

    if s.s_type != TOK_END {
        return Ok(None)
    }

    optimize(&mut root);
    Ok(Some(root))
}

/// Interprets a string expression as a mathematical expresion, evaluates it and returns its result.
///
/// # Examples
///
/// ```
/// extern crate tinyexpr;
///
/// // "result" should contain a "4"
/// let result = tinyexpr::interp("2+2").unwrap();
/// ```
pub fn interp(expression: &str) -> Result<f64> {
    match compile(expression, None) {
        Ok(Some(expr)) => Ok(eval(&expr)),
        Err(e)         => Err(e),
        _              => Err(error::TinyExprError::Other(String::from("NaN")))
    }
}

// todo
fn eval(n: &Expr) -> f64 {
    match type_mask!(n.e_type) {
        TE_CONSTANT => n.value,
        TE_VARIABLE => n.bound as f64,
        TE_FUNCTION0 | TE_FUNCTION1 | TE_FUNCTION2 | TE_FUNCTION3 |
        TE_FUNCTION4 | TE_FUNCTION5 | TE_FUNCTION6 | TE_FUNCTION7 => {
            match arity!(n.e_type) {
                // todo: REALLY need more function pointer types to avoid hacks like this 0.0 here...
                0 => ((*n).function)(0.0, 0.0),
                1 => ((*n).function)(eval(&n.parameters[0]), 0.0),
                2 => ((*n).function)(eval(&n.parameters[0]), eval(&n.parameters[1])),
                _ => panic!("todo: add more f. pointers (type is {})", arity!(n.e_type))
            }
        }
        _ => 0.0
    }
}