interactive testing
[gnucap-felix.git] / src / m_expression.h
blob3c3859dcc42d6118a83a7eb1b8c09788d1657e9d
1 /*$Id: -*- C++ -*-
2 * Copyright (C) 2003 Albert Davis
3 * Author: Albert Davis <aldavis@gnu.org>
5 * This file is part of "Gnucap", the Gnu Circuit Analysis Package
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.
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.
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 *------------------------------------------------------------------
23 #ifndef EXPR_H__
24 #define EXPR_H__
25 #include "m_base.h"
26 /*--------------------------------------------------------------------------*/
27 //class Symbol_Table;
28 class CARD_LIST;
29 class Expression;
30 /*--------------------------------------------------------------------------*/
31 class Token
32 :public Base
34 private:
35 std::string _name;
36 const Base* _data;
37 std::string _aRgs;
38 public:
39 void parse(CS&) {unreachable();}
40 public:
41 void dump(std::ostream&out)const;
42 void dump(OMSTREAM& o)const { untested();
43 stringstream a;
44 dump(a);
45 o << a.str();
47 protected:
48 explicit Token(const std::string Name, const Base* Data, const std::string Args)
49 : _name(Name), _data(Data), _aRgs(Args) {}
50 explicit Token(const Token& P)
51 : Base(), _name(P._name), _data(P._data), _aRgs(P._aRgs) {assert(!_data);}
52 public:
53 virtual ~Token() {if (_data) {
54 delete _data;}else{
56 virtual Token* clone()const = 0;
57 const std::string& name()const {return _name;}
58 const Base* data()const {return _data;}
59 const std::string& aRgs()const {return _aRgs;}
60 const std::string full_name()const {return name() + aRgs();}
61 template<class S>
62 S& full_dump(S& s)const {return _full_dump(s);}
63 virtual void stack_op(Expression*)const {unreachable();}
64 bool operator==(const Token& P) {return (typeid(*this)==typeid(P))
65 && (data()==P.data()) && (name()==P.name()) && (aRgs()==P.aRgs());}
66 protected:
67 virtual OMSTREAM& _full_dump(OMSTREAM& s)const {return s << full_name();}
68 virtual std::ostream& _full_dump(std::ostream& s)const {untested(); return s << full_name();}
70 /*--------------------------------------------------------------------------*/
71 template<class S>
72 inline S& operator<<(S& out, const Token& d) {d.dump(out); return out;}
73 /*--------------------------------------------------------------------------*/
74 class Token_SYMBOL : public Token
76 public:
77 explicit Token_SYMBOL(const std::string Name, const std::string Args)
78 : Token(Name, NULL, Args) {}
79 explicit Token_SYMBOL(const Token_SYMBOL& P) : Token(P) {untested();}
80 Token* clone()const {untested();return new Token_SYMBOL(*this);}
81 void stack_op(Expression*)const;
83 /*--------------------------------------------------------------------------*/
84 template<class S>
85 inline S& operator<<(S& out, const Token_SYMBOL& d) {untested(); d.dump(out); return out;}
86 /*--------------------------------------------------------------------------*/
87 class Token_BINOP : public Token
89 public:
90 explicit Token_BINOP(const std::string Name)
91 : Token(Name, NULL, "") {}
92 explicit Token_BINOP(const Token_BINOP& P) : Token(P) {}
93 Token* clone()const {return new Token_BINOP(*this);}
94 Token* op(const Token* t1, const Token* t2)const;
95 void stack_op(Expression*)const;
97 class Token_STOP : public Token
99 public:
100 explicit Token_STOP(const std::string Name)
101 : Token(Name, NULL, "") {}
102 explicit Token_STOP(const Token_STOP& P) : Token(P) {}
103 Token* clone()const {return new Token_STOP(*this);}
104 void stack_op(Expression*)const;
106 class Token_PARLIST : public Token
108 public:
109 explicit Token_PARLIST(const std::string Name)
110 : Token(Name, NULL, "") {}
111 explicit Token_PARLIST(const Token_PARLIST& P) : Token(P) {untested();}
112 Token* clone()const {untested();return new Token_PARLIST(*this);}
113 void stack_op(Expression*)const;
115 class Token_UNARY : public Token
117 public:
118 explicit Token_UNARY(const std::string Name)
119 : Token(Name, NULL, "") {}
120 explicit Token_UNARY(const Token_UNARY& P) : Token(P) {untested();}
121 Token* clone()const {untested();return new Token_UNARY(*this);}
122 Token* op(const Token* t1)const;
123 void stack_op(Expression*)const;
125 class Token_CONSTANT : public Token
127 public:
128 explicit Token_CONSTANT(const std::string Name, const Base* Data, const std::string Args)
129 : Token(Name, Data, Args) {}
130 explicit Token_CONSTANT(const Token_CONSTANT& P) : Token(P) {untested();}
131 Token* clone()const {untested();return new Token_CONSTANT(*this);}
132 void stack_op(Expression*)const;
133 protected:
134 std::ostream& _full_dump(std::ostream& s)const {data()->dump(s); return s;}
135 OMSTREAM& _full_dump(OMSTREAM& s)const {data()->dump(s); return s;}
137 /*--------------------------------------------------------------------------*/
138 class INTERFACE Expression
139 :public List_Base<Token>
141 public:
142 const CARD_LIST* _scope;
143 public:
144 void parse(CS&);
145 void dump(std::ostream&out)const {return _dump(out);}
146 void dump(OMSTREAM&out)const {return _dump(out); }
147 private:
148 template<class S> void _dump(S&)const;
149 private: // expression-in.cc
150 void arglisttail(CS& File);
151 void arglist(CS& File);
152 void leaf(CS& File);
153 void factor(CS& File);
154 void factortail(CS& File);
155 void termtail(CS& File);
156 void term(CS& File);
157 void addexptail(CS& File);
158 void addexp(CS& File);
159 void logicaltail(CS& File);
160 void logical(CS& File);
161 void andtail(CS& File);
162 void andarg(CS& File);
163 void exptail(CS& File);
164 void expression(CS& File);
165 public:
166 explicit Expression() : _scope(NULL) {
168 explicit Expression(CS& File) : _scope(NULL) {
169 parse(File);
171 private: // expression-reduce.cc
172 void reduce_copy(const Expression&);
173 public:
174 explicit Expression(const Expression&, const CARD_LIST*);
175 public: // other
176 bool as_bool()const { untested();
177 return (!is_empty() && back()->data());
179 double eval()const {
180 const Float* f = dynamic_cast<const Float*>(back()->data());
181 return ((f && size()==1) ? (f->value()) : (NOT_INPUT));
184 /*--------------------------------------------------------------------------*/
185 template<class S>
186 inline S& operator<<(S& out, const Expression& d) {d.dump(out); return out;}
187 /*--------------------------------------------------------------------------*/
188 #endif
189 // vim:ts=8:sw=2:noet: