]> www.ginac.de Git - ginac.git/blob - ginac/parser/default_reader.tpl
Allow user defined functions to be parsed.
[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 const prototype_table& get_default_reader()
35 {
36         using std::make_pair;
37         static bool initialized = false;
38         static prototype_table reader;
39         if (!initialized) {
40                 try {
41                         for ( unsigned ser=0; ; ++ser ) {
42                                 GiNaC::function f(ser);
43                                 std::string name = f.get_name();
44                                 for ( std::size_t nargs=0; ; ++nargs ) {
45                                         try {
46                                                 function::find_function(name, nargs);
47                                                 prototype proto = std::pair<std::string, std::size_t>(name, nargs);
48                                                 std::pair<prototype_table::iterator, bool> ins = reader.insert(std::pair<prototype,reader_func>(proto, (reader_func)ser));
49                                                 if ( ins.second ) break;
50                                         }
51                                         catch ( std::runtime_error ) { }
52                                 }
53                         }
54                 }
55                 catch ( std::runtime_error ) { }
56                 initialized = true;
57         }
58         return reader;
59 }
60 } // namespace GiNaC
61