1 /*
2     This file is part of GNU APL, a free implementation of the
3     ISO/IEC Standard 13751, "Programming Language APL, Extended"
4 
5     Copyright (C) 2008-2015  Dr. Jürgen Sauermann
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 of the License, or
10     (at your option) 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, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #ifndef __NAMED_OBJECT_HH_DEFINED__
22 #define __NAMED_OBJECT_HH_DEFINED__
23 
24 #include "Id.hh"
25 #include "Value.hh"
26 
27 class Function;
28 class Symbol;
29 class UCS_string;
30 class Value;
31 
32 //-----------------------------------------------------------------------------
33 /// The possible values returned by \b ⎕NC.
34 enum NameClass
35 {
36   NC_INVALID          = -1,   ///< invalid name class.
37   NC_UNUSED_USER_NAME =  0,   ///< unused user name eg. pushed but not assigned
38   NC_LABEL            =  1,   ///< Label.
39   NC_VARIABLE         =  2,   ///< (assigned) variable.
40   NC_FUNCTION         =  3,   ///< (user defined) function.
41   NC_OPERATOR         =  4,   ///< (user defined) operator.
42   NC_SHARED_VAR       =  5,   ///< shared variable.
43 };
44 //-----------------------------------------------------------------------------
45 /**
46  A named object is something with a name.
47  The name can be user defined or system defined.
48 
49   User define names are handled by class Symbol, while
50    system defined names are handled by class Id.
51 
52   User define names are used for (user-defined) variables, functions,
53   or operators.
54 
55    System names are used by system variables and functions (⎕xx) and
56    for primitive functions and operators.
57  **/
58 /// A user-defined variable or function
59 class NamedObject
60 {
61 public:
62    /// constructor from Id
NamedObject(Id i)63    NamedObject(Id i)
64    : id(i)
65    {}
66 
67    /// return the name of the named object
get_name() const68    virtual UCS_string get_name() const
69       { return ID::get_name_UCS(id); }
70 
71    /// return the function for this Id (if any) or 0 if this Id does
72    /// (currently) represent a function.
get_function() const73    virtual const Function * get_function() const  { return 0; }
74 
75    /// return the variable value for this Id (if any) or 0 if this Id does
76    /// not (currently) represent a variable.
get_value()77    virtual Value_P get_value()     { return Value_P(); }
78 
79    /// return the symbol for this user defined symbol (if any) or 0 if this Id
80    /// refers to a system name
get_symbol()81    virtual Symbol * get_symbol()   { return 0; }
82 
83    /// return the symbol for this user defined symbol (if any) or 0 if this Id
84    /// refers to a system name
get_symbol() const85    virtual const Symbol * get_symbol() const  { return 0; }
86 
87    /// return the Id of this object (ID_USER_SYMBOL for user defined objects)
get_Id() const88    Id get_Id() const
89       { return id; }
90 
91    /// return true, iff this object is user-defined
is_user_defined() const92    bool is_user_defined() const
93       { return id == ID_USER_SYMBOL; }
94 
95    /// Get current \b NameClass of \b this name.
96    NameClass get_nc() const;
97 
98    /// the object's id
99    const Id id;
100 };
101 
102 #endif // __NAMED_OBJECT_HH_DEFINED__
103