Annotation of capa/capa51/pProj/capaGrammarDef.y, revision 1.10
1.1 albertel 1: /* ========================================================================== */
2: /* capaGrammarDef.y created by Isaac Tsai */
3: /* 1998, 1999 copyrighted by Isaac Tsai */
4: /* no longer there is a length constrain on string concatenation July 13 1998 */
5: /* /RMAP() function */
6: /* TODO: new mechanism to collect answer informations */
7: /*****************************************************************************/
8: /*****************************************************************************/
9: /*****************************************************************************/
10: %{
11: #include <stdio.h>
12: #include <ctype.h>
13: #include <string.h>
14: #include <math.h>
15: #include "capaParser.h" /* _symbol structure def */
16: #include "capaCommon.h"
17: #include "capaFunction.h"
18: #include "capaRQO.h"
19: #ifdef __hpux
20: #include <stdlib.h>
21: #include <alloca.h>
22: #endif
23:
24: #ifdef YACC_DBUG
25: #define YYDBUG_PR1(xx) { printf(xx); fflush(stdout); }
26: #define YYDBUG_PR2(xx,yy) { printf(xx,yy); fflush(stdout); }
27: #define YYDBUG_PR3(xx,yy,zz) { printf(xx,yy,zz); fflush(stdout); }
28: #define YYDBUG_PR4(xx,yy,zz,aa) { printf(xx,yy,zz,aa); fflush(stdout); }
29: #define YYDBUG_SYM(xx) { switch((xx)->s_type) { \
30: case IDENTIFIER: \
31: printf("ID(%s)\n",(xx)->s_name); break; \
32: case I_VAR: case I_CONSTANT: \
33: printf("INT(%d)\n",(xx)->s_int); break; \
34: case R_VAR: case R_CONSTANT: \
35: printf("REAL(%.16g)\n",(xx)->s_real); break; \
36: case S_VAR: case S_CONSTANT: \
37: printf("STR(%s)\n",(xx)->s_str); break; \
38: } }
1.9 albertel 39: #else /* YACC_DBUG */
1.1 albertel 40: #define YYDBUG_PR1(xx) { }
41: #define YYDBUG_PR2(xx,yy) { }
42: #define YYDBUG_PR3(xx,yy,zz) { }
43: #define YYDBUG_PR4(xx,yy,zz,aa) { }
44: #define YYDBUG_SYM(xx) { }
1.9 albertel 45: #endif /* YACC_DBUG */
1.1 albertel 46:
47: int yylex();
48: void yyerror(char*);
49: void free_calc_expr(Symbol*);
1.8 albertel 50: void assign_pts (Symbol *, Symbol *, Symbol *);
1.1 albertel 51: /******************************************************************************/
52: /* GLOBAL VARIABLES */
53: /******************************************************************************/
54: int Lexi_qnum;
55:
56: extern int Lexi_line;
57: extern int Lexi_pos[MAX_OPENED_FILE];
58: extern char Opened_filename[MAX_OPENED_FILE][QUARTER_K];
59: extern int Input_idx;
60: int Current_line[MAX_OPENED_FILE];
61: extern int Func_idx;
62: extern Symbol FuncStack[MAX_FUNC_NEST];
63: extern int IFstatus[MAX_FUNC_NEST];
64: extern int IFcount;
65: extern int gUnitError;
66:
67: int Parsemode_f; /* Parser mode flag */
68:
69: Problem_t *FirstProblem_p; /* First problem */
70: Problem_t *LastProblem_p; /* Last problem */
71: Problem_t *LexiProblem_p; /* Current problem */
72: char *EndText_p;
73: char *StartText_p;
74: char *ErrorMsg_p;
75: int ErrorMsg_count;
76: WarnMsg_t *WarnMsg_p;
77: int WarnMsg_count;
78: int Answer_infospec;
79: void (*Status_Func)();
80: AnswerInfo_t CurrAnsInfo;
81:
82: RandQO_t *QuestionOrder;
83: PointsList_t *CurrPtsList;
84: PointsList_t *LastPtsList;
85:
86:
87:
88:
89: #ifdef YYSTYPE
90: #undef YYSTYPE
91: #endif
92: #define YYSTYPE Symbol_p
93:
94:
95: #define ADD_op 1
96: #define SUB_op 2
97: #define MUL_op 3
98: #define DIV_op 4
99: #define IDIV_op 5
100: #define NOT_DEFINED_op 9
101:
102: /*#define yyerror printf*/
103:
104: %}
105: %token NEW_ID
106: %token I_CONSTANT R_CONSTANT S_CONSTANT
107: %token I_VAR R_VAR S_VAR
108:
109: %token IDENTIFIER FUNCTION_ID ARRAY_ID
110: %token HINT_LINE EXPLAIN_LINE TEXT_LINE IMPORT_LINE
111: %token CAPA_LET CAPA_DEF CAPA_DIS CAPA_END CAPA_VAR
112: %token CAPA_ESC CAPA_MAP CAPA_FIG CAPA_ANS CAPA_RMAP
113: %token CAPA_IF CAPA_ELSE CAPA_ENDIF CAPA_SUBJ CAPA_WHILE
114: %token CAPA_RQO CAPA_ENDWHILE CAPA_START
115: %token ANS_AND ANS_BOX_SHOW ANS_CALC ANS_CI ANS_COMPARE ANS_CS
116: %token ANS_EVAL ANS_EXPLAIN ANS_EXTERNAL ANS_FMT
117: %token ANS_FORMULA ANS_HINT ANS_MC ANS_MINUS
118: %token ANS_OFF ANS_ON ANS_OR ANS_ORDERED
119: %token ANS_PATH ANS_PCREDIT ANS_PLUS ANS_RANGE
120: %token ANS_SHOW_BR ANS_SIG ANS_TOLERANCE ANS_TRY ANS_TYPE
1.10 ! albertel 121: %token ANS_UNFMT ANS_UNIT ANS_VERBATIM ANS_WEIGHT
1.1 albertel 122: %token VAR_RANGE VERBATIM
123: %token SLASH FORMAT
124: %token EQ_op NE_op GT_op GE_op LT_op LE_op AND_op OR_op EoL
125:
126: %start prob_set
127:
128: %%
129:
130: prob_set : startQ questions CAPA_END { YYDBUG_PR1(" prob_set := startQ questions END\n\n"); }
131: ;
132:
133: questions : a_line { YYDBUG_PR1(" questions <= a_line");
134: if (Status_Func != NULL) Status_Func();
135: }
136: | questions a_line { YYDBUG_PR1(" questions <= questions a_line");
137: if (Status_Func != NULL) Status_Func();
138: }
139: ;
140:
141: startL : CAPA_LET { YYDBUG_PR1("\n begin_let::"); }
142: ;
143:
144: startV : CAPA_VAR { YYDBUG_PR1(" begin_var"); }
145: ;
146:
147: startA : CAPA_ANS { YYDBUG_PR1("\n START ANSWER(/ANS)::\n");
148: init_answerinfo();
149: }
150: ;
151:
152: startSA : CAPA_SUBJ { YYDBUG_PR1("\n START SUBJECT ANSWER(/SUBJECTIVE)::\n");
153: init_answerinfo();
154: }
155: ;
156:
157: startM : CAPA_MAP { YYDBUG_PR1("\n begin_map::"); }
158: ;
159:
160: startR : CAPA_RMAP { YYDBUG_PR1("\n begin_rmap::"); }
161: ;
162:
163: startRQO : CAPA_RQO { YYDBUG_PR1("\n begin_rqo::"); }
164: ;
165:
166: ans_and_op : ANS_AND { add_answer_cnt(ANS_AND); YYDBUG_PR1("(AND ,new an answer info)"); }
167: ;
168:
169: ans_or_op : ANS_OR { add_answer_cnt(ANS_OR); YYDBUG_PR1("(OR ,new an answer info)"); }
170: ;
171:
172: a_line : startL statement EoL { YYDBUG_PR1(" a_line <= startL statement CR\n"); }
173: | CAPA_END { YYDBUG_PR1(" a_line <= END\n\n"); }
174: | startRQO rqo_def EoL { YYDBUG_PR1(" aline <= CAPA_RQO\n");
175: rqo_finish();
176: }
177: | CAPA_START { YYDBUG_PR1(" aline <= CAPA_START\n");
178: start_question_over();
179: }
180: | HINT_LINE { append_hint($1->s_str);
181: YYDBUG_PR2(" a_line <= Hint_line(%s)\n",$1->s_str);
182: capa_mfree($1->s_str); capa_mfree((char *)$1);
183: }
184: | EXPLAIN_LINE { append_explain($1->s_str);
185: YYDBUG_PR2(" a_line <= Explain_line(%s)\n",$1->s_str);
186: capa_mfree($1->s_str); capa_mfree((char*)$1);
187: }
188: | IMPORT_LINE EoL { YYDBUG_PR1(" a_line <= import_line CR\n"); }
189: | q_text EoL { YYDBUG_PR1(" a_line <= Qtext CR\n"); append_text("\n"); }
190: | answer_expr { YYDBUG_PR1(" a_line <= answer_expr (init a new prob) CR\n");
191: init_new_prob(); }
192: | if_expr { YYDBUG_PR1(" a_line <= if_expr\n"); }
193: | while_expr { YYDBUG_PR1(" a_line <= while_expr\n"); }
194: | map_expr EoL { YYDBUG_PR1(" a_line <= map_expr CR\n"); }
195: | EoL { YYDBUG_PR1(" a_line <= (CR)\n"); }
196: | VERBATIM { YYDBUG_PR1(" a_line <= (VERBATIM)\n");
197: switch(Parsemode_f) {
1.2 albertel 198: case TeX_MODE: append_text("\\begin{verbatim}");
1.1 albertel 199: break;
200: case HTML_MODE: append_text("<PRE>");
201: break;
202: }
203: append_text($1->s_str);
204: capa_mfree($1->s_str); capa_mfree((char *)$1);
205: switch(Parsemode_f) {
1.2 albertel 206: case TeX_MODE: append_text("\\end{verbatim}\n");
1.1 albertel 207: break;
1.2 albertel 208: case HTML_MODE: append_text("</PRE>\n");
1.1 albertel 209: break;
210: }
211: }
212: | error EoL { char warn_msg[WARN_MSG_LENGTH];
213: YYDBUG_PR1(" a_line := ERROR(CR)\n");
214: sprintf(warn_msg," Question %d: Syntax error.\n", Lexi_qnum+1);
215: capa_msg(MESSAGE_ERROR,warn_msg);
216: begin_text();
217: }
218:
219: ;
220:
221: statement : IDENTIFIER '=' calc_expr
222: { char warn_msg[WARN_MSG_LENGTH];
223:
224: if ( $1 != $3 ) { /* /LET a = a */
225: switch($1->s_type) {
226: case IDENTIFIER:
227: case I_VAR: case I_CONSTANT:
228: case R_VAR: case R_CONSTANT: break;
229: case S_VAR: case S_CONSTANT:
230: capa_mfree($1->s_str); $1->s_str = NULL; break;
231: default: break;
232: }
233: switch($3->s_type) {
234: case IDENTIFIER:
235: sprintf(warn_msg,"var \"%s\" not defined before use.\n",$3->s_name);
236: capa_msg(MESSAGE_ERROR,warn_msg);
237: break;
238: case I_VAR: case I_CONSTANT:
239: $1->s_type = I_VAR;
240: $1->s_int = $3->s_int;
241: break;
242: case R_VAR: case R_CONSTANT:
243: $1->s_type = R_VAR;
244: $1->s_real = $3->s_real;
245: break;
246: case S_VAR: case S_CONSTANT:
247: $1->s_type = S_VAR;
248: $1->s_str = strsave($3->s_str);
249: break;
250: }
251: YYDBUG_PR1(" statement <= ID = calc_expr:: "); YYDBUG_SYM($3);
252: free_calc_expr($3);
253: }
254: }
255: | ARRAY_ID '[' calc_expr ']' '=' calc_expr
256: { Symbol *s_p;
257: char warn_msg[WARN_MSG_LENGTH];
258:
259: s_p = get_array_symbol($1,$3,1);
260: switch(s_p->s_type) {
261: case IDENTIFIER:
262: case I_VAR: case I_CONSTANT:
263: case R_VAR: case R_CONSTANT: break;
264: case S_VAR: case S_CONSTANT:
265: capa_mfree(s_p->s_str); s_p->s_str = NULL; break;
266: default: break;
267: }
268: switch($6->s_type) {
269: case IDENTIFIER:
270: sprintf(warn_msg,"var \"%s\" not defined before use.\n",$6->s_name);
271: capa_msg(MESSAGE_ERROR,warn_msg);
272: break;
273: case I_VAR: case I_CONSTANT:
274: s_p->s_type = I_VAR;
275: s_p->s_int = $6->s_int;
276: break;
277: case R_VAR: case R_CONSTANT:
278: s_p->s_type = R_VAR;
279: s_p->s_real = $6->s_real;
280: break;
281: case S_VAR: case S_CONSTANT:
282: s_p->s_type = S_VAR;
283: s_p->s_str = strsave($6->s_str);
284: break;
285: }
286: free_calc_expr($6);
287: }
288: ;
289:
290: rqo_def : rqo_speca { rqo_1spec(); }
1.8 albertel 291: | rqo_speca ',' rqo_def { rqo_2spec(); }
1.1 albertel 292: ;
293:
294: rqo_speca : an_integer {start_rqo_type(SINGLE);append_rqo($1);}
1.8 albertel 295: | an_integer '!' {start_rqo_type(IMMOBILE);append_rqo($1);}
296: | an_integer '-' an_integer {start_rqo_type(RANGE);append_rqo($1);
1.1 albertel 297: append_rqo($3);
298: }
1.8 albertel 299: | an_integer '~' rqo_specb {prefix_rqo($1);}
300: | an_integer '+' rqo_specc {prefix_rqo($1);}
1.1 albertel 301: ;
302:
303: rqo_specb : an_integer {start_rqo_type(ALL_MIX);append_rqo($1);}
1.8 albertel 304: | an_integer '~' rqo_specb {prefix_rqo($1);}
305: | an_integer '+' an_integer {start_rqo_type(LAST_FIXED);append_rqo($1);
1.1 albertel 306: append_rqo($3);}
307: ;
308:
309: rqo_specc : an_integer {start_rqo_type(ALL_FIXED);append_rqo($1);}
1.8 albertel 310: | an_integer '+' rqo_specd {prefix_rqo($1);}
311: | an_integer '~' rqo_spece {prefix_rqo($1);}
1.1 albertel 312: ;
313:
314: rqo_specd : an_integer {start_rqo_type(ALL_FIXED);append_rqo($1);}
1.8 albertel 315: | an_integer '+' rqo_specd {prefix_rqo($1);}
1.1 albertel 316: ;
317:
318: rqo_spece : an_integer {start_rqo_type(FIRST_FIXED);append_rqo($1);}
1.8 albertel 319: | an_integer '~' rqo_spece {prefix_rqo($1);}
320: | an_integer '+' an_integer {start_rqo_type(BOTH_FIXED);append_rqo($1);
1.1 albertel 321: append_rqo($3);}
322: ;
323:
324:
325: q_text : TEXT_LINE { append_text($1->s_str);
326: capa_mfree($1->s_str); capa_mfree((char *)$1);
327: }
328: | var_expr { }
329: | q_text var_expr { }
330: | q_text TEXT_LINE { append_text($2->s_str);
331: capa_mfree($2->s_str); capa_mfree((char *)$2);
332: }
333: ;
334:
335: if_expr : CAPA_IF '(' calc_expr ')'
1.4 albertel 336: { int leng=0; /* begin_next_line(); no use, can be get rid of */
1.1 albertel 337: YYDBUG_PR2("(IF expr <IFcount=%d>)\n",IFcount);
338: switch($3->s_type) {
339: case IDENTIFIER:
340: IFstatus[IFcount] = IF_FALSE;
341: begin_next_line();
342: break;
343: case I_CONSTANT: case I_VAR:
344: if(!$3->s_int) {
345: IFstatus[IFcount] = IF_FALSE;
346: begin_if_skip();
347: } else {
348: IFstatus[IFcount] = IF_TRUE;
349: begin_next_line();
350: }
351: break;
352: case R_CONSTANT: case R_VAR:
353: if($3->s_real == 0.0) {
354: IFstatus[IFcount] = IF_FALSE;
355: begin_if_skip();
356: }else{
357: IFstatus[IFcount] = IF_TRUE;
358: begin_next_line();
359: }
360: break;
361: case S_CONSTANT:
1.4 albertel 362: if ( $3->s_str) {
363: leng = strlen($3->s_str);
364: capa_mfree($3->s_str);
365: }
1.1 albertel 366: if(leng == 0) {
367: IFstatus[IFcount] = IF_FALSE;
368: begin_if_skip();
369: }else{
370: IFstatus[IFcount] = IF_TRUE;
371: begin_next_line();
372: }
373: break;
374: case S_VAR:
1.4 albertel 375: if ( $3->s_str) {
376: leng = strlen($3->s_str);
377: capa_mfree($3->s_str);
378: }
1.5 albertel 379: if(leng == 0) {
1.1 albertel 380: IFstatus[IFcount] = IF_FALSE;
381: begin_if_skip();
382: }else{
383: IFstatus[IFcount] = IF_TRUE;
384: begin_next_line();
385: }
386: break;
387: }
388: capa_mfree((char*)$3);
389: }
390: ;
391:
392: while_expr : CAPA_WHILE '(' calc_expr ')'
393: {
394: int leng;
395: YYDBUG_PR1("(WHILE expr)\n");
396: switch($3->s_type) {
397: case IDENTIFIER: /* undefined identifier regarded as false */
398: begin_while_skip();
399: break;
400: case I_CONSTANT: case I_VAR:
401: if(!$3->s_int) {
402: begin_while_skip();
403: } else {
404: begin_next_line(); /* skip to EoL and begin S_TEXT */
405: }
406: break;
407: case R_CONSTANT: case R_VAR:
408: if($3->s_real == 0.0) {
409: begin_while_skip();
410: }else{
411: begin_next_line(); /* skip to EoL and begin S_TEXT */
412: }
413: break;
414: case S_CONSTANT:
415: leng = strlen($3->s_str);
416: capa_mfree($3->s_str);
417: if(leng == 0) {
418: begin_while_skip();
419: }else{
420: begin_next_line(); /* skip to EoL and begin S_TEXT */
421: }
422: break;
423: case S_VAR:
424: leng = strlen($3->s_str);
425: if(leng == 0) {
426: begin_while_skip();
427: }else{
428: begin_next_line(); /* skip to EoL and begin S_TEXT */
429: }
430: break;
431: }
432: capa_mfree((char*)$3);
433: }
434: ;
435:
436: var_expr : startV '(' formated_ans ')'
437: { display_var( $3 ) ; }
438: ;
439:
440: answer_expr : answer_spec { finish_answer_info();
441: }
442: | answer_expr ans_and_op answer_spec
443: { finish_answer_info();
444: YYDBUG_PR1(" answer_expr <-- AND answers (copy answerinfo)\n"); }
445: | answer_expr ans_or_op answer_spec
446: { finish_answer_info();
447: YYDBUG_PR1(" answer_expr <-- OR answers (copy answerinfo)\n");
448: } | startSA '(' answer_info ')'
449: { YYDBUG_PR1("\n subjective answer\n");
450: finish_answer_info();
451: LexiProblem_p->ans_type = ANSWER_IS_SUBJECTIVE;
452: }
453: | startSA '(' ')'
454: { YYDBUG_PR1("\n subjective answer\n");
455: finish_answer_info();
456: LexiProblem_p->ans_type = ANSWER_IS_SUBJECTIVE;
457: }
458:
459: ;
460:
461: answer_spec : startA '(' formated_ans ')'
462: { assign_answer( $3 );
463: YYDBUG_PR1("\nASSIGN Answer\n");
464: }
465: | startA '(' formated_ans ',' answer_info ')'
466: { assign_answer( $3 );
467: YYDBUG_PR1("\nASSIGN Answers + Answer Info\n");
468: }
469: ;
470:
471: answer_info : ans_infospec
472: | answer_info ',' ans_infospec
473: ;
474:
475:
476: ans_infospec : ANS_TOLERANCE '=' a_number
477: { YYDBUG_PR1(" ans_infospec:= TOL=a_number");
478: assign_tolerance(TOL_ABSOLUTE,$3);
479: }
480: | ANS_TOLERANCE '=' IDENTIFIER
481: { assign_tolerance(TOL_ABSOLUTE,$3);
482: }
483: | ANS_TOLERANCE '=' IDENTIFIER '%'
484: { assign_tolerance(TOL_PERCENTAGE,$3);
485: }
486: | ANS_TOLERANCE '=' a_number '%'
487: { assign_tolerance(TOL_PERCENTAGE,$3);
488: }
489: | ANS_COMPARE '=' answer_comp { }
490: | ANS_SIG '=' answer_sig { }
491: | ANS_WEIGHT '=' an_integer
492: { assign_weight( $3 );
493: }
494: | ANS_WEIGHT '=' IDENTIFIER
495: { assign_weight( $3 );
496: }
497: | ANS_HINT '=' an_integer { assign_hint( $3 );
498: }
499: | ANS_HINT '=' IDENTIFIER { assign_hint( $3 );
500: }
501: | ANS_PCREDIT '=' ANS_ON { LexiProblem_p->partial_cdt = 1;
502: }
503: | ANS_PCREDIT '=' ANS_OFF { LexiProblem_p->partial_cdt = 0;
504: }
505: | ANS_SHOW_BR '=' ANS_ON { LexiProblem_p->show_br = DO_SHOW;
506: }
507: | ANS_SHOW_BR '=' ANS_OFF { LexiProblem_p->show_br = DONOT_SHOW;
508: }
1.10 ! albertel 509: | ANS_VERBATIM '=' ANS_ON { LexiProblem_p->verbatim = DO_VERBATIM;
! 510: }
! 511: | ANS_VERBATIM '=' ANS_OFF { LexiProblem_p->verbatim = DONOT_VERBATIM;
! 512: }
1.1 albertel 513: | ANS_BOX_SHOW '=' ANS_ON { LexiProblem_p->show_ans_box = DO_SHOW;
514: }
515: | ANS_BOX_SHOW '=' ANS_OFF { LexiProblem_p->show_ans_box = DONOT_SHOW;
516: }
517: | ANS_CALC '=' ANS_FMT { CurrAnsInfo.ans_calc = CALC_FORMATED;
518: }
519: | ANS_CALC '=' ANS_UNFMT { CurrAnsInfo.ans_calc = CALC_UNFORMATED;
520: }
521: | ANS_TRY '=' an_integer { assign_try_limits( $3 );
522: }
523: | ANS_TRY '=' IDENTIFIER { assign_try_limits( $3 );
524: }
525: | ANS_UNIT '=' S_CONSTANT { assign_units( $3 ); capa_mfree($3->s_str); capa_mfree((char *)$3);
526: }
527: | ANS_UNIT '=' IDENTIFIER { assign_units( $3 );
528: }
529: | ANS_EVAL '=' var_range { CurrAnsInfo.ans_pts_list = CurrPtsList;
530: CurrPtsList=NULL; LastPtsList = NULL;
531: }
532: ;
533:
534:
535: var_range : '<' S_CONSTANT '@' pt_list '>' { assign_id_list( $2 );
536: capa_mfree($2->s_str);
537: capa_mfree((char *)$2);
538: }
539: | '<' IDENTIFIER '@' pt_list '>' { assign_id_list( $2 ); }
540: ;
541:
542: pt_list : pt_list ',' point_coord { int idx;
543: idx = LastPtsList->pts_idx; idx++;
544: LastPtsList->pts_next = new_ptslist( $3 );
545: LastPtsList = LastPtsList->pts_next;
546: LastPtsList->pts_idx = idx;
547: CurrPtsList->pts_idx = idx;
548: if( $3->s_type == S_CONSTANT ) {
549: capa_mfree($3->s_str); capa_mfree((char *)$3);
550: }
551: }
552: | pt_list ',' pt_range { }
553: | point_coord { CurrPtsList = new_ptslist( $1 );
554: LastPtsList = CurrPtsList;
555: if( $1->s_type == S_CONSTANT ) {
556: capa_mfree($1->s_str); capa_mfree((char *)$1);
557: }
558: }
559: | pt_range { }
560: ;
561:
1.6 albertel 562: pt_range : point_coord ':' point_coord '#' IDENTIFIER
563: {
564: assign_pts($1,$3,$5);
565: /*PointsList_t *pt;
566: if( LastPtsList != NULL ) {
567: LastPtsList->pts_next = gen_ptslist( $1, $3, $5 );
568: pt = LastPtsList->pts_next;
569: while( pt->pts_next != NULL ) {
570: pt = pt->pts_next;
571: }
572: LastPtsList = pt;
573: } else {
574: CurrPtsList = gen_ptslist( $1, $3, $5 );
575: LastPtsList = CurrPtsList;
576: }
577: if( $1->s_type == S_CONSTANT ) {
578: capa_mfree($1->s_str); capa_mfree((char *)$1);
579: }
580: if( $3->s_type == S_CONSTANT ) {
581: capa_mfree($3->s_str); capa_mfree((char *)$3);
582: }
583: */
584: }
585: | point_coord ':' point_coord '#' ARRAY_ID '[' calc_expr ']'
586: { assign_pts($1,$3,get_array_symbol($5,$7,1)); }
587: | point_coord ':' point_coord '#' a_number
588: {
589: assign_pts($1,$3,$5);
590: /*PointsList_t *pt;
591:
592: if( LastPtsList != NULL ) {
593: LastPtsList->pts_next = gen_ptslist( $1, $3, $5 );
594: pt = LastPtsList->pts_next;
595: while( pt->pts_next != NULL ) {
596: pt = pt->pts_next;
597: }
598: LastPtsList = pt;
599: } else {
600: CurrPtsList = gen_ptslist( $1, $3, $5 );
601: LastPtsList = CurrPtsList;
602: }
603: if( $1->s_type == S_CONSTANT ) {
604: capa_mfree($1->s_str); capa_mfree((char *)$1);
605: }
606: if( $3->s_type == S_CONSTANT ) {
607: capa_mfree($3->s_str); capa_mfree((char *)$3);
608: }
609: if( $5->s_type == I_CONSTANT || $5->s_type == R_CONSTANT) {
610: capa_mfree((char *)$5);
611: }
612: */
613: }
1.1 albertel 614: ;
615:
616:
1.6 albertel 617: point_coord : IDENTIFIER { $$ = $1; }
618: | ARRAY_ID '[' calc_expr ']' { $$ = get_array_symbol($1,$3,1); }
619: | S_CONSTANT { $$ = $1; }
1.1 albertel 620: ;
621:
622:
623:
624: formated_ans : calc_expr { $1->s_distype = DEFAULT_FORMAT;
625: $$ = $1;
626: $1->s_format = NULL;
627: YYDBUG_PR2(" formated_ans := calc_expr (type %d)",$1->s_type);
628: }
629: | calc_expr FORMAT { $1->s_distype = $2->s_distype;
630: $1->s_format = strsave($2->s_str); /* **** */
631: capa_mfree($2->s_str); capa_mfree((char *)$2);
632: $$ = $1;
633: YYDBUG_PR1(" formated_ans <= calc_expr FORMAT");
634: }
635: ;
636:
637:
638:
639: answer_sig : an_integer { assign_sigs( $1->s_int,$1->s_int);
640: capa_mfree((char *)$1);
641: }
642: | an_integer ANS_PLUS an_integer
643: { assign_sigs($1->s_int,$1->s_int + $3->s_int);
644: capa_mfree((char *)$1); capa_mfree((char *)$3);
645: }
646: | an_integer ANS_MINUS an_integer
647: { assign_sigs($1->s_int - $3->s_int,$1->s_int);
648: capa_mfree((char *)$1); capa_mfree((char *)$3);
649: }
650: | an_integer ANS_PLUS an_integer ANS_MINUS an_integer
651: { assign_sigs($1->s_int - $5->s_int,$1->s_int + $3->s_int);
652: capa_mfree((char *)$1); capa_mfree((char *)$3); capa_mfree((char *)$5);
653: }
654: | an_integer ANS_MINUS an_integer ANS_PLUS an_integer
655: { assign_sigs($1->s_int - $3->s_int,$1->s_int + $5->s_int);
656: capa_mfree((char *)$1); capa_mfree((char *)$3); capa_mfree((char *)$5);
657: }
658: ;
659:
660: answer_comp : ANS_CS { CurrAnsInfo.ans_type = ANSWER_IS_STRING_CS; }
661: | ANS_CI { CurrAnsInfo.ans_type = ANSWER_IS_STRING_CI; }
662: | ANS_MC { CurrAnsInfo.ans_type = ANSWER_IS_CHOICE; }
663: | ANS_FORMULA { CurrAnsInfo.ans_type = ANSWER_IS_FORMULA; }
664: | ANS_EXTERNAL { CurrAnsInfo.ans_type = ANSWER_IS_EXTERNAL; }
665: ;
666:
667:
668: map_expr : startM '(' basic_constr ';' var_list ';' arg_list ')'
1.3 albertel 669: { char key[SMALL_LINE_BUFFER];
1.1 albertel 670: char warn_msg[WARN_MSG_LENGTH];
671: int result=0;
672:
673: YYDBUG_PR1(" map_expr body executed\n");
674: sprintf(key,"%ld",$3->s_int);
675: if( $5->s_argc == $7->s_argc ) {
676: result=do_map(key, $5->s_argp, $7->s_argp, $5->s_argc, FORWARD_MAP);
677: } else {
678: if ($5->s_argc==1) {
679: Symbol *a_sp;
680: a_sp=build_array_list($5,$7->s_argc);
681: result=do_map(key, a_sp->s_argp, $7->s_argp, a_sp->s_argc, FORWARD_MAP);
682: free_arglist(a_sp->s_argp);
683: } else {
684: sprintf(warn_msg,"/MAP arg. counts are not matched.\n");
685: capa_msg(MESSAGE_ERROR,warn_msg);
686: }
687: }
688: if (result!=0) {
689: sprintf(warn_msg,
690: "/MAP had invalid arguments.\n");
691: capa_msg(MESSAGE_ERROR,warn_msg);
692: }
693: free_arglist($5->s_argp);
694: free_arglist($7->s_argp);
695: }
696: | startR '(' basic_constr ';' var_list ';' arg_list ')'
1.3 albertel 697: { char key[SMALL_LINE_BUFFER];
1.1 albertel 698: char warn_msg[WARN_MSG_LENGTH];
699: int result=0;
700:
701: YYDBUG_PR1(" rmap_expr body executed\n");
702: sprintf(key,"%ld",$3->s_int);
703: if( $5->s_argc == $7->s_argc ) {
704: result=do_map(key, $5->s_argp, $7->s_argp, $5->s_argc, REVERSE_MAP);
705:
706: } else {
707: if ($5->s_argc==1) {
708: Symbol *a_sp;
709: a_sp=build_array_list($5,$7->s_argc);
710: result=do_map(key, a_sp->s_argp, $7->s_argp, a_sp->s_argc, FORWARD_MAP);
711: free_arglist(a_sp->s_argp);
712: } else {
713: sprintf(warn_msg,"/RMAP arg. counts are not matched.\n");
714: capa_msg(MESSAGE_ERROR,warn_msg);
715: }
716: }
717: if (result!=0) {
718: sprintf(warn_msg,
719: "/MAP had invalid arguments.\n");
720: capa_msg(MESSAGE_ERROR,warn_msg);
721: }
722: free_arglist($5->s_argp);
723: free_arglist($7->s_argp);
724: }
725: ;
726:
727:
728:
729: calc_expr : calc_expr EQ_op block { $$ = symbols_op($1, $3, EQ_op); }
730: | calc_expr NE_op block { $$ = symbols_op($1, $3, NE_op); }
731: | calc_expr GE_op block { $$ = symbols_op($1, $3, GE_op); }
732: | calc_expr GT_op block { $$ = symbols_op($1, $3, GT_op); }
733: | calc_expr LE_op block { $$ = symbols_op($1, $3, LE_op); }
734: | calc_expr LT_op block { $$ = symbols_op($1, $3, LT_op); }
735: | calc_expr AND_op block { $$ = symbols_op($1, $3, AND_op); }
736: | calc_expr OR_op block { $$ = symbols_op($1, $3, OR_op); }
737: | block { $$ = $1;
738: YYDBUG_PR1(" calc_expr <= block "); YYDBUG_SYM($1); }
739: ;
740:
741: block : block '+' term { $$ = symbols_op($1, $3, ADD_op); YYDBUG_PR1("block <= block '+' term "); YYDBUG_SYM($$); }
742: | block '-' term { $$ = symbols_op($1, $3, SUB_op); }
743: | term { $$ = $1; YYDBUG_PR2(" block <= term YYSTATE(%d) ",yystate); YYDBUG_SYM($1); }
744: ;
745:
746: term : term '*' basic_constr { $$ = symbols_op($1, $3, MUL_op); }
747: | term '/' basic_constr { $$ = symbols_op($1, $3, DIV_op); }
748: | term '%' basic_constr { $$ = symbols_op($1, $3, IDIV_op); }
749: | basic_constr { $$ = $1;
750: YYDBUG_PR1(" term <= basic_constr "); YYDBUG_SYM($1); }
751: ;
752:
753: basic_constr : FUNCTION_ID '(' ')' { int tmp;
754:
755: Func_idx--;
756: if(Func_idx >= 0 ) {
757: tmp = match_function(FuncStack[Func_idx].s_name,0);
758: $$ = do_function(tmp, 0, NULL );
759: capa_mfree(FuncStack[Func_idx].s_name);
760: }
761:
762: }
763: | FUNCTION_ID '(' arg_list ')'
764: { int tmp;
765:
766: Func_idx--;
767: YYDBUG_PR4(" basic_constr <= FUNCTION<%s><argc=%d> YYSTATE(%d) ",
768: FuncStack[Func_idx].s_name,$3->s_argc,yystate);
769:
770: if(Func_idx >= 0 ) {
771: tmp = match_function(FuncStack[Func_idx].s_name,$3->s_argc);
772: $$ = do_function(tmp, $3->s_argc, $3->s_argp);
773: capa_mfree(FuncStack[Func_idx].s_name);
774: free_arglist($3->s_argp);
775: }
776: YYDBUG_PR1(" basic_constr <= RETURN FUNCT "); YYDBUG_SYM($$);
777:
778: }
779: | an_array { $$ = $1; }
780: | IDENTIFIER { /* do not free identifier */
781: $$ = $1;
782: }
783: | '-' basic_constr { $$ = $2;
784: switch($2->s_type) {
785: case I_VAR: $$ = (Symbol *)capa_malloc(sizeof(Symbol),1);
786: $$->s_type = I_CONSTANT;
787: case I_CONSTANT: $$->s_int = - $2->s_int; break;
788: case R_VAR: $$ = (Symbol *)capa_malloc(sizeof(Symbol),1);
789: $$->s_type = R_CONSTANT;
790: case R_CONSTANT: $$->s_real = (-1.0)*($2->s_real);
791: break;
792: case S_VAR:
793: case S_CONSTANT: break;
794: default: break;
795: }
796: }
797: | '+' basic_constr { $$ = $2; }
798: | S_CONSTANT { $$ = $1; }
799: | a_number { $$ = $1; }
800: | '(' calc_expr ')' { $$ = $2; }
801: ;
802:
803: arg_list : arg_list ',' calc_expr { $$ = $1;
804: $$->s_argc++;
805: $$->s_argp = addto_arglist($1->s_argp, $3);
806: }
807: | calc_expr { $$ = $1;
808: $$->s_argc = 1;
809: $$->s_argp = new_arglist($1);
810: }
811: ;
812:
813:
814: var_list : IDENTIFIER { /* do not free identifier */
815: YYDBUG_PR1(" var_list <= ID");
816: $$ = $1;
817: $$->s_argc = 1;
818: $$->s_argp = new_arglist($1);
819: }
820: | ARRAY_ID '[' calc_expr ']'{
821: YYDBUG_PR1(" var_list <= ARRAYID,calc");
822: $$ = get_array_symbol($1,$3,1);
823: $$->s_argc = 1;
824: $$->s_argp = new_arglist($$);
825: }
826: | var_list ',' ARRAY_ID '[' calc_expr ']' {
827: YYDBUG_PR1(" var_list <= var_list,ARRAYID,calc");
828: $$ = $1;
829: $$->s_argc++;
830: $$->s_argp = addto_arglist($1->s_argp,
831: get_array_symbol($3,$5,1));
832: }
833: | var_list ',' IDENTIFIER { /* do not free identifier */
834: YYDBUG_PR1(" var_list <= var_list,ID");
835: $$ = $1;
836: $$->s_argc++;
837: $$->s_argp = addto_arglist($1->s_argp, $3);
838: }
839: ;
840:
841:
842: a_number : an_integer { $$ = $1; }
843: | a_real { $$ = $1; }
844: ;
845:
846: an_integer : I_CONSTANT { $$ = $1; }
847: ;
848:
849: a_real : R_CONSTANT { $$ = $1; }
850: ;
851:
1.9 albertel 852: an_array : ARRAY_ID '[' calc_expr ']' {
853: YYDBUG_PR1(" an_array <= ARRAY_ID '['calc_expr ']' ");
854: $$=get_array_symbol($1,$3,1);
855: }
1.1 albertel 856: ;
857:
858: startQ : { /* first matching will occur before first line of input text */
859: YYDBUG_PR1(" startQ := begin_question\n");
860: begin_question(); Answer_infospec = 0;
861: }
862: ;
863:
864:
865:
866: %%
867: /* ============================================================================ */
868: ExpNode_p
869: mk_node(op, left, right) int op; ExpNode_p left; ExpNode_p right;
870: {
871: ExpNode *np;
872:
873: np = (ExpNode* )malloc(sizeof(ExpNode));
874: np->e_type = op;
875: np->e_lsibp = left;
876: np->e_rsibp = right;
877: left->e_parentp = np;
878: right->e_parentp = np;
879: return (np);
880: }
881:
882: ExpNode_p
883: mk_leaf(type, valp) int type; Symbol_p valp;
884: {
885: ExpNode *np;
886:
887: np = (ExpNode*)malloc(sizeof(ExpNode));
888: np->e_type = IDENTIFIER;
889: np->e_sp = valp;
890: return (np);
891:
892: }
893:
894: /* ------------------------------------------------------------- */
895: void free_calc_expr(Symbol *expr)
896: {
897: switch(expr->s_type) {
898: case I_CONSTANT:
899: case R_CONSTANT: capa_mfree((char *)expr); break;
900: case S_CONSTANT: capa_mfree(expr->s_str); capa_mfree((char *)expr); break;
901: default: break;
902: }
903: }
904:
905: /* this is the entry point to array symbol retrieval */
906: /* array main name and index are used to locate the symbol */
907: /* name of the array is used to retrieve the array symbol */
908:
909: Symbol* get_array_symbol ( name,index,free_symbols )
910: Symbol *name,*index;int free_symbols;
911: {
912: Symbol *s_p, *a_p;
913: char *key, *tmp;
914: int leng, idx_len;
915: leng = strlen(name->s_name)+8; /* [ ] */
916:
917: switch(index->s_type) {
918: case I_VAR:
919: case I_CONSTANT: tmp = (char *)capa_malloc(64,1);
920: sprintf(tmp,"%ld",index->s_int);
921: break;
922: case R_VAR:
923: case R_CONSTANT: tmp = (char *)capa_malloc(64,1);
924: sprintf(tmp,"%g",index->s_real);
925: break;
926: case S_VAR:
927: case S_CONSTANT: idx_len = strlen(index->s_str); tmp = (char *)capa_malloc(idx_len+4,1);
928: sprintf(tmp,"\"%s\"",index->s_str); /* string array index is a bit different from integer one */
929: break;
930: default: break;
931: }
932: idx_len = strlen(tmp);
933:
934: key = (char *)capa_malloc(idx_len+leng,1);
935: sprintf(key,"%s[%s]",name->s_name,tmp);
936:
937: a_p = find_arrayid(name->s_name); /* use the array name to search array tree */
1.8 albertel 938: /* did not check for error! */
1.1 albertel 939: s_p = find_array_by_index(a_p,key); /* use the index portion to search along array linked list */
940: capa_mfree((char *)tmp); capa_mfree((char *)key);
941:
942: if (free_symbols) { /* free both the name symbol and index symbol */
943: if( (index->s_type == I_CONSTANT) || (index->s_type == R_CONSTANT) )
944: capa_mfree((char *)index);
945: if(index->s_type == S_CONSTANT) {
946: capa_mfree(index->s_str); capa_mfree((char *)index);
947: }
948: capa_mfree(name->s_name); capa_mfree((char *)name);
949: }
950: return (s_p);
951: }
952:
953: Symbol * build_array_list(ar_name,num_elem)
954: Symbol *ar_name;int num_elem;
955: {
956: int i;
957: Symbol *arg_list,*a_p;
1.3 albertel 958: char idx_str[MAX_BUFFER_SIZE];
1.1 albertel 959:
1.3 albertel 960: a_p = find_arrayid(ar_name->s_name);
1.1 albertel 961: i = 1;
1.3 albertel 962: sprintf(idx_str,"%s[%d]",ar_name->s_name,i); /* create array elements with integer index */
1.1 albertel 963: arg_list = find_array_by_index(a_p,idx_str); /* will create a new element if not found */
964: arg_list->s_argc=1;
965: arg_list->s_argp=new_arglist(arg_list);
966:
967: for (i=2;i<=num_elem;i++) {
1.3 albertel 968: sprintf(idx_str,"%s[%d]",ar_name->s_name,i);
1.1 albertel 969: arg_list->s_argc++;
970: arg_list->s_argp=addto_arglist(arg_list->s_argp,find_array_by_index(a_p,idx_str));
971: }
972: return arg_list;
973: }
974:
975:
976:
977:
978:
979:
980:
981: /* ------------------------------------------------------------- */
982: void
983: append_text(str) char *str;
984: {
985: char *q;
986: int i;
987:
988: if (!LexiProblem_p->question) {
989: if (!(q = capa_malloc(strlen(str)+1,1))) printf("No room to append.");
990: strncpy(q,str,strlen(str)+1);
991: } else {
992: i = strlen(LexiProblem_p->question);
993: i += (strlen(str)+1);
994: q = capa_malloc(i,1); /* *** */
995: if (!q) printf("No room to append().");
996: strcat(strncpy(q,LexiProblem_p->question, strlen(LexiProblem_p->question)+1), str);
997: capa_mfree(LexiProblem_p->question);
998: }
999: LexiProblem_p->question=q;
1000: }
1001:
1002: /******************************************************************************/
1003: /* ADD A STRING TO THE CURRENT HINT TEXT BLOCK */
1004: /******************************************************************************/
1005: void /* RETURNS: nothing */
1006: append_hint(str) /* ARGUMENTS: */
1007: char *str; /* String to add */
1008: { /* LOCAL VARIABLES: */
1009: char *q; /* New string */
1010:
1011: if (!LexiProblem_p->hint) {
1012: if (!(q = capa_malloc(strlen(str)+1,1)))
1013: printf("no room");
1014: strncpy(q,str,strlen(str)+1);
1015: } else {
1016: if (!(q = capa_malloc(strlen(LexiProblem_p->hint)+strlen(str)+1,1)))
1017: printf("no room");
1018: strcat(strncpy(q,LexiProblem_p->hint,strlen(LexiProblem_p->hint)), str);
1019: capa_mfree(LexiProblem_p->hint);
1020: }
1021: LexiProblem_p->hint=q;
1022: /* printf("APPEND HINT: %s\n", str); */
1023: }
1024: /******************************************************************************/
1025: /* ADD A STRING TO THE CURRENT EXPLAIN TEXT BLOCK */
1026: /******************************************************************************/
1027: void /* RETURNS: nothing */
1028: append_explain(str) /* ARGUMENTS: */
1029: char *str; /* String to add */
1030: { /* LOCAL VARIABLES: */
1031: char *q; /* New string */
1032:
1033: if (!LexiProblem_p->explain) {
1034: if (!(q = capa_malloc(strlen(str)+1,1)))
1035: printf("no room");
1036: strncpy(q,str, strlen(str)+1);
1037: } else {
1038: if (!(q = capa_malloc(strlen(LexiProblem_p->explain)+strlen(str)+1,1)))
1039: printf("no room");
1040: strcat(strncpy(q,LexiProblem_p->explain,strlen(LexiProblem_p->explain)+1), str);
1041: capa_mfree(LexiProblem_p->explain);
1042: }
1043: LexiProblem_p->explain=q;
1044: /* printf("APPEND EXPLAIN: %s\n", str); */
1045: }
1046:
1047: void
1048: append_error(str) char *str;
1049: {
1050: char *q;
1051: int i;
1052:
1053: ErrorMsg_count++;
1054: if (!ErrorMsg_p) {
1055: if (!(q = capa_malloc(strlen(str)+1,1))) printf("No room in append.");
1056: strncpy(q,str,strlen(str)+1);
1057: } else {
1058: i = strlen(ErrorMsg_p);
1059: i += (strlen(str)+1);
1060: q = capa_malloc(i,1); /* *** */
1061: if (!q) printf("No room in append()");
1062: strcat(strncpy(q,ErrorMsg_p, strlen(ErrorMsg_p)+1), str);
1063: capa_mfree(ErrorMsg_p);
1064: }
1065: ErrorMsg_p=q;
1066: /* printf("APPEND ERROR: %s\n", str); */
1067: }
1068: void
1069: append_warn(type,str) int type;char *str;
1070: {
1071: WarnMsg_t *p, *t;
1072: char *q;
1073:
1074: WarnMsg_count++;
1075: if (!WarnMsg_p) {
1076: if (!(p = (WarnMsg_t *)capa_malloc(sizeof(WarnMsg_t),1))) printf("No room in create WarnMsg_t.");
1077: if (!(q = capa_malloc(strlen(str)+1,1))) printf("No room in allocating space for warn msg.");
1078: strncpy(q,str,strlen(str)+1);
1079: p->warn_next = NULL;
1080: p->warn_type = type;
1081: p->warn_str = q;
1082: WarnMsg_p=p;
1083: } else {
1084: for(t=WarnMsg_p;t->warn_next;t=t->warn_next) { } /* do nothing within for loop */
1085: if (!(p = (WarnMsg_t *)capa_malloc(sizeof(WarnMsg_t),1))) printf("No room in create WarnMsg_t.");
1086: if (!(q = capa_malloc(strlen(str)+1,1))) printf("No room in allocating space for warn msg.");
1087: strncpy(q,str,strlen(str)+1);
1088: p->warn_next = NULL;
1089: p->warn_type = type;
1090: p->warn_str = q;
1091: t->warn_next = p;
1092: }
1093: }
1094:
1095: /*****************************************************************************/
1096: /*********** if *b is a constant symbol, destroy (free) b ********************/
1097: /*********** if *a is a var symbol, create a new symbol **********************/
1098: /* do not free(*a) */
1.7 albertel 1099: /* If either a or b is invalid it propagates the error up the parse tree */
1.1 albertel 1100: Symbol *
1101: symbols_op(a, b, op) Symbol *a; Symbol *b; int op;
1102: {
1103: int type, new, leng;
1104: long tmp_i, tmp_j;
1105: double tmp_p, tmp_q;
1106: char a_str[QUARTER_K], *b_str=NULL, *r_strp;
1107: char warn_msg[ONE_K];
1108: Symbol *a_symp;
1109:
1110: if( a->s_type == IDENTIFIER || b->s_type == IDENTIFIER ) {
1111: if(a->s_type == IDENTIFIER) { /* a is IDENTIFIER */
1112: sprintf(warn_msg,"var \"%s\" not defined before use.\n", a->s_name);
1113: capa_msg(MESSAGE_ERROR,warn_msg);
1.7 albertel 1114: return (a);
1.1 albertel 1115: } else { /* b is IDENTIFIER */
1.7 albertel 1116: sprintf(warn_msg,"var \"%s\" not defined before use.\n",b->s_name);
1.1 albertel 1117: capa_msg(MESSAGE_ERROR,warn_msg);
1.7 albertel 1118: return (b);
1.1 albertel 1119: }
1120: } else { /* a and b are neither identifiers */
1121: if( (a->s_type == I_VAR) ||
1122: (a->s_type == R_VAR) ||
1123: (a->s_type == S_VAR) ) {
1124: a_symp = (Symbol *)capa_malloc(sizeof(Symbol),1); /* *** */
1125: new = 1;
1126: } else {
1127: new = 0;
1128: a_symp = a; /* re-use symbol *a */
1129: }
1130: if( ((a->s_type == I_CONSTANT)||(a->s_type == I_VAR)) &&
1131: ((b->s_type == I_CONSTANT)||(b->s_type == I_VAR)) ) { /* both a and b are integer */
1132: type = I_CONSTANT;
1133: switch( op ) {
1134: case ADD_op: a_symp->s_int = a->s_int + b->s_int ; break;
1135: case SUB_op: a_symp->s_int = a->s_int - b->s_int ; break;
1136: case MUL_op: a_symp->s_int = a->s_int * b->s_int ; break;
1137: case DIV_op: if(b->s_int != 0) {
1138: a_symp->s_int = a->s_int / b->s_int ;
1139: } else {
1140: sprintf(warn_msg,"division (/) by zero!\n");
1141: capa_msg(MESSAGE_ERROR,warn_msg);
1142: } break;
1143: case IDIV_op: if(b->s_int != 0) {
1144: a_symp->s_int = (a->s_int % b->s_int );
1145: } else {
1146: sprintf(warn_msg,"integer division (%%) by zero!\n");
1147: capa_msg(MESSAGE_ERROR,warn_msg);
1148: } break;
1149: case EQ_op: a_symp->s_int = ((a->s_int == b->s_int)? 1: 0); break;
1150: case NE_op: a_symp->s_int = ((a->s_int == b->s_int)? 0: 1); break;
1151: case GT_op: a_symp->s_int = ((a->s_int > b->s_int)? 1: 0); break;
1152: case GE_op: a_symp->s_int = ((a->s_int >= b->s_int)? 1: 0); break;
1153: case LT_op: a_symp->s_int = ((a->s_int < b->s_int)? 1: 0); break;
1154: case LE_op: a_symp->s_int = ((a->s_int <= b->s_int)? 1: 0); break;
1155: case AND_op: a_symp->s_int = ((a->s_int && b->s_int)? 1: 0); break;
1156: case OR_op: a_symp->s_int = ((a->s_int || b->s_int)? 1: 0); break;
1157: }
1158: } else { /* a, b neither are integers */
1159: if( (a->s_type == S_VAR) || (a->s_type == S_CONSTANT) ||
1160: (b->s_type == S_VAR) || (b->s_type == S_CONSTANT) ) { /* either a or b is a string */
1161: type = S_CONSTANT; /* the return type is a string */
1162: if( (a->s_type == S_VAR) || (a->s_type == S_CONSTANT) ) { /* a is a string */
1.4 albertel 1163: if (a->s_str == NULL ||
1164: (((b->s_type == S_VAR) || (b->s_type == S_CONSTANT)) && b->s_str == NULL)) {
1165: if (a->s_str == NULL) {
1166: sprintf(warn_msg,"variable %s has not yet been assigned a value.\n",a->s_name);
1167: capa_msg(MESSAGE_ERROR,warn_msg);
1168: }
1169: if (((b->s_type == S_VAR) || (b->s_type == S_CONSTANT)) && b->s_str == NULL) {
1170: sprintf(warn_msg,"variable %s has not yet been assigned a value.\n",a->s_name);
1171: capa_msg(MESSAGE_ERROR,warn_msg);
1172: }
1173: } else { /* a is a valid string */
1174: switch( b->s_type ) {
1175: case I_VAR:
1176: case I_CONSTANT:
1177: leng = SMALL_LINE_BUFFER; /* assuming a long integer does not exceed 128 digits*/
1178: b_str= capa_malloc(sizeof(char), leng);
1179: sprintf(b_str,"%ld", b->s_int);
1180: break;
1181: case R_VAR:
1182: case R_CONSTANT:
1183: leng = SMALL_LINE_BUFFER;/*assuming a double does not exceed128chars*/
1184: b_str= capa_malloc(sizeof(char), leng);
1185: sprintf(b_str,"%.15g", b->s_real);
1186: break;
1187: case S_VAR:
1188: case S_CONSTANT: /* DONE: get rid of limitations on b_str[] */
1189: leng = strlen( b->s_str ) + 1;
1190: b_str= capa_malloc(sizeof(char), leng);
1191: sprintf(b_str,"%s",b->s_str);
1192: /*if(b->s_type == S_CONSTANT) capa_mfree(b->s_str);*/
1193: break;
1194: }
1195: switch( op ) {
1196: case ADD_op:
1197: leng = strlen( a->s_str ) + strlen(b_str) + 1;
1198: r_strp = capa_malloc(sizeof(char), leng); /* **** */
1199: strcat(r_strp, a->s_str);
1200: strcat(r_strp, b_str); /* concatenate two strings together */
1201: if( !new ) capa_mfree(a->s_str);
1202: a_symp->s_str = r_strp;
1203: break;
1204: case SUB_op:
1205: case MUL_op:
1206: case DIV_op:
1207: case IDIV_op:
1208: if( !new ) capa_mfree(a->s_str);
1209: a_symp->s_str = strsave("<<Op NOT DEFINED>>");
1210: sprintf(warn_msg,"integer division (%%) cannot accept string operand!\n");
1211: capa_msg(MESSAGE_ERROR,warn_msg);
1212: break;
1213: case EQ_op: a_symp->s_int = (strcmp(a->s_str, b_str) == 0? 1: 0);
1214: type = I_CONSTANT; break;
1215: case NE_op: a_symp->s_int = (strcmp(a->s_str, b_str) == 0? 0: 1);
1216: type = I_CONSTANT; break;
1217: case GT_op: a_symp->s_int = (strcmp(a->s_str, b_str) > 0? 1: 0);
1218: type = I_CONSTANT; break;
1219: case GE_op: a_symp->s_int = (strcmp(a->s_str, b_str) >= 0? 1: 0);
1220: type = I_CONSTANT; break;
1221: case LT_op: a_symp->s_int = (strcmp(a->s_str, b_str) < 0? 1: 0);
1222: type = I_CONSTANT; break;
1223: case LE_op: a_symp->s_int = (strcmp(a->s_str, b_str) <= 0? 1: 0);
1224: type = I_CONSTANT; break;
1225: case AND_op:
1226: if( (a->s_str[0] != 0) && (b_str[0] != 0)) {
1227: a_symp->s_int = 1;
1228: } else {
1229: a_symp->s_int = 0;
1230: }
1231: type = I_CONSTANT; break;
1232: case OR_op:
1233: if( (a->s_str[0] != 0) || (b_str[0] != 0)) {
1234: a_symp->s_int = 1;
1235: } else {
1236: a_symp->s_int = 0;
1237: }
1238: type = I_CONSTANT; break;
1239: }
1240: }
1.1 albertel 1241: if (b_str!=NULL) capa_mfree(b_str);
1242: } else { /* b is string and a is either integer or real */
1243: switch( a->s_type ) {
1244: case I_VAR:
1245: case I_CONSTANT:
1246: sprintf(a_str,"%ld", a->s_int); break;
1247: case R_VAR:
1248: case R_CONSTANT:
1249: sprintf(a_str,"%.15g", a->s_real); break;
1250: }
1251: switch( op ) {
1252: case ADD_op:
1253: leng = strlen( b->s_str ) + strlen(a_str) + 1;
1254: r_strp = capa_malloc(sizeof(char), leng); /* *** */
1255: strcat(r_strp, a_str);
1256: strcat(r_strp, b->s_str);
1257: /*if( b->s_type == S_CONSTANT ) capa_mfree(b->s_str);*/
1258: a_symp->s_str = r_strp; break;
1259: case SUB_op:
1260: case MUL_op:
1261: case DIV_op:
1262: case IDIV_op:
1263: a_symp->s_str = strsave("<<Op NOT DEFINED>>");
1264: sprintf(warn_msg,"integer division (%%) cannot accept string operand!\n");
1265: capa_msg(MESSAGE_ERROR,warn_msg);
1266: break;
1267: case EQ_op: a_symp->s_int = (strcmp(a_str, b->s_str) == 0? 1: 0);
1268: type = I_CONSTANT; break;
1269: case NE_op: a_symp->s_int = (strcmp(a_str, b->s_str) == 0? 0: 1);
1270: type = I_CONSTANT; break;
1271: case GT_op: a_symp->s_int = (strcmp(a_str, b->s_str) > 0? 1: 0);
1272: type = I_CONSTANT; break;
1273: case GE_op: a_symp->s_int = (strcmp(a_str, b->s_str) >= 0? 1: 0);
1274: type = I_CONSTANT; break;
1275: case LT_op: a_symp->s_int = (strcmp(a_str, b->s_str) < 0? 1: 0);
1276: type = I_CONSTANT; break;
1277: case LE_op: a_symp->s_int = (strcmp(a_str,b->s_str) <= 0? 1: 0);
1278: type = I_CONSTANT; break;
1279: case AND_op: if( (a_str[0] != 0) && (b->s_str[0] != 0)) {
1280: a_symp->s_int = 1;
1281: } else {
1282: a_symp->s_int = 0;
1283: }
1284: type = I_CONSTANT; break;
1285: case OR_op: if( (a_str[0] != 0) || (b_str[0] != 0)) {
1286: a_symp->s_int = 1;
1287: } else {
1288: a_symp->s_int = 0;
1289: }
1290: type = I_CONSTANT; break;
1291: }
1292: }
1293:
1294: } else { /* both a and b are real */
1295: type = R_CONSTANT;
1296: if( (a->s_type == R_CONSTANT)||(a->s_type == R_VAR) ) {
1297: tmp_p = a->s_real;
1298: } else {
1299: tmp_p = (double)a->s_int;
1300: }
1301: if( (b->s_type == R_CONSTANT)||(b->s_type == R_VAR) ) {
1302: tmp_q = b->s_real;
1303: } else {
1304: tmp_q = (double)b->s_int;
1305: }
1306: switch( op ) {
1307: case ADD_op: a_symp->s_real = tmp_p + tmp_q ; break;
1308: case SUB_op: a_symp->s_real = tmp_p - tmp_q ; break;
1309: case MUL_op: a_symp->s_real = tmp_p * tmp_q ; break;
1310: case DIV_op: if(tmp_q != 0.0) {
1311: a_symp->s_real = tmp_p / tmp_q ;
1312: } else {
1313: /* printf("FDIVISION by ZERO\n"); */
1314: sprintf(warn_msg,"division (/) by zero!\n");
1315: capa_msg(MESSAGE_ERROR,warn_msg);
1316: }
1317: break;
1318: case IDIV_op: if(tmp_q != 0.0 ) {
1319: tmp_i = (long)tmp_p;
1320: tmp_j = (long)tmp_q;
1321: a_symp->s_int = tmp_i % tmp_j;
1322: type = I_CONSTANT;
1323: } else {
1324: /* printf("DIVISION by ZERO\n"); */
1325: sprintf(warn_msg,"division (/) by zero!\n");
1326: capa_msg(MESSAGE_ERROR,warn_msg);
1327: }
1328: break;
1329: case EQ_op: type = I_CONSTANT;
1330: a_symp->s_int = ((tmp_p == tmp_q)? 1: 0); break;
1331: case NE_op: type = I_CONSTANT;
1332: a_symp->s_int = ((tmp_p == tmp_q)? 0: 1); break;
1333: case GT_op: type = I_CONSTANT;
1334: a_symp->s_int = ((tmp_p > tmp_q)? 1: 0); break;
1335: case GE_op: type = I_CONSTANT;
1336: a_symp->s_int = ((tmp_p >= tmp_q)? 1: 0); break;
1337: case LT_op: type = I_CONSTANT;
1338: a_symp->s_int = ((tmp_p < tmp_q)? 1: 0); break;
1339: case LE_op: type = I_CONSTANT;
1340: a_symp->s_int = ((tmp_p <= tmp_q)? 1: 0); break;
1341: case AND_op: type = I_CONSTANT;
1342: a_symp->s_int = ((tmp_p && tmp_q)? 1: 0); break;
1343: case OR_op: type = I_CONSTANT;
1344: a_symp->s_int = ((tmp_p || tmp_q)? 1: 0); break;
1345: }
1346: }
1347: }
1348: if( (b->s_type == I_CONSTANT) ||
1349: (b->s_type == R_CONSTANT) ) capa_mfree((char *)b); /* free symbol *b only */
1350: if( (b->s_type == S_CONSTANT) ) {
1351: capa_mfree((char *)b->s_str);
1352: capa_mfree((char *)b);
1353: }
1354: a_symp->s_type = type;
1355: return (a_symp);
1356: }
1357: }
1358:
1359: /* ------------------------------------------------------ */
1360: char *
1361: format_toTeX( real ) char *real;
1362: {
1363: int idx, length, fraclength, i_exp;
1.3 albertel 1364: char *expo_p, fracS[SMALL_LINE_BUFFER], result[ONE_K], *areal;
1.1 albertel 1365: char warn_msg[WARN_MSG_LENGTH];
1366:
1367: length = strlen(real);
1368: if( index( real, 'e' ) == NULL ) {
1369: if( index( real, 'E' ) == NULL ) {
1370: sprintf(result,"%s", real);
1371: } else {
1372: expo_p = index(real, 'E'); /*** hpux complained */
1373: fraclength = length - strlen(expo_p);
1374: expo_p++; if(expo_p[0] == '+') expo_p++;
1375: sscanf(expo_p,"%d",&i_exp);
1376: for(idx=0;idx<fraclength;idx++) fracS[idx] = real[idx];
1377: fracS[fraclength] = 0;
1378: if(i_exp == 0 ) {
1379: sprintf(result,"$%s$", fracS);
1380: } else {
1381: sprintf(result,"$%s \\times 10^{%d}$", fracS, i_exp);
1382: }
1383: }
1384: } else {
1385: if( index( real, 'E' ) == NULL ) {
1386: expo_p = index(real, 'e'); /*** hpux complained */
1387: fraclength = length - strlen(expo_p);
1388: expo_p++; if(expo_p[0] == '+') expo_p++;
1389: sscanf(expo_p,"%d",&i_exp);
1390: for(idx=0;idx<fraclength;idx++) fracS[idx] = real[idx];
1391: fracS[fraclength] = 0;
1392: if(i_exp == 0 ) {
1393: sprintf(result,"$%s$", fracS);
1394: } else {
1395: sprintf(result,"$%s \\times 10^{%d}$", fracS, i_exp);
1396: }
1397: } else {
1398: sprintf(result,"<<Ill-formed REAL>>");
1399: sprintf(warn_msg,"number %s is not a valid real number!\n",real);
1400: capa_msg(MESSAGE_ERROR,warn_msg);
1401: }
1402: }
1403: areal = (char *) capa_malloc(strlen(result)+1, 1);
1404: strcpy(areal,result);
1405: return (areal);
1406: }
1407: /* ------------------------------------------------------ */
1408: char *
1409: format_toHTML( real ) char *real;
1410: {
1411: int idx, length, fraclength, i_exp;
1.3 albertel 1412: char *expo_p, fracS[SMALL_LINE_BUFFER], result[ONE_K], *areal;
1.1 albertel 1413: char warn_msg[WARN_MSG_LENGTH];
1414:
1415: length = strlen(real);
1416: if( index( real, 'e' ) == NULL ) {
1417: if( index( real, 'E' ) == NULL ) {
1418: sprintf(result,"%s", real);
1419: } else {
1420: expo_p = index(real, 'E'); /*** hpux complained */
1421: fraclength = length - strlen(expo_p);
1422: expo_p++; if(expo_p[0] == '+') expo_p++;
1423: sscanf(expo_p,"%d",&i_exp);
1424: for(idx=0;idx<fraclength;idx++) fracS[idx] = real[idx];
1425: fracS[fraclength] = 0;
1426: if(i_exp == 0 ) {
1427: sprintf(result,"%s", fracS);
1428: } else {
1429: sprintf(result,"%s×10<sup>%d</sup>", fracS, i_exp); /* × is code for x */
1430: }
1431: }
1432: } else { /* the string contains 'e' char */
1433: if( index( real, 'E' ) == NULL ) {
1434: expo_p = index(real, 'e'); /*** hpux complained */
1435: fraclength = length - strlen(expo_p);
1436: expo_p++; if(expo_p[0] == '+') expo_p++;
1437: sscanf(expo_p,"%d",&i_exp);
1438: for(idx=0;idx<fraclength;idx++) fracS[idx] = real[idx];
1439: fracS[fraclength] = 0;
1440: if(i_exp == 0 ) {
1441: sprintf(result,"%s", fracS);
1442: } else {
1443: sprintf(result,"%s×10<sup>%d</sup>", fracS, i_exp); /* × is code for x */
1444: }
1445: } else {
1446: sprintf(result,"<<Ill-formed REAL>>");
1447: sprintf(warn_msg,"number %s is not a valid real number!\n",real);
1448: capa_msg(MESSAGE_ERROR,warn_msg);
1449: }
1450: }
1451: areal = (char *) capa_malloc(strlen(result)+1, 1);
1452: strcpy(areal,result);
1453: return (areal);
1454: }
1455: /* -- This routine is called when a /ANS is encountered -- */
1456: void
1457: init_answerinfo()
1458: {
1459:
1460: CurrAnsInfo.ans_str = NULL;
1461: CurrAnsInfo.ans_type = 0;
1462: CurrAnsInfo.ans_calc = CALC_DEFAULT;
1463: CurrAnsInfo.ans_tol_type = TOL_ABSOLUTE;
1464: CurrAnsInfo.ans_tol = TOL_DEFAULT;
1465: CurrAnsInfo.ans_sig_ub = SIG_UB_DEFAULT;
1466: CurrAnsInfo.ans_sig_lb = SIG_LB_DEFAULT;
1467: CurrAnsInfo.ans_id_list = NULL;
1468: CurrAnsInfo.ans_pts_list = NULL;
1469: CurrAnsInfo.ans_fmt[0] = '\0';
1470: CurrAnsInfo.ans_unit_str[0] = '\0';
1471: CurrAnsInfo.ans_unit = NULL;
1472: CurrAnsInfo.ans_next = NULL;
1473: }
1474: /* when encountered a /DIS(variable) */
1475: void
1476: display_var( s )Symbol *s;
1477: {
1478: char *aline;
1479: char *tmp_p;
1480: char warn_msg[WARN_MSG_LENGTH];
1481:
1482:
1483: switch(s->s_type) {
1484: case IDENTIFIER:
1485: aline = (char *)capa_malloc(sizeof(char)*ONE_K,1);
1486: sprintf(aline,"VAR \"%s\" NOT DEFINED!", s->s_name);
1487: sprintf(warn_msg,"display var \"%s\" not defined before use.\n",s->s_name);
1488: capa_msg(MESSAGE_ERROR,warn_msg);
1489: break;
1490: case I_VAR: case I_CONSTANT:
1491: aline = (char *)capa_malloc(sizeof(char)*ONE_K,1);
1492: sprintf(aline, "%ld", s->s_int);
1493: break;
1494: case R_VAR: case R_CONSTANT:
1495: aline = (char *)capa_malloc(sizeof(char)*ONE_K,1);
1496: if(Parsemode_f == TeX_MODE) {
1497: if(s->s_distype == DEFAULT_FORMAT ) {
1498: sprintf(aline,"%.15g",s->s_real);
1499: } else {
1500: sprintf(aline,s->s_format,s->s_real);
1501: }
1502: tmp_p = format_toTeX(aline);
1503: sprintf(aline,"%s",tmp_p);
1504: capa_mfree( (char *)tmp_p);
1505:
1506: } else {
1507: if(Parsemode_f == HTML_MODE ) {
1508: if(s->s_distype == DEFAULT_FORMAT ) {
1509: sprintf(aline,"%.15g",s->s_real);
1510: } else {
1511: sprintf(aline,s->s_format,s->s_real);
1512: }
1513: tmp_p = format_toHTML(aline);
1514: sprintf(aline,"%s",tmp_p);
1515: capa_mfree( (char *)tmp_p);
1516: } else {
1517: if(s->s_distype == DEFAULT_FORMAT ) {
1518: sprintf(aline,"%.15g",s->s_real);
1519: } else {
1520: sprintf(aline,s->s_format,s->s_real);
1521: }
1522: }
1523: }
1524: break;
1.4 albertel 1525: case S_VAR: case S_CONSTANT:
1526: if (s->s_str == NULL) {
1527: sprintf(warn_msg,"variable %s has not yet been assigned a value.\n",
1528: s->s_name);
1529: capa_msg(MESSAGE_ERROR,warn_msg);
1530: aline=(char *)capa_malloc(9,1);
1531: sprintf(aline,"NO VALUE");
1532: } else {
1533: aline = (char *)capa_malloc(strlen(s->s_str)+1,1);
1534: sprintf(aline,"%s",s->s_str);
1535: }
1.1 albertel 1536: break;
1537: }
1538: append_text(aline);
1539: capa_mfree((char *)aline);
1.4 albertel 1540: if(s->s_format) { capa_mfree((char *)s->s_format); }
1.1 albertel 1541: s->s_format = NULL;
1542: switch(s->s_type) { /* free up spaces taken by constants */
1543: case I_CONSTANT:
1544: case R_CONSTANT: capa_mfree((char *)s); break;
1545: case S_CONSTANT: capa_mfree(s->s_str); capa_mfree((char *)s); break;
1546: default: break;
1547: }
1548:
1549: }
1550:
1551: /* Assign the correct answer to CurrAnsInfo first */
1552: void
1553: assign_answer( s ) Symbol *s;
1554: {
1555: char aline[QUARTER_K];
1556: char warn_msg[WARN_MSG_LENGTH];
1557:
1558: /*problem_default(LexiProblem_p);*/
1559: switch(s->s_type) {
1560: case IDENTIFIER:
1561: sprintf(warn_msg,"File %s, Line %3d: in /ANS, var %s not defined before use.\n",
1562: Opened_filename[Input_idx],Current_line[Input_idx],s->s_name);
1563: capa_msg(MESSAGE_ERROR,warn_msg);
1564: CurrAnsInfo.ans_str = strsave("ANSWER NOT DEFINED!");
1565: CurrAnsInfo.ans_type = ANSWER_IS_STRING_CI;
1566: sprintf(CurrAnsInfo.ans_fmt,"%%s");
1567: if (CurrAnsInfo.ans_tol == 0.0) {
1568: sprintf(warn_msg, "File %s, Line %3d: answer has a numerical value of %ld and an implicit zero tolerance.\n",
1569: Opened_filename[Input_idx],Current_line[Input_idx],s->s_int);
1570: capa_msg(MESSAGE_WARN,warn_msg);
1571: }
1572: break;
1573: case I_VAR: case I_CONSTANT:
1574: sprintf(aline, "%ld", s->s_int);
1575: CurrAnsInfo.ans_str = strsave(aline);
1576: CurrAnsInfo.ans_type = ANSWER_IS_INTEGER;
1577: sprintf(CurrAnsInfo.ans_fmt,"%%ld");
1578: break;
1579: case R_VAR: case R_CONSTANT:
1580: if(s->s_distype == DEFAULT_FORMAT ) {
1581: sprintf(aline,"%.15g",s->s_real);
1582: sprintf(CurrAnsInfo.ans_fmt,"%%.15g");
1583: } else {
1584: sprintf(aline,"%.15g",s->s_real);
1585: strcpy(CurrAnsInfo.ans_fmt,s->s_format);
1586: }
1587: CurrAnsInfo.ans_str = strsave(aline);
1588: CurrAnsInfo.ans_type = ANSWER_IS_FLOAT;
1589: if( CurrAnsInfo.ans_tol == 0.0 ) {
1590: sprintf(warn_msg,"File %s, Line %3d: answer has a numerical value of %s and a zero tolerance.\n",
1591: Opened_filename[Input_idx],Current_line[Input_idx],aline);
1592: capa_msg(MESSAGE_WARN,warn_msg);
1593: }
1594: break;
1595: case S_VAR: case S_CONSTANT:
1596: CurrAnsInfo.ans_str = strsave(s->s_str);
1597: if (s->s_str!=NULL && (strlen(s->s_str)>ANSWER_STRING_LENG-1)) {
1598: sprintf(warn_msg,"File %s, Line %3d: answer is too long, max allowed length is %d, current answer is %d\n",
1599: Opened_filename[Input_idx],Current_line[Input_idx],
1600: ANSWER_STRING_LENG-1, strlen(s->s_str));
1601: capa_msg(MESSAGE_ERROR,warn_msg);
1602: CurrAnsInfo.ans_str[ANSWER_STRING_LENG-1]='\0';
1603: }
1604: if ( !CurrAnsInfo.ans_type ) { /* not yet specified by str= answer info */
1605: CurrAnsInfo.ans_type = ANSWER_IS_STRING_CI;
1606: }
1607: sprintf(CurrAnsInfo.ans_fmt,"%%s");
1608: break;
1609: }
1610: if(s->s_format) {
1611: capa_mfree((char *)s->s_format);
1612: }
1613: s->s_format = NULL;
1614: switch(s->s_type) {
1615: case I_CONSTANT:
1616: case R_CONSTANT: capa_mfree((char *)s); break;
1617: case S_CONSTANT: capa_mfree(s->s_str); capa_mfree((char *)s); break;
1618: default: break;
1619: }
1620:
1621: }
1622:
1623: /* Assign tolerance to CurrAnsInfo first */
1624: void
1625: assign_tolerance(tol_type, s) int tol_type; Symbol *s;
1626: {
1627: char warn_msg[WARN_MSG_LENGTH];
1628:
1629: CurrAnsInfo.ans_tol_type = tol_type;
1630: switch( s->s_type ) {
1631: case IDENTIFIER:
1632: sprintf(warn_msg,"TOL = var, \"%s\" not defined before use.\n",s->s_name);
1633: capa_msg(MESSAGE_ERROR,warn_msg);
1634: CurrAnsInfo.ans_tol = 0.0;
1635: break;
1636: case I_VAR: case I_CONSTANT:
1637: CurrAnsInfo.ans_tol =(double)s->s_int;
1638: break;
1639: case R_VAR: case R_CONSTANT:
1640: CurrAnsInfo.ans_tol = s->s_real;
1641: break;
1642: case S_VAR: case S_CONSTANT: CurrAnsInfo.ans_tol = 0.0;
1643: break;
1644: }
1645: free_calc_expr(s);
1646: }
1647:
1648: /* Problem weight is per problem based */
1649: void
1650: assign_weight( s ) Symbol *s;
1651: { char warn_msg[WARN_MSG_LENGTH];
1652: YYDBUG_PR1(" weight = identifier\n");
1653: switch( s->s_type ) {
1654: case IDENTIFIER:
1655: sprintf(warn_msg,"WGT = var, \"%s\" not defined before use.\n", s->s_name);
1656: capa_msg(MESSAGE_ERROR,warn_msg);
1657: LexiProblem_p->weight = WEIGHT_DEFAULT;
1658: break;
1659: case I_VAR: case I_CONSTANT:
1660: if( s->s_int < 0 ) {
1661: sprintf(warn_msg,"WGT = %ld, weight cannot be less than zero.\n", s->s_int);
1662: capa_msg(MESSAGE_ERROR,warn_msg);
1663: LexiProblem_p->weight = WEIGHT_DEFAULT;
1664: } else {
1665: LexiProblem_p->weight = s->s_int;
1666: }
1667: break;
1668: case R_VAR: case R_CONSTANT:
1669: if( s->s_real < 0.0 ) {
1670: sprintf(warn_msg,"WGT = %g, weight cannot be less than zero.\n", s->s_real);
1671: capa_msg(MESSAGE_ERROR,warn_msg);
1672: LexiProblem_p->weight = WEIGHT_DEFAULT;
1673: } else {
1674: LexiProblem_p->weight = (int)(s->s_real);
1675: }
1676: break;
1677: case S_VAR: case S_CONSTANT: LexiProblem_p->weight = WEIGHT_DEFAULT; break;
1678: }
1679: free_calc_expr(s);
1680: }
1681: /* Answer try limit is per problem based */
1682: void
1683: assign_try_limits( s ) Symbol *s;
1684: { char warn_msg[WARN_MSG_LENGTH];
1685: switch( s->s_type ) {
1686: case IDENTIFIER:
1687: sprintf(warn_msg,"TRY = var, \"%s\" not defined before use.\n",s->s_name);
1688: capa_msg(MESSAGE_ERROR,warn_msg);
1689: LexiProblem_p->tries = MAX_TRIES;
1690: break;
1691: case I_VAR: case I_CONSTANT:
1692: if(s->s_int <= 0) {
1693: sprintf(warn_msg,"TRIES = %ld, tries cannot be less than or equal to zero.\n",s->s_int);
1694: capa_msg(MESSAGE_ERROR,warn_msg);
1695: LexiProblem_p->tries = MAX_TRIES;
1696: } else {
1697: LexiProblem_p->tries = s->s_int;
1698: }
1699: break;
1700: case R_VAR: case R_CONSTANT:
1701: if(s->s_real <= 0.0) {
1702: sprintf(warn_msg,"TRIES = %g, tries cannot be less than or equal to zero.\n",s->s_real);
1703: capa_msg(MESSAGE_ERROR,warn_msg);
1704: LexiProblem_p->tries = MAX_TRIES;
1705: } else {
1706: LexiProblem_p->tries = (int)(s->s_real);
1707: }
1708: break;
1709: case S_VAR: case S_CONSTANT: LexiProblem_p->tries = MAX_TRIES; break;
1710: }
1711: free_calc_expr(s);
1712: }
1713: /* Answer hint is per problem based */
1714: void
1715: assign_hint( s ) Symbol *s;
1716: { char warn_msg[WARN_MSG_LENGTH];
1717:
1718: switch( s->s_type ) {
1719: case IDENTIFIER:
1720: sprintf(warn_msg,"HINT = var, \"%s\" not defined before use.\n", s->s_name);
1721: capa_msg(MESSAGE_ERROR,warn_msg);
1722: LexiProblem_p->show_hint = SHOW_HINT_DEFAULT;
1723: break;
1724: case I_VAR: case I_CONSTANT:
1725: if( s->s_int < 0 ) {
1726: sprintf(warn_msg,"HINT = %ld, show hint cannot be less than zero.\n", s->s_int);
1727: capa_msg(MESSAGE_ERROR,warn_msg);
1728: LexiProblem_p->show_hint = SHOW_HINT_DEFAULT;
1729: } else {
1730: LexiProblem_p->show_hint = s->s_int;
1731: }
1732: break;
1733: case R_VAR: case R_CONSTANT:
1734: if( s->s_real < 0.0 ) {
1735: sprintf(warn_msg,"HINT = %g, show hint cannot be less than zero.\n", s->s_real);
1736: capa_msg(MESSAGE_ERROR,warn_msg);
1737: LexiProblem_p->show_hint = SHOW_HINT_DEFAULT;
1738: } else {
1739: LexiProblem_p->weight = (int)(s->s_real);
1740: }
1741: break;
1742: case S_VAR: case S_CONSTANT: LexiProblem_p->show_hint = SHOW_HINT_DEFAULT; break;
1743: }
1744: free_calc_expr(s);
1745: }
1746:
1747: /* Assign answer units string to CurrAnsInfo first */
1748: void
1749: assign_units( s ) Symbol *s;
1750: {
1751: char symb_str[ONE_TWO_EIGHT];
1752: char warn_msg[WARN_MSG_LENGTH];
1753:
1754: switch( s->s_type ) {
1755: case IDENTIFIER:
1756: sprintf(warn_msg,"UNIT = var, \"%s\" not defined before use.\n", s->s_name);
1757: capa_msg(MESSAGE_ERROR,warn_msg);
1758: break;
1759: case I_VAR: case I_CONSTANT:
1760: sprintf(warn_msg,"UNIT = %ld, unit cannot be a number.\n", s->s_int);
1761: capa_msg(MESSAGE_ERROR,warn_msg);
1762: break;
1763: case R_VAR: case R_CONSTANT:
1764: sprintf(warn_msg,"UNIT = %g, unit cannot be a number.\n", s->s_real);
1765: capa_msg(MESSAGE_ERROR,warn_msg);
1766: break;
1767: case S_VAR: case S_CONSTANT:
1768: strcpy(symb_str,s->s_str);
1769: strcpy(CurrAnsInfo.ans_unit_str,symb_str);
1770: CurrAnsInfo.ans_unit = u_parse_unit(symb_str);
1771: if (gUnitError) {
1772: sprintf(warn_msg,"Error in unit specified: %s\n",symb_str);
1773: capa_msg(MESSAGE_ERROR,warn_msg);
1774: }
1775: break;
1776: }
1777: }
1778: void
1779: assign_sigs( lb, ub ) int lb; int ub;
1780: {
1781: CurrAnsInfo.ans_sig_lb = lb;
1782: CurrAnsInfo.ans_sig_ub = ub;
1783: }
1784:
1785: void
1786: assign_id_list( s ) Symbol *s;
1787: {
1788: char warn_msg[WARN_MSG_LENGTH];
1789:
1790: switch( s->s_type ) {
1791: case IDENTIFIER:
1792: sprintf(warn_msg,"Eval = < ID @ Pts >, \"%s\" not defined before use.\n", s->s_name);
1793: capa_msg(MESSAGE_ERROR,warn_msg);
1794: break;
1795: case I_VAR: case I_CONSTANT:
1796: sprintf(warn_msg,"Eval = < %ld @ Pts >, ID cannot be a number.\n", s->s_int);
1797: capa_msg(MESSAGE_ERROR,warn_msg);
1798: break;
1799: case R_VAR: case R_CONSTANT:
1800: sprintf(warn_msg,"Eval = < %.16g @ Pts >, ID cannot be a number.\n", s->s_real);
1801: capa_msg(MESSAGE_ERROR,warn_msg);
1802: break;
1803: case S_VAR: case S_CONSTANT:
1804: CurrAnsInfo.ans_id_list = strsave(s->s_str);
1805: break;
1.6 albertel 1806: }
1807: }
1808:
1.8 albertel 1809: /* void assign_pts ( Symbol* coord1, Symbol* coord2, Symbol* num) { */
1810: void assign_pts (coord1, coord2, num)Symbol *coord1;Symbol *coord2;Symbol *num;
1811: {
1.6 albertel 1812: PointsList_t *pt;
1813: if( LastPtsList != NULL ) {
1814: LastPtsList->pts_next = gen_ptslist( coord1, coord2, num );
1815: pt = LastPtsList->pts_next;
1816: while( pt->pts_next != NULL ) {
1817: pt = pt->pts_next;
1818: }
1819: LastPtsList = pt;
1820: } else {
1821: CurrPtsList = gen_ptslist( coord1, coord2, num );
1822: LastPtsList = CurrPtsList;
1823: }
1824: if(coord1->s_type == S_CONSTANT) {
1825: capa_mfree(coord1->s_str); capa_mfree((char *)coord1);
1826: }
1827: if(coord2->s_type == S_CONSTANT) {
1828: capa_mfree(coord2->s_str); capa_mfree((char *)coord2);
1829: }
1830: if(num->s_type == I_CONSTANT || num->s_type == R_CONSTANT) {
1831: capa_mfree((char *)num);
1.1 albertel 1832: }
1833: }
1834:
1835: /* =========================================================================== */
1836:
1837:
1838:
1839:
1840:
1841:
1842: /* =========================================================================== */
1843:
1844: void start_question_over()
1845: {
1846: free_problems(LexiProblem_p);
1847: LexiProblem_p = (Problem_t *) capa_malloc(sizeof(Problem_t),1);
1848: problem_default(LexiProblem_p);
1849: begin_question();
1850: }
1851:
1852: void
1853: init_new_prob()
1854: {
1855: if (LastProblem_p) {
1856: LastProblem_p->next = LexiProblem_p;
1857: } else {
1858: FirstProblem_p = LexiProblem_p;
1859: }
1860: LastProblem_p = LexiProblem_p;
1861: Lexi_qnum++;
1862: LexiProblem_p = (Problem_t *) capa_malloc(sizeof(Problem_t),1); /* *** */
1863: problem_default(LexiProblem_p);
1864: }
1865: void
1866: add_answer_cnt(op)int op;
1867: {
1868: LexiProblem_p->ans_cnt++;
1869: if(LexiProblem_p->ans_op == 0) { /* the very first /AND or /OR */
1870: LexiProblem_p->ans_op = op;
1871: } else {
1872: if( LexiProblem_p->ans_op != op ) { char warn_msg[WARN_MSG_LENGTH];
1873: sprintf(warn_msg,"When specifying multiple answers, either /AND or /OR can be used, but not both.\n");
1874: capa_msg(MESSAGE_ERROR,warn_msg);
1875: }
1876: }
1877: }
1878:
1879: /* -- called when forming answer_expr */
1880: void
1881: finish_answer_info()
1882: {
1883: AnswerInfo_t *ai;
1884:
1885: if( LexiProblem_p->ans_cnt == 1 ) { /* Only one answer is defined */
1886: LexiProblem_p->answer = CurrAnsInfo.ans_str;
1887: LexiProblem_p->ans_type = CurrAnsInfo.ans_type;
1888: LexiProblem_p->calc = CurrAnsInfo.ans_calc;
1889: LexiProblem_p->tol_type = CurrAnsInfo.ans_tol_type;
1890: LexiProblem_p->tolerance = CurrAnsInfo.ans_tol;
1891: LexiProblem_p->sig_ubound= CurrAnsInfo.ans_sig_ub;
1892: LexiProblem_p->sig_lbound= CurrAnsInfo.ans_sig_lb;
1893: LexiProblem_p->id_list = CurrAnsInfo.ans_id_list;
1894: LexiProblem_p->pts_list = CurrAnsInfo.ans_pts_list;
1895: strcpy(LexiProblem_p->ans_fmt,CurrAnsInfo.ans_fmt);
1896: strcpy(LexiProblem_p->unit_str,CurrAnsInfo.ans_unit_str);
1897: LexiProblem_p->ans_unit = CurrAnsInfo.ans_unit;
1898: } else {
1899: ai = (AnswerInfo_t *)capa_malloc(sizeof(AnswerInfo_t),1);
1900: ai->ans_str = CurrAnsInfo.ans_str;
1901: ai->ans_type = CurrAnsInfo.ans_type;
1902: ai->ans_calc = CurrAnsInfo.ans_calc;
1903: ai->ans_tol_type = CurrAnsInfo.ans_tol_type;
1904: ai->ans_tol = CurrAnsInfo.ans_tol;
1905: ai->ans_sig_ub = CurrAnsInfo.ans_sig_ub;
1906: ai->ans_sig_lb = CurrAnsInfo.ans_sig_lb;
1907: ai->ans_id_list = CurrAnsInfo.ans_id_list;
1908: ai->ans_pts_list = CurrAnsInfo.ans_pts_list;
1909: strcpy(ai->ans_fmt,CurrAnsInfo.ans_fmt);
1910: strcpy(ai->ans_unit_str,CurrAnsInfo.ans_unit_str);
1911: ai->ans_unit = CurrAnsInfo.ans_unit;
1912: ai->ans_next = NULL;
1913: if(LexiProblem_p->ans_cnt == 2) {
1914: LexiProblem_p->ans_list = ai;
1915: } else {
1916: (LexiProblem_p->ans_list_last)->ans_next = ai;
1917: }
1918: LexiProblem_p->ans_list_last = ai;
1919: }
1920:
1921: }
1922:
1923: /* === End of capaGrammarDef.y ============================================= */
1924:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>