PageRenderTime 64ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/xbmc/visualizations/XBMCProjectM/libprojectM/Parser.cpp

http://github.com/xbmc/xbmc
C++ | 2577 lines | 1656 code | 566 blank | 355 comment | 649 complexity | a03b8c0b72e06072b3dab5840c0adff0 MD5 | raw file
Possible License(s): GPL-3.0, CC-BY-SA-3.0, LGPL-2.0, 0BSD, Unlicense, GPL-2.0, AGPL-1.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0
  1. /**
  2. * projectM -- Milkdrop-esque visualisation SDK
  3. * Copyright (C)2003-2004 projectM Team
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. * See 'LICENSE.txt' included within this release
  19. *
  20. */
  21. /* parser.c */
  22. #include <stdio.h>
  23. #include <string>
  24. #include <cstring>
  25. #include <iostream>
  26. #include <stdlib.h>
  27. #include "Common.hpp"
  28. #include "fatal.h"
  29. #include "CustomWave.hpp"
  30. #include "CustomShape.hpp"
  31. #include "Expr.hpp"
  32. #include "Eval.hpp"
  33. #include "Func.hpp"
  34. #include "InitCond.hpp"
  35. #include "Param.hpp"
  36. #include "Preset.hpp"
  37. #include "Parser.hpp"
  38. #include "PerFrameEqn.hpp"
  39. #include "PerPixelEqn.hpp"
  40. #include <map>
  41. #include "ParamUtils.hpp"
  42. #include "wipemalloc.h"
  43. #include <iostream>
  44. #include <sstream>
  45. #include "BuiltinFuncs.hpp"
  46. /* Grabs the next token from the file. The second argument points
  47. to the raw string */
  48. line_mode_t Parser::line_mode;
  49. CustomWave *Parser::current_wave;
  50. CustomShape *Parser::current_shape;
  51. int Parser::string_line_buffer_index;
  52. char Parser::string_line_buffer[STRING_LINE_SIZE];
  53. unsigned int Parser::line_count;
  54. int Parser::per_frame_eqn_count;
  55. int Parser::per_frame_init_eqn_count;
  56. int Parser::last_custom_wave_id;
  57. int Parser::last_custom_shape_id;
  58. char Parser::last_eqn_type[MAX_TOKEN_SIZE];
  59. int Parser::last_token_size;
  60. std::string Parser::lastLinePrefix("");
  61. #define white_space(c) ((c) == ' ' || (c) == '\t')
  62. #define valid_digit(c) ((c) >= '0' && (c) <= '9')
  63. #define end_char(c) ((c) == '\0' || (c) == '\r')
  64. #define math_char(c) ((c) == '+' || (c) == '-'|| (c) == '.' || (c) == 'e'|| (c) == 'E' )
  65. double fastatof (const char *p, bool &b_validformat)
  66. {
  67. //check if it is a valid float format (only basic checking with no sequence )
  68. const char *p1=p;
  69. while (!end_char(*p1) && ((math_char(*p1) || valid_digit(*p1))))
  70. p1 += 1;
  71. if (!end_char(*p1) || p1 == p)
  72. {
  73. b_validformat = false;
  74. return 0;
  75. }
  76. b_validformat = true;
  77. int frac = 0;
  78. double sign, value, scale;
  79. // Skip leading white space, if any.
  80. while (white_space(*p) ) {
  81. p += 1;
  82. }
  83. // Get sign, if any.
  84. sign = 1.0;
  85. if (*p == '-') {
  86. sign = -1.0;
  87. p += 1;
  88. } else if (*p == '+') {
  89. p += 1;
  90. }
  91. // Get digits before decimal point or exponent, if any.
  92. value = 0.0;
  93. while (valid_digit(*p)) {
  94. value = value * 10.0 + (*p - '0');
  95. p += 1;
  96. }
  97. // Get digits after decimal point, if any.
  98. if (*p == '.') {
  99. double pow10 = 10.0;
  100. p += 1;
  101. while (valid_digit(*p)) {
  102. value += (*p - '0') / pow10;
  103. pow10 *= 10.0;
  104. p += 1;
  105. }
  106. }
  107. // Handle exponent, if any.
  108. scale = 1.0;
  109. if ((*p == 'e') || (*p == 'E')) {
  110. unsigned int expon;
  111. p += 1;
  112. // Get sign of exponent, if any.
  113. frac = 0;
  114. if (*p == '-') {
  115. frac = 1;
  116. p += 1;
  117. } else if (*p == '+') {
  118. p += 1;
  119. }
  120. // Get digits of exponent, if any.
  121. expon = 0;
  122. while (valid_digit(*p)) {
  123. expon = expon * 10 + (*p - '0');
  124. p += 1;
  125. }
  126. if (expon > 308) expon = 308;
  127. // Calculate scaling factor.
  128. while (expon >= 50) { scale *= 1E50; expon -= 50; }
  129. while (expon >= 8) { scale *= 1E8; expon -= 8; }
  130. while (expon > 0) { scale *= 10.0; expon -= 1; }
  131. }
  132. // Return signed and scaled floating point result.
  133. return sign * (frac ? (value / scale) : (value * scale));
  134. }
  135. bool Parser::tokenWrapAroundEnabled(false);
  136. token_type Parser::parseToken(std::istream & fs, char * string)
  137. {
  138. char c;
  139. int i;
  140. if (string != NULL)
  141. memset(string, 0, MAX_TOKEN_SIZE);
  142. /* Loop until a delimiter is found, or the maximum string size is found */
  143. for (i = 0; i < MAX_TOKEN_SIZE;i++)
  144. {
  145. //c = fgetc(fs);
  146. if (!fs || fs.eof())
  147. c = EOF;
  148. else
  149. c = fs.get();
  150. last_token_size++;
  151. /* If the string line buffer is full, quit */
  152. if (string_line_buffer_index == (STRING_LINE_SIZE - 1))
  153. return tStringBufferFilled;
  154. /* Otherwise add this character to the string line buffer */
  155. string_line_buffer[string_line_buffer_index++] = tolower(c);
  156. /* Now interpret the character */
  157. switch (c)
  158. {
  159. case '+':
  160. return tPlus;
  161. case '-':
  162. return tMinus;
  163. case '%':
  164. return tMod;
  165. case '/':
  166. /* check for line comment here */
  167. if (!fs || fs.eof())
  168. c = EOF;
  169. else
  170. c = fs.get();
  171. if (c == '/')
  172. {
  173. while (true)
  174. {
  175. if (!fs || fs.eof())
  176. c = EOF;
  177. else
  178. c = fs.get();
  179. if (c == EOF)
  180. {
  181. line_mode = UNSET_LINE_MODE;
  182. return tEOF;
  183. }
  184. if (c == '\n')
  185. {
  186. line_mode = UNSET_LINE_MODE;
  187. return tEOL;
  188. }
  189. }
  190. }
  191. /* Otherwise, just a regular division operator */
  192. fs.unget();
  193. return tDiv;
  194. case '*':
  195. return tMult;
  196. case '|':
  197. return tOr;
  198. case '&':
  199. return tAnd;
  200. case '(':
  201. return tLPr;
  202. case ')':
  203. return tRPr;
  204. case '[':
  205. return tLBr;
  206. case ']':
  207. return tRBr;
  208. case '=':
  209. return tEq;
  210. case '\n':
  211. line_count++;
  212. /// @note important hack implemented here to handle "wrap around tokens"
  213. // In particular: "per_frame_1=x=t+1.\nper_frame_2=37 + 5;" implies
  214. // "per_frame_1=x=t+1.37 + 5;". Thus, we have a global flag to determine
  215. // if "token wrap mode" is enabled. If so, we consider a token *continuing*
  216. // to the next line, but only attaching to the token buffer anything following
  217. // the first equals sign on that line.
  218. //
  219. // We can safely assume the next line must be part of the token if we allow the
  220. // semi colon to be a guaranteed delimiter in the grammar for all equation-based lines.
  221. // This IS NO LONGER assumed here. Instead, we check if the next line prefix
  222. // matches with the previous line prefix. If it passes a simple comparison, we wrap around.
  223. if (tokenWrapAroundEnabled)
  224. {
  225. std::ostringstream buffer;
  226. if (PARSE_DEBUG) std::cerr << "token wrap! line " << line_count << std::endl;
  227. while (c != '=')
  228. {
  229. if (!fs || fs.eof())
  230. {
  231. line_count = 1;
  232. line_mode = UNSET_LINE_MODE;
  233. if (PARSE_DEBUG) std::cerr << "token wrap: end of file" << std::endl;
  234. return tEOF;
  235. }
  236. else {
  237. c = fs.get();
  238. if ( c != '=')
  239. buffer << c;
  240. }
  241. }
  242. if (PARSE_DEBUG) std::cerr << "parseToken: parsed away equal sign, line prefix is \"" << buffer.str()
  243. << "\"" << std::endl;
  244. --i;
  245. if (!wrapsToNextLine(buffer.str())) {
  246. tokenWrapAroundEnabled = false;
  247. int buf_size = (int)buffer.str().length();
  248. // <= to also remove equal sign parsing from stream
  249. for (int k = 0; k <= buf_size; k++) {
  250. if (fs)
  251. fs.unget();
  252. else
  253. abort();
  254. }
  255. return tEOL;
  256. }
  257. // if (fs && fs.get() == '\n') {
  258. // line_mode = UNSET_LINE_MODE;
  259. // return tEOL;
  260. // } else if (fs)
  261. //fs.unget();
  262. break;
  263. }
  264. line_mode = UNSET_LINE_MODE;
  265. return tEOL;
  266. case ',':
  267. return tComma;
  268. case ';':
  269. tokenWrapAroundEnabled = false;
  270. if (PARSE_DEBUG) std::cerr << "token wrap around = false (LINE " << line_count << ")" << std::endl;
  271. return tSemiColon;
  272. case ' ': /* space, skip the character */
  273. i--;
  274. break;
  275. case EOF:
  276. line_count = 1;
  277. line_mode = UNSET_LINE_MODE;
  278. return tEOF;
  279. case '\r':
  280. i--;
  281. break;
  282. default:
  283. if (string != NULL)
  284. {
  285. /// @bug remove this nonsense
  286. if (c == '\r')
  287. {
  288. std::cerr << "R" << std::endl;
  289. abort();
  290. }
  291. if (c == '\b')
  292. {
  293. std::cerr << "B" << std::endl;
  294. abort();
  295. }
  296. string[i] = tolower(c);
  297. //string[i+1] = 0;
  298. //std::cerr << "string is \n\"" << string << "\"" << std::endl;
  299. }
  300. }
  301. }
  302. /* String reached maximum length, return special token error */
  303. return tStringTooLong;
  304. }
  305. /* Parse input in the form of "exp, exp, exp, ...)"
  306. Returns a general expression list */
  307. GenExpr **Parser::parse_prefix_args(std::istream & fs, int num_args, Preset * preset)
  308. {
  309. int i, j;
  310. GenExpr ** expr_list; /* List of arguments to function */
  311. GenExpr * gen_expr;
  312. /* Malloc the expression list */
  313. expr_list = (GenExpr**)wipemalloc(sizeof(GenExpr*)*num_args);
  314. /* Malloc failed */
  315. if (expr_list == NULL)
  316. return NULL;
  317. i = 0;
  318. while (i < num_args)
  319. {
  320. //if (PARSE_DEBUG) printf("parse_prefix_args: parsing argument %d...\n", i+1);
  321. /* Parse the ith expression in the list */
  322. if ((gen_expr = parse_gen_expr(fs, NULL, preset)) == NULL)
  323. {
  324. //if (PARSE_DEBUG) printf("parse_prefix_args: failed to get parameter # %d for function (LINE %d)\n", i+1, line_count);
  325. for (j = 0; j < i; j++)
  326. delete expr_list[j];
  327. free(expr_list);
  328. expr_list = NULL;
  329. return NULL;
  330. }
  331. /* Assign entry in expression list */
  332. expr_list[i++] = gen_expr;
  333. }
  334. //if (PARSE_DEBUG) printf("parse_prefix_args: finished parsing %d arguments (LINE %d)\n", num_args, line_count);
  335. /* Finally, return the resulting expression list */
  336. return expr_list;
  337. }
  338. /* Parses a comment at the top of the file. Stops when left bracket is found */
  339. int Parser::parse_top_comment(std::istream & fs)
  340. {
  341. char string[MAX_TOKEN_SIZE];
  342. token_type token;
  343. /* Process tokens until left bracket is found */
  344. while ((token = parseToken(fs, string)) != tLBr)
  345. {
  346. if (token == tEOF)
  347. return PROJECTM_PARSE_ERROR;
  348. }
  349. /* Done, return success */
  350. return PROJECTM_SUCCESS;
  351. }
  352. /* Right Bracket is parsed by this function.
  353. puts a new string into name */
  354. int Parser::parse_preset_name(std::istream & fs, char * name)
  355. {
  356. token_type token;
  357. if (name == NULL)
  358. return PROJECTM_FAILURE;
  359. if ((token = parseToken(fs, name)) != tRBr)
  360. return PROJECTM_PARSE_ERROR;
  361. return PROJECTM_SUCCESS;
  362. }
  363. /* Parses per pixel equations */
  364. int Parser::parse_per_pixel_eqn(std::istream & fs, Preset * preset, char * init_string)
  365. {
  366. char string[MAX_TOKEN_SIZE];
  367. GenExpr * gen_expr;
  368. if (init_string != 0)
  369. {
  370. strncpy(string, init_string, strlen(init_string));
  371. }
  372. else
  373. {
  374. if (parseToken(fs, string) != tEq)
  375. { /* parse per pixel operator name */
  376. return PROJECTM_PARSE_ERROR;
  377. }
  378. }
  379. /* Parse right side of equation as an expression */
  380. if ((gen_expr = parse_gen_expr(fs, NULL, preset)) == NULL)
  381. {
  382. return PROJECTM_PARSE_ERROR;
  383. }
  384. /* Add the per pixel equation */
  385. if (preset->add_per_pixel_eqn(string, gen_expr) < 0)
  386. {
  387. if (PARSE_DEBUG)
  388. {
  389. }
  390. delete gen_expr;
  391. return PROJECTM_PARSE_ERROR;
  392. }
  393. return PROJECTM_SUCCESS;
  394. }
  395. /* Parses an equation line, this function is way too big, should add some helper functions */
  396. int Parser::parse_line(std::istream & fs, Preset * preset)
  397. {
  398. char eqn_string[MAX_TOKEN_SIZE];
  399. token_type token;
  400. InitCond * init_cond;
  401. PerFrameEqn * per_frame_eqn;
  402. /* Clear the string line buffer */
  403. memset(string_line_buffer, 0, STRING_LINE_SIZE);
  404. string_line_buffer_index = 0;
  405. tokenWrapAroundEnabled = false;
  406. token = parseToken( fs, eqn_string );
  407. switch (token )
  408. {
  409. /* Invalid Cases */
  410. case tRBr:
  411. case tLPr:
  412. case tRPr:
  413. case tComma:
  414. case tLBr:
  415. case tPlus:
  416. case tMinus:
  417. case tMod:
  418. case tMult:
  419. case tOr:
  420. case tAnd:
  421. case tDiv:
  422. if (PARSE_DEBUG) std::cerr << "parse_line: invalid token found at start of line (LINE "
  423. << line_count << ")" << std::endl;
  424. /* Invalid token found, return a parse error */
  425. return PROJECTM_PARSE_ERROR;
  426. case tEOL: /* Empty line */
  427. line_mode = UNSET_LINE_MODE;
  428. return PROJECTM_SUCCESS;
  429. case tEOF: /* End of File */
  430. line_mode = UNSET_LINE_MODE;
  431. line_count = 1;
  432. tokenWrapAroundEnabled = false;
  433. return EOF;
  434. case tSemiColon: /* Indicates end of expression */
  435. tokenWrapAroundEnabled = false;
  436. return PROJECTM_SUCCESS;
  437. /* Valid Case, either an initial condition or equation should follow */
  438. case tEq:
  439. lastLinePrefix = std::string(eqn_string);
  440. if (PARSE_DEBUG) std::cout << "last line prefix = \"" << eqn_string << "\"" << std::endl;
  441. // std::cerr << "parse_line: tEQ case, fs.peek()=\'" << fs.peek() << "\'" << std::endl;
  442. if (!fs)
  443. return PROJECTM_PARSE_ERROR;
  444. // char z = fs.get();
  445. char tmpChar;
  446. if ((tmpChar = fs.get()) == '\n') {
  447. tokenWrapAroundEnabled = false;
  448. return PROJECTM_PARSE_ERROR;
  449. } else if (tmpChar == '\r') {
  450. tokenWrapAroundEnabled = false;
  451. return PROJECTM_PARSE_ERROR;
  452. } else
  453. fs.unget();
  454. // if (z == 2)
  455. // ;
  456. // return PROJECTM_PARSE_ERROR;
  457. //else
  458. // fs.unget();
  459. /* CASE: PER FRAME INIT EQUATION */
  460. if (!strncmp(eqn_string, PER_FRAME_INIT_STRING, PER_FRAME_INIT_STRING_LENGTH))
  461. {
  462. tokenWrapAroundEnabled = true;
  463. //if (PARSE_DEBUG) printf("parse_line: per frame init equation found...(LINE %d)\n", line_count);
  464. /* Parse the per frame equation */
  465. if ((init_cond = parse_per_frame_init_eqn(fs, preset, NULL)) == NULL)
  466. {
  467. //if (PARSE_DEBUG) printf("parse_line: per frame init equation parsing failed (LINE %d)\n", line_count);
  468. tokenWrapAroundEnabled = false;
  469. return PROJECTM_PARSE_ERROR;
  470. }
  471. /* Insert the equation in the per frame equation tree */
  472. preset->per_frame_init_eqn_tree.insert(std::make_pair(init_cond->param->name, init_cond));
  473. line_mode = PER_FRAME_INIT_LINE_MODE;
  474. return PROJECTM_SUCCESS;
  475. }
  476. /* Per frame equation case */
  477. if (!strncmp(eqn_string, PER_FRAME_STRING, PER_FRAME_STRING_LENGTH))
  478. {
  479. tokenWrapAroundEnabled = true;
  480. /* Sometimes per frame equations are implicitly defined without the
  481. per_frame_ prefix. This informs the parser that one could follow */
  482. line_mode = PER_FRAME_LINE_MODE;
  483. //if (PARSE_DEBUG) printf("parse_line: per frame equation found...(LINE %d)\n", line_count);
  484. /* Parse the per frame equation */
  485. if ((per_frame_eqn = parse_per_frame_eqn(fs, ++per_frame_eqn_count, preset)) == NULL)
  486. {
  487. if (PARSE_DEBUG) printf("parse_line: per frame equation parsing failed (LINE %d)\n", line_count);
  488. tokenWrapAroundEnabled = false;
  489. return PROJECTM_PARSE_ERROR;
  490. }
  491. /* Insert the equation in the per frame equation tree */
  492. preset->per_frame_eqn_tree.push_back(per_frame_eqn);
  493. return PROJECTM_SUCCESS;
  494. }
  495. /* Wavecode initial condition case */
  496. if (!strncmp(eqn_string, WAVECODE_STRING, WAVECODE_STRING_LENGTH))
  497. {
  498. line_mode = CUSTOM_WAVE_WAVECODE_LINE_MODE;
  499. return parse_wavecode(eqn_string, fs, preset);
  500. }
  501. /* Custom Wave Prefix */
  502. if ((!strncmp(eqn_string, WAVE_STRING, WAVE_STRING_LENGTH)) &&
  503. ((eqn_string[5] >= 48) && (eqn_string[5] <= 57)))
  504. {
  505. tokenWrapAroundEnabled = true;
  506. // if (PARSE_DEBUG) printf("parse_line wave prefix found: \"%s\"\n", eqn_string);
  507. return parse_wave(eqn_string, fs, preset);
  508. }
  509. /* Shapecode initial condition case */
  510. if (!strncmp(eqn_string, SHAPECODE_STRING, SHAPECODE_STRING_LENGTH))
  511. {
  512. line_mode = CUSTOM_SHAPE_SHAPECODE_LINE_MODE;
  513. if (PARSE_DEBUG) printf("parse_line: shapecode prefix found: \"%s\"\n", eqn_string);
  514. return parse_shapecode(eqn_string, fs, preset);
  515. }
  516. /* Custom Shape Prefix */
  517. if ((!strncmp(eqn_string, SHAPE_STRING, SHAPE_STRING_LENGTH)) &&
  518. ((eqn_string[6] >= 48) && (eqn_string[6] <= 57)))
  519. {
  520. tokenWrapAroundEnabled = true;
  521. if (PARSE_DEBUG) printf("parse_line shape prefix found: \"%s\"\n", eqn_string);
  522. return parse_shape(eqn_string, fs, preset);
  523. }
  524. /* Per pixel equation case */
  525. if (!strncmp(eqn_string, PER_PIXEL_STRING, PER_PIXEL_STRING_LENGTH))
  526. {
  527. tokenWrapAroundEnabled = true;
  528. line_mode = PER_PIXEL_LINE_MODE;
  529. if (parse_per_pixel_eqn(fs, preset, 0) < 0)
  530. {
  531. tokenWrapAroundEnabled = false;
  532. return PROJECTM_PARSE_ERROR;
  533. }
  534. if (PARSE_DEBUG) printf("parse_line: finished parsing per pixel equation (LINE %d)\n", line_count);
  535. return PROJECTM_SUCCESS;
  536. }
  537. /* Sometimes equations are written implicitly in milkdrop files, in the form
  538. per_frame_1 = p1 = eqn1; p2 = eqn2; p3 = eqn3;..;
  539. which is analagous to:
  540. per_frame_1 = p1 = eqn1; per_frame_2 = p2 = eqn2; per_frame_3 = p3 = eqn3; ...;
  541. The following line mode hack allows such implicit declaration of the
  542. prefix that specifies the equation type. An alternative method
  543. may be to associate each equation line as list of equations separated
  544. by semicolons (and a new line ends the list). Instead, however, a global
  545. variable called "line_mode" specifies the last type of equation found,
  546. and bases any implicitly typed input on this fact
  547. Note added by Carmelo Piccione (carmelo.piccione@gmail.com) 10/19/03
  548. */
  549. /* Per frame line mode previously, try to parse the equation implicitly */
  550. if (line_mode == PER_FRAME_LINE_MODE)
  551. {
  552. tokenWrapAroundEnabled = true;
  553. if ((per_frame_eqn = parse_implicit_per_frame_eqn(fs, eqn_string, ++per_frame_eqn_count, preset)) == NULL)
  554. {
  555. tokenWrapAroundEnabled = false;
  556. return PROJECTM_PARSE_ERROR;
  557. }
  558. /* Insert the equation in the per frame equation tree */
  559. preset->per_frame_eqn_tree.push_back(per_frame_eqn);
  560. return PROJECTM_SUCCESS;
  561. }
  562. else if (line_mode == PER_FRAME_INIT_LINE_MODE)
  563. {
  564. tokenWrapAroundEnabled = true;
  565. if (PARSE_DEBUG) printf("parse_line: parsing implicit per frame init eqn)\n");
  566. if ((init_cond = parse_per_frame_init_eqn(fs, preset, NULL)) == NULL)
  567. {
  568. tokenWrapAroundEnabled = false;
  569. return PROJECTM_PARSE_ERROR;
  570. }
  571. ++per_frame_init_eqn_count;
  572. /* Insert the equation in the per frame equation tree */
  573. preset->per_frame_init_eqn_tree.insert(std::make_pair(init_cond->param->name, init_cond));
  574. return PROJECTM_SUCCESS;
  575. }
  576. else if (line_mode == PER_PIXEL_LINE_MODE)
  577. {
  578. tokenWrapAroundEnabled = true;
  579. if (PARSE_DEBUG) printf("parse_line: implicit per pixel eqn (LINE %d)\n", line_count);
  580. return parse_per_pixel_eqn(fs, preset, eqn_string);
  581. }
  582. else if (line_mode == CUSTOM_WAVE_PER_POINT_LINE_MODE)
  583. {
  584. tokenWrapAroundEnabled = true;
  585. if (PARSE_DEBUG) printf("parse_line: implicit cwave ppoint eqn found (LINE %d)\n", line_count);
  586. //int len = strlen(eqn_string);
  587. if (parse_wave_helper(fs, preset, last_custom_wave_id, last_eqn_type, eqn_string) < 0)
  588. {
  589. if (PARSE_DEBUG) printf("parse_line: failed to parse an implicit custom wave per point eqn\n");
  590. return PROJECTM_FAILURE;
  591. }
  592. return PROJECTM_SUCCESS;
  593. }
  594. else if (line_mode == CUSTOM_WAVE_PER_FRAME_LINE_MODE)
  595. {
  596. tokenWrapAroundEnabled = true;
  597. //Added by PJS. I hope I did it right
  598. CustomWave * custom_wave;
  599. /* Retrieve custom shape associated with this id */
  600. if ((custom_wave = Preset::find_custom_object(last_custom_wave_id, preset->customWaves)) == NULL)
  601. return PROJECTM_FAILURE;
  602. return parse_wave_per_frame_eqn(fs, custom_wave, preset);
  603. }
  604. else if (line_mode == CUSTOM_WAVE_WAVECODE_LINE_MODE)
  605. {
  606. if (PARSE_DEBUG) printf("unsupported line mode: CUSTOM_WAVE_WAVECODE_LINE_MODE\n");
  607. return PROJECTM_FAILURE;
  608. }
  609. else if (line_mode == CUSTOM_SHAPE_SHAPECODE_LINE_MODE)
  610. {
  611. if (PARSE_DEBUG) printf("unsupported line mode: CUSTOM_SHAPE_SHAPECODE_LINE_MODE\n");
  612. return PROJECTM_FAILURE;
  613. }
  614. else if (line_mode == CUSTOM_SHAPE_PER_FRAME_LINE_MODE)
  615. {
  616. tokenWrapAroundEnabled = true;
  617. CustomShape * custom_shape;
  618. /* Retrieve custom shape associated with this id */
  619. if ((custom_shape = Preset::find_custom_object(last_custom_shape_id, preset->customShapes)) == NULL)
  620. return PROJECTM_FAILURE;
  621. return parse_shape_per_frame_eqn(fs, custom_shape, preset);
  622. }
  623. else if (line_mode == CUSTOM_SHAPE_PER_FRAME_INIT_LINE_MODE)
  624. {
  625. tokenWrapAroundEnabled = true;
  626. CustomShape * custom_shape;
  627. /* Retrieve custom shape associated with this id */
  628. if ((custom_shape = preset->find_custom_object(last_custom_shape_id, preset->customShapes)) == NULL)
  629. return PROJECTM_FAILURE;
  630. return parse_shape_per_frame_init_eqn(fs, custom_shape, preset);
  631. }
  632. if (PARSE_DEBUG) printf("parse_line: found initial condition: name = \"%s\" (LINE %d)\n", eqn_string, line_count);
  633. /* Evaluate the initial condition */
  634. if ((init_cond = parse_init_cond(fs, eqn_string, preset)) == NULL)
  635. {
  636. if (PARSE_DEBUG) printf("parse_line: failed to parse initial condition (LINE %d)\n", line_count);
  637. return PROJECTM_PARSE_ERROR;
  638. }
  639. /* Add equation to initial condition tree */
  640. preset->init_cond_tree.insert(std::make_pair(init_cond->param->name, init_cond));
  641. /* Finished with initial condition line */
  642. // if (PARSE_DEBUG) printf("parse_line: initial condition parsed successfully\n");
  643. return PROJECTM_SUCCESS;
  644. /* END INITIAL CONDITIONING PARSING */
  645. default: /* an uncaught type or an error has occurred */
  646. if (PARSE_DEBUG) printf("parse_line: uncaught case, token val = %d\n", token);
  647. return PROJECTM_PARSE_ERROR;
  648. }
  649. /* Because of the default in the case statement,
  650. control flow should never actually reach here */
  651. return PROJECTM_PARSE_ERROR;
  652. }
  653. /* Parses a general expression, this function is the meat of the parser */
  654. GenExpr * Parser::parse_gen_expr ( std::istream & fs, TreeExpr * tree_expr, Preset * preset)
  655. {
  656. int i;
  657. char string[MAX_TOKEN_SIZE];
  658. token_type token;
  659. GenExpr * gen_expr;
  660. float val;
  661. Param * param = NULL;
  662. Func * func;
  663. GenExpr ** expr_list;
  664. switch (token = parseToken(fs,string))
  665. {
  666. /* Left Parentice Case */
  667. case tLPr:
  668. //std::cerr << "token before tLPr:" << string << std::endl;
  669. /* CASE 1 (Left Parentice): See if the previous string before this parentice is a function name */
  670. if ((func = BuiltinFuncs::find_func(string)) != NULL)
  671. {
  672. if (PARSE_DEBUG)
  673. {
  674. std::cerr << "parse_gen_expr: found prefix function (name = \""
  675. << func->getName() << "\") (LINE " << line_count << ")" << std::endl;
  676. }
  677. /* Parse the functions arguments */
  678. if ((expr_list = parse_prefix_args(fs, func->getNumArgs(), preset)) == NULL)
  679. {
  680. if (PARSE_DEBUG)
  681. {
  682. std::cerr << "parse_prefix_args: failed to generate an expresion list! (LINE "
  683. << line_count << ")" << std::endl;
  684. }
  685. if ( tree_expr != NULL )
  686. {
  687. delete tree_expr;
  688. }
  689. return NULL;
  690. }
  691. /* Convert function to expression */
  692. if ((gen_expr = GenExpr::prefun_to_expr((float (*)(void *))func->func_ptr, expr_list, func->getNumArgs())) == NULL)
  693. {
  694. if (PARSE_DEBUG) printf("parse_prefix_args: failed to convert prefix function to general expression (LINE %d) \n",
  695. line_count);
  696. if (tree_expr)
  697. delete tree_expr;
  698. for (i = 0; i < func->getNumArgs();i++)
  699. delete expr_list[i];
  700. free(expr_list);
  701. expr_list = NULL;
  702. return NULL;
  703. }
  704. token = parseToken(fs, string);
  705. if (*string != 0)
  706. {
  707. if (PARSE_DEBUG) printf("parse_prefix_args: empty string expected, but not found...(LINE %d)\n", line_count);
  708. /* continue anyway for now, could be implicit multiplication */
  709. }
  710. return parse_infix_op(fs, token, insert_gen_expr(gen_expr, &tree_expr), preset);
  711. }
  712. /* Case 2: (Left Parentice), a string coupled with a left parentice. Either an error or implicit
  713. multiplication operator. For now treat it as an error */
  714. if (*string != 0)
  715. {
  716. std::cerr << "token prefix is " << *string << std::endl;
  717. if (PARSE_DEBUG) printf("parse_gen_expr: implicit multiplication case unimplemented!\n");
  718. if (tree_expr)
  719. delete tree_expr;
  720. return NULL;
  721. }
  722. /* CASE 3 (Left Parentice): the following is enclosed parentices to change order
  723. of operations. So we create a new expression tree */
  724. if ((gen_expr = parse_gen_expr(fs, NULL, preset)) == NULL)
  725. {
  726. if (PARSE_DEBUG) printf("parse_gen_expr: found left parentice, but failed to create new expression tree \n");
  727. if (tree_expr)
  728. delete tree_expr;
  729. return NULL;
  730. }
  731. if (PARSE_DEBUG) printf("parse_gen_expr: finished enclosed expression tree...\n");
  732. token = parseToken(fs, string);
  733. return parse_infix_op(fs, token, insert_gen_expr(gen_expr, &tree_expr), preset);
  734. /* Plus is a prefix operator check */
  735. case tPlus:
  736. if (*string == 0)
  737. {
  738. if (PARSE_DEBUG) printf("parse_gen_expr: plus used as prefix (LINE %d)\n", line_count);
  739. /* Treat prefix plus as implict 0 preceding operator */
  740. gen_expr = GenExpr::const_to_expr(0);
  741. return parse_infix_op(fs, tPositive, insert_gen_expr(gen_expr, &tree_expr), preset);
  742. }
  743. /* Minus is a prefix operator check */
  744. case tMinus:
  745. if (*string == 0)
  746. {
  747. /* Use the negative infix operator, but first add an implicit zero to the operator tree */
  748. gen_expr = GenExpr::const_to_expr(0);
  749. //return parse_gen_expr(fs, insert_gen_expr(gen_expr, &tree_expr), preset);
  750. return parse_infix_op(fs, tNegative, insert_gen_expr(gen_expr, &tree_expr), preset);
  751. }
  752. /* All the following cases are strings followed by an infix operator or terminal */
  753. case tRPr:
  754. case tEOL:
  755. case tEOF:
  756. case tSemiColon:
  757. case tComma:
  758. /* CASE 1 (terminal): string is empty, but not null. Not sure if this will actually happen
  759. any more. */
  760. if (*string == 0)
  761. {
  762. if (PARSE_DEBUG) printf("parse_gen_expr: empty string coupled with terminal (LINE %d) \n", line_count);
  763. return parse_infix_op(fs, token, tree_expr, preset);
  764. }
  765. default:
  766. /* CASE 0: Empty string, parse error */
  767. if (*string == 0)
  768. {
  769. if (tree_expr)
  770. delete tree_expr;
  771. return NULL;
  772. }
  773. /* CASE 1: Check if string is a just a floating point number */
  774. if (string_to_float(string, &val) != PROJECTM_PARSE_ERROR)
  775. {
  776. if ((gen_expr = GenExpr::const_to_expr(val)) == NULL)
  777. {
  778. if (tree_expr)
  779. delete tree_expr;
  780. return NULL;
  781. }
  782. /* Parse the rest of the line */
  783. return parse_infix_op(fs, token, insert_gen_expr(gen_expr, &tree_expr), preset);
  784. }
  785. /* CASE 4: custom shape variable */
  786. if (current_shape != NULL)
  787. {
  788. if ((param = ParamUtils::find<ParamUtils::NO_CREATE>(std::string(string), &current_shape->param_tree)) == NULL)
  789. {
  790. if ((param = preset->builtinParams.find_builtin_param(std::string(string))) == NULL)
  791. if ((param = ParamUtils::find<ParamUtils::AUTO_CREATE>(std::string(string), &current_shape->param_tree)) == NULL)
  792. {
  793. if (tree_expr)
  794. delete tree_expr;
  795. return NULL;
  796. }
  797. }
  798. if (PARSE_DEBUG)
  799. {
  800. std::cerr << "parse_gen_expr: custom shape parameter (name = "
  801. << param->name << ")" << std::endl;
  802. }
  803. /* Convert parameter to an expression */
  804. if ((gen_expr = GenExpr::param_to_expr(param)) == NULL)
  805. {
  806. delete tree_expr;
  807. return NULL;
  808. }
  809. //if (PARSE_DEBUG) printf("converted to expression (LINE %d)\n", line_count);
  810. /* Parse the rest of the line */
  811. return parse_infix_op(fs, token, insert_gen_expr(gen_expr, &tree_expr), preset);
  812. }
  813. /* CASE 5: custom wave variable */
  814. if (current_wave != NULL)
  815. {
  816. if ((param = ParamUtils::find<ParamUtils::NO_CREATE>(std::string(string), &current_wave->param_tree)) == NULL)
  817. {
  818. if ((param = preset->builtinParams.find_builtin_param(std::string(string))) == NULL)
  819. if ((param = ParamUtils::find<ParamUtils::AUTO_CREATE>(std::string(string), &current_wave->param_tree)) == NULL)
  820. {
  821. if (tree_expr)
  822. delete tree_expr;
  823. return NULL;
  824. }
  825. }
  826. assert(param);
  827. if (PARSE_DEBUG)
  828. {
  829. std::cerr << "parse_gen_expr: custom wave parameter (name = " << param->name << ")" << std::endl;
  830. }
  831. /* Convert parameter to an expression */
  832. if ((gen_expr = GenExpr::param_to_expr(param)) == NULL)
  833. {
  834. delete tree_expr;
  835. return NULL;
  836. }
  837. //if (PARSE_DEBUG) printf("converted to expression (LINE %d)\n", line_count);
  838. /* Parse the rest of the line */
  839. return parse_infix_op(fs, token, insert_gen_expr(gen_expr, &tree_expr), preset);
  840. }
  841. /* CASE 6: regular parameter. Will be created if necessary and the string has no invalid characters */
  842. if ((param = ParamUtils::find(string, &preset->builtinParams, &preset->user_param_tree)) != NULL)
  843. {
  844. if (PARSE_DEBUG)
  845. {
  846. std::cerr << "parse_gen_expr: parameter (name = \"" << param->name << "\")..." << std::endl;
  847. }
  848. /* Convert parameter to an expression */
  849. if ((gen_expr = GenExpr::param_to_expr(param)) == NULL)
  850. {
  851. delete tree_expr;
  852. return NULL;
  853. }
  854. //if (PARSE_DEBUG) printf("converted to expression (LINE %d)\n", line_count);
  855. /* Parse the rest of the line */
  856. return parse_infix_op(fs, token, insert_gen_expr(gen_expr, &tree_expr), preset);
  857. }
  858. /* CASE 7: Bad string, give up */
  859. if (PARSE_DEBUG)
  860. {
  861. printf( "parse_gen_expr: syntax error [string = \"%s\"] (LINE %d)\n", string, line_count);
  862. }
  863. if (tree_expr)
  864. delete tree_expr;
  865. return NULL;
  866. }
  867. }
  868. /* Inserts expressions into tree according to operator precedence.
  869. If root is null, a new tree is created, with infix_op as only element */
  870. TreeExpr * Parser::insert_infix_op(InfixOp * infix_op, TreeExpr **root)
  871. {
  872. TreeExpr * new_root;
  873. /* Sanity check */
  874. if (infix_op == NULL)
  875. return NULL;
  876. /* The root is null, so make this operator
  877. the new root */
  878. if (*root == NULL)
  879. {
  880. new_root = new TreeExpr(infix_op, NULL, NULL, NULL);
  881. *root = new_root;
  882. return new_root;
  883. }
  884. /* The root node is not an infix function,
  885. so we make this infix operator the new root */
  886. if ((*root)->infix_op == NULL)
  887. {
  888. new_root = new TreeExpr(infix_op, NULL, *root, NULL);
  889. (*root) = new_root;
  890. return new_root;
  891. }
  892. /* The root is an infix function. If the precedence
  893. of the item to be inserted is greater than the root's
  894. precedence, then make gen_expr the root */
  895. if (infix_op->precedence > (*root)->infix_op->precedence)
  896. {
  897. new_root = new TreeExpr(infix_op, NULL, *root, NULL);
  898. (*root) = new_root;
  899. return new_root;
  900. }
  901. /* If control flow reaches here, use a recursive helper
  902. with the knowledge that the root is higher precedence
  903. than the item to be inserted */
  904. insert_infix_rec(infix_op, *root);
  905. return *root;
  906. }
  907. TreeExpr * Parser::insert_gen_expr(GenExpr * gen_expr, TreeExpr ** root)
  908. {
  909. TreeExpr * new_root;
  910. /* If someone foolishly passes a null
  911. pointer to insert, return the original tree */
  912. if (gen_expr == NULL)
  913. {
  914. return *root;
  915. }
  916. /* If the root is null, generate a new expression tree,
  917. using the passed expression as the root element */
  918. if (*root == NULL)
  919. {
  920. new_root = new TreeExpr(NULL, gen_expr, NULL, NULL);
  921. *root = new_root;
  922. return new_root;
  923. }
  924. /* Otherwise. the new element definitely will not replace the current root.
  925. Use a recursive helper function to do insertion */
  926. insert_gen_rec(gen_expr, *root);
  927. return *root;
  928. }
  929. /* A recursive helper function to insert general expression elements into the operator tree */
  930. int Parser::insert_gen_rec(GenExpr * gen_expr, TreeExpr * root)
  931. {
  932. /* Trivial Case: root is null */
  933. if (root == NULL)
  934. {
  935. //if (PARSE_DEBUG) printf("insert_gen_rec: root is null, returning failure\n");
  936. return PROJECTM_FAILURE;
  937. }
  938. /* The current node's left pointer is null, and this
  939. current node is an infix operator, so insert the
  940. general expression at the left pointer */
  941. if ((root->left == NULL) && (root->infix_op != NULL))
  942. {
  943. root->left = new TreeExpr(NULL, gen_expr, NULL, NULL);
  944. return PROJECTM_SUCCESS;
  945. }
  946. /* The current node's right pointer is null, and this
  947. current node is an infix operator, so insert the
  948. general expression at the right pointer */
  949. if ((root->right == NULL) && (root->infix_op != NULL))
  950. {
  951. root->right = new TreeExpr(NULL, gen_expr, NULL, NULL);
  952. return PROJECTM_SUCCESS;
  953. }
  954. /* Otherwise recurse down to the left. If
  955. this succeeds then return. If it fails, try
  956. recursing down to the right */
  957. if (insert_gen_rec(gen_expr, root->left) == PROJECTM_FAILURE)
  958. return insert_gen_rec(gen_expr, root->right);
  959. /* Impossible for control flow to reach here, but in
  960. the world of C programming, who knows... */
  961. if (PARSE_DEBUG) printf("insert_gen_rec: should never reach here!\n");
  962. return PROJECTM_FAILURE;
  963. }
  964. /* A recursive helper function to insert infix arguments by operator precedence */
  965. int Parser::insert_infix_rec(InfixOp * infix_op, TreeExpr * root)
  966. {
  967. /* Shouldn't happen, implies a parse error */
  968. if (root == NULL)
  969. return PROJECTM_FAILURE;
  970. /* Also shouldn't happen, also implies a (different) parse error */
  971. if (root->infix_op == NULL)
  972. return PROJECTM_FAILURE;
  973. /* Left tree is empty, attach this operator to it.
  974. I don't think this will ever happen */
  975. if (root->left == NULL)
  976. {
  977. root->left = new TreeExpr(infix_op, NULL, root->left, NULL);
  978. return PROJECTM_SUCCESS;
  979. }
  980. /* Right tree is empty, attach this operator to it */
  981. if (root->right == NULL)
  982. {
  983. root->right = new TreeExpr(infix_op, NULL, root->right, NULL);
  984. return PROJECTM_SUCCESS;
  985. }
  986. /* The left element can now be ignored, since there is no way for this
  987. operator to use those expressions */
  988. /* If the right element is not an infix operator,
  989. then insert the expression here, attaching the old right branch
  990. to the left of the new expression */
  991. if (root->right->infix_op == NULL)
  992. {
  993. root->right = new TreeExpr(infix_op, NULL, root->right, NULL);
  994. return PROJECTM_SUCCESS;
  995. }
  996. /* Traverse deeper if the inserting operator precedence is less than the
  997. the root's right operator precedence */
  998. if (infix_op->precedence < root->right->infix_op->precedence)
  999. return insert_infix_rec(infix_op, root->right);
  1000. /* Otherwise, insert the operator here */
  1001. root->right = new TreeExpr(infix_op, NULL, root->right, NULL);
  1002. return PROJECTM_SUCCESS;
  1003. }
  1004. /* Parses an infix operator */
  1005. GenExpr * Parser::parse_infix_op(std::istream & fs, token_type token, TreeExpr * tree_expr, Preset * preset)
  1006. {
  1007. GenExpr * gen_expr;
  1008. switch (token)
  1009. {
  1010. /* All the infix operators */
  1011. case tPlus:
  1012. if (PARSE_DEBUG) printf("parse_infix_op: found addition operator (LINE %d)\n", line_count);
  1013. if (PARSE_DEBUG) std::cerr << "WRAP AROUND IS " << tokenWrapAroundEnabled << std::endl;
  1014. return parse_gen_expr(fs, insert_infix_op(Eval::infix_add, &tree_expr), preset);
  1015. case tMinus:
  1016. if (PARSE_DEBUG) printf("parse_infix_op: found subtraction operator (LINE %d)\n", line_count);
  1017. return parse_gen_expr(fs, insert_infix_op(Eval::infix_minus, &tree_expr), preset);
  1018. case tMult:
  1019. if (PARSE_DEBUG) printf("parse_infix_op: found multiplication operator (LINE %d)\n", line_count);
  1020. return parse_gen_expr(fs, insert_infix_op(Eval::infix_mult, &tree_expr), preset);
  1021. case tDiv:
  1022. if (PARSE_DEBUG) printf("parse_infix_op: found division operator (LINE %d)\n", line_count);
  1023. return parse_gen_expr(fs, insert_infix_op(Eval::infix_div, &tree_expr), preset);
  1024. case tMod:
  1025. if (PARSE_DEBUG) printf("parse_infix_op: found modulo operator (LINE %d)\n", line_count);
  1026. return parse_gen_expr(fs, insert_infix_op(Eval::infix_mod, &tree_expr), preset);
  1027. case tOr:
  1028. if (PARSE_DEBUG) printf("parse_infix_op: found bitwise or operator (LINE %d)\n", line_count);
  1029. return parse_gen_expr(fs, insert_infix_op(Eval::infix_or, &tree_expr), preset);
  1030. case tAnd:
  1031. if (PARSE_DEBUG) printf("parse_infix_op: found bitwise and operator (LINE %d)\n", line_count);
  1032. return parse_gen_expr(fs, insert_infix_op(Eval::infix_and, &tree_expr), preset);
  1033. case tPositive:
  1034. if (PARSE_DEBUG) printf("parse_infix_op: found positive operator (LINE %d)\n", line_count);
  1035. return parse_gen_expr(fs, insert_infix_op(Eval::infix_positive, &tree_expr), preset);
  1036. case tNegative:
  1037. if (PARSE_DEBUG) printf("parse_infix_op: found negative operator (LINE %d)\n", line_count);
  1038. return parse_gen_expr(fs, insert_infix_op(Eval::infix_negative, &tree_expr), preset);
  1039. case tEOL:
  1040. case tEOF:
  1041. case tSemiColon:
  1042. case tRPr:
  1043. case tComma:
  1044. if (PARSE_DEBUG) printf("parse_infix_op: terminal found (LINE %d)\n", line_count);
  1045. gen_expr = new GenExpr(TREE_T, (void*)tree_expr);
  1046. assert(gen_expr);
  1047. return gen_expr;
  1048. default:
  1049. if (PARSE_DEBUG) printf("parse_infix_op: operator or terminal expected, but not found (LINE %d)\n", line_count);
  1050. delete tree_expr;
  1051. return NULL;
  1052. }
  1053. /* Will never happen */
  1054. return NULL;
  1055. }
  1056. /* Parses an integer, checks for +/- prefix */
  1057. int Parser::parse_int(std::istream & fs, int * int_ptr)
  1058. {
  1059. char string[MAX_TOKEN_SIZE];
  1060. token_type token;
  1061. int sign;
  1062. char * end_ptr = (char*)" ";
  1063. token = parseToken(fs, string);
  1064. switch (token)
  1065. {
  1066. case tMinus:
  1067. sign = -1;
  1068. token = parseToken(fs, string);
  1069. break;
  1070. case tPlus:
  1071. sign = 1;
  1072. token = parseToken(fs, string);
  1073. break;
  1074. default:
  1075. sign = 1;
  1076. break;
  1077. }
  1078. if (string[0] == 0)
  1079. return PROJECTM_PARSE_ERROR;
  1080. /* Convert the string to an integer. *end_ptr
  1081. should end up pointing to null terminator of 'string'
  1082. if the conversion was successful. */
  1083. // printf("STRING: \"%s\"\n", string);
  1084. (*int_ptr) = sign*strtol(string, &end_ptr, 10);
  1085. /* If end pointer is a return character or null terminator, all is well */
  1086. if ((*end_ptr == '\r') || (*end_ptr == '\0'))
  1087. return PROJECTM_SUCCESS;
  1088. return PROJECTM_PARSE_ERROR;
  1089. }
  1090. /* Parses a floating point number */
  1091. int Parser::string_to_float(char * string, float * float_ptr)
  1092. {
  1093. bool conv_success;
  1094. if (*string == 0)
  1095. return PROJECTM_PARSE_ERROR;
  1096. (*float_ptr) = fastatof(string, conv_success);
  1097. if (conv_success)
  1098. return PROJECTM_SUCCESS;
  1099. else
  1100. return PROJECTM_PARSE_ERROR;
  1101. }
  1102. /* Parses a floating point number */
  1103. int Parser::parse_float(std::istream & fs, float * float_ptr)
  1104. {
  1105. char string[MAX_TOKEN_SIZE];
  1106. bool conv_success;
  1107. token_type token;
  1108. int sign;
  1109. token = parseToken(fs, string);
  1110. switch (token)
  1111. {
  1112. case tMinus:
  1113. sign = -1;
  1114. token = parseToken(fs, string);
  1115. break;
  1116. case tPlus:
  1117. sign = 1;
  1118. token = parseToken(fs, string);
  1119. break;
  1120. default:
  1121. sign = 1;
  1122. }
  1123. if (string[0] == 0)
  1124. return PROJECTM_PARSE_ERROR;
  1125. (*float_ptr) = sign*fastatof(string, conv_success);
  1126. if (conv_success)
  1127. return PROJECTM_SUCCESS;
  1128. else
  1129. return PROJECTM_PARSE_ERROR;
  1130. }
  1131. /* Parses a per frame equation. That is, interprets a stream of data as a per frame equation */
  1132. PerFrameEqn * Parser::parse_per_frame_eqn(std::istream & fs, int index, Preset * preset)
  1133. {
  1134. char string[MAX_TOKEN_SIZE];
  1135. Param * param;
  1136. PerFrameEqn * per_frame_eqn;
  1137. GenExpr * gen_expr;
  1138. if (parseToken(fs, string) != tEq)
  1139. {
  1140. if (PARSE_DEBUG) printf("parse_per_frame_eqn: no equal sign after string \"%s\" (LINE %d)\n", string, line_count);
  1141. return NULL;
  1142. }
  1143. /* Find the parameter associated with the string, create one if necessary */
  1144. if ((param = ParamUtils::find(string, &preset->builtinParams, &preset->user_param_tree)) == NULL)
  1145. {
  1146. return NULL;
  1147. }
  1148. if (PARSE_DEBUG) std::cerr << "parse_per_frame_eqn: parameter \"" << param->name << "\" retrieved (LINE" << line_count << ")" << std::endl;
  1149. /* Make sure parameter is writable */
  1150. if (param->flags & P_FLAG_READONLY)
  1151. {
  1152. if (PARSE_DEBUG) std::cerr << "parse_per_frame_eqn: parameter \"" << param->name << "\" %s is marked as read only (LINE " << line_count << ")" << std::endl;
  1153. return NULL;
  1154. }
  1155. /* Parse right side of equation as an expression */
  1156. if ((gen_expr = parse_gen_expr(fs, NULL, preset)) == NULL)
  1157. {
  1158. if (PARSE_DEBUG) printf("parse_per_frame_eqn: equation evaluated to null (LINE %d)\n", line_count);
  1159. return NULL;
  1160. }
  1161. if (PARSE_DEBUG) printf("parse_per_frame_eqn: finished per frame equation evaluation (LINE %d)\n", line_count);
  1162. /* Create a new per frame equation */
  1163. if ((per_frame_eqn = new PerFrameEqn(index, param, gen_expr)) == NULL)
  1164. {
  1165. if (PARSE_DEBUG) printf("parse_per_frame_eqn: failed to create a new per frame eqn, out of memory?\n");
  1166. delete gen_expr;
  1167. return NULL;
  1168. }
  1169. if (PARSE_DEBUG) printf("parse_per_frame_eqn: per_frame eqn parsed succesfully\n");
  1170. return per_frame_eqn;
  1171. }
  1172. /* Parses an 'implicit' per frame equation. That is, interprets a stream of data as a per frame equation without a prefix */
  1173. PerFrameEqn * Parser::parse_implicit_per_frame_eqn(std::istream & fs, char * param_string, int index, Preset * preset)
  1174. {
  1175. Param * param;
  1176. PerFrameEqn * per_frame_eqn;
  1177. GenExpr * gen_expr;
  1178. if (fs == NULL)
  1179. return NULL;
  1180. if (param_string == NULL)
  1181. return NULL;
  1182. if (preset == NULL)
  1183. return NULL;
  1184. //rintf("param string: %s\n", param_string);
  1185. /* Find the parameter associated with the string, create one if necessary */
  1186. if ((param = ParamUtils::find(param_string, &preset->builtinParams, &preset->user_param_tree)) == NULL)
  1187. {
  1188. return NULL;
  1189. }
  1190. //printf("parse_implicit_per_frame_eqn: param is %s\n", param->name);
  1191. /* Make sure parameter is writable */
  1192. if (param->flags & P_FLAG_READONLY)
  1193. {
  1194. if (PARSE_DEBUG) printf("parse_implicit_per_frame_eqn: parameter %s is marked as read only (LINE %d)\n", param->name.c_str(), line_count);
  1195. return NULL;
  1196. }
  1197. /* Parse right side of equation as an expression */
  1198. if ((gen_expr = parse_gen_expr(fs, NULL, preset)) == NULL)
  1199. {
  1200. if (PARSE_DEBUG) printf("parse_implicit_per_frame_eqn: equation evaluated to null (LINE %d)\n", line_count);
  1201. return NULL;
  1202. }
  1203. if (PARSE_DEBUG) printf("parse_implicit_per_frame_eqn: finished per frame equation evaluation (LINE %d)\n", line_count);
  1204. /* Create a new per frame equation */
  1205. if ((per_frame_eqn = new PerFrameEqn(index, param, gen_expr)) == NULL)
  1206. {
  1207. if (PARSE_DEBUG) printf("parse_implicit_per_frame_eqn: failed to create a new per frame eqn, out of memory?\n");
  1208. delete gen_expr;
  1209. return NULL;
  1210. }
  1211. if (PARSE_DEBUG) printf("parse_implicit_per_frame_eqn: per_frame eqn parsed succesfully\n");
  1212. return per_frame_eqn;
  1213. }
  1214. /* Parses an initial condition */
  1215. InitCond * Parser::parse_init_cond(std::istream & fs, char * name, Preset * preset)
  1216. {
  1217. Param * param;
  1218. CValue init_val;
  1219. InitCond * init_cond;
  1220. if (name == NULL)
  1221. return NULL;
  1222. if (preset == NULL)
  1223. return NULL;
  1224. /* Search for the paramater in the database, creating it if necessary */
  1225. if ((param = ParamUtils::find(name, &preset->builtinParams, &preset->user_param_tree)) == NULL)
  1226. {
  1227. return NULL;
  1228. }
  1229. if (PARSE_DEBUG) printf("parse_init_cond: parameter = \"%s\" (LINE %d)\n", param->name.c_str(), line_count);
  1230. if (param->flags & P_FLAG_READONLY)
  1231. {
  1232. if (PARSE_DEBUG) printf("parse_init_cond: builtin parameter \"%s\" marked as read only!\n", param->name.c_str());
  1233. return NULL;
  1234. }
  1235. /* At this point, a parameter has been created or was found
  1236. in the database. */
  1237. if (PARSE_DEBUG) printf("parse_init_cond: parsing initial condition value... (LINE %d)\n", line_count);
  1238. /* integer value (boolean is an integer in C) */
  1239. if ( (param->type == P_TYPE_BOOL))
  1240. {
  1241. int bool_test;
  1242. if ((parse_int(fs, &bool_test)) == PROJECTM_PARSE_ERROR)
  1243. {
  1244. if (PARSE_DEBUG) printf("parse_init_cond: error parsing integer!\n");
  1245. return NULL;
  1246. }
  1247. init_val.bool_val = bool_test;
  1248. }
  1249. else if ((param->type == P_TYPE_INT))
  1250. {
  1251. if ((parse_int(fs, (int*)&init_val.int_val)) == PROJECTM_PARSE_ERROR)
  1252. {
  1253. if (PARSE_DEBUG) printf("parse_init_cond: error parsing integer!\n");
  1254. return NULL;
  1255. }
  1256. }
  1257. /* float value */
  1258. else if (param->type == P_TYPE_DOUBLE)
  1259. {
  1260. if ((parse_float(fs, (float*)&init_val.float_val)) == PROJECTM_PARSE_ERROR)
  1261. {
  1262. if (PARSE_DEBUG) printf("parse_init_cond: error parsing float!\n");
  1263. return NULL;
  1264. }
  1265. }
  1266. /* Unknown value */
  1267. else
  1268. {
  1269. if (PARSE_DEBUG) printf("parse_init_cond: unknown parameter type!\n");
  1270. return NULL;
  1271. }
  1272. /* Create new initial condition */
  1273. if ((init_cond = new InitCond(param, init_val)) == NULL)
  1274. {
  1275. if (PARSE_DEBUG) printf("parse_init_cond: new_init_cond failed!\n");
  1276. return NULL;
  1277. }
  1278. /* Finished */
  1279. return init_cond;
  1280. }
  1281. /* Parses a per frame init equation, not sure if this works right now */
  1282. InitCond * Parser::parse_per_frame_init_eqn(std::istream & fs, Preset * preset, std::map<std::string,Param*> * database)
  1283. {
  1284. char name[MAX_TOKEN_SIZE];
  1285. Param * param = NULL;
  1286. CValue init_val;
  1287. InitCond * init_cond;
  1288. GenExpr * gen_expr;
  1289. float val;
  1290. token_type token;
  1291. if (preset == NULL)
  1292. return NULL;
  1293. if (fs == NULL)
  1294. return NULL;
  1295. if ((token = parseToken(fs, name)) != tEq)
  1296. return NULL;
  1297. /* If a database was specified,then use ParamUtils::find_db instead */
  1298. if ((database != NULL) && ((param = ParamUtils::find<ParamUtils::AUTO_CREATE>(name, database)) == NULL))
  1299. {
  1300. return NULL;
  1301. }
  1302. /* Otherwise use the builtin parameter and user databases. This is confusing. Sorry. */
  1303. if ((param == NULL) && ((param = ParamUtils::find(name, &preset->builtinParams, &preset->user_param_tree)) == NULL))
  1304. {
  1305. return NULL;
  1306. }
  1307. if (PARSE_DEBUG) printf("parse_per_frame_init_eqn: parameter = \"%s\" (LINE %d)\n", param->name.c_str(), line_count);
  1308. if (param->flags & P_FLAG_READONLY)
  1309. {
  1310. if (PARSE_DEBUG) printf("pars_per_frame_init_eqn: builtin parameter \"%s\" marked as read only!\n", param->name.c_str());
  1311. return NULL;
  1312. }
  1313. /* At this point, a parameter has been created or was found
  1314. in the database. */
  1315. if (PARSE_DEBUG) printf("parse_per_frame_init_eqn: parsing right hand side of per frame init equation.. (LINE %d)\n", line_count);
  1316. if ((gen_expr = parse_gen_expr(fs, NULL, preset)) == NULL)
  1317. {
  1318. if (PARSE_DEBUG) printf("parse_per_frame_init_eqn: failed to parse general expresion!\n");
  1319. return NULL;
  1320. }
  1321. /* Compute initial condition value */
  1322. val = gen_expr->eval_gen_expr(-1,-1);
  1323. /* Free the general expression now that we are done with it */
  1324. delete gen_expr;
  1325. /* integer value (boolean is an integer in C) */
  1326. if (param->type == P_TYPE_BOOL)
  1327. {
  1328. init_val.bool_val = (bool)val;
  1329. }
  1330. else if ((param->type == P_TYPE_INT))
  1331. {
  1332. init_val.int_val = (int)val;
  1333. }
  1334. /* float value */
  1335. else if (param->type == P_TYPE_DOUBLE)
  1336. {
  1337. init_val.float_val = val;
  1338. }
  1339. /* Unknown value */
  1340. else
  1341. {
  1342. if (PARSE_DEBUG) printf("pase_per_frame_init_eqn: unknown parameter type!\n");
  1343. return NULL;
  1344. }
  1345. /* Create new initial condition */
  1346. if ((init_cond = new InitCond(param, init_val)) == NULL)
  1347. {
  1348. if (PARSE_DEBUG) printf("parse_per_frame_init_eqn: new_init_cond failed!\n");
  1349. return NULL;
  1350. }
  1351. init_cond->evaluate(true);
  1352. /* Finished */
  1353. return init_cond;
  1354. }
  1355. int Parser::parse_wavecode(char * token, std::istream & fs, Preset * preset)
  1356. {
  1357. char * var_string;
  1358. InitCond * init_cond;
  1359. CustomWave * custom_wave;
  1360. int id;
  1361. CValue init_val;
  1362. Param * param;
  1363. assert(preset);
  1364. assert(fs);
  1365. assert(token);
  1366. /* token should be in the form wavecode_N_var, such as wavecode_1_samples */
  1367. /* Get id and variable name from token string */
  1368. if (parse_wavecode_prefix(token, &id, &var_string) < 0)
  1369. return PROJECTM_PARSE_ERROR;
  1370. last_custom_wave_id = id;
  1371. if (PARSE_DEBUG) printf("parse_wavecode: wavecode id = %d, parameter = \"%s\"\n", id, var_string);
  1372. /* Retrieve custom wave information from preset, allocating new one if necessary */
  1373. if ((custom_wave = Preset::find_custom_object(id, preset->customWaves)) == NULL)
  1374. {
  1375. std::cerr << "parse_wavecode: failed to load (or create) custom wave (id = "
  1376. << id << ")!\n" << std::endl;
  1377. return PROJECTM_FAILURE;
  1378. }
  1379. if (PARSE_DEBUG) printf("parse_wavecode: custom wave found (id = %d)\n", custom_wave->id);
  1380. /* Retrieve parameter from this custom waves parameter db */
  1381. if ((param = ParamUtils::find<ParamUtils::AUTO_CREATE>(var_string,&custom_wave->param_tree)) == NULL)
  1382. return PROJECTM_FAILURE;
  1383. if (PARSE_DEBUG) printf("parse_wavecode: custom wave parameter found (name = %s)\n", param->name.c_str());
  1384. /* integer value (boolean is an integer in C) */
  1385. if ((param->type == P_TYPE_BOOL))
  1386. {
  1387. int bool_test;
  1388. if ((parse_int(fs, &bool_test)) == PROJECTM_PARSE_ERROR)
  1389. {
  1390. if (PARSE_DEBUG) printf("parse_wavecode: error parsing integer!\n");
  1391. return PROJECTM_PARSE_ERROR;
  1392. }
  1393. init_val.bool_val = bool_test;
  1394. }
  1395. else if ((param->type == P_TYPE_INT))
  1396. {
  1397. if ((parse_int(fs, (int*)&init_val.int_val)) == PROJECTM_PARSE_ERROR)
  1398. {
  1399. if (PARSE_DEBUG) printf("parse_wavecode: error parsing integer!\n");
  1400. return PROJECTM_PARSE_ERROR;
  1401. }
  1402. }
  1403. /* float value */
  1404. else if (param->type == P_TYPE_DOUBLE)
  1405. {
  1406. if ((parse_float(fs, (float*)&init_val.float_val)) == PROJECTM_PARSE_ERROR)
  1407. {
  1408. if (PARSE_DEBUG) printf("parse_wavecode: error parsing float!\n");
  1409. return PROJECTM_PARSE_ERROR;
  1410. }
  1411. }
  1412. /* Unknown value */
  1413. else
  1414. {
  1415. if (PARSE_DEBUG) printf("parse_wavecode: unknown parameter type!\n");
  1416. return PROJECTM_PARSE_ERROR;
  1417. }
  1418. /* Create new initial condition */
  1419. if ((init_cond = new InitCond(param, init_val)) == NULL)
  1420. {
  1421. if (PARSE_DEBUG) printf("parse_wavecode: new_init_cond failed!\n");
  1422. return PROJECTM_FAILURE;
  1423. }
  1424. std::pair<std::map<std::string, InitCond*>::iterator, bool> inserteePair =
  1425. custom_wave->init_cond_tree.insert(std::make_pair(init_cond->param->name, init_cond));
  1426. assert(inserteePair.second);
  1427. line_mode = CUSTOM_WAVE_WAVECODE_LINE_MODE;
  1428. if (PARSE_DEBUG) printf("parse_wavecode: [success]\n");
  1429. return PROJECTM_SUCCESS;
  1430. }
  1431. int Parser::parse_shapecode(char * token, std::istream & fs, Preset * preset)
  1432. {
  1433. char * var_string;
  1434. InitCond * init_cond;
  1435. CustomShape * custom_shape;
  1436. int id;
  1437. CValue init_val;
  1438. Param * param;
  1439. /* Null argument checks */
  1440. if (preset == NULL)
  1441. return PROJECTM_FAILURE;
  1442. if (fs == NULL)
  1443. return PROJECTM_FAILURE;
  1444. if (token == NULL)
  1445. return PROJECTM_FAILURE;
  1446. /* token should be in the form shapecode_N_var, such as shapecode_1_samples */
  1447. /* Get id and variable name from token string */
  1448. if (parse_shapecode_prefix(token, &id, &var_string) < 0)
  1449. return PROJECTM_PARSE_ERROR;
  1450. last_custom_shape_id = id;
  1451. if (PARSE_DEBUG) printf("parse_shapecode: shapecode id = %d, parameter = \"%s\"\n", id, var_string);
  1452. /* Retrieve custom shape information from preset. The 3rd argument
  1453. if true creates a custom shape if one does not exist */
  1454. if ((custom_shape = Preset::find_custom_object(id, preset->customShapes)) == NULL)
  1455. {
  1456. if (PARSE_DEBUG) printf("parse_shapecode: failed to load (or create) custom shape (id = %d)!\n", id);
  1457. return PROJECTM_FAILURE;
  1458. }
  1459. if (PARSE_DEBUG) printf("parse_shapecode: custom shape found (id = %d)\n", custom_shape->id);
  1460. if ((param = ParamUtils::find<ParamUtils::NO_CREATE>(var_string, &custom_shape->text_properties_tree)) != NULL)
  1461. {
  1462. std::string text;//[MAX_TOKEN_SIZE];
  1463. //token_type token = parseToken(fs, text);
  1464. fs >> text;
  1465. *((std::string*)param->engine_val) = text;
  1466. if (PARSE_DEBUG)
  1467. std::cerr << "parse_shapecode: found image url, text is \""
  1468. << text << "\"" << std::endl;
  1469. return PROJECTM_SUCCESS;
  1470. }
  1471. /* Retrieve parameter from this custom shapes parameter db */
  1472. if ((param = ParamUtils::find<ParamUtils::AUTO_CREATE>(var_string, &custom_shape->param_tree)) == NULL)
  1473. {
  1474. if (PARSE_DEBUG) printf("parse_shapecode: failed to create parameter.\n");
  1475. return PROJECTM_FAILURE;
  1476. }
  1477. if (PARSE_DEBUG) printf("parse_shapecode: custom shape parameter found (name = %s)\n", param->name.c_str());
  1478. /* integer value (boolean is an integer in C) */
  1479. if ((param->type == P_TYPE_BOOL))
  1480. {
  1481. int bool_test;
  1482. if ((parse_int(fs, &bool_test)) == PROJECTM_PARSE_ERROR)
  1483. {
  1484. if (PARSE_DEBUG) printf("parse_shapecode: error parsing integer!\n");
  1485. return PROJECTM_PARSE_ERROR;
  1486. }
  1487. init_val.bool_val = bool_test;
  1488. }
  1489. else if ((param->type == P_TYPE_INT))
  1490. {
  1491. if ((parse_int(fs, (int*)&init_val.int_val)) == PROJECTM_PARSE_ERROR)
  1492. {
  1493. if (PARSE_DEBUG) printf("parse_shapecode: error parsing integer!\n");
  1494. return PROJECTM_PARSE_ERROR;
  1495. }
  1496. }
  1497. /* float value */
  1498. else if (param->type == P_TYPE_DOUBLE)
  1499. {
  1500. if ((parse_float(fs, (float*)&init_val.float_val)) == PROJECTM_PARSE_ERROR)
  1501. {
  1502. if (PARSE_DEBUG) printf("parse_shapecode: error parsing float!\n");
  1503. return PROJECTM_PARSE_ERROR;
  1504. }
  1505. }
  1506. /* Unknown value */
  1507. else
  1508. {
  1509. if (PARSE_DEBUG) printf("parse_shapecode: unknown parameter type!\n");
  1510. return PROJECTM_PARSE_ERROR;
  1511. }
  1512. /* Create new initial condition */
  1513. if ((init_cond = new InitCond(param, init_val)) == NULL)
  1514. {
  1515. if (PARSE_DEBUG) printf("parse_shapecode: new_init_cond failed!\n");
  1516. return PROJECTM_FAILURE;
  1517. }
  1518. custom_shape->init_cond_tree.insert(std::make_pair(param->name,init_cond));
  1519. line_mode = CUSTOM_SHAPE_SHAPECODE_LINE_MODE;
  1520. if (PARSE_DEBUG) printf("parse_shapecode: [success]\n");
  1521. return PROJECTM_SUCCESS;
  1522. }
  1523. int Parser::parse_wavecode_prefix(char * token, int * id, char ** var_string)
  1524. {
  1525. int len, i, j;
  1526. if (token == NULL)
  1527. return PROJECTM_FAILURE;
  1528. /*
  1529. if (*var_string == NULL)
  1530. return PROJECTM_FAILURE;
  1531. */
  1532. if (id == NULL)
  1533. return PROJECTM_FAILURE;
  1534. len = strlen(token);
  1535. /* Move pointer passed "wavecode_" prefix */
  1536. if (len <= WAVECODE_STRING_LENGTH)
  1537. return PROJECTM_FAILURE;
  1538. i = WAVECODE_STRING_LENGTH;
  1539. j = 0;
  1540. (*id) = 0;
  1541. /* This loop grabs the integer id for this custom wave */
  1542. while ((i < len) && (token[i] >= 48) && (token[i] <= 57))
  1543. {
  1544. if (j >= MAX_TOKEN_SIZE)
  1545. return PROJECTM_FAILURE;
  1546. (*id) = 10*(*id) + (token[i]-48);
  1547. j++;
  1548. i++;
  1549. }
  1550. if (i > (len - 2))
  1551. return PROJECTM_FAILURE;
  1552. *var_string = token + i + 1;
  1553. return PROJECTM_SUCCESS;
  1554. }
  1555. int Parser::parse_shapecode_prefix(char * token, int * id, char ** var_string)
  1556. {
  1557. int len, i, j;
  1558. if (token == NULL)
  1559. return PROJECTM_FAILURE;
  1560. /*
  1561. if (*var_string == NULL)
  1562. return PROJECTM_FAILURE;
  1563. */
  1564. if (id == NULL)
  1565. return PROJECTM_FAILURE;
  1566. len = strlen(token);
  1567. /* Move pointer passed "shapecode_" prefix */
  1568. if (len <= SHAPECODE_STRING_LENGTH)
  1569. return PROJECTM_FAILURE;
  1570. i = SHAPECODE_STRING_LENGTH;
  1571. j = 0;
  1572. (*id) = 0;
  1573. /* This loop grabs the integer id for this custom shape */
  1574. while ((i < len) && (token[i] >= 48) && (token[i] <= 57))
  1575. {
  1576. if (j >= MAX_TOKEN_SIZE)
  1577. return PROJECTM_FAILURE;
  1578. (*id) = 10*(*id) + (token[i]-48);
  1579. j++;
  1580. i++;
  1581. }
  1582. if (i > (len - 2))
  1583. return PROJECTM_FAILURE;
  1584. *var_string = token + i + 1;
  1585. return PROJECTM_SUCCESS;
  1586. }
  1587. int Parser::parse_wave_prefix(char * token, int * id, char ** eqn_string)
  1588. {
  1589. int len, i, j;
  1590. if (token == NULL)
  1591. return PROJECTM_FAILURE;
  1592. if (eqn_string == NULL)
  1593. return PROJECTM_FAILURE;
  1594. if (id == NULL)
  1595. return PROJECTM_FAILURE;
  1596. len = strlen(token);
  1597. if (len <= WAVE_STRING_LENGTH)
  1598. return PROJECTM_FAILURE;
  1599. i = WAVE_STRING_LENGTH;
  1600. j = 0;
  1601. (*id) = 0;
  1602. /* This loop grabs the integer id for this custom wave */
  1603. while ((i < len) && (token[i] >= 48) && (token[i] <= 57))
  1604. {
  1605. if (j >= MAX_TOKEN_SIZE)
  1606. return PROJECTM_FAILURE;
  1607. (*id) = 10*(*id) + (token[i]-48);
  1608. j++;
  1609. i++;
  1610. }
  1611. if (i > (len - 2))
  1612. return PROJECTM_FAILURE;
  1613. *eqn_string = token + i + 1;
  1614. if (PARSE_DEBUG) printf("parse_wave_prefix: prefix = %s\n (LINE %d)", *eqn_string, line_count);
  1615. return PROJECTM_SUCCESS;
  1616. }
  1617. int Parser::parse_shape_prefix(char * token, int * id, char ** eqn_string)
  1618. {
  1619. int len, i, j;
  1620. if (token == NULL)
  1621. return PROJECTM_FAILURE;
  1622. if (eqn_string == NULL)
  1623. return PROJECTM_FAILURE;
  1624. if (id == NULL)
  1625. return PROJECTM_FAILURE;
  1626. len = strlen(token);
  1627. if (len <= SHAPE_STRING_LENGTH)
  1628. return PROJECTM_FAILURE;
  1629. i = SHAPE_STRING_LENGTH;
  1630. j = 0;
  1631. (*id) = 0;
  1632. /* This loop grabs the integer id for this custom wave */
  1633. while ((i < len) && (token[i] >= 48) && (token[i] <= 57))
  1634. {
  1635. if (j >= MAX_TOKEN_SIZE)
  1636. return PROJECTM_FAILURE;
  1637. (*id) = 10*(*id) + (token[i]-48);
  1638. j++;
  1639. i++;
  1640. }
  1641. if (i > (len - 2))
  1642. return PROJECTM_FAILURE;
  1643. *eqn_string = token + i + 1;
  1644. return PROJECTM_SUCCESS;
  1645. }
  1646. /* Parses custom wave equations */
  1647. int Parser::parse_wave(char * token, std::istream & fs, Preset * preset)
  1648. {
  1649. int id;
  1650. char * eqn_type;
  1651. if (PARSE_DEBUG) printf("parse_wave:begin\n");
  1652. if (token == NULL)
  1653. return PROJECTM_FAILURE;
  1654. if (fs == NULL)
  1655. return PROJECTM_FAILURE;
  1656. if (preset == NULL)
  1657. return PROJECTM_FAILURE;
  1658. /* Grab custom wave id and equation type (per frame or per point) from string token */
  1659. if (parse_wave_prefix(token, &id, &eqn_type) < 0)
  1660. {
  1661. if (PARSE_DEBUG) printf("parse_wave: syntax error in custom wave prefix!\n");
  1662. return PROJECTM_FAILURE;
  1663. }
  1664. strncpy(last_eqn_type, eqn_type, MAX_TOKEN_SIZE);
  1665. return parse_wave_helper(fs, preset, id, eqn_type, 0);
  1666. }
  1667. int Parser::parse_wave_helper(std::istream & fs, Preset * preset, int id, char * eqn_type, char * init_string)
  1668. {
  1669. Param * param;
  1670. GenExpr * gen_expr;
  1671. char string[MAX_TOKEN_SIZE];
  1672. PerFrameEqn * per_frame_eqn;
  1673. CustomWave * custom_wave;
  1674. InitCond * init_cond;
  1675. /* Retrieve custom wave associated with this id */
  1676. if ((custom_wave = Preset::find_custom_object(id, preset->customWaves)) == NULL)
  1677. {
  1678. if (PARSE_DEBUG) printf("parse_wave_helper: custom wave id %d not found!\n", id);
  1679. return PROJECTM_FAILURE;
  1680. }
  1681. /* per frame init equation case */
  1682. if (!strncmp(eqn_type, WAVE_INIT_STRING, WAVE_INIT_STRING_LENGTH))
  1683. {
  1684. if (PARSE_DEBUG) printf("parse_wave_helper (per frame init): [begin] (LINE %d)\n", line_count);
  1685. /* Parse the per frame init equation */
  1686. if ((init_cond = parse_per_frame_init_eqn(fs, preset, &custom_wave->param_tree)) == NULL)
  1687. {
  1688. if (PARSE_DEBUG) printf("parse_wave_helper (per frame init): equation parsing failed (LINE %d)\n", line_count);
  1689. return PROJECTM_PARSE_ERROR;
  1690. }
  1691. /* Insert the equation in the per frame equation tree */
  1692. custom_wave->per_frame_init_eqn_tree.insert(std::make_pair(init_cond->param->name,init_cond));
  1693. {
  1694. if (PARSE_DEBUG) printf("parse_wave_helper: failed to update string buffer (LINE %d)\n", line_count);
  1695. return PROJECTM_FAILURE;
  1696. }
  1697. line_mode = CUSTOM_WAVE_PER_FRAME_INIT_LINE_MODE;
  1698. init_cond->evaluate(true);
  1699. return PROJECTM_SUCCESS;
  1700. }
  1701. /* per frame equation case */
  1702. if (!strncmp(eqn_type, PER_FRAME_STRING_NO_UNDERSCORE, PER_FRAME_STRING_NO_UNDERSCORE_LENGTH))
  1703. {
  1704. if (PARSE_DEBUG) printf("parse_wave_helper (per_frame): [start] (custom wave id = %d)\n", custom_wave->id);
  1705. if (parseToken(fs, string) != tEq)
  1706. {
  1707. if (PARSE_DEBUG) printf("parse_wave (per_frame): no equal sign after string \"%s\" (LINE %d)\n", string, line_count);
  1708. return PROJECTM_PARSE_ERROR;
  1709. }
  1710. /* Find the parameter associated with the string in the custom wave database */
  1711. if ((param = ParamUtils::find<ParamUtils::AUTO_CREATE>(string, &custom_wave->param_tree)) == NULL)
  1712. {
  1713. if (PARSE_DEBUG) printf("parse_wave (per_frame): parameter \"%s\" not found or cannot be wipemalloc'ed!!\n", string);
  1714. return PROJECTM_FAILURE;
  1715. }
  1716. /* Make sure parameter is writable */
  1717. if (param->flags & P_FLAG_READONLY)
  1718. {
  1719. if (PARSE_DEBUG) printf("parse_wave (per_frame): parameter %s is marked as read only (LINE %d)\n", param->name.c_str(), line_count);
  1720. return PROJECTM_FAILURE;
  1721. }
  1722. /* Parse right side of equation as an expression */
  1723. current_wave = custom_wave;
  1724. if ((gen_expr = parse_gen_expr(fs, NULL, preset)) == NULL)
  1725. {
  1726. if (PARSE_DEBUG) printf("parse_wave (per_frame): equation evaluated to null (LINE %d)\n", line_count);
  1727. current_wave = NULL;
  1728. return PROJECTM_PARSE_ERROR;
  1729. }
  1730. current_wave = NULL;
  1731. if (PARSE_DEBUG) printf("parse_wave (per_frame): [finished parsing equation] (LINE %d)\n", line_count);
  1732. /* Create a new per frame equation */
  1733. if ((per_frame_eqn = new PerFrameEqn(custom_wave->per_frame_count++, param, gen_expr)) == NULL)
  1734. {
  1735. if (PARSE_DEBUG) printf("parse_wave (per_frame): failed to create a new per frame eqn, out of memory?\n");
  1736. delete gen_expr;
  1737. return PROJECTM_FAILURE;
  1738. }
  1739. custom_wave->per_frame_eqn_tree.push_back(per_frame_eqn);
  1740. if (PARSE_DEBUG) printf("parse_wave (per_frame): equation %d associated with custom wave %d [success]\n",
  1741. per_frame_eqn->index, custom_wave->id);
  1742. /* Need to add stuff to string buffer so the editor can read the equations.
  1743. Why not make a nice little helper function for this? - here it is: */
  1744. line_mode = CUSTOM_WAVE_PER_FRAME_LINE_MODE;
  1745. return PROJECTM_SUCCESS;
  1746. }
  1747. /* per point equation case */
  1748. if (!strncmp(eqn_type, PER_POINT_STRING, PER_POINT_STRING_LENGTH))
  1749. {
  1750. if (PARSE_DEBUG) printf("parse_wave_helper (per_point): per_pixel equation parsing start...(LINE %d)\n", line_count);
  1751. /// HACK the parse_line code already parsed the per_pixel variable name. This handles that case
  1752. /// Parser needs reworked. Don't have time for it. So this is the result.
  1753. if (init_string)
  1754. strncpy(string, init_string, strlen(init_string));
  1755. else
  1756. {
  1757. if (parseToken(fs, string) != tEq)
  1758. { /* parse per pixel operator name */
  1759. if (PARSE_DEBUG) printf("parse_wave_helper (per_point): equal operator missing after per pixel operator. Last token = \"%s\" (LINE %d)\n", string, line_count);
  1760. return PROJECTM_PARSE_ERROR;
  1761. }
  1762. }
  1763. /* Parse right side of equation as an expression, First tell parser we are parsing a custom wave */
  1764. current_wave = custom_wave;
  1765. if ((gen_expr = parse_gen_expr(fs, NULL, preset)) == NULL)
  1766. {
  1767. if (PARSE_DEBUG) printf("parse_wave_helper (per_point): equation evaluated to null? (LINE %d)\n", line_count);
  1768. return PROJECTM_PARSE_ERROR;
  1769. }
  1770. /* Add the per point equation */
  1771. if (custom_wave->add_per_point_eqn(string, gen_expr) < 0)
  1772. {
  1773. delete gen_expr;
  1774. return PROJECTM_PARSE_ERROR;
  1775. }
  1776. // This tells the parser we are no longer parsing a custom wave
  1777. current_wave = NULL;
  1778. line_mode = CUSTOM_WAVE_PER_POINT_LINE_MODE;
  1779. if (PARSE_DEBUG) printf("parse_wave_helper (per_point): [finished] (custom wave id = %d)\n", custom_wave->id);
  1780. return PROJECTM_SUCCESS;
  1781. }
  1782. return PROJECTM_FAILURE;
  1783. }
  1784. /* Parses custom shape equations */
  1785. int Parser::parse_shape(char * token, std::istream & fs, Preset * preset)
  1786. {
  1787. int id;
  1788. char * eqn_type;
  1789. CustomShape * custom_shape;
  1790. if (token == NULL)
  1791. return PROJECTM_FAILURE;
  1792. if (fs == NULL)
  1793. return PROJECTM_FAILURE;
  1794. if (preset == NULL)
  1795. return PROJECTM_FAILURE;
  1796. /* Grab custom shape id and equation type (per frame or per point) from string token */
  1797. if (parse_shape_prefix(token, &id, &eqn_type) < 0)
  1798. {
  1799. if (PARSE_DEBUG) printf("parse_shape: syntax error in custom shape prefix!\n");
  1800. return PROJECTM_PARSE_ERROR;
  1801. }
  1802. /* Retrieve custom shape associated with this id */
  1803. if ((custom_shape = Preset::find_custom_object(id,preset->customShapes)) == NULL)
  1804. return PROJECTM_FAILURE;
  1805. /* per frame init equation case */
  1806. if (!strncmp(eqn_type, SHAPE_INIT_STRING, SHAPE_INIT_STRING_LENGTH))
  1807. {
  1808. return parse_shape_per_frame_init_eqn(fs, custom_shape, preset);
  1809. }
  1810. /* per frame equation case */
  1811. if (!strncmp(eqn_type, PER_FRAME_STRING_NO_UNDERSCORE, PER_FRAME_STRING_NO_UNDERSCORE_LENGTH))
  1812. {
  1813. return parse_shape_per_frame_eqn(fs, custom_shape, preset);
  1814. }
  1815. /* Syntax error, return parse error */
  1816. return PROJECTM_PARSE_ERROR;
  1817. }
  1818. /* Helper function to update the string buffers used by the editor */
  1819. int Parser::update_string_buffer(char * buffer, int * index)
  1820. {
  1821. int string_length;
  1822. int skip_size;
  1823. if (!buffer)
  1824. return PROJECTM_FAILURE;
  1825. if (!index)
  1826. return PROJECTM_FAILURE;
  1827. /* If the string line buffer used by the parser is already full then quit */
  1828. if (string_line_buffer_index == (STRING_LINE_SIZE-1))
  1829. return PROJECTM_FAILURE;
  1830. if ((skip_size = get_string_prefix_len(string_line_buffer)) == PROJECTM_FAILURE)
  1831. return PROJECTM_FAILURE;
  1832. string_line_buffer[string_line_buffer_index++] = '\n';
  1833. // string_length = strlen(string_line_buffer + strlen(eqn_string)+1);
  1834. if (skip_size >= STRING_LINE_SIZE)
  1835. return PROJECTM_FAILURE;
  1836. string_length = strlen(string_line_buffer + skip_size);
  1837. if (skip_size > (STRING_LINE_SIZE-1))
  1838. return PROJECTM_FAILURE;
  1839. /* Add line to string buffer */
  1840. strncpy(buffer + (*index),
  1841. string_line_buffer + skip_size, string_length);
  1842. /* Buffer full, quit */
  1843. if ((*index) > (STRING_BUFFER_SIZE - 1))
  1844. {
  1845. if (PARSE_DEBUG) printf("update_string_buffer: string buffer full!\n");
  1846. return PROJECTM_FAILURE;
  1847. }
  1848. /* Otherwise, increment string index by the added string length */
  1849. (*index)+=string_length;
  1850. return PROJECTM_SUCCESS;
  1851. }
  1852. /* Helper function: returns the length of the prefix portion in the line
  1853. buffer (the passed string here). In other words, given
  1854. the string 'per_frame_1 = x = ....', return the length of 'per_frame_1 = '
  1855. Returns -1 if syntax error
  1856. */
  1857. int Parser::get_string_prefix_len(char * string)
  1858. {
  1859. int i = 0;
  1860. /* Null argument check */
  1861. if (string == NULL)
  1862. return PROJECTM_FAILURE;
  1863. /* First find the equal sign */
  1864. while (string[i] != '=')
  1865. {
  1866. if (string[i] == 0)
  1867. return PROJECTM_FAILURE;
  1868. i++;
  1869. }
  1870. /* If the string already ends at the next char then give up */
  1871. if (string[i+1] == 0)
  1872. return PROJECTM_FAILURE;
  1873. /* Move past the equal sign */
  1874. i++;
  1875. /* Now found the start of the LHS variable, ie skip the spaces */
  1876. while(string[i] == ' ')
  1877. {
  1878. i++;
  1879. }
  1880. /* If this is the end of the string then its a syntax error */
  1881. if (string[i] == 0)
  1882. return PROJECTM_FAILURE;
  1883. /* Finished succesfully, return the length */
  1884. return i;
  1885. }
  1886. int Parser::parse_shape_per_frame_init_eqn(std::istream & fs, CustomShape * custom_shape, Preset * preset)
  1887. {
  1888. InitCond * init_cond;
  1889. if (PARSE_DEBUG) printf("parse_shape (per frame init): [begin] (LINE %d)\n", line_count);
  1890. /* Parse the per frame equation */
  1891. if ((init_cond = parse_per_frame_init_eqn(fs, preset, &custom_shape->param_tree)) == NULL)
  1892. {
  1893. if (PARSE_DEBUG) printf("parse_shape (per frame init): equation parsing failed (LINE %d)\n", line_count);
  1894. return PROJECTM_PARSE_ERROR;
  1895. }
  1896. /// \idea possibly a good place to update a string buffer;
  1897. line_mode = CUSTOM_SHAPE_PER_FRAME_INIT_LINE_MODE;
  1898. init_cond->evaluate(true);
  1899. return PROJECTM_SUCCESS;
  1900. }
  1901. int Parser::parse_shape_per_frame_eqn(std::istream & fs, CustomShape * custom_shape, Preset * preset)
  1902. {
  1903. Param * param;
  1904. GenExpr * gen_expr;
  1905. PerFrameEqn * per_frame_eqn;
  1906. char string[MAX_TOKEN_SIZE];
  1907. if (PARSE_DEBUG) printf("parse_shape (per_frame): [start] (custom shape id = %d)\n", custom_shape->id);
  1908. if (parseToken(fs, string) != tEq)
  1909. {
  1910. if (PARSE_DEBUG) printf("parse_shape (per_frame): no equal sign after string \"%s\" (LINE %d)\n", string, line_count);
  1911. return PROJECTM_PARSE_ERROR;
  1912. }
  1913. /* Find the parameter associated with the string in the custom shape database */
  1914. if ((param = ParamUtils::find<ParamUtils::AUTO_CREATE>(string, &custom_shape->param_tree)) == NULL)
  1915. {
  1916. if (PARSE_DEBUG) printf("parse_shape (per_frame): parameter \"%s\" not found or cannot be wipemalloc'ed!!\n", string);
  1917. return PROJECTM_FAILURE;
  1918. }
  1919. /* Make sure parameter is writable */
  1920. if (param->flags & P_FLAG_READONLY)
  1921. {
  1922. if (PARSE_DEBUG) printf("parse_shape (per_frame): parameter %s is marked as read only (LINE %d)\n", param->name.c_str(), line_count);
  1923. return PROJECTM_PARSE_ERROR;
  1924. }
  1925. /* Parse right side of equation as an expression */
  1926. current_shape = custom_shape;
  1927. if ((gen_expr = parse_gen_expr(fs, NULL, preset)) == NULL)
  1928. {
  1929. if (PARSE_DEBUG) printf("parse_shape (per_frame): equation evaluated to null (LINE %d)\n", line_count);
  1930. current_shape = NULL;
  1931. return PROJECTM_PARSE_ERROR;
  1932. }
  1933. current_shape = NULL;
  1934. if (PARSE_DEBUG) printf("parse_shape (per_frame): [finished parsing equation] (LINE %d)\n", line_count);
  1935. /* Create a new per frame equation */
  1936. if ((per_frame_eqn = new PerFrameEqn(custom_shape->per_frame_count++, param, gen_expr)) == NULL)
  1937. {
  1938. if (PARSE_DEBUG) printf("parse_shape (per_frame): failed to create a new per frame eqn, out of memory?\n");
  1939. delete gen_expr;
  1940. return PROJECTM_FAILURE;
  1941. }
  1942. custom_shape->per_frame_eqn_tree.push_back(per_frame_eqn);
  1943. /// \idea add string buffer update for easy >> and <<
  1944. line_mode = CUSTOM_SHAPE_PER_FRAME_LINE_MODE;
  1945. return PROJECTM_SUCCESS;
  1946. }
  1947. int Parser::parse_wave_per_frame_eqn(std::istream & fs, CustomWave * custom_wave, Preset * preset)
  1948. {
  1949. Param * param;
  1950. GenExpr * gen_expr;
  1951. PerFrameEqn * per_frame_eqn;
  1952. char string[MAX_TOKEN_SIZE];
  1953. if (PARSE_DEBUG) printf("parse_wave (per_frame): [start] (custom shape id = %d)\n", custom_wave->id);
  1954. if (parseToken(fs, string) != tEq)
  1955. {
  1956. if (PARSE_DEBUG) printf("parse_wave (per_frame): no equal sign after string \"%s\" (LINE %d)\n", string, line_count);
  1957. return PROJECTM_PARSE_ERROR;
  1958. }
  1959. /* Find the parameter associated with the string in the custom shape database */
  1960. if ((param = ParamUtils::find<ParamUtils::AUTO_CREATE>(string, &custom_wave->param_tree)) == NULL)
  1961. {
  1962. if (PARSE_DEBUG) printf("parse_wave (per_frame): parameter \"%s\" not found or cannot be wipemalloc'ed!!\n", string);
  1963. return PROJECTM_FAILURE;
  1964. }
  1965. /* Make sure parameter is writable */
  1966. if (param->flags & P_FLAG_READONLY)
  1967. {
  1968. if (PARSE_DEBUG) printf("parse_wave (per_frame): parameter %s is marked as read only (LINE %d)\n", param->name.c_str(), line_count);
  1969. return PROJECTM_FAILURE;
  1970. }
  1971. /* Parse right side of equation as an expression */
  1972. current_wave = custom_wave;
  1973. if ((gen_expr = parse_gen_expr(fs, NULL, preset)) == NULL)
  1974. {
  1975. if (PARSE_DEBUG) printf("parse_wave (per_frame): equation evaluated to null (LINE %d)\n", line_count);
  1976. current_wave = NULL;
  1977. return PROJECTM_PARSE_ERROR;
  1978. }
  1979. current_wave = NULL;
  1980. if (PARSE_DEBUG) printf("parse_wave (per_frame): [finished parsing equation] (LINE %d)\n", line_count);
  1981. /* Create a new per frame equation */
  1982. if ((per_frame_eqn = new PerFrameEqn(custom_wave->per_frame_count++, param, gen_expr)) == NULL)
  1983. {
  1984. if (PARSE_DEBUG) printf("parse_wave (per_frame): failed to create a new per frame eqn, out of memory?\n");
  1985. delete gen_expr;
  1986. return PROJECTM_FAILURE;
  1987. }
  1988. custom_wave->per_frame_eqn_tree.push_back(per_frame_eqn);
  1989. if (PARSE_DEBUG) printf("parse_wave (per_frame): equation %d associated with custom wave %d [success]\n",
  1990. per_frame_eqn->index, custom_wave->id);
  1991. /* Need to add stuff to string buffer so the editor can read the equations.
  1992. Why not make a nice little helper function for this? - here it is: */
  1993. line_mode = CUSTOM_WAVE_PER_FRAME_LINE_MODE;
  1994. return PROJECTM_SUCCESS;
  1995. }
  1996. bool Parser::wrapsToNextLine(const std::string & str) {
  1997. std::size_t lastLineEndIndex =
  1998. lastLinePrefix.find_last_not_of("0123456789");
  1999. std::size_t thisLineEndIndex =
  2000. str.find_last_not_of("0123456789");
  2001. std::size_t startIndex = 0;
  2002. if ((str.compare(startIndex, lastLineEndIndex, lastLinePrefix.c_str(), thisLineEndIndex)) == 0)
  2003. return true;
  2004. else
  2005. return false;
  2006. }