8 #include "glps_parser.h"
9 #include "flame/config.h"
11 # define M_PI 3.14159265358979323846
17 int unary_negate(parse_context* ctxt, expr_value_t *R,
const expr_t *
const *A)
19 *R = -boost::get<double>(A[0]->value);
23 int unary_sin(parse_context* ctxt, expr_value_t *R,
const expr_t *
const *A)
25 *R = sin(boost::get<double>(A[0]->value));
28 int unary_cos(parse_context* ctxt, expr_value_t *R,
const expr_t *
const *A)
30 *R = cos(boost::get<double>(A[0]->value));
33 int unary_tan(parse_context* ctxt, expr_value_t *R,
const expr_t *
const *A)
35 *R = tan(boost::get<double>(A[0]->value));
38 int unary_asin(parse_context* ctxt, expr_value_t *R,
const expr_t *
const *A)
40 *R = asin(boost::get<double>(A[0]->value));
43 int unary_acos(parse_context* ctxt, expr_value_t *R,
const expr_t *
const *A)
45 *R = acos(boost::get<double>(A[0]->value));
48 int unary_atan(parse_context* ctxt, expr_value_t *R,
const expr_t *
const *A)
50 *R = atan(boost::get<double>(A[0]->value));
54 int unary_deg2rad(parse_context* ctxt, expr_value_t *R,
const expr_t *
const *A)
56 double val = boost::get<double>(A[0]->value);
61 int unary_rad2deg(parse_context* ctxt, expr_value_t *R,
const expr_t *
const *A)
63 double val = boost::get<double>(A[0]->value);
69 int binary_add(parse_context* ctxt, expr_value_t *R,
const expr_t *
const *A)
71 *R = boost::get<double>(A[0]->value)+boost::get<double>(A[1]->value);
74 int binary_sub(parse_context* ctxt, expr_value_t *R,
const expr_t *
const *A)
76 *R = boost::get<double>(A[0]->value)-boost::get<double>(A[1]->value);
79 int binary_mult(parse_context* ctxt, expr_value_t *R,
const expr_t *
const *A)
81 *R = boost::get<double>(A[0]->value)*boost::get<double>(A[1]->value);
84 int binary_div(parse_context* ctxt, expr_value_t *R,
const expr_t *
const *A)
86 double result = boost::get<double>(A[0]->value)/boost::get<double>(A[1]->value);
87 if(!isfinite(result)) {
88 ctxt->last_error =
"division results in non-finite value";
97 int unary_bl_negate(parse_context* ctxt, expr_value_t *R,
const expr_t *
const *A)
100 const std::vector<std::string>& line = boost::get<std::vector<std::string> >(A[0]->value);
101 std::vector<std::string> ret(line.size());
102 std::copy(line.rbegin(),
109 template<
int MULT,
int LINE>
110 int binary_bl_mult(parse_context* ctxt, expr_value_t *R,
const expr_t *
const *A)
113 assert(A[MULT]->etype==glps_expr_number);
114 assert(A[LINE]->etype==glps_expr_line);
116 double factor = boost::get<double>(A[MULT]->value);
118 if(factor<0.0 || factor>std::numeric_limits<unsigned>::max()) {
119 ctxt->last_error =
"beamline scale by negative value or out of range value";
122 unsigned factori = (unsigned)factor;
124 const std::vector<std::string>& line = boost::get<std::vector<std::string> >(A[LINE]->value);
126 std::vector<std::string> ret(line.size()*factori);
130 std::vector<std::string>::iterator outi = ret.begin();
133 outi = std::copy(line.begin(),
141 int unary_parse(parse_context* ctxt, expr_value_t *R,
const expr_t *
const *A)
143 using namespace boost::filesystem;
144 assert(A[0]->etype==glps_expr_string);
145 path name(canonical(boost::get<std::string>(A[0]->value), ctxt->cwd));
150 boost::shared_ptr<Config> ret(P.
parse_file(name.native().c_str()));
155 int unary_file(parse_context* ctxt, expr_value_t *R,
const expr_t *
const *A)
157 using namespace boost::filesystem;
158 const std::string& inp = boost::get<std::string>(A[0]->value);
159 path ret(canonical(inp, ctxt->cwd));
162 std::ostringstream strm;
163 strm<<
"\""<<ret<<
"\" does not exist";
164 ctxt->last_error = strm.str();
172 int unary_h5file(parse_context* ctxt, expr_value_t *R,
const expr_t *
const *A)
174 using namespace boost::filesystem;
175 const std::string& inp = boost::get<std::string>(A[0]->value);
183 size_t sep = inp.npos;
186 sep = inp.find_last_of(
'/', sep-1);
188 path fname(absolute(inp.substr(0, sep), ctxt->cwd));
191 *R = canonical(fname).native() + inp.substr(sep);
193 }
else if(sep==inp.npos) {
198 std::ostringstream strm;
199 strm<<
"\""<<inp<<
"\" does not exist";
200 ctxt->last_error = strm.str();
206 parse_context::parse_context(
const char *path)
207 :last_line(0), printer(NULL), error_scratch(300), scanner(NULL)
210 cwd = boost::filesystem::canonical(path);
212 cwd = boost::filesystem::current_path();
213 addop(
"-", &unary_negate, glps_expr_number, 1, glps_expr_number);
215 addop(
"sin", &unary_sin, glps_expr_number, 1, glps_expr_number);
216 addop(
"cos", &unary_cos, glps_expr_number, 1, glps_expr_number);
217 addop(
"tan", &unary_tan, glps_expr_number, 1, glps_expr_number);
218 addop(
"asin",&unary_asin,glps_expr_number, 1, glps_expr_number);
219 addop(
"acos",&unary_acos,glps_expr_number, 1, glps_expr_number);
220 addop(
"atan",&unary_atan,glps_expr_number, 1, glps_expr_number);
222 addop(
"arcsin",&unary_asin,glps_expr_number, 1, glps_expr_number);
223 addop(
"arccos",&unary_acos,glps_expr_number, 1, glps_expr_number);
224 addop(
"arctan",&unary_atan,glps_expr_number, 1, glps_expr_number);
226 addop(
"deg2rad",&unary_deg2rad,glps_expr_number, 1, glps_expr_number);
227 addop(
"rad2deg",&unary_rad2deg,glps_expr_number, 1, glps_expr_number);
229 addop(
"+", &binary_add, glps_expr_number, 2, glps_expr_number, glps_expr_number);
230 addop(
"-", &binary_sub, glps_expr_number, 2, glps_expr_number, glps_expr_number);
231 addop(
"*", &binary_mult,glps_expr_number, 2, glps_expr_number, glps_expr_number);
232 addop(
"/", &binary_div, glps_expr_number, 2, glps_expr_number, glps_expr_number);
234 addop(
"-", &unary_bl_negate, glps_expr_line, 1, glps_expr_line);
236 addop(
"*", &binary_bl_mult<0,1>, glps_expr_line, 2, glps_expr_number, glps_expr_line);
237 addop(
"*", &binary_bl_mult<1,0>, glps_expr_line, 2, glps_expr_line, glps_expr_number);
239 addop(
"parse", &unary_parse, glps_expr_config, 1, glps_expr_string);
240 addop(
"file", &unary_file, glps_expr_string, 1, glps_expr_string);
241 addop(
"dir", &unary_file, glps_expr_string, 1, glps_expr_string);
242 addop(
"h5file", &unary_h5file, glps_expr_string, 1, glps_expr_string);
245 parse_context::~parse_context()
void setPrinter(std::ostream *)
Set output for lexer/parser error messages.
Interface to lattice file parser.
Config * parse_file(const char *fname)
Open and parse a file.