1 /*$Id: m_expression.h,v 26.138 2013/04/24 02:32:27 al Exp $ -*- C++ -*- 2 * Copyright (C) 2003 Albert Davis 3 * Author: Albert Davis <aldavis@gnu.org> 4 * 5 * This file is part of "Gnucap", the Gnu Circuit Analysis Package 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 3, or (at your option) 10 * any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 20 * 02110-1301, USA. 21 *------------------------------------------------------------------ 22 */ 23 //testing=script 2009.08.12 24 #include "m_base.h" 25 /*--------------------------------------------------------------------------*/ 26 class Symbol_Table; 27 class CARD_LIST; 28 class Expression; 29 /*--------------------------------------------------------------------------*/ 30 class Token 31 :public Base 32 { 33 private: 34 std::string _name; 35 const Base* _data; 36 std::string _aRgs; 37 public: parse(CS &)38 void parse(CS&) {unreachable();} 39 public: 40 void dump(std::ostream&)const; 41 protected: Token(const std::string Name,const Base * Data,const std::string Args)42 explicit Token(const std::string Name, const Base* Data, const std::string Args) 43 : _name(Name), _data(Data), _aRgs(Args) {} Token(const Token & P)44 explicit Token(const Token& P) 45 : Base(), _name(P._name), _data(P._data), _aRgs(P._aRgs) {assert(!_data);} 46 public: ~Token()47 virtual ~Token() {if (_data) {delete _data;}else{}} 48 virtual Token* clone()const = 0; name()49 const std::string& name()const {return _name;} data()50 const Base* data()const {return _data;} aRgs()51 const std::string& aRgs()const {return _aRgs;} full_name()52 const std::string full_name()const {return name() + aRgs();} stack_op(Expression *)53 virtual void stack_op(Expression*)const {unreachable();} 54 bool operator==(const Token& P) {return (typeid(*this)==typeid(P)) 55 && (data()==P.data()) && (name()==P.name()) && (aRgs()==P.aRgs());} 56 }; 57 /*--------------------------------------------------------------------------*/ 58 class Token_SYMBOL : public Token 59 { 60 public: Token_SYMBOL(const std::string Name,const std::string Args)61 explicit Token_SYMBOL(const std::string Name, const std::string Args) 62 : Token(Name, NULL, Args) {} Token_SYMBOL(const Token_SYMBOL & P)63 explicit Token_SYMBOL(const Token_SYMBOL& P) : Token(P) {untested();} clone()64 Token* clone()const {untested();return new Token_SYMBOL(*this);} 65 void stack_op(Expression*)const; 66 }; 67 /*--------------------------------------------------------------------------*/ 68 class Token_BINOP : public Token 69 { 70 public: Token_BINOP(const std::string Name)71 explicit Token_BINOP(const std::string Name) 72 : Token(Name, NULL, "") {} Token_BINOP(const Token_BINOP & P)73 explicit Token_BINOP(const Token_BINOP& P) : Token(P) {} clone()74 Token* clone()const {return new Token_BINOP(*this);} 75 Token* op(const Token* t1, const Token* t2)const; 76 void stack_op(Expression*)const; 77 }; 78 /*--------------------------------------------------------------------------*/ 79 class Token_STOP : public Token 80 { 81 public: Token_STOP(const std::string Name)82 explicit Token_STOP(const std::string Name) 83 : Token(Name, NULL, "") {} Token_STOP(const Token_STOP & P)84 explicit Token_STOP(const Token_STOP& P) : Token(P) {} clone()85 Token* clone()const {return new Token_STOP(*this);} 86 void stack_op(Expression*)const; 87 }; 88 /*--------------------------------------------------------------------------*/ 89 class Token_PARLIST : public Token 90 { 91 public: Token_PARLIST(const std::string Name)92 explicit Token_PARLIST(const std::string Name) 93 : Token(Name, NULL, "") {} Token_PARLIST(const Token_PARLIST & P)94 explicit Token_PARLIST(const Token_PARLIST& P) : Token(P) {untested();} clone()95 Token* clone()const {untested();return new Token_PARLIST(*this);} 96 void stack_op(Expression*)const; 97 }; 98 /*--------------------------------------------------------------------------*/ 99 class Token_UNARY : public Token 100 { 101 public: Token_UNARY(const std::string Name)102 explicit Token_UNARY(const std::string Name) 103 : Token(Name, NULL, "") {} Token_UNARY(const Token_UNARY & P)104 explicit Token_UNARY(const Token_UNARY& P) : Token(P) {untested();} clone()105 Token* clone()const {untested();return new Token_UNARY(*this);} 106 Token* op(const Token* t1)const; 107 void stack_op(Expression*)const; 108 }; 109 /*--------------------------------------------------------------------------*/ 110 class Token_CONSTANT : public Token 111 { 112 public: Token_CONSTANT(const std::string Name,const Base * Data,const std::string Args)113 explicit Token_CONSTANT(const std::string Name, const Base* Data, const std::string Args) 114 : Token(Name, Data, Args) {} Token_CONSTANT(const Token_CONSTANT & P)115 explicit Token_CONSTANT(const Token_CONSTANT& P) : Token(P) {untested();} clone()116 Token* clone()const {untested();return new Token_CONSTANT(*this);} 117 void stack_op(Expression*)const; 118 }; 119 /*--------------------------------------------------------------------------*/ 120 class INTERFACE Expression 121 :public List_Base<Token> 122 { 123 public: 124 const CARD_LIST* _scope; 125 public: 126 void parse(CS&); 127 void dump(std::ostream&)const; 128 private: // expression-in.cc 129 void arglisttail(CS& File); 130 void arglist(CS& File); 131 void leaf(CS& File); 132 void factor(CS& File); 133 void termtail(CS& File); 134 void term(CS& File); 135 void addexptail(CS& File); 136 void addexp(CS& File); 137 void logicaltail(CS& File); 138 void logical(CS& File); 139 void andtail(CS& File); 140 void andarg(CS& File); 141 void exptail(CS& File); 142 void expression(CS& File); 143 public: Expression()144 explicit Expression() : _scope(NULL) {untested();} Expression(CS & File)145 explicit Expression(CS& File) : _scope(NULL) {parse(File);} 146 private: // expression-reduce.cc 147 void reduce_copy(const Expression&); 148 public: 149 explicit Expression(const Expression&, const CARD_LIST*); 150 public: // other as_bool()151 bool as_bool()const {untested();return (!is_empty() && back()->data());} eval()152 double eval()const { 153 const Float* f = dynamic_cast<const Float*>(back()->data()); 154 return ((f && size()==1) ? (f->value()) : (NOT_INPUT)); 155 } 156 }; 157 /*--------------------------------------------------------------------------*/ 158 /*--------------------------------------------------------------------------*/ 159