/*
 * Expression parser/evaluator
 * Copyright (C) 2000 Andrew Hunter
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 */

#ifndef __parse_h
#define __parse_h

#include "number.h"

/*                          ----=====88888=====----                          */

/* Lexer */

typedef enum
{
  Error = 0,
  Number,
  Operator,
  OpenBracket,
  CloseBracket,
  Function,
  Variable,
  Eol,
  Point = 100
} LexerType;

typedef struct LexerData
{
  LexerType type;
  
  union
  {
    double number;
    Char   operator;
    Char*  funcname;
    Char*  varname;

    union DoubleType d;
  } data;
  UInt16 strlen;
} LexerData;

typedef struct {
  UInt16     nextPos;
  Char*      string;

  MemHandle         storeHandle;
  struct ParseTree* store;
  UInt16            storeSize;
  UInt16            storePos;
  
  struct ParseTree* result;
} ParserData;

#ifndef __grammar_h
#include "grammar.h"

extern int Lexer(YYSTYPE* data, ParserData* pdata);
#endif

/*                          ----=====88888=====----                          */

/* Parser */

typedef struct ParseTree
{
  LexerData nodeData;

  struct ParseTree* left;
  struct ParseTree* right;
} ParseTree;

extern void CPerror(Char* s);

extern ParseTree* Parser       (Char* string);
extern Char*      ParseError   (void);
extern void       ParseFinished(void);
extern double     Evaluate     (ParseTree* expression);
extern Char*      EvaluateError(void);

#endif

