source: git/Singular/grammar.y @ c513493

spielwiese
Last change on this file since c513493 was c513493, checked in by Hans Schönemann <hannes@…>, 26 years ago
*** empty log message *** git-svn-id: file:///usr/local/Singular/svn/trunk@1689 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 38.4 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: grammar.y,v 1.31 1998-05-11 12:48:21 Singular Exp $ */
5/*
6* ABSTRACT: SINGULAR shell grammatik
7*/
8%{
9
10#include <stdio.h>
11#include <stddef.h>
12#include <stdlib.h>
13#include <stdarg.h>
14#include <string.h>
15#include <limits.h>
16#ifdef __MWERKS__
17  #ifdef __POWERPC__
18    #include <alloca.h>
19  #else
20    #ifdef macintosh
21      #define alloca malloc /* this is not corect! */
22    #else
23      #include <malloc.h>
24    #endif
25  #endif
26#endif
27
28#include "mod2.h"
29#include "tok.h"
30#include "stype.h"
31#include "ipid.h"
32#include "intvec.h"
33#include "febase.h"
34#include "matpol.h"
35#include "ring.h"
36#include "kstd1.h"
37#include "mmemory.h"
38#include "subexpr.h"
39#include "ipshell.h"
40#include "ipconv.h"
41#include "ideals.h"
42#include "numbers.h"
43#include "polys.h"
44#include "weight.h"
45#include "stairc.h"
46#include "timer.h"
47#include "cntrlc.h"
48#include "maps.h"
49#include "syz.h"
50#include "lists.h"
51#include "libparse.h"
52
53
54extern int   yylineno;
55extern FILE* yyin;
56
57char       my_yylinebuf[80];
58char *     currid;
59BOOLEAN    noringvars=FALSE;
60BOOLEAN    expected_parms;
61int        cmdtok;
62int        inerror = 0;
63
64#define TESTSETINT(a,i)                                \
65   if ((a).Typ() != INT_CMD)                           \
66   {                                                   \
67     WerrorS("no int expression");                     \
68     YYERROR;                                          \
69   }                                                   \
70   (i) = (int)(a).Data();
71
72#define MYYERROR(a) { WerrorS(a); YYERROR; }
73
74void yyerror(char * fmt)
75{
76  int oldInError=inerror;
77
78  inerror = 1;
79  BOOLEAN old_errorreported=errorreported;
80  errorreported = TRUE;
81  if (currid!=NULL)
82  {
83    killid(currid,&idroot);
84    currid = NULL;
85  }
86  if(!oldInError)
87  {
88    Werror( "error occurred in %s line %d: `%s`"
89           ,VoiceName(), yylineno, my_yylinebuf);
90    if (cmdtok!=0)
91    {
92      char *s=Tok2Cmdname(cmdtok);
93      if (expected_parms)
94      {
95        Werror("expected %s-expression. type \'help %s;\'",s,s);
96      }
97      else
98      {
99        Werror("wrong type declaration. type \'help %s;\'",s);
100      }
101    }
102    if (!old_errorreported && (lastreserved!=NULL))
103    {
104      Werror("last reserved name was `%s`",lastreserved);
105    }
106  }
107  else
108    inerror=oldInError;
109  if ((currentVoice!=NULL)
110  && (currentVoice->prev!=NULL)
111  &&(myynest>0))
112  {
113    Werror("leaving %s",VoiceName());
114    exitBuffer(BT_proc);
115  }
116}
117
118%}
119
120/* %expect 22 */
121%pure_parser
122
123/* special symbols */
124%token DOTDOT
125%token EQUAL_EQUAL
126%token GE
127%token LE
128%token INTDIV
129%token MINUSMINUS
130%token NOT
131%token NOTEQUAL
132%token PLUSPLUS
133%token COLONCOLON
134
135/* types, part 1 (ring indep.)*/
136%token <i> DRING_CMD
137%token <i> INTMAT_CMD
138%token <i> PROC_CMD
139%token <i> RING_CMD
140
141/* valid when ring defined ! */
142%token <i> BEGIN_RING
143/* types, part 2 */
144%token <i> IDEAL_CMD
145%token <i> MAP_CMD
146%token <i> MATRIX_CMD
147%token <i> MODUL_CMD
148%token <i> NUMBER_CMD
149%token <i> POLY_CMD
150%token <i> RESOLUTION_CMD
151%token <i> VECTOR_CMD
152/* end types */
153
154/* ring dependent cmd:*/
155%token <i> BAREISS_CMD
156%token <i> BETTI_CMD
157%token <i> COEFFS_CMD
158%token <i> COEF_CMD
159%token <i> CONTRACT_CMD
160%token <i> DEGREE_CMD
161%token <i> DEG_CMD
162%token <i> DIFF_CMD
163%token <i> DIM_CMD
164%token <i> ELIMINATION_CMD
165%token <i> E_CMD
166%token <i> FETCH_CMD
167%token <i> FREEMODULE_CMD
168%token <i> KEEPRING_CMD
169%token <i> HILBERT_CMD
170%token <i> HOMOG_CMD
171%token <i> IMAP_CMD
172%token <i> INDEPSET_CMD
173%token <i> INTERRED_CMD
174%token <i> INTERSECT_CMD
175%token <i> JACOB_CMD
176%token <i> JET_CMD
177%token <i> KBASE_CMD
178%token <i> KOSZUL_CMD
179%token <i> LEADCOEF_CMD
180%token <i> LEADEXP_CMD
181%token <i> LEAD_CMD
182%token <i> LIFTSTD_CMD
183%token <i> LIFT_CMD
184%token <i> MAXID_CMD
185%token <i> MINBASE_CMD
186%token <i> MINOR_CMD
187%token <i> MINRES_CMD
188%token <i> MODULO_CMD
189%token <i> MRES_CMD
190%token <i> MULTIPLICITY_CMD
191%token <i> ORD_CMD
192%token <i> PAR_CMD
193%token <i> PARDEG_CMD
194%token <i> PREIMAGE_CMD
195%token <i> QUOTIENT_CMD
196%token <i> QHWEIGHT_CMD
197%token <i> REDUCE_CMD
198%token <i> REGULARITY_CMD
199%token <i> RES_CMD
200%token <i> SIMPLIFY_CMD
201%token <i> SORTVEC_CMD
202%token <i> SRES_CMD
203%token <i> STD_CMD
204%token <i> SUBST_CMD
205%token <i> SYZYGY_CMD
206%token <i> VAR_CMD
207%token <i> VDIM_CMD
208%token <i> WEDGE_CMD
209%token <i> WEIGHT_CMD
210
211/*system variables in ring block*/
212%token <i> VALTVARS
213%token <i> VMAXDEG
214%token <i> VMAXMULT
215%token <i> VNOETHER
216%token <i> VMINPOLY
217
218%token <i> END_RING
219/* end of ring definitions */
220
221%token <i> CMD_1
222%token <i> CMD_2
223%token <i> CMD_3
224%token <i> CMD_12
225%token <i> CMD_23
226%token <i> CMD_123
227%token <i> CMD_M
228%token <i> ROOT_DECL
229        /* put variables of this type into the idroot list */
230%token <i> ROOT_DECL_LIST
231        /* put variables of this type into the idroot list */
232%token <i> RING_DECL
233        /* put variables of this type into the currRing list */
234%token <i> EXAMPLE_CMD
235%token <i> EXECUTE_CMD
236%token <i> EXPORT_CMD
237%token <i> HELP_CMD
238%token <i> KILL_CMD
239%token <i> LIB_CMD
240%token <i> LISTVAR_CMD
241%token <i> SETRING_CMD
242%token <i> TYPE_CMD
243
244%token <name> STRINGTOK BLOCKTOK INT_CONST
245%token <name> UNKNOWN_IDENT RINGVAR PROC_DEF
246
247/* control */
248%token <i> BREAK_CMD
249%token <i> CONTINUE_CMD
250%token <i> ELSE_CMD
251%token <i> EVAL
252%token <i> QUOTE
253%token <i> FOR_CMD
254%token <i> IF_CMD
255%token <i> SYS_BREAK
256%token <i> WHILE_CMD
257%token <i> RETURN
258%token <i> PARAMETER
259
260/* system variables */
261%token <i> SYSVAR
262
263%type <name> extendedid
264%type <lv>   rlist ordering OrderingList orderelem
265%type <name> stringexpr
266%type <lv>   expr elemexpr exprlist expr_arithmetic
267%type <lv>   declare_ip_variable left_value
268%type <i>    ordername
269%type <i>    cmdeq
270%type <i>    currring_lists
271%type <i>    setrings
272%type <i>    ringcmd1
273
274%type <i>    '=' '<' '>' '+' '-'
275%type <i>    '*' '/' '[' ']' '^' ',' ';'
276
277
278/*%nonassoc '=' PLUSEQUAL DOTDOT*/
279%nonassoc '=' DOTDOT COLONCOLON
280%left '|' '&'
281%left EQUAL_EQUAL NOTEQUAL
282%left '<' '>' GE LE
283%left '+' '-'
284%left '*' '/' '%' INTDIV
285%left UMINUS
286%left  '^'
287%left '[' ']'
288%left '(' ')'
289%left PLUSPLUS
290%left MINUSMINUS
291
292%%
293lines:
294        /**/
295        | lines pprompt
296          {
297            if (timerv)
298            {
299              writeTime("used time:");
300              startTimer();
301            }
302            #ifdef HAVE_RTIMER
303            if (rtimerv)
304            {
305              writeRTime("used real time:");
306              startRTimer();
307            }
308            #endif
309            prompt_char = '>';
310            currid = NULL;
311            if(siCntrlc)
312            {
313              siCntrlc=FALSE;
314              MYYERROR("abort...");
315            }
316            if (errorreported)
317            {
318              yyerror("");
319            }
320            if (inerror==2) PrintLn();
321            errorreported = inerror = cmdtok = 0;
322            lastreserved=NULL;
323            expected_parms=FALSE;
324            noringvars = FALSE;
325            siCntrlc = FALSE;
326          }
327        ;
328
329pprompt:
330        flowctrl                       /* if, while, for, proc */
331        | command ';'                  /* commands returning no value */
332          {currentVoice->ifsw=0;}
333        | declare_ip_variable ';'      /* default initialization */
334          { $1.CleanUp(); currentVoice->ifsw=0;}
335        | returncmd
336          {
337            YYACCEPT;
338          }
339        | SYS_BREAK
340          {
341            currentVoice->ifsw=0;
342            iiDebug();
343          }
344        | ';'                    /* ignore empty statements */
345          {currentVoice->ifsw=0;}
346        | error ';'
347          {
348            feBufferTypes t=currentVoice->Typ();
349            #ifdef SIQ
350            siq=0;
351            #endif
352            currentVoice->ifsw=0;
353            if (inerror&&($1.i<UMINUS) && ($1.i>' '))
354            {
355              // 1: yyerror called
356              // 2: scanner put actual string
357              // 3: error rule put token+\n
358              inerror=3;
359              Print(" error at token `%s`\n",iiTwoOps($1.i));
360            }
361            if (!errorreported) WerrorS("...parse error");
362            yyerror("");
363            yyerrok;
364            if (myynest>0)
365            {
366              //PrintS("leaving yyparse\n");
367              if (t==BT_example)
368              {
369                errorreported = inerror = cmdtok = 0;
370                lastreserved=NULL;
371                expected_parms=FALSE;
372                inerror = 0;
373                yyerrok;
374                YYACCEPT;
375              }
376              else
377                YYABORT;
378            }
379            else if (currentVoice->prev!=NULL)
380            {
381              exitVoice();
382            }
383            errorreported = FALSE;
384            lastreserved=NULL;
385          }
386        ;
387
388flowctrl: ifcmd
389          | whilecmd
390          | forcmd
391          | proccmd
392          | filecmd
393          | helpcmd
394            {currentVoice->ifsw=0;}
395        ;
396
397command: assign
398         | executecmd
399         | exportcmd
400         | killcmd
401         | listcmd
402         | parametercmd
403         | ringcmd
404         | scriptcmd
405         | setringcmd
406         | typecmd
407         ;
408
409assign: left_value exprlist
410          {
411            if(iiAssign(&$1,&$2)) YYERROR;
412          }
413        ;
414
415elemexpr:
416        RINGVAR
417          {
418            if (currRing==NULL) MYYERROR("no ring active");
419            syMake(&$$,$1);
420          }
421        | extendedid
422          {
423            syMake(&$$,$1);
424          }
425        | elemexpr '('  ')'
426          {
427            if(iiExprArith1(&$$,&$1,'(')) YYERROR;
428          }
429        | elemexpr '(' exprlist ')'
430          {
431            if ($1.rtyp==LIB_CMD)
432            {
433              if(iiExprArith1(&$$,&$3,LIB_CMD)) YYERROR;
434            }
435            else
436            {
437              $1.next=(leftv)Alloc(sizeof(sleftv));
438              memcpy($1.next,&$3,sizeof(sleftv));
439              if(iiExprArithM(&$$,&$1,'(')) YYERROR;
440            }
441          }
442        | elemexpr COLONCOLON extendedid
443          {
444            idhdl r=idroot;
445            if ($1.Typ()!=PACKAGE_CMD) MYYERROR("<package>::<id> expected");
446            idroot=IDPACKAGE((idhdl)$1.data)->idroot;
447            syMake(&$$,$3);
448            idroot=r;
449          }
450        | '[' exprlist ']'
451          {
452            if (currRingHdl==NULL) MYYERROR("no ring active");
453            int j = 0;
454            memset(&$$,0,sizeof(sleftv));
455            $$.rtyp=VECTOR_CMD;
456            leftv v = &$2;
457            while (v!=NULL)
458            {
459              poly p = pOne();
460              pSetCompP(p,++j);
461              int k = (int)(v->Data());
462              if (k!=0)
463              {
464                if (v->Typ() == INT_CMD)
465                {
466                  $$.data = (void *)pAdd((poly)$$.data,pMult(p,pISet(k)));
467                }
468                else if (v->Typ() == POLY_CMD)
469                {
470                  $$.data = (void *)pAdd((poly)$$.data,
471                                         pMult(p,(poly)(v->CopyD())) );
472                }
473                else
474                  MYYERROR("expected '[poly,...'");
475              }
476              else
477                pDelete1(&p);
478              v=v->next;
479            }
480            $2.CleanUp();
481          }
482        | INT_CONST
483          {
484            memset(&$$,0,sizeof($$));
485            int i = atoi($1);
486            /*remember not to FreeL($1)
487            *because it is a part of the scanner buffer*/
488            $$.rtyp  = INT_CMD;
489            $$.data = (void *)i;
490
491            /* check: out of range input */
492            int l = strlen($1)+2;
493            if (l >= MAX_INT_LEN)
494            {
495              char tmp[100];
496              sprintf(tmp,"%d",i);
497              if (strcmp(tmp,$1)!=0)
498              {
499                if (currRing==NULL)
500                {
501                  Werror("`%s` greater than %d(max. integer representation)"
502                         ,$1,INT_MAX);
503                  YYERROR;
504                }
505                char *t1=mstrdup($1);
506                syMake(&$$,t1);
507              }
508            }
509          }
510        | SYSVAR
511          {
512            memset(&$$,0,sizeof($$));
513            $$.rtyp = $1;
514            $$.data = $$.Data();
515          }
516        | stringexpr
517          {
518            memset(&$$,0,sizeof($$));
519            $$.rtyp  = STRING_CMD;
520            $$.data = $1;
521          }
522        ;
523
524exprlist:
525        expr ',' exprlist
526          {
527            leftv v = &$1;
528            while (v->next!=NULL)
529            {
530              v=v->next;
531            }
532            v->next = (leftv)Alloc(sizeof(sleftv));
533            memcpy(v->next,&($3),sizeof(sleftv));
534            $$ = $1;
535          }
536        | expr
537          {
538            $$ = $1;
539          }
540        | '(' expr ',' exprlist ')'
541          {
542            leftv v = &$2;
543            while (v->next!=NULL)
544            {
545              v=v->next;
546            }
547            v->next = (leftv)Alloc(sizeof(sleftv));
548            memcpy(v->next,&($4),sizeof(sleftv));
549            $$ = $2;
550          }
551        ;
552
553expr:   expr_arithmetic
554          {
555            /*if ($1.typ == eunknown) YYERROR;*/
556            $$ = $1;
557          }
558        | elemexpr       { $$ = $1; }
559        | '(' expr ')'    { $$ = $2; }
560        | expr '[' expr ',' expr ']'
561          {
562            if(iiExprArith3(&$$,'[',&$1,&$3,&$5)) YYERROR;
563          }
564        | expr '[' expr ']'
565          {
566            if(iiExprArith2(&$$,&$1,'[',&$3)) YYERROR;
567          }
568        | ROOT_DECL '(' expr ')'
569          {
570            if(iiExprArith1(&$$,&$3,$1)) YYERROR;
571          }
572        | ROOT_DECL_LIST '(' exprlist ')'
573          {
574            if(iiExprArithM(&$$,&$3,$1)) YYERROR;
575          }
576        | ROOT_DECL_LIST '(' ')'
577          {
578            if(iiExprArithM(&$$,NULL,$1)) YYERROR;
579          }
580        | RING_DECL '(' expr ')'
581          {
582            if(iiExprArith1(&$$,&$3,$1)) YYERROR;
583          }
584        | currring_lists '(' exprlist ')'
585          {
586            if(iiExprArithM(&$$,&$3,$1)) YYERROR;
587          }
588        | currring_lists '(' ')'
589          {
590            if(iiExprArithM(&$$,NULL,$1)) YYERROR;
591          }
592        | CMD_1 '(' expr ')'
593          {
594            if(iiExprArith1(&$$,&$3,$1)) YYERROR;
595          }
596        | CMD_2 '(' expr ',' expr ')'
597          {
598            if(iiExprArith2(&$$,&$3,$1,&$5,TRUE)) YYERROR;
599          }
600        | CMD_3 '(' expr ',' expr ',' expr ')'
601          {
602            if(iiExprArith3(&$$,$1,&$3,&$5,&$7)) YYERROR;
603          }
604        | CMD_M '(' ')'
605          {
606            if(iiExprArithM(&$$,NULL,$1)) YYERROR;
607          }
608        | CMD_23 '(' expr ',' expr ')'
609          {
610            if(iiExprArith2(&$$,&$3,$1,&$5,TRUE)) YYERROR;
611          }
612        | CMD_23 '(' expr ',' expr ',' expr ')'
613          {
614            if(iiExprArith3(&$$,$1,&$3,&$5,&$7)) YYERROR;
615          }
616        | CMD_12 '(' expr ')'
617          {
618            if(iiExprArith1(&$$,&$3,$1)) YYERROR;
619          }
620        | CMD_12 '(' expr ',' expr ')'
621          {
622            if(iiExprArith2(&$$,&$3,$1,&$5,TRUE)) YYERROR;
623          }
624        | CMD_123 '(' expr ')'
625          {
626            if(iiExprArith1(&$$,&$3,$1)) YYERROR;
627          }
628        | CMD_123 '(' expr ',' expr ')'
629          {
630            if(iiExprArith2(&$$,&$3,$1,&$5,TRUE)) YYERROR;
631          }
632        | CMD_123 '(' expr ',' expr ',' expr ')'
633          {
634            if(iiExprArith3(&$$,$1,&$3,&$5,&$7)) YYERROR;
635          }
636        | MATRIX_CMD '(' expr ',' expr ',' expr ')'
637          {
638            if(iiExprArith3(&$$,MATRIX_CMD,&$3,&$5,&$7)) YYERROR;
639          }
640        | MATRIX_CMD '(' expr ')'
641          {
642            if(iiExprArith1(&$$,&$3,MATRIX_CMD)) YYERROR;
643          }
644        | INTMAT_CMD '(' expr ')'
645          {
646            if(iiExprArith1(&$$,&$3,INTMAT_CMD)) YYERROR;
647          }
648        | CMD_M '(' exprlist ')'
649          {
650            if(iiExprArithM(&$$,&$3,$1)) YYERROR;
651          }
652        | quote_start expr quote_end
653          {
654            $$=$2;
655          }
656        | quote_start expr '=' expr quote_end
657          {
658            #ifdef SIQ
659            siq++;
660            if (siq>0)
661            { if (iiExprArith2(&$$,&$2,'=',&$4)) YYERROR; }
662            else
663            #endif
664            {
665              memset(&$$,0,sizeof($$));
666              $$.rtyp=NONE;
667              if (iiAssign(&$2,&$4)) YYERROR;
668            }
669            #ifdef SIQ
670            siq--;
671            #endif
672          }
673        | EVAL  '('
674          {
675            #ifdef SIQ
676            siq--;
677            #endif
678          }
679          expr ')'
680          {
681            #ifdef SIQ
682            if (siq<=0) $4.Eval();
683            #endif
684            $$=$4;
685            #ifdef SIQ
686            siq++;
687            #endif
688          }
689          ;
690
691quote_start:    QUOTE  '('
692          {
693            #ifdef SIQ
694            siq++;
695            #endif
696          }
697          ;
698
699quote_end: ')'
700          {
701            #ifdef SIQ
702            siq--;
703            #endif
704          }
705          ;
706
707expr_arithmetic:
708          expr PLUSPLUS     %prec PLUSPLUS
709          {
710            if(iiExprArith1(&$$,&$1,PLUSPLUS)) YYERROR;
711          }
712        | expr MINUSMINUS   %prec MINUSMINUS
713          {
714            if(iiExprArith1(&$$,&$1,MINUSMINUS)) YYERROR;
715          }
716        | expr '+' expr
717          {
718            if(iiExprArith2(&$$,&$1,'+',&$3)) YYERROR;
719          }
720        | expr '-' expr
721          {
722            if(iiExprArith2(&$$,&$1,'-',&$3)) YYERROR;
723          }
724        | expr '*' expr
725          {
726            if(iiExprArith2(&$$,&$1,'*',&$3)) YYERROR;
727          }
728        | expr '/' expr
729          {
730            if(iiExprArith2(&$$,&$1,'/',&$3)) YYERROR;
731          }
732        | expr INTDIV expr
733          {
734            if(iiExprArith2(&$$,&$1,INTDIV,&$3)) YYERROR;
735          }
736        | expr '^' expr
737          {
738            if(iiExprArith2(&$$,&$1,'^',&$3)) YYERROR;
739          }
740        | expr '%' expr
741          {
742            if(iiExprArith2(&$$,&$1,'%',&$3)) YYERROR;
743          }
744        | expr '>' expr
745          {
746            if(iiExprArith2(&$$,&$1,'>',&$3)) YYERROR;
747          }
748        | expr '<' expr
749          {
750            if(iiExprArith2(&$$,&$1,'<',&$3)) YYERROR;
751          }
752        | expr '&' expr
753          {
754            if(iiExprArith2(&$$,&$1,'&',&$3)) YYERROR;
755          }
756        | expr '|' expr
757          {
758            if(iiExprArith2(&$$,&$1,'|',&$3)) YYERROR;
759          }
760        | expr NOTEQUAL expr
761          {
762            if(iiExprArith2(&$$,&$1,NOTEQUAL,&$3)) YYERROR;
763          }
764        | expr EQUAL_EQUAL expr
765          {
766            if(iiExprArith2(&$$,&$1,EQUAL_EQUAL,&$3)) YYERROR;
767          }
768        | expr GE  expr
769          {
770            if(iiExprArith2(&$$,&$1,GE,&$3)) YYERROR;
771          }
772        | expr LE expr
773          {
774            if(iiExprArith2(&$$,&$1,LE,&$3)) YYERROR;
775          }
776        | expr DOTDOT expr
777          {
778            if(iiExprArith2(&$$,&$1,DOTDOT,&$3)) YYERROR;
779          }
780        | NOT '(' expr ')'
781          {
782            memset(&$$,0,sizeof($$));
783            int i; TESTSETINT($3,i);
784            $$.rtyp  = INT_CMD;
785            $$.data = (void *)(i == 0 ? 1 : 0);
786          }
787        | '-' expr %prec UMINUS
788          {
789            if(iiExprArith1(&$$,&$2,'-')) YYERROR;
790          }
791        ;
792
793left_value:
794        declare_ip_variable cmdeq  { $$ = $1; }
795        | exprlist '='
796          {
797            if ($1.rtyp==0)
798            {
799              Werror("`%s` is undefined",$1.Name());
800              YYERROR;
801            }
802            $$ = $1;
803          }
804        ;
805
806
807extendedid:
808        UNKNOWN_IDENT
809        | '`' expr '`'
810          {
811            if ($2.Typ()!=STRING_CMD)
812            {
813              MYYERROR("string expression expected");
814            }
815            $$ = (char *)$2.CopyD();
816            $2.CleanUp();
817          }
818        ;
819
820currring_lists:
821        IDEAL_CMD | MODUL_CMD
822        /* put variables into the current ring */
823        ;
824
825declare_ip_variable:
826        ROOT_DECL elemexpr
827          {
828            if (iiDeclCommand(&$$,&$2,myynest,$1,&idroot)) YYERROR;
829          }
830        | ROOT_DECL_LIST elemexpr
831          {
832            if (iiDeclCommand(&$$,&$2,myynest,$1,&idroot)) YYERROR;
833          }
834        | RING_DECL elemexpr
835          {
836            if (iiDeclCommand(&$$,&$2,myynest,$1,&(currRing->idroot))) YYERROR;
837          }
838        | currring_lists elemexpr
839          {
840            if (iiDeclCommand(&$$,&$2,myynest,$1,&(currRing->idroot))) YYERROR;
841          }
842        | MATRIX_CMD elemexpr '[' expr ']' '[' expr ']'
843          {
844            if (iiDeclCommand(&$$,&$2,myynest,$1,&(currRing->idroot))) YYERROR;
845            int r; TESTSETINT($4,r);
846            int c; TESTSETINT($7,c);
847            if (r < 1)
848              MYYERROR("rows must be greater than 0");
849            if (c < 0)
850              MYYERROR("cols must be greater than -1");
851            leftv v=&$$;
852            //while (v->next!=NULL) { v=v->next; }
853            idhdl h=(idhdl)v->data;
854            idDelete(&IDIDEAL(h));
855            IDMATRIX(h) = mpNew(r,c);
856            if (IDMATRIX(h)==NULL) YYERROR;
857          }
858        | MATRIX_CMD elemexpr
859          {
860            if (iiDeclCommand(&$$,&$2,myynest,$1,&(currRing->idroot))) YYERROR;
861          }
862        | INTMAT_CMD elemexpr '[' expr ']' '[' expr ']'
863          {
864            if (iiDeclCommand(&$$,&$2,myynest,$1,&idroot)) YYERROR;
865            int r; TESTSETINT($4,r);
866            int c; TESTSETINT($7,c);
867            if (r < 1)
868              MYYERROR("rows must be greater than 0");
869            if (c < 0)
870              MYYERROR("cols must be greater than -1");
871            leftv v=&$$;
872            //while (v->next!=NULL) { v=v->next; }
873            idhdl h=(idhdl)v->data;
874            delete IDINTVEC(h);
875            IDINTVEC(h) = new intvec(r,c,0);
876            if (IDINTVEC(h)==NULL) YYERROR;
877          }
878        | INTMAT_CMD elemexpr
879          {
880            if (iiDeclCommand(&$$,&$2,myynest,$1,&idroot)) YYERROR;
881            leftv v=&$$;
882            idhdl h;
883            do
884            {
885               h=(idhdl)v->data;
886               delete IDINTVEC(h);
887               IDINTVEC(h) = new intvec(1,1,0);
888               v=v->next;
889            } while (v!=NULL);
890          }
891        | declare_ip_variable ',' elemexpr
892          {
893            int t=$1.Typ();
894            sleftv r;
895            memset(&r,0,sizeof(sleftv));
896            if ((BEGIN_RING<t) && (t<END_RING))
897            {
898              if (iiDeclCommand(&r,&$3,myynest,t,&(currRing->idroot))) YYERROR;
899            }
900            else
901            {
902              if (iiDeclCommand(&r,&$3,myynest,t,&idroot)) YYERROR;
903            }
904            leftv v=&$1;
905            while (v->next!=NULL) v=v->next;
906            v->next=(leftv)Alloc(sizeof(sleftv));
907            memcpy(v->next,&r,sizeof(sleftv));
908            $$=$1;
909          }
910        | PROC_CMD elemexpr
911          {
912            if (iiDeclCommand(&$$,&$2,myynest,PROC_CMD,&idroot,TRUE)) YYERROR;
913          }
914        ;
915
916stringexpr:
917        STRINGTOK
918        ;
919
920rlist:
921        expr
922        | '(' expr ',' exprlist ')'
923          {
924            leftv v = &$2;
925            while (v->next!=NULL)
926            {
927              v=v->next;
928            }
929            v->next = (leftv)Alloc(sizeof(sleftv));
930            memcpy(v->next,&($4),sizeof(sleftv));
931            $$ = $2;
932          }
933        ;
934
935ordername:
936        UNKNOWN_IDENT
937        {
938          if (!($$=rOrderName($1)))
939            YYERROR;
940        }
941        ;
942
943orderelem:
944        ordername
945          {
946            memset(&$$,0,sizeof($$));
947            intvec *iv = new intvec(2);
948            (*iv)[0] = 1;
949            (*iv)[1] = $1;
950            $$.rtyp = INTVEC_CMD;
951            $$.data = (void *)iv;
952          }
953        | ordername '(' exprlist ')'
954          {
955            memset(&$$,0,sizeof($$));
956            leftv sl = &$3;
957            int slLength;
958            {
959              slLength =  exprlist_length(sl);
960              int l = 2 +  slLength;
961              intvec *iv = new intvec(l);
962              (*iv)[0] = slLength;
963              (*iv)[1] = $1;
964
965              int i = 2;
966              while ((i<l) && (sl!=NULL))
967              {
968                if (sl->Typ() == INT_CMD)
969                {
970                  (*iv)[i++] = (int)(sl->Data());
971                }
972                else if ((sl->Typ() == INTVEC_CMD)
973                ||(sl->Typ() == INTMAT_CMD))
974                {
975                  intvec *ivv = (intvec *)(sl->Data());
976                  int ll = 0,l = ivv->length();
977                  for (; l>0; l--)
978                  {
979                    (*iv)[i++] = (*ivv)[ll++];
980                  }
981                }
982                else
983                {
984                  delete iv;
985                  $3.CleanUp();
986                  MYYERROR("wrong type in ordering");
987                }
988                sl = sl->next;
989              }
990              $$.rtyp = INTVEC_CMD;
991              $$.data = (void *)iv;
992            }
993            $3.CleanUp();
994          }
995        ;
996
997OrderingList:
998        orderelem
999        |  orderelem ',' OrderingList
1000          {
1001            $$ = $1;
1002            $$.next = (sleftv *)Alloc(sizeof(sleftv));
1003            memcpy($$.next,&$3,sizeof(sleftv));
1004          }
1005        ;
1006
1007ordering:
1008        orderelem
1009        | '(' OrderingList ')'
1010          {
1011            $$ = $2;
1012          }
1013        ;
1014
1015cmdeq:  '='
1016          {
1017            expected_parms = TRUE;
1018          }
1019        ;
1020
1021
1022/* --------------------------------------------------------------------*/
1023/* section of pure commands                                            */
1024/* --------------------------------------------------------------------*/
1025
1026executecmd:
1027        EXECUTE_CMD expr
1028          {
1029            if ($2.Typ() == STRING_CMD)
1030            {
1031              char * s = (char *)AllocL(strlen((char *)$2.Data()) + 4);
1032              strcpy( s, (char *)$2.Data());
1033              strcat( s, "\n;\n");
1034              $2.CleanUp();
1035              newBuffer(s,BT_execute);
1036            }
1037            else
1038            {
1039              MYYERROR("string expected");
1040            }
1041          }
1042        | EXAMPLE_CMD extendedid
1043          {
1044            singular_help($2,TRUE);
1045            FreeL((ADDRESS)$2);
1046          }
1047        | EXAMPLE_CMD BLOCKTOK
1048          {
1049            FreeL((ADDRESS)$2);
1050          }
1051        ;
1052
1053filecmd:
1054        '<' stringexpr
1055          { if((feFilePending=feFopen($2,"r",NULL,TRUE))==NULL) YYERROR; }
1056        ';'
1057          { newFile($2,feFilePending); }
1058        ;
1059
1060helpcmd:
1061        HELP_CMD STRINGTOK ';'
1062          {
1063            singular_help($2,FALSE);
1064            FreeL((ADDRESS)$2);
1065          }
1066        | HELP_CMD ';'
1067          {
1068            char *s=(char *)Alloc(10);
1069            strcpy(s,"index");
1070            singular_help(s,FALSE);
1071            Free((ADDRESS)s,10);
1072          }
1073        ;
1074
1075exportcmd:
1076        EXPORT_CMD exprlist
1077        {
1078          if (iiExport(&$2,0)) YYERROR;
1079        }
1080        | EXPORT_CMD exprlist extendedid expr
1081        {
1082          if ((strcmp($3,"to")!=0) ||
1083          (($4.Typ()!=PACKAGE_CMD) && ($4.Typ()!=INT_CMD)))
1084            MYYERROR("export <id> to <package|int>");
1085          FreeL((ADDRESS)$3);
1086          if ($4.Typ()==INT_CMD)
1087          {
1088            if (iiExport(&$2,((int)$4.Data())-1)) YYERROR;
1089          }
1090          else
1091          {
1092            if (iiExport(&$2,0,IDPACKAGE((idhdl)$4.data)->idroot))
1093              YYERROR;
1094          }
1095        }
1096        ;
1097
1098killcmd:
1099        KILL_CMD kill_any_identifier {;}
1100        ;
1101
1102kill_any_identifier:
1103        elemexpr
1104          {
1105            leftv v=&$1;
1106            do
1107            {
1108              if (v->rtyp!=IDHDL)
1109              {
1110                if (v->name!=NULL) Werror("`%s` is undefined in kill",v->name);
1111                else               WerrorS("kill what ?");
1112              }
1113              else
1114              {
1115                killhdl((idhdl)v->data);
1116              }
1117              v=v->next;
1118            } while (v!=NULL);
1119            $1.CleanUp();
1120          }
1121        | kill_any_identifier ',' elemexpr
1122          {
1123            if ($3.rtyp!=IDHDL)
1124            {
1125              if ($3.name!=NULL) Werror("`%s` is undefined",$3.name);
1126              else               WerrorS("kill what ?");
1127            }
1128            else
1129            {
1130              killhdl((idhdl)$3.data);
1131            }
1132            $3.CleanUp();
1133          }
1134        ;
1135
1136listcmd:
1137        LISTVAR_CMD '(' ROOT_DECL ')'
1138          {
1139            list_cmd($3,NULL,"// ",TRUE);
1140          }
1141        | LISTVAR_CMD '(' ROOT_DECL_LIST ')'
1142          {
1143            list_cmd($3,NULL,"// ",TRUE);
1144          }
1145        | LISTVAR_CMD '(' RING_DECL ')'
1146          {
1147            if ($3==QRING_CMD) $3=RING_CMD;
1148            list_cmd($3,NULL,"// ",TRUE);
1149          }
1150        | LISTVAR_CMD '(' currring_lists ')'
1151          {
1152            list_cmd($3,NULL,"// ",TRUE);
1153          }
1154        | LISTVAR_CMD '(' RING_CMD ')'
1155          {
1156            list_cmd(RING_CMD,NULL,"// ",TRUE);
1157          }
1158        | LISTVAR_CMD '(' MATRIX_CMD ')'
1159          {
1160            list_cmd(MATRIX_CMD,NULL,"// ",TRUE);
1161           }
1162        | LISTVAR_CMD '(' INTMAT_CMD ')'
1163          {
1164            list_cmd(INTMAT_CMD,NULL,"// ",TRUE);
1165          }
1166        | LISTVAR_CMD '(' PROC_CMD ')'
1167          {
1168            list_cmd(PROC_CMD,NULL,"// ",TRUE);
1169          }
1170        | LISTVAR_CMD '(' elemexpr ')'
1171          {
1172            list_cmd(0,$3.Name(),"// ",TRUE);
1173            $3.CleanUp();
1174          }
1175        | LISTVAR_CMD '(' ')'
1176          {
1177            list_cmd(-1,NULL,"// ",TRUE);
1178          }
1179        ;
1180
1181ringcmd1:
1182       RING_CMD { noringvars = TRUE; }
1183       ;
1184
1185ringcmd:
1186        ringcmd1
1187          elemexpr cmdeq
1188          rlist     ','      /* description of coeffs */
1189          rlist     ','      /* var names */
1190          ordering           /* list of (multiplier ordering (weight(s))) */
1191          {
1192            //noringvars = FALSE;
1193            if(!rInit($2.name,        /* ringname */
1194                     &$4,            /* characteristik and list of parameters*/
1195                     &$6,            /* names of ringvariables */
1196                     &$8,            /* ordering */
1197                     FALSE))         /* is not a dring */
1198            {
1199              MYYERROR("cannot make ring");
1200            }
1201            $4.CleanUp();
1202            $6.CleanUp();
1203            $8.CleanUp();
1204          }
1205        | ringcmd1 elemexpr
1206          {
1207            //noringvars = FALSE;
1208            if (!inerror) rDefault($2.name);
1209          }
1210        | DRING_CMD { noringvars = TRUE; }
1211          elemexpr cmdeq
1212          rlist     ','       /* description of coeffs */
1213          rlist     ','       /* var names */
1214          ordering           /* list of (multiplier ordering (weight(s))) */
1215          {
1216            #ifdef DRING
1217            idhdl h;
1218            //noringvars = FALSE;
1219            if(!(h=rInit($3.name,    /* ringname */
1220                     &$5,            /* characteristik and list of parameters*/
1221                     &$7,            /* names of ringvariables */
1222                     &$9,            /* ordering */
1223                     TRUE)))         /* is a dring */
1224            {
1225              YYERROR;
1226            }
1227            setFlag(h,FLAG_DRING);
1228            rDSet();
1229            $5.CleanUp();
1230            $7.CleanUp();
1231            $9.CleanUp();
1232            #endif
1233          }
1234        ;
1235
1236scriptcmd:
1237         SYSVAR stringexpr
1238          {
1239            if (($1!=LIB_CMD)||(iiLibCmd($2))) YYERROR;
1240          }
1241        ;
1242
1243setrings:  SETRING_CMD | KEEPRING_CMD ;
1244
1245setringcmd:
1246        setrings expr
1247          {
1248            if (($1==KEEPRING_CMD) && (myynest==0))
1249               MYYERROR("only inside a proc allowed");
1250            const char * n=$2.Name();
1251            if ((($2.Typ()==RING_CMD)||($2.Typ()==QRING_CMD))
1252            && ($2.rtyp==IDHDL))
1253            {
1254              idhdl h=(idhdl)$2.data;
1255              if ($2.e!=NULL) h=rFindHdl((ring)$2.Data(),NULL);
1256              if ($1==KEEPRING_CMD)
1257              {
1258                if (h!=NULL)
1259                {
1260                  if (IDLEV(h)!=0)
1261                  {
1262                    if (iiExport(&$2,myynest-1)) YYERROR;
1263                    //if (TEST_OPT_KEEPVARS)
1264                    //{
1265                      idhdl p=IDRING(h)->idroot;
1266                      int prevlev=myynest-1;
1267                      while (p!=NULL)
1268                      {
1269                        if (IDLEV(p)==myynest) IDLEV(p)=prevlev;
1270                        p=IDNEXT(p);
1271                      }
1272                    //}
1273                  }
1274                  iiLocalRing[myynest-1]=IDRING(h);
1275                }
1276                else
1277                {
1278                  Werror("%s is no identifier",n);
1279                  $2.CleanUp();
1280                  YYERROR;
1281                }
1282              }
1283              if (h!=NULL) rSetHdl(h,TRUE);
1284              else
1285              {
1286                Werror("cannot find the name of the basering %s",n);
1287                $2.CleanUp();
1288                YYERROR;
1289              }
1290              $2.CleanUp();
1291            }
1292            else
1293            {
1294              Werror("%s is no name of a ring/qring",n);
1295              $2.CleanUp();
1296              YYERROR;
1297            }
1298          }
1299        ;
1300
1301typecmd:
1302        TYPE_CMD expr
1303          {
1304            if ($2.rtyp!=IDHDL) MYYERROR("identifier expected");
1305            idhdl h = (idhdl)$2.data;
1306            type_cmd(h);
1307          }
1308        | exprlist
1309          {
1310            //Print("typ is %d, rtyp:%d\n",$1.Typ(),$1.rtyp);
1311            #ifdef SIQ
1312            if ($1.rtyp!=COMMAND)
1313            {
1314            #endif
1315              if ($1.Typ()==UNKNOWN)
1316              {
1317                if ($1.name!=NULL)
1318                {
1319                  Werror("`%s` is undefined",$1.name);
1320                  FreeL((ADDRESS)$1.name);
1321                }
1322                YYERROR;
1323              }
1324            #ifdef SIQ
1325            }
1326            #endif
1327            $1.Print(&sLastPrinted);
1328            $1.CleanUp();
1329          }
1330        ;
1331
1332/* --------------------------------------------------------------------*/
1333/* section of flow control                                             */
1334/* --------------------------------------------------------------------*/
1335
1336ifcmd: IF_CMD '(' expr ')' BLOCKTOK
1337          {
1338            int i; TESTSETINT($3,i);
1339            if (i!=0)
1340            {
1341              newBuffer( $5, BT_if);
1342            }
1343            else
1344            {
1345              FreeL((ADDRESS)$5);
1346              currentVoice->ifsw=1;
1347            }
1348          }
1349        | ELSE_CMD BLOCKTOK
1350          {
1351            if (currentVoice->ifsw==1)
1352            {
1353              currentVoice->ifsw=0;
1354              newBuffer( $2, BT_else);
1355            }
1356            else
1357            {
1358              if (currentVoice->ifsw!=2)
1359              {
1360                char *s=$2+strlen($2)-1;
1361                while ((*s=='\0')||(*s=='\n')) s--;
1362                s[1]='\0';
1363                Warn("`else` without `if` in level %d",myynest);
1364              }
1365              FreeL((ADDRESS)$2);
1366            }
1367            currentVoice->ifsw=0;
1368          }
1369        | IF_CMD '(' expr ')' BREAK_CMD
1370          {
1371            int i; TESTSETINT($3,i);
1372            if (i)
1373            {
1374              if (exitBuffer(BT_break)) YYERROR;
1375            }
1376            currentVoice->ifsw=0;
1377          }
1378        | BREAK_CMD
1379          {
1380            if (exitBuffer(BT_break)) YYERROR;
1381            currentVoice->ifsw=0;
1382          }
1383        | CONTINUE_CMD
1384          {
1385            if (contBuffer(BT_break)) YYERROR;
1386            currentVoice->ifsw=0;
1387          }
1388      ;
1389
1390whilecmd:
1391        WHILE_CMD STRINGTOK BLOCKTOK
1392          {
1393            /* -> if(!$2) break; $3; continue;*/
1394            char * s = (char *)AllocL( strlen($2) + strlen($3) + 36);
1395            sprintf(s,"whileif (!(%s)) break;\n%scontinue;\n " ,$2,$3);
1396            newBuffer(s,BT_break);
1397            FreeL((ADDRESS)$2);
1398            FreeL((ADDRESS)$3);
1399          }
1400        ;
1401
1402forcmd:
1403        FOR_CMD STRINGTOK STRINGTOK STRINGTOK BLOCKTOK
1404          {
1405            /* $2 */
1406            /* if (!$3) break; $5; $4; continue; */
1407            char * s = (char *)AllocL( strlen($3)+strlen($4)+strlen($5)+36);
1408            sprintf(s,"forif (!(%s)) break;\n%s%s;\ncontinue;\n "
1409                   ,$3,$5,$4);
1410            FreeL((ADDRESS)$3);
1411            FreeL((ADDRESS)$4);
1412            FreeL((ADDRESS)$5);
1413            newBuffer(s,BT_break);
1414            s = (char *)AllocL( strlen($2) + 3);
1415            sprintf(s,"%s;\n",$2);
1416            FreeL((ADDRESS)$2);
1417            newBuffer(s,BT_if);
1418          }
1419        ;
1420
1421proccmd:
1422        PROC_CMD extendedid BLOCKTOK
1423          {
1424            procinfov pi;
1425            idhdl h = enterid($2,myynest,PROC_CMD,&idroot,FALSE);
1426            if (h==NULL) {FreeL((ADDRESS)$3); YYERROR;}
1427            iiInitSingularProcinfo(IDPROC(h),"", $2, 0, 0);
1428            IDPROC(h)->data.s.body = (char *)AllocL(strlen($3)+31);;
1429            sprintf(IDPROC(h)->data.s.body,"parameter list #;\n%s;return();\n\n",$3);
1430            FreeL((ADDRESS)$3);
1431          }
1432        | PROC_DEF STRINGTOK BLOCKTOK
1433          {
1434            idhdl h = enterid($1,myynest,PROC_CMD,&idroot,FALSE);
1435            if (h==NULL)
1436            {
1437              FreeL((ADDRESS)$2);
1438              FreeL((ADDRESS)$3);
1439              YYERROR;
1440            }
1441            char *args=iiProcArgs($2,FALSE);
1442            procinfov pi;
1443            FreeL((ADDRESS)$2);
1444            iiInitSingularProcinfo(IDPROC(h),"", $1, 0, 0);
1445            IDPROC(h)->data.s.body = (char *)AllocL(strlen($3)+strlen(args)+14);;
1446            sprintf(IDPROC(h)->data.s.body,"%s\n%s;return();\n\n",args,$3);
1447            FreeL((ADDRESS)args);
1448            FreeL((ADDRESS)$3);
1449            //Print(">>%s<<\n",IDPROC(h)->data.s.body);
1450          }
1451        | PROC_DEF STRINGTOK STRINGTOK BLOCKTOK
1452          {
1453            FreeL((ADDRESS)$3);
1454            idhdl h = enterid($1,myynest,PROC_CMD,&idroot,FALSE);
1455            if (h==NULL)
1456            {
1457              FreeL((ADDRESS)$2);
1458              FreeL((ADDRESS)$4);
1459              YYERROR;
1460            }
1461            char *args=iiProcArgs($2,FALSE);
1462            procinfov pi;
1463            FreeL((ADDRESS)$2);
1464            iiInitSingularProcinfo(IDPROC(h),"", $1, 0, 0);
1465            IDPROC(h)->data.s.body = (char *)AllocL(strlen($4)+strlen(args)+14);;
1466            sprintf(IDPROC(h)->data.s.body,"%s\n%s;return();\n\n",args,$4);
1467            FreeL((ADDRESS)args);
1468            FreeL((ADDRESS)$4);
1469            //Print(">>%s<<\n",IDPROC(h)->data.s.body);
1470          }
1471        ;
1472
1473parametercmd:
1474        PARAMETER declare_ip_variable
1475          {
1476            //Print("par:%s, %d\n",$2.Name(),$2.Typ());
1477            //yylineno--;
1478            if (iiParameter(&$2)) YYERROR;
1479          }
1480        | PARAMETER expr
1481          {
1482            //Print("par:%s, %d\n",$2.Name(),$2.Typ());
1483            sleftv tmp_expr;
1484            //yylineno--;
1485            if ((iiDeclCommand(&tmp_expr,&$2,myynest,DEF_CMD,&idroot))
1486            || (iiParameter(&tmp_expr)))
1487              YYERROR;
1488          }
1489        ;
1490
1491returncmd:
1492        RETURN '(' exprlist ')'
1493          {
1494            if(iiRETURNEXPR==NULL) YYERROR;
1495            iiRETURNEXPR[myynest].Copy(&$3);
1496            $3.CleanUp();
1497            if (exitBuffer(BT_proc)) YYERROR;
1498          }
1499        | RETURN '(' ')'
1500          {
1501            if ($1==RETURN)
1502            {
1503              if(iiRETURNEXPR==NULL) YYERROR;
1504              iiRETURNEXPR[myynest].Init();
1505              iiRETURNEXPR[myynest].rtyp=NONE;
1506              if (exitBuffer(BT_proc)) YYERROR;
1507            }
1508          }
1509        ;
Note: See TracBrowser for help on using the repository browser.