]> www.ginac.de Git - ginac.git/blob - ginac/parser/default_reader.tpl
e65802a439c6b866a79103c3e6dfd77e398da512
[ginac.git] / ginac / parser / default_reader.tpl
1 [+ AutoGen5 template .cpp +][+ 
2 COMMENT a part of GiNaC parser -- construct functions from a byte stream.
3 +][+
4 (use-modules (ice-9 format))
5
6 (define (sequence start end . step)
7   (let ((step (if (null? step) 1 (car step))))
8     (let loop ((n start))
9       (if (> n end) '() (cons n (loop (+ step n)))))))
10 +]/*
11 [+ (dne " * " " * " ) +]
12  *
13  * If you want to change this file, edit either `[+ (def-file) +]' or
14  * `[+ (tpl-file) +]' file, and run the following command:
15  *
16  * autogen -T [+ (tpl-file) +] [+ (def-file) +]
17  */
18 #include "parse_context.h"
19 #include "power.h"
20 #include "operators.h"
21 #include "inifcns.h"
22
23 namespace GiNaC
24 {
25 [+ FOR function +]
26 static ex [+ (get "name") +]_reader(const exvector& ev)
27 {
28         return GiNaC::[+ (get "name") +]([+
29                 (let ((nargs (if (exist? "args")
30                                  (string->number (get "args")) 1)))
31                   (format '#f "~{ev[~a]~^, ~}" (sequence 0 (- nargs 1)))) +]);
32 }[+ ENDFOR +]
33
34 // function::registered_functions() is protected, but we need to access it
35 class registered_functions_hack : public function
36 {
37 public:
38         static const std::vector<function_options>& get_registered_functions()
39         {
40                 return function::registered_functions();
41         }
42 private:
43         registered_functions_hack();
44         registered_functions_hack(const registered_functions_hack&);
45         registered_functions_hack& operator=(const registered_functions_hack&);
46 };
47
48 // Encode an integer into a pointer to a function. Since functions
49 // are aligned (the minimal alignment depends on CPU architecture)
50 // we can distinguish between pointers and integers.
51 static reader_func encode_serial_as_reader_func(unsigned serial)
52 {
53         uintptr_t u = (uintptr_t)serial;
54         u = (u << 1) | (uintptr_t)1;
55         reader_func ptr = reinterpret_cast<reader_func>((void *)u);
56         return ptr;
57 }
58
59 const prototype_table& get_default_reader()
60 {
61         using std::make_pair;
62         static bool initialized = false;
63         static prototype_table reader;
64         if (!initialized) {
65                 [+ FOR function +]
66                 reader[make_pair("[+ (get "name") +]", [+ 
67                         (if (exist? "args") (get "args") "1")
68                         +])] = [+ (get "name") +]_reader;[+
69                 ENDFOR +]
70                 std::vector<function_options>::const_iterator it =
71                         registered_functions_hack::get_registered_functions().begin();
72                 std::vector<function_options>::const_iterator end =
73                         registered_functions_hack::get_registered_functions().end();
74                 unsigned serial = 0;
75                 for (; it != end; ++it) {
76                         prototype proto = make_pair(it->get_name(), it->get_nparams());
77                         reader[proto] = encode_serial_as_reader_func(serial);
78                         ++serial;
79                 }
80                 initialized = true;
81         }
82         return reader;
83 }
84
85 const prototype_table& get_builtin_reader()
86 {
87         using std::make_pair;
88         static bool initialized = false;
89         static prototype_table reader;
90         if (!initialized) {
91                 [+ FOR function +]
92                 reader[make_pair("[+ (get "name") +]", [+ 
93                         (if (exist? "args") (get "args") "1")
94                         +])] = [+ (get "name") +]_reader;[+
95                 ENDFOR +]
96                 enum {
97                         log,
98                         exp,
99                         sin,
100                         cos,
101                         tan,
102                         asin,
103                         acos,
104                         atan,
105                         sinh,
106                         cosh,
107                         tanh,
108                         asinh,
109                         acosh,
110                         atanh,
111                         atan2,
112                         Li2,
113                         Li3,
114                         zetaderiv,
115                         Li,
116                         S,
117                         H,
118                         lgamma,
119                         tgamma,
120                         beta,
121                         factorial,
122                         binomial,
123                         Order,
124                         NFUNCTIONS
125                 };
126                 std::vector<function_options>::const_iterator it =
127                         registered_functions_hack::get_registered_functions().begin();
128                 unsigned serial = 0;
129                 for ( ; serial<NFUNCTIONS; ++it, ++serial ) {
130                         prototype proto = make_pair(it->get_name(), it->get_nparams());
131                         reader[proto] = encode_serial_as_reader_func(serial);
132                 }
133                 initialized = true;
134         }
135         return reader;
136 }
137
138 } // namespace GiNaC