Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Examples

YCode.h

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                                                                      |
00003 |                      __   __    ____ _____ ____                      |
00004 |                      \ \ / /_ _/ ___|_   _|___ \                     |
00005 |                       \ V / _` \___ \ | |   __) |                    |
00006 |                        | | (_| |___) || |  / __/                     |
00007 |                        |_|\__,_|____/ |_| |_____|                    |
00008 |                                                                      |
00009 |                               core system                            |
00010 |                                                        (C) SuSE GmbH |
00011 \----------------------------------------------------------------------/
00012 
00013    File:        YCode.h
00014 
00015    Author:      Klaus Kaempf <kkaempf@suse.de>
00016    Maintainer:  Klaus Kaempf <kkaempf@suse.de>
00017 
00018 /-*/
00019 // -*- c++ -*-
00020 
00021 #ifndef YCode_h
00022 #define YCode_h
00023 
00024 #include <string>
00025 using std::string;
00026 
00027 // MemUsage.h defines/undefines D_MEMUSAGE
00028 #include <y2util/MemUsage.h>
00029 #include "ycp/YCodePtr.h"
00030 
00031 #include "ycp/YCPValue.h"
00032 #include "ycp/YCPString.h"
00033 #include "ycp/Type.h"
00034 #include "ycp/SymbolEntry.h"
00035 
00043 struct ycodelist {
00044     struct ycodelist *next;
00045     YCodePtr code;
00046 };
00047 typedef struct ycodelist ycodelist_t;
00048 
00052 class YCode : public Rep
00053 #ifdef D_MEMUSAGE
00054   , public MemUsage
00055 #endif
00056 {
00057     REP_BODY(YCode);
00058 public:
00059     enum ykind {
00060         yxError = 0,
00061         // [1] Constants        (-> YCPValue, except(!) term -> yeLocale)
00062         ycVoid, ycBoolean, ycInteger, ycFloat,  // constants
00063         ycString, ycByteblock, ycPath, ycSymbol,
00064         ycList,                                 // list
00065         ycMap,                                  // map
00066         ycTerm,                                 // term
00067 
00068         ycEntry,
00069 
00070         ycConstant,             // -- placeholder --
00071         ycLocale,                               // locale constant (gettext)
00072         ycFunction,                             // a function definition (parameters and body)
00073 
00074         // [16] Expressions     (-> declaration_t + values)
00075         yePropagate,            // type propagation (value, type)
00076         yeUnary,                // unary (prefix) operator
00077         yeBinary,               // binary (infix) operator
00078         yeTriple,               // <exp> ? <exp> : <exp>
00079         yeCompare,              // compare
00080 
00081         // [21] Value expressions (-> values + internal)
00082         yeLocale,               // locale expression (ngettext)
00083         yeList,                 // list expression
00084         yeMap,                  // map expression
00085         yeTerm,                 // <name> ( ...)
00086         yeIs,                   // is()
00087         yeBracket,              // <name> [ <expr>, ... ] : <expr>
00088 
00089         // [27] Block (-> linked list of statements)
00090         yeBlock,                // block expression
00091         yeReturn,               // quoted expression, e.g. "``(<exp>)" which really is "{ return <exp>; }"
00092 
00093         // [29] Symbolref (-> SymbolEntry)
00094         yeVariable,             // variable ref
00095         yeBuiltin,              // builtin ref + args
00096         yeFunction,             // function ref + args
00097         yeReference,            // reference to a variable (identical to yeVariable but with different semantics)
00098 
00099         yeExpression,           // -- placeholder --
00100 
00101         // [34] Statements      (-> YCode + next)
00102         ysTypedef,              // typedef
00103         ysVariable,             // variable defintion (-> YSAssign)
00104         ysFunction,             // function definition
00105         ysAssign,               // variable assignment (-> YSAssign)
00106         ysBracket,              // <name> [ <expr>, ... ] = <expr>
00107         ysIf,                   // if, then, else
00108         ysWhile,                // while () do ...
00109         ysDo,                   // do ... while ()
00110         ysRepeat,               // repeat ... until ()
00111         ysExpression,           //  any expression (function call)
00112         ysReturn,               // return
00113         ysBreak,                // break
00114         ysContinue,             // continue
00115         ysTextdomain,           // textdomain
00116         ysInclude,              // include
00117         ysFilename,             //  restore filename after include
00118         ysImport,               // import
00119         ysBlock,                // [51] a block as statement
00120         ysStatement             // -- placeholder --
00121     };
00122 
00123 protected:
00124     ykind m_kind;
00125     bool m_valid;
00126 
00127 public:
00128 
00132     YCode (ykind kind);
00133 
00137     virtual ~YCode();
00138 
00142     ykind kind() const;
00143    
00147     bool valid() const;
00148 
00152     virtual string toString() const;
00153     static string toString(ykind kind);
00154 
00159     virtual std::ostream & toStream (std::ostream & str) const = 0;
00160 
00164     bool isConstant () const;
00165 
00169     bool isError () const;
00170 
00174     bool isStatement () const;
00175 
00179     bool isBlock () const;
00180 
00184     bool isReferenceable () const;
00185 
00193     virtual YCPValue evaluate (bool cse = false);
00194 
00198   virtual constTypePtr type() const;
00199 };
00200 
00201 
00208 class YError : public YCode
00209 {
00210     REP_BODY(YError);
00211     int m_line;
00212     const char *m_msg;
00213 public:
00214     YError (int line=0, const char *msg=0);
00215     ~YError () {}
00216     YCPValue evaluate (bool cse = false);
00217     string toString() const;
00218     std::ostream & toStream (std::ostream & str) const;
00219     constTypePtr type() const { return Type::Error; }
00220 };
00221 
00226 class YConst : public YCode
00227 {
00228     REP_BODY(YConst);
00229     YCPValue m_value;           // constant (not allowed in union :-( )
00230 public:
00231     YConst (ykind kind, YCPValue value);                // Constant
00232     YConst (ykind kind, std::istream & str);
00233     ~YConst () {};
00234     YCPValue value() const;
00235     string toString() const;
00236     std::ostream & toStream (std::ostream & str) const;
00237     YCPValue evaluate (bool cse = false);
00238     constTypePtr type() const;
00239 };
00240 
00241 #include <ext/hash_map>
00242 #include <string>
00243 #include <cstddef>
00244 
00251 class YELocale;
00252 
00253 class YLocale : public YCode
00254 {
00255     REP_BODY(YLocale);
00256     const char *m_locale;               // the string to be translated
00257 
00258     struct eqstr
00259     {
00260         bool operator()(const char* s1, const char* s2) const
00261         {
00262             return strcmp(s1, s2) == 0;
00263         }
00264     };
00265 
00266 public:
00267     typedef __gnu_cxx::hash_map<const char*, bool, __gnu_cxx::hash<const char*>, eqstr> t_uniquedomains;
00268 
00269     static t_uniquedomains domains;     // keep every textdomain only once
00270     static t_uniquedomains::const_iterator setDomainStatus (const string& domain, bool status);
00271     static void ensureBindDomain (const string& domain);
00272 
00273     YLocale (const char *locale, const char *textdomain);
00274     YLocale (std::istream & str);
00275     ~YLocale ();
00276     const char *value () const;
00277     const char *domain () const;
00278     string toString() const;
00279     std::ostream & toStream (std::ostream & str) const;
00280     YCPValue evaluate (bool cse = false);
00281     constTypePtr type() const { return Type::Locale; }
00282     
00283 private:
00284 
00285     t_uniquedomains::const_iterator m_domain;
00286 
00287 };
00288 
00293 class YDeclaration : public YCode
00294 {
00295     REP_BODY(YDeclaration);
00296     declaration_t *m_value;     // builtin declaration
00297 public:
00298     YDeclaration (ykind kind, declaration_t *value);    // Builtin decl
00299     YDeclaration (std::istream & str);
00300     ~YDeclaration () {};
00301     declaration_t *value() const;
00302     string toString() const;
00303     std::ostream & toStream (std::ostream & str) const;
00304     YCPValue evaluate (bool cse = false);
00305     constTypePtr type() const { return m_value->type; }
00306 };
00307 
00314 class YFunction : public YCode
00315 {
00316     REP_BODY(YFunction);
00317     // array of formal arguments of a function
00318     // the formal parameters must be available in the local scope during parse
00319     // of the function body (startDefinition()) and removed afterwards (endDefintion()).
00320     // Keep track of the table entries in this block.
00321     //
00322     // When calling a function during execution, the actual
00323     // arguments (values) are bound to the formal arguments
00324     // (this array) so the function body can be evaluated.
00325     // @see YEFunction::attachActualParameter()
00326     //
00327     // if NULL, it's a (void) function
00328     YBlockPtr m_declaration;
00329 
00330     // the function definition ('body') is the block defining this function
00331     YBlockPtr m_definition;
00332 
00333     bool m_is_global;
00334 
00335 public:
00336     YFunction (YBlockPtr parameterblock, const SymbolEntryPtr entry = 0);
00337     YFunction (std::istream & str);
00338     ~YFunction ();
00339 
00340     // access to formal parameters
00341     unsigned int parameterCount () const;
00342     YBlockPtr declaration () const;
00343     SymbolEntryPtr parameter (unsigned int position) const;
00344 
00345     // access to definition block (= 0 if declaration only)
00346     YBlockPtr definition () const;
00347     void setDefinition (YBlockPtr body);
00348     // read definition from stream
00349     void setDefinition (std::istream & str);
00350 
00351     string toStringDeclaration () const;
00352     string toString () const;
00353     std::ostream & toStreamDefinition (std::ostream & str) const;
00354     std::ostream & toStream (std::ostream & str) const;
00355     virtual YCPValue evaluate (bool cse = false);
00356     constTypePtr type() const;
00357 };
00358 
00359 
00360 #endif   // YCode_h

Generated on Fri Nov 9 18:15:22 2007 for yast2-core by doxygen 1.3.6