/*
 * Copyright (c) 2011, Robert E. Cousins
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the <organization> nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * $Id: main.c,v 1.3 2011/09/06 22:09:03 rec Exp $
 */

#include <stdio.h>
#include <stdlib.h>
#include <err.h>
#include <string.h>		/* for strchr(3) */
#include "exp.h"

/*
 * An example of a unary operator. THis one converts a value
 * to a larger value by multiplying by 1024. This allows the
 * short cut of writing 10K instead of 10240. One could
 * add M, G, T, P, etc. if desired.
 */
void my_k()
{
  double a, b;

  a = exp_num_pop();		/* get the argument */
  b = a * 1024.0;		/* perform the calcuation */
  exp_num_push(b);		/* return the value */
}
/*
 * An example of a unary operator to calculate factoral.
 */
void my_factorial()
{
  double a, b, c;

  a = exp_num_pop();
  b = 1.0; c = 2.0;
  while (c <= a) {
    b *= c;
    c += 1;
  }
  exp_num_push(b);
}
/*
 * A user added function. Note it takes one argument and returns
 * one value. Functions may take 0 to 3 arguments and must return
 * exactly one value.
 */
double my_percent(double val)
{
  return val * 0.01;
}
/*
 * A user added function which requires two arguments.
 * 
 * Note: If you divide by zero, then you may have problems. :-)
 */
double my_ratio(double v1, double v2)
{
  return v1 / v2;
}

int 
main()
{
  double t, var, *varp;
  char buf[100], *q;

  /*
   * Step 1: Initialize the library
   */
  exp_init();
  /*
   * Step 2: Add user variables, functions, and operators
   */
  exp_add_operator("K",1,4,0,my_k);
  exp_add_operator("!",1,4,0,my_factorial);
  exp_add_function("percentage", 1, my_percent);
  exp_add_function("ratio", 2, my_ratio);
  exp_add_variable("PI",3.1415);
  var = 10.0;
  exp_add_variable("var",var);
  //  exp_dump_symboltable();
  /*
   * Step 3: Iteratively execute some expressions from
   * the user.
   */

  while (1) {
    printf("---> ");
    q = fgets(buf, sizeof(buf), stdin);
    if (q == NULL) break;
    if (q = strchr(buf,'\n')) 
      *q = '\0';
    t = exp_eval(buf);
    printf("Evaluating '%s' = %g\n", buf, t);
    
    exp_fetch_variable("var",&var);
    exp_update_variable("var",1.0 + var);
  }
  /*
   * Step 4: Free the memory.
   */
  exp_shutdown();
  printf("End-of-run\n");
  return 0;
}
