PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/SDF-parse.y

https://bitbucket.org/darkskysims/sdf
Happy | 719 lines | 646 code | 73 blank | 0 comment | 0 complexity | c3eaff54238b0ef4099dfcd0a6629cdd MD5 | raw file
  1. %{
  2. /*
  3. SDF Library for reading Self-Describing Files
  4. Copyright (C) 1991,1992 John K. Salmon
  5. Distributed under the terms of the BSD 3-clause License.
  6. The full license is in the file COPYING.txt, distributed with this software.
  7. */
  8. /* We don't rely on bison's -p argument any more.
  9. Instead, just #define the name changes ourselves. These are taken
  10. from the beginning of bison -p output. These are far from a complete
  11. set of external 'yy' names, as a quick run throug 'nm' will show. Maybe
  12. all the others come from lex. I dunno. In any event, the namespace is only
  13. partially cleaned up. Perhaps we should apply for Superfund money
  14. to finish the cleanup? bison -p does no better.
  15. */
  16. #define yyparse SDFyyparse
  17. #define yylex SDFyylex
  18. #define yyerror SDFyyerror
  19. #define yylval SDFyylval
  20. #define yychar SDFyychar
  21. #define yydebug SDFyydebug
  22. #define yynerrs SDFyynerrs
  23. #include <stdarg.h>
  24. #include <inttypes.h>
  25. #include <float.h>
  26. #include <string.h>
  27. #include <limits.h>
  28. #include "SDF-private.h"
  29. #define YYDEBUG 1
  30. #if YYDEBUG
  31. /* yacc sends its debug output throught printf. We change that... */
  32. #define printf Msg_do /* MUST be after protos.h!!! */
  33. #endif
  34. #ifdef cray
  35. /* This wants to be a long on the cray?? */
  36. extern long int yydebug;
  37. #else
  38. extern int yydebug;
  39. #endif
  40. extern void yyerror(char *fmt, ...);
  41. extern int yylex(void);
  42. static enum SDF_type_enum curtype;
  43. static blk_descrip_t cur_blk;
  44. static int64_t cur_file_offset;
  45. static int64_t cur_data_offset;
  46. static SDF *cur_hdr;
  47. static int no_more_data_blks;
  48. static int zero_len_blknum;
  49. /* N.B. These are indexed by SDF_type_enum. Don't change one
  50. without the other */
  51. char *SDFtype_names[] = {"notype", "char", "int16_t", "int32_t", "int64_t",
  52. "float", "double", "string"};
  53. #if CHAR_BIT != 8
  54. #error "CHAR_BIT!=8. You could try commenting this #error out. It might work..."
  55. #endif
  56. int SDFtype_sizes[] = {0, 1, 2, 4, 8,
  57. sizeof(float), sizeof(double), sizeof(char *)};
  58. static int do_value_param(enum value_param_enum type, const_t value);
  59. static int data_dcl(declaration_t dcl);
  60. static int const_dcl(declaration_t dcl, const_list_t consts);
  61. static void finish_curblk(void);
  62. static const_t convert_const(const_t *cp, enum SDF_type_enum type);
  63. static int finish_parse(void);
  64. %}
  65. %token STRUCT
  66. %token <string> NAME
  67. %token <type> TYPE
  68. %token <constant> CONST
  69. %token <valueparam> VALUEPARAM
  70. %token PARAMETER
  71. %token EOHDR LEXERROR
  72. %token ';' '=' '[' ']' '{' '}' ','
  73. %type <declaration> declaration
  74. %type <one_dcl> dcl1
  75. %type <const_list> const_lst comma_sep_consts
  76. %type <dcl_list> comma_sep_dcls typed_dcl_list many_typed_dcl_list
  77. %union{
  78. enum SDF_type_enum type;
  79. enum value_param_enum valueparam;
  80. char *string;
  81. const_t constant;
  82. declaration_t declaration;
  83. dcl_list_t dcl_list;
  84. one_dcl_t one_dcl;
  85. const_list_t const_list;
  86. }
  87. %%
  88. hdr : hdr1 {if(finish_parse()) YYERROR;}
  89. | hdr1 EOHDR {if(finish_parse()) YYERROR; else YYACCEPT;}
  90. | LEXERROR {YYERROR;}
  91. ;
  92. hdr1 : stmt
  93. | hdr1 stmt
  94. ;
  95. stmt : declaration ';' {if(data_dcl($1)) YYERROR;}
  96. | declaration '=' const_lst ';' {if(const_dcl($1, $3)) YYERROR;}
  97. | PARAMETER VALUEPARAM '=' CONST ';' {if(do_value_param($2, $4)) YYERROR;}
  98. ;
  99. declaration : typed_dcl_list {$$.dcl_list = $1; $$.Nrec = 1;}
  100. | STRUCT '{' many_typed_dcl_list ';' '}' {$$.dcl_list=$3; $$.Nrec=1;}
  101. | STRUCT '{' many_typed_dcl_list ';' '}' '[' CONST ']'
  102. {
  103. if( $7.type != SDF_INT64 ){
  104. yyerror("Expected integer constant");
  105. YYERROR;
  106. }else{
  107. $$.dcl_list = $3; $$.Nrec = $7.u.int64val;
  108. }
  109. }
  110. | STRUCT '{' many_typed_dcl_list ';' '}' '[' ']'
  111. { $$.dcl_list = $3; $$.Nrec = 0;}
  112. ;
  113. many_typed_dcl_list : typed_dcl_list {$$ = $1;}
  114. | many_typed_dcl_list ';' typed_dcl_list
  115. {
  116. int sz;
  117. $$.ndcl = $1.ndcl + $3.ndcl;
  118. $$.obs = $1.obs;
  119. sz = obstack_object_size(&$3.obs);
  120. (void)obstack_grow(&$$.obs, obstack_finish(&$3.obs), sz);
  121. (void)obstack_free(&$3.obs, NULL);
  122. }
  123. ;
  124. typed_dcl_list: TYPE {curtype = $1;} comma_sep_dcls {$$ = $3;}
  125. ;
  126. comma_sep_dcls: dcl1
  127. {
  128. obstack_begin(&$$.obs, 16*sizeof($1));
  129. $$.ndcl = 1;
  130. (void)obstack_grow(&$$.obs, &$1, sizeof($1));
  131. }
  132. | comma_sep_dcls ',' dcl1
  133. {
  134. $$ = $1;
  135. $$.ndcl += 1;
  136. (void)obstack_grow(&$$.obs, &$3, sizeof($3));
  137. }
  138. ;
  139. dcl1 : NAME {$$.name = $1; $$.type = curtype; $$.arrcnt = 1;}
  140. | NAME '[' CONST ']'
  141. {
  142. if( $3.type != SDF_INT64 ){
  143. yyerror("Expected integer constant");
  144. YYERROR;
  145. }else{
  146. $$.name = $1; $$.type = curtype; $$.arrcnt = $3.u.int64val;
  147. }
  148. }
  149. | NAME '[' ']' {$$.name=$1; $$.type=curtype; $$.arrcnt = 0;}
  150. ;
  151. const_lst : CONST
  152. {
  153. $$.nconst = 1;
  154. obstack_begin(&$$.obs, 16*sizeof($1));
  155. (void)obstack_grow(&$$.obs, &$1, sizeof($1));
  156. }
  157. | '{' comma_sep_consts '}' {$$ = $2;}
  158. ;
  159. comma_sep_consts : CONST
  160. {
  161. $$.nconst = 1;
  162. obstack_begin(&$$.obs, 16*sizeof($1));
  163. (void)obstack_grow(&$$.obs, &$1, sizeof($1));
  164. }
  165. | comma_sep_consts ',' CONST
  166. {
  167. $$ = $1;
  168. $$.nconst += 1;
  169. (void)obstack_grow(&$$.obs, &$3, sizeof($3));
  170. }
  171. ;
  172. %%
  173. static int SDFlineno;
  174. static const char *Dataname, *Hdrname;
  175. #ifdef STANDALONE
  176. char SDFerrstring[512];
  177. unsigned int SDFcpubyteorder(void){return 0;}
  178. main(int argc, char **argv)
  179. {
  180. SDF hdr;
  181. if(argc > 1){
  182. yydebug = 1;
  183. }else{
  184. yydebug = 0;
  185. }
  186. SDFyyprepare(&hdr, "-", "-");
  187. if(yyparse()){
  188. printf("Terminated on error. \n");
  189. exit(1);
  190. }
  191. exit(0);
  192. }
  193. #endif
  194. static int SDF_iomode = MPMY_RDONLY | MPMY_MULTI;
  195. void SDFsetiomode(int mode)
  196. {
  197. if (mode == SDF_SINGL) {
  198. SDF_iomode = MPMY_RDONLY | MPMY_SINGL;
  199. } else {
  200. SDF_iomode = MPMY_RDONLY | MPMY_MULTI;
  201. }
  202. }
  203. void SDFyyerror(char *fmt, ...)
  204. {
  205. char *p;
  206. va_list ap;
  207. va_start(ap, fmt);
  208. vsprintf(SDFerrstring, fmt, ap);
  209. p = SDFerrstring + strlen(SDFerrstring);
  210. sprintf(p, " lineno = %d\n", SDFlineno);
  211. va_end(ap);
  212. }
  213. int SDFyyprepare(SDF *hdr, const char *hdrname, const char *dataname)
  214. {
  215. no_more_data_blks = 0;
  216. cur_file_offset = 0;
  217. cur_data_offset = 0;
  218. cur_blk.Nrec = 1;
  219. cur_blk.inmem = 1;
  220. cur_blk.begin_offset = 0;
  221. cur_blk.reclen = 0;
  222. cur_hdr = hdr;
  223. cur_hdr->nblks = 0;
  224. cur_hdr->nvecs = 0;
  225. /* Assume that MPMY_Fopen does the 'right' thing with "-" */
  226. if( SDF_Hdropen(hdrname) < 0 ){
  227. sprintf(SDFerrstring, "SDFopen: could not open %s\n", hdrname);
  228. return -1;
  229. }
  230. Dataname = dataname;
  231. Hdrname = hdrname;
  232. SDFlineno = 0; /* or 1? It's always +-1 anyway */
  233. SDFlexprepare();
  234. obstack_begin(&cur_hdr->blks_obs, 16*sizeof(blk_descrip_t));
  235. obstack_begin(&cur_hdr->vecs_obs, 32*sizeof(vec_descrip_t));
  236. obstack_begin(&cur_hdr->data_obs, 2048);
  237. return 0;
  238. }
  239. static int finish_parse(void)
  240. {
  241. int i;
  242. finish_curblk();
  243. cur_hdr->blks = (blk_descrip_t *)obstack_finish(&cur_hdr->blks_obs);
  244. cur_hdr->vecs = (vec_descrip_t *)obstack_finish(&cur_hdr->vecs_obs);
  245. cur_hdr->data = obstack_finish(&cur_hdr->data_obs);
  246. cur_hdr->vec_names = Malloc(cur_hdr->nvecs * sizeof(char *));
  247. for(i=0; i<cur_hdr->nvecs; i++){
  248. cur_hdr->vec_names[i] = cur_hdr->vecs[i].name;
  249. }
  250. if( (Dataname == NULL) || (Dataname[0] == '\0')
  251. || (strcmp(Hdrname, Dataname)==0)){
  252. cur_hdr->begin_file_offset = SDF_Hdroffset();
  253. if( cur_hdr->begin_file_offset < 0 ){
  254. yyerror("Can't get offset of end of header\n");
  255. return -1;
  256. }
  257. }else{
  258. cur_hdr->begin_file_offset = 0;
  259. }
  260. SDF_Hdrclose();
  261. /* cur_hdr->datafp = MPMY_Fopen(Dataname, MPMY_RDONLY); */
  262. /* If we come up with a better model for IO, call it here.. */
  263. cur_hdr->datafp = MPMY_Fopen(Dataname, SDF_iomode);
  264. Msgf("cur_hdr->datafp = %p\n", cur_hdr->datafp);
  265. if( cur_hdr->datafp == NULL ){
  266. sprintf(SDFerrstring, "SDFopen: could not open data file: %s\n",
  267. Dataname);
  268. return -1;
  269. }
  270. if(no_more_data_blks){
  271. blk_descrip_t *zerolenblk;
  272. off_t bytesleft, recsleft;
  273. off_t datalen;
  274. zerolenblk = &cur_hdr->blks[zero_len_blknum];
  275. if(zerolenblk->Nrec != 0){
  276. yyerror("Zero length block has non-zero length!?\n");
  277. return -1;
  278. }
  279. if( cur_hdr->begin_file_offset < 0 ){
  280. yyerror("Can't have zero-len blk in an unseekable file\n");
  281. return -1;
  282. }
  283. Msgf("About to call MPMY_Flen\n");
  284. if( (datalen = MPMY_Flen(cur_hdr->datafp)) < 0 ){
  285. yyerror("Could not get length of data file.\n");
  286. return -1;
  287. }
  288. bytesleft = datalen
  289. - (zerolenblk->begin_offset + cur_hdr->begin_file_offset);
  290. Msgf("datalen = %ld, bytesleft = %ld\n",
  291. (long)datalen, (long)bytesleft);
  292. if( bytesleft < 0 ){
  293. yyerror("File too short.\n");
  294. return -1;
  295. }
  296. recsleft = bytesleft/zerolenblk->reclen;
  297. if( recsleft*zerolenblk->reclen != bytesleft ){
  298. printf("datalen is %ld, bytesleft is %ld\n", (long)datalen, (long)bytesleft);
  299. yyerror("File ends between record boundaries\n");
  300. return -1;
  301. }
  302. zerolenblk->Nrec = recsleft;
  303. }
  304. return 0;
  305. }
  306. static int do_value_param(enum value_param_enum param, const_t value)
  307. {
  308. switch(param){
  309. case BYTEORDER:
  310. if( value.type != SDF_INT64 )
  311. return -1;
  312. cur_hdr->byteorder = value.u.int64val;
  313. cur_hdr->swapping = (cur_hdr->byteorder != SDFcpubyteorder());
  314. break;
  315. }
  316. return 0;
  317. }
  318. static int data_dcl(declaration_t dcl)
  319. {
  320. dcl_list_t *dcl_list;
  321. one_dcl_t *base, *dclp;
  322. int i, offset;
  323. vec_descrip_t vec_descrip;
  324. #if YYDEBUG
  325. if(yydebug)
  326. printf("Declaration of %" PRId64 " records:\n", dcl.Nrec);
  327. #endif
  328. if(no_more_data_blks){
  329. yyerror("You can't have data following an implicit-length dcl.\n");
  330. return -1;
  331. }
  332. /* Test to see if we can append this dcl to the current */
  333. /* block. */
  334. if(cur_blk.inmem || cur_blk.Nrec != 1 || dcl.Nrec != 1){
  335. finish_curblk();
  336. cur_blk.Nrec = dcl.Nrec;
  337. cur_blk.reclen = 0;
  338. cur_blk.inmem = 0;
  339. cur_blk.begin_offset = cur_file_offset;
  340. #if YYDEBUG
  341. if(yydebug)
  342. printf("New block (%d) at offset %" PRId64 " in file\n",
  343. cur_hdr->nblks, cur_file_offset);
  344. #endif
  345. }
  346. if(dcl.Nrec == 0){
  347. no_more_data_blks = 1;
  348. zero_len_blknum = cur_hdr->nblks;
  349. }
  350. offset = cur_blk.reclen;
  351. dcl_list = &dcl.dcl_list;
  352. base = (one_dcl_t *)obstack_base(&dcl_list->obs);
  353. for(i=0; i<dcl_list->ndcl; i++){
  354. dclp = &base[i];
  355. vec_descrip.name = dclp->name;
  356. vec_descrip.arrcnt = dclp->arrcnt;
  357. vec_descrip.type = dclp->type;
  358. vec_descrip.blk_off = offset;
  359. vec_descrip.blk_num = cur_hdr->nblks;
  360. vec_descrip.nread = 0;
  361. offset += SDFtype_sizes[dclp->type] * dclp->arrcnt;
  362. cur_hdr->nvecs++;
  363. (void)obstack_grow(&cur_hdr->vecs_obs,
  364. &vec_descrip, sizeof(vec_descrip));
  365. #if YYDEBUG
  366. if(yydebug){
  367. printf("\t %s %s[%d]", SDFtype_names[dclp->type], dclp->name,
  368. dclp->arrcnt);
  369. printf(" in block %"PRId64" at offset %" PRId64 "\n",
  370. vec_descrip.blk_num, vec_descrip.blk_off);
  371. }
  372. #endif
  373. }
  374. (void)obstack_free(&dcl_list->obs, NULL);
  375. cur_blk.reclen = offset;
  376. return 0;
  377. }
  378. static void finish_curblk(void)
  379. {
  380. cur_hdr->nblks++;
  381. (void)obstack_grow(&cur_hdr->blks_obs, &cur_blk, sizeof(cur_blk));
  382. if(cur_blk.inmem){
  383. cur_data_offset += cur_blk.reclen * cur_blk.Nrec;
  384. }else{
  385. cur_file_offset += cur_blk.reclen * cur_blk.Nrec;
  386. }
  387. }
  388. static int const_dcl(declaration_t dcl, const_list_t consts)
  389. {
  390. dcl_list_t *dcl_list;
  391. one_dcl_t *dclbase, *dclp;
  392. const_t *cp, *cbase, converted;
  393. vec_descrip_t vec_descrip;
  394. int i, j, k;
  395. int offset;
  396. void *to;
  397. dcl_list = &dcl.dcl_list;
  398. if(dcl.Nrec == 0){
  399. dcl.Nrec = consts.nconst;
  400. #if 0 /* Was it really this easy?? */
  401. yyerror("Cannot deal with implicit length constant dcls.");
  402. return -1;
  403. #endif
  404. }
  405. /* Test to see if we can append this dcl to the current */
  406. /* block. */
  407. if(!cur_blk.inmem || cur_blk.Nrec != 1 || dcl.Nrec != 1){
  408. finish_curblk();
  409. cur_blk.Nrec = dcl.Nrec;
  410. cur_blk.reclen = 0;
  411. cur_blk.inmem = 1;
  412. cur_blk.begin_offset = cur_data_offset;
  413. #if YYDEBUG
  414. if(yydebug)
  415. printf("New block (%d) at offset %" PRId64 " in data\n",
  416. cur_hdr->nblks, cur_data_offset);
  417. #endif
  418. }
  419. offset = cur_blk.reclen;
  420. cbase = (const_t *)obstack_base(&consts.obs);
  421. dclbase = (one_dcl_t *)obstack_base(&dcl_list->obs);
  422. for(i=0; i<dcl_list->ndcl; i++){
  423. dclp = &dclbase[i];
  424. if(dclp->arrcnt == 0){
  425. if(dclp->type == SDF_CHAR
  426. && cbase[i].type == SDF_STRING
  427. && dcl.Nrec == 1){
  428. dclp->arrcnt = strlen(cbase[i].u.stringval)+1;
  429. /* Round up for padding purposes. */
  430. dclp->arrcnt = (dclp->arrcnt + 7)& (~0x7);
  431. }else if(i == dcl_list->ndcl-1 && dcl.Nrec == 1){
  432. dclp->arrcnt = consts.nconst - i;
  433. }else{
  434. yyerror("Can't figure out implicit dcl from context.");
  435. return -1;
  436. }
  437. }
  438. vec_descrip.name = dclp->name;
  439. vec_descrip.arrcnt = dclp->arrcnt;
  440. vec_descrip.type = dclp->type;
  441. vec_descrip.blk_off = offset;
  442. vec_descrip.blk_num = cur_hdr->nblks;
  443. vec_descrip.nread = 0;
  444. offset += SDFtype_sizes[dclp->type] * dclp->arrcnt;
  445. cur_hdr->nvecs++;
  446. (void)obstack_grow(&cur_hdr->vecs_obs,
  447. &vec_descrip, sizeof(vec_descrip));
  448. #if YYDEBUG
  449. if(yydebug){
  450. printf("\t %s %s[%d]", SDFtype_names[dclp->type], dclp->name,
  451. dclp->arrcnt);
  452. printf(" in block %"PRId64" at offset %"PRId64"\n",
  453. vec_descrip.blk_num, vec_descrip.blk_off);
  454. }
  455. #endif
  456. }
  457. cur_blk.reclen = offset;
  458. cp = cbase;
  459. for(i=0; i<dcl.Nrec; i++){
  460. for(j=0; j<dcl_list->ndcl; j++){
  461. dclp = &dclbase[j];
  462. #if YYDEBUG
  463. if(yydebug)
  464. printf("\t %s %s[%d] (%d) = ",
  465. SDFtype_names[dclp->type], dclp->name,
  466. dclp->arrcnt, i);
  467. #endif
  468. if( dclp->type == SDF_CHAR && cp->type == SDF_STRING){
  469. #if YYDEBUG
  470. if(yydebug)
  471. printf("\"%s\"\n", cp->u.stringval);
  472. #endif
  473. to = obstack_next_free(&cur_hdr->data_obs);
  474. (void)obstack_blank(&cur_hdr->data_obs, dclp->arrcnt);
  475. /* Should we warn
  476. if strlen(cp->u.stringval) > dclp->arrcnt ????
  477. It implies that the 'string' won't be null-terminated? */
  478. (void)strncpy(to, cp->u.stringval, dclp->arrcnt);
  479. #ifdef YYDEBUG
  480. if(yydebug)
  481. printf("Freeing const string 0x%lx\n",
  482. (unsigned long)cp->u.stringval);
  483. #endif
  484. Free(cp->u.stringval);
  485. cp++;
  486. continue;
  487. }
  488. for(k=0; k<dclp->arrcnt; k++){
  489. converted = convert_const(cp, dclp->type);
  490. if(converted.type == SDF_NOTYPE){
  491. yyerror("Failed constant conversion.");
  492. return -1;
  493. }
  494. (void)obstack_grow(&cur_hdr->data_obs, &converted.u,
  495. SDFtype_sizes[converted.type]);
  496. #ifdef YYDEBUG
  497. if(yydebug){
  498. printf("(%s)", SDFtype_names[cp->type]);
  499. switch(converted.type){
  500. case SDF_CHAR:
  501. printf("%c", converted.u.charval);
  502. break;
  503. case SDF_INT16:
  504. printf("%" PRId16, converted.u.int16val);
  505. break;
  506. case SDF_INT32:
  507. printf("%" PRId32, converted.u.int32val);
  508. break;
  509. case SDF_INT64:
  510. printf("%"PRId64, converted.u.int64val);
  511. break;
  512. case SDF_FLOAT:
  513. printf("%.9g", converted.u.floatval);
  514. break;
  515. case SDF_DOUBLE:
  516. printf("%.17g", converted.u.doubleval);
  517. break;
  518. default:
  519. printf("Unrecognized type: %d\n", converted.type);
  520. break;
  521. }
  522. if(k == dclp->arrcnt-1){
  523. printf("\n");
  524. }else{
  525. printf(", ");
  526. }
  527. }
  528. #endif
  529. cp++;
  530. }
  531. }
  532. }
  533. (void)obstack_free(&dcl_list->obs, NULL);
  534. (void)obstack_free(&consts.obs, NULL);
  535. return 0;
  536. }
  537. static const_t convert_const(const_t *cp, enum SDF_type_enum newtype)
  538. /* Return a constant of type NEWTYPE, with the same value as */
  539. /* *CP. If the conversion does not preserve value, then */
  540. /* return a constant of NOTYPE, with garbage value. */
  541. {
  542. const_t value;
  543. double dval = 0.;
  544. int64_t ival = 0;
  545. if(cp->type == newtype){
  546. /* IRIX -32 bug fix */
  547. memcpy(&value, cp, sizeof(value));
  548. /* value = *cp; */
  549. return value;
  550. }
  551. if(cp->type == SDF_STRING || newtype == SDF_STRING){
  552. value.type = SDF_NOTYPE;
  553. yyerror("Cannot do const conversions with strings.\n");
  554. return value;
  555. }
  556. /* Now rely on the fact that a double can faithfully hold */
  557. /* any other arithmetic type (except long ints on 64-bit archs). */
  558. switch(cp->type){
  559. case SDF_CHAR:
  560. dval = (double)cp->u.charval;
  561. break;
  562. case SDF_INT16:
  563. dval = (double)cp->u.int16val;
  564. break;
  565. case SDF_INT32:
  566. dval = (double)cp->u.int32val;
  567. break;
  568. case SDF_INT64:
  569. dval = (double)cp->u.int64val;
  570. ival = cp->u.int64val;
  571. break;
  572. case SDF_FLOAT:
  573. dval = cp->u.floatval;
  574. break;
  575. case SDF_DOUBLE:
  576. dval = cp->u.doubleval;
  577. break;
  578. default:
  579. dval = 0.0;
  580. yyerror("Cannot do const conversions with strings.\n");
  581. }
  582. value.type = newtype;
  583. switch(newtype){
  584. case SDF_CHAR:
  585. value.u.charval = (char)dval;
  586. if( value.u.charval != dval ){
  587. yyerror("Can't fit value into char.");
  588. value.type = SDF_NOTYPE;
  589. }
  590. break;
  591. case SDF_INT16:
  592. value.u.int16val = (int16_t)dval;
  593. if( value.u.int16val != dval ){
  594. yyerror("Can't fit value into int16_t.");
  595. value.type = SDF_NOTYPE;
  596. }
  597. break;
  598. case SDF_INT32:
  599. value.u.int32val = (int32_t)dval;
  600. if( value.u.int32val != dval ){
  601. yyerror("Can't fit value into int16_t.");
  602. value.type = SDF_NOTYPE;
  603. }
  604. break;
  605. case SDF_INT64:
  606. value.u.int64val = ival;
  607. break;
  608. case SDF_FLOAT:
  609. if(dval > FLT_MAX || dval < -FLT_MAX){
  610. yyerror("Can't fit value into float.");
  611. value.type = SDF_NOTYPE;
  612. }
  613. value.u.floatval = dval;
  614. break;
  615. case SDF_DOUBLE:
  616. value.u.doubleval = dval;
  617. break;
  618. default:
  619. yyerror("Impossible case.\n");
  620. break;
  621. }
  622. return value;
  623. }
  624. void *SDFobstack_chunk_alloc(size_t n)
  625. {
  626. void *p = Malloc(n);
  627. #if YYDEBUG
  628. if(yydebug)
  629. printf("malloc(%ld) = 0x%lx\n", (long)n, (unsigned long)p);
  630. #endif
  631. return p;
  632. }
  633. void SDFobstack_chunk_free(void *p)
  634. {
  635. #if YYDEBUG
  636. if(yydebug)
  637. printf("free(0x%lx)\n", (unsigned long)p);
  638. #endif
  639. Free(p);
  640. }
  641. /* This symbol tells a Flex-based scanner not to bother trying to
  642. call isatty to figure out whether to do char-at-a-time input. The
  643. actual behavior is under our explicit control anyway (see SDF-lex.l),
  644. but linking against fileno() and isatty() can be annoying. */
  645. #define YY_ALWAYS_INTERACTIVE 1
  646. #include "SDF-lex.c"