00001 00002 /****************************************************************************** 00003 * MODULE : generic_function.hpp 00004 * DESCRIPTION: Generic functions for interpreter 00005 * COPYRIGHT : (C) 2010 Gregoire Lecerf 00006 ******************************************************************************* 00007 * This software falls under the GNU general public license and comes WITHOUT 00008 * ANY WARRANTY WHATSOEVER. See the file $TEXMACS_PATH/LICENSE for more details. 00009 * If you don't have this file, write to the Free Software Foundation, Inc., 00010 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00011 ******************************************************************************/ 00012 00013 #ifndef __MMX__GENERIC_FUNCTION__HPP 00014 #define __MMX__GENERIC_FUNCTION__HPP 00015 #include <basix/syntactic.hpp> 00016 #include <basix/vector.hpp> 00017 #include <basix/wrap.hpp> 00018 00019 namespace mmx { 00020 00021 /****************************************************************************** 00022 * Generic functions 00023 ******************************************************************************/ 00024 00025 struct generic_function; 00026 00027 struct generic_function_rep: public rep_struct { 00028 inline generic_function_rep () {} 00029 inline virtual ~generic_function_rep () {} 00030 inline generic_function me () const; 00031 virtual syntactic Flatten () const = 0; 00032 virtual vector<nat> Signature () const = 0; 00033 virtual generic Quote_apply (const vector<generic>& args) const = 0; 00034 }; 00035 00036 struct generic_function { 00037 INDIRECT_PROTO (generic_function, generic_function_rep) 00038 inline void* operator * () const { return (void*) rep; } 00039 inline generic_function (); 00040 }; 00041 00042 inline generic_function 00043 generic_function_rep::me () const { 00044 return generic_function (this, true); } 00045 00046 WRAP_INDIRECT_IMPL(inline,generic_function) 00047 INDIRECT_IMPL (generic_function, generic_function_rep) 00048 00049 inline syntactic flatten (const generic_function& fun) { 00050 return fun -> Flatten (); } 00051 00052 inline vector<nat> signature (const generic_function& fun) { 00053 return fun -> Signature (); } 00054 00055 /****************************************************************************** 00056 * Default constructor: nil function 00057 ******************************************************************************/ 00058 00059 struct nil_function_rep: generic_function_rep { 00060 inline nil_function_rep () {} 00061 inline syntactic Flatten () const { return syntactic ("nil_function"); } 00062 inline vector<nat> Signature () const { return vector<nat> (); } 00063 virtual generic Quote_apply (const vector<generic>& args) const { 00064 mmerr << "Quote_apply called on nil function with arguments " 00065 << args << "\n"; 00066 ERROR ("Quote_apply not implemented for nil functions"); 00067 return generic (); } 00068 }; 00069 00070 generic_function::generic_function () : 00071 rep ((generic_function_rep*) new nil_function_rep ()) {} 00072 00073 inline bool is_nil (const generic_function& f) { 00074 return N (f -> Signature ()) == 0; } 00075 00076 } // namespace mmx 00077 #endif // __MMX__GENERIC_FUNCTION__HPP