expr.h

00001 /* expr.h -- Expression Tree
00002    Copyright 2003 Free Software Foundation, Inc.
00003    Written by Stephane Carrez (stcarrez@nerim.fr)
00004 
00005 This file is part of GEL.
00006 
00007 GEL is free software; you can redistribute it and/or modify
00008 it under the terms of the GNU General Public License as published by
00009 the Free Software Foundation; either version 2, or (at your option)
00010 any later version.
00011 
00012 GEL is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with GEL; see the file COPYING.  If not, write to
00019 the Free Software Foundation, 59 Temple Place - Suite 330,
00020 Boston, MA 02111-1307, USA.  */
00021 
00022 #ifndef _GEL_INTERPRETOR_EXPR_H
00023 #define _GEL_INTERPRETOR_EXPR_H
00024 
00025 #include "config.h"
00026 
00027 #undef far
00028 #undef bank
00029 
00030 // Expression Tree implementation is put in memory bank1.
00031 // - The 'bank' attribute is used on all methods
00032 // - The 'far' attribute is only used on public entry points.
00033 #define bank SECT_ATTR(".text.bank1")
00034 #define far FAR_ATTR(".text.bank1")
00035 
00036 class Variable;
00037 typedef long Value;
00038 
00042 class Expression {
00043 protected:
00044   enum token
00045     {
00046       T_NAME,
00047       T_DIGIT,
00048       T_MINUS,
00049       T_PLUS,
00050       T_AND,
00051       T_OR,
00052       T_XOR,
00053       T_MUL,
00054       T_DIV,
00055       T_NOT,
00056       T_PARENT_OPEN,
00057       T_PARENT_CLOSE,
00058       T_UNKNOWN,
00059       T_EOF
00060     };
00061   enum token type_;
00062 
00063   static bank enum token get_token (const char*& line);
00064   static bank void rewind (enum token t, const char*& line);
00065 
00066   static bank Expression* parse_unary (const char*& line);
00067   static bank Expression* parse_binary (const char*& line, unsigned char prio);
00068   static bank Expression* parse_term (const char*& line);
00069   static bank int get_value(const char*& line, Value& value);
00070   static bank unsigned char get_priority(enum token t);
00071   
00072   virtual far Value eval();
00073 
00074 public:
00075    Expression(enum token t) {
00076      type_ = t;
00077    }
00078    virtual ~Expression();
00079 
00080    far Value evaluate();
00081 
00082    static far Expression* parse(const char*& line);
00083 };
00084 
00086 class BinaryExpression : public Expression {
00087 protected:
00088    Expression* left_;
00089    Expression* right_;
00090 
00091    virtual far Value eval();
00092 
00093    virtual ~BinaryExpression()
00094      {
00095        delete left_;
00096        delete right_;
00097      }
00098 
00099 public:
00100    BinaryExpression(enum token t, Expression* l, Expression* r)
00101      : Expression(t), left_(l), right_(r)
00102      {
00103      }
00104 };
00105 
00107 class UnaryExpression : public Expression {
00108 protected:
00109   Expression* expr_;
00110   
00111   virtual far Value eval();
00112   
00113   virtual ~UnaryExpression()
00114     {
00115       delete expr_;
00116     }
00117 
00118 public:
00119   UnaryExpression(enum token t, Expression* l)
00120     : Expression(t), expr_(l)
00121     {
00122     }
00123 };
00124 
00126 class ValueExpression : public Expression {
00127   long value_;
00128 
00129   virtual far Value eval();
00130 public:
00131   ValueExpression(long value) : Expression(T_DIGIT) 
00132     {
00133       value_ = value;
00134     }
00135 };
00136 
00137 #endif