[GiNaC-list] Adding custom functions to the parser
Vitaly Magerya
vmagerya at gmail.com
Mon Feb 20 14:02:16 CET 2023
Hi, all. I'm trying to add custom functions to GiNaC::parser in
a way similar to how it's done in default_reader.cpp:
#include <ginac/ginac.h>
#include <ginac/parser.h>
#include <iostream>
#include <sstream>
#include <string>
static GiNaC::ex sqrt_reader(const GiNaC::exvector& ev)
{ return GiNaC::sqrt(ev[0]); }
int main() {
std::string input = "sqrt(4)";
std::istringstream instream(input);
auto proto = GiNaC::prototype_table();
proto[std::make_pair("sqrt", 1)] = sqrt_reader;
auto symtab = GiNaC::symtab();
auto reader = GiNaC::parser(symtab, false, proto);
GiNaC::ex expr = reader(instream);
std::cout << input << " = " << expr << "\n";
}
This unfortunately crashes before entering sqrt_reader. Here's a backtrace:
#0 GiNaC::function::eval (this=0x7fffffffcd10) at function.cpp:1450
#1 0x000000000045c212 in GiNaC::ex::construct_from_basic (other=...)
at ex.cpp:309
#2 0x000000000059e0d5 in GiNaC::ex::ex (other=...,
this=0x7fffffffcc50) at ./ex.h:268
#3 GiNaC::dispatch_reader_fcn (args=std::vector of length 1, capacity
1 = {...}, ptr=<optimized out>) at parser/parser.cpp:62
#4 GiNaC::parser::parse_identifier_expr (this=<optimized out>) at
parser/parser.cpp:103
#5 0x000000000059db7d in GiNaC::parser::parse_primary
(this=this at entry=0x7fffffffd310) at parser/parser.cpp:173
#6 0x000000000059dc3f in GiNaC::parser::parse_expression
(this=this at entry=0x7fffffffd310) at parser/parser.cpp:194
#7 0x000000000059e919 in GiNaC::parser::operator()
(this=0x7fffffffd310, input=...) at parser/parser.cpp:226
#8 0x0000000000423e35 in main () at parsingcrash.cpp:21
Can anyone tell me how to do this correctly?
More information about the GiNaC-list
mailing list