source: git/Singular/grammar.y @ 356dc3d

spielwiese
Last change on this file since 356dc3d was 356dc3d, checked in by Hans Schönemann <hannes@…>, 26 years ago
* hannes: removed END_GRAMMAR ("RETURN") from iparith.cc, grammar.y git-svn-id: file:///usr/local/Singular/svn/trunk@1444 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 37.6 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: grammar.y,v 1.27 1998-04-23 18:52:54 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        ;
1048
1049filecmd:
1050        '<' stringexpr
1051          { if((feFilePending=feFopen($2,"r",NULL,TRUE))==NULL) YYERROR; }
1052        ';'
1053          { newFile($2,feFilePending); }
1054        ;
1055
1056helpcmd:
1057        HELP_CMD STRINGTOK ';'
1058          {
1059            singular_help($2,FALSE);
1060            FreeL((ADDRESS)$2);
1061          }
1062        | HELP_CMD ';'
1063          {
1064            char *s=(char *)Alloc(10);
1065            strcpy(s,"index");
1066            singular_help(s,FALSE);
1067            Free((ADDRESS)s,10);
1068          }
1069        ;
1070
1071exportcmd:
1072        EXPORT_CMD exprlist
1073        {
1074          if (iiExport(&$2,0)) YYERROR;
1075        }
1076        | EXPORT_CMD exprlist extendedid expr
1077        {
1078          if ((strcmp($3,"to")!=0) ||
1079          (($4.Typ()!=PACKAGE_CMD) && ($4.Typ()!=INT_CMD)))
1080            MYYERROR("export <id> to <package|int>");
1081          FreeL((ADDRESS)$3);
1082          if ($4.Typ()==INT_CMD)
1083          {
1084            if (iiExport(&$2,((int)$4.Data())-1)) YYERROR;
1085          }
1086          else
1087          {
1088            if (iiExport(&$2,0,IDPACKAGE((idhdl)$4.data)->idroot))
1089              YYERROR;
1090          }
1091        }
1092        ;
1093
1094killcmd:
1095        KILL_CMD kill_any_identifier {;}
1096        ;
1097
1098kill_any_identifier:
1099        elemexpr
1100          {
1101            leftv v=&$1;
1102            do
1103            {
1104              if (v->rtyp!=IDHDL)
1105              {
1106                if (v->name!=NULL) Werror("`%s` is undefined in kill",v->name);
1107                else               WerrorS("kill what ?");
1108              }
1109              else
1110              {
1111                killhdl((idhdl)v->data);
1112              }
1113              v=v->next;
1114            } while (v!=NULL);
1115            $1.CleanUp();
1116          }
1117        | kill_any_identifier ',' elemexpr
1118          {
1119            if ($3.rtyp!=IDHDL)
1120            {
1121              if ($3.name!=NULL) Werror("`%s` is undefined",$3.name);
1122              else               WerrorS("kill what ?");
1123            }
1124            else
1125            {
1126              killhdl((idhdl)$3.data);
1127            }
1128            $3.CleanUp();
1129          }
1130        ;
1131
1132listcmd:
1133        LISTVAR_CMD '(' ROOT_DECL ')'
1134          {
1135            list_cmd($3,NULL,"// ",TRUE);
1136          }
1137        | LISTVAR_CMD '(' ROOT_DECL_LIST ')'
1138          {
1139            list_cmd($3,NULL,"// ",TRUE);
1140          }
1141        | LISTVAR_CMD '(' RING_DECL ')'
1142          {
1143            if ($3==QRING_CMD) $3=RING_CMD;
1144            list_cmd($3,NULL,"// ",TRUE);
1145          }
1146        | LISTVAR_CMD '(' currring_lists ')'
1147          {
1148            list_cmd($3,NULL,"// ",TRUE);
1149          }
1150        | LISTVAR_CMD '(' RING_CMD ')'
1151          {
1152            list_cmd(RING_CMD,NULL,"// ",TRUE);
1153          }
1154        | LISTVAR_CMD '(' MATRIX_CMD ')'
1155          {
1156            list_cmd(MATRIX_CMD,NULL,"// ",TRUE);
1157           }
1158        | LISTVAR_CMD '(' INTMAT_CMD ')'
1159          {
1160            list_cmd(INTMAT_CMD,NULL,"// ",TRUE);
1161          }
1162        | LISTVAR_CMD '(' PROC_CMD ')'
1163          {
1164            list_cmd(PROC_CMD,NULL,"// ",TRUE);
1165          }
1166        | LISTVAR_CMD '(' elemexpr ')'
1167          {
1168            list_cmd(0,$3.Name(),"// ",TRUE);
1169            $3.CleanUp();
1170          }
1171        | LISTVAR_CMD '(' ')'
1172          {
1173            list_cmd(-1,NULL,"// ",TRUE);
1174          }
1175        ;
1176
1177ringcmd1:
1178       RING_CMD { noringvars = TRUE; }
1179       ;
1180
1181ringcmd:
1182        ringcmd1
1183          elemexpr cmdeq
1184          rlist     ','      /* description of coeffs */
1185          rlist     ','      /* var names */
1186          ordering           /* list of (multiplier ordering (weight(s))) */
1187          {
1188            //noringvars = FALSE;
1189            if(!rInit($2.name,        /* ringname */
1190                     &$4,            /* characteristik and list of parameters*/
1191                     &$6,            /* names of ringvariables */
1192                     &$8,            /* ordering */
1193                     FALSE))         /* is not a dring */
1194            {
1195              MYYERROR("cannot make ring");
1196            }
1197            $4.CleanUp();
1198            $6.CleanUp();
1199            $8.CleanUp();
1200          }
1201        | ringcmd1 elemexpr
1202          {
1203            //noringvars = FALSE;
1204            if (!inerror) rDefault($2.name);
1205          }
1206        | DRING_CMD { noringvars = TRUE; }
1207          elemexpr cmdeq
1208          rlist     ','       /* description of coeffs */
1209          rlist     ','       /* var names */
1210          ordering           /* list of (multiplier ordering (weight(s))) */
1211          {
1212            #ifdef DRING
1213            idhdl h;
1214            //noringvars = FALSE;
1215            if(!(h=rInit($3.name,    /* ringname */
1216                     &$5,            /* characteristik and list of parameters*/
1217                     &$7,            /* names of ringvariables */
1218                     &$9,            /* ordering */
1219                     TRUE)))         /* is a dring */
1220            {
1221              YYERROR;
1222            }
1223            setFlag(h,FLAG_DRING);
1224            rDSet();
1225            $5.CleanUp();
1226            $7.CleanUp();
1227            $9.CleanUp();
1228            #endif
1229          }
1230        ;
1231
1232scriptcmd:
1233         SYSVAR stringexpr
1234          {
1235            if (($1!=LIB_CMD)||(iiLibCmd($2))) YYERROR;
1236          }
1237        ;
1238
1239setrings:  SETRING_CMD | KEEPRING_CMD ;
1240
1241setringcmd:
1242        setrings expr
1243          {
1244            if (($1==KEEPRING_CMD) && (myynest==0))
1245               MYYERROR("only inside a proc allowed");
1246            const char * n=$2.Name();
1247            if ((($2.Typ()==RING_CMD)||($2.Typ()==QRING_CMD))
1248            && ($2.rtyp==IDHDL))
1249            {
1250              idhdl h=(idhdl)$2.data;
1251              if ($2.e!=NULL) h=rFindHdl((ring)$2.Data(),NULL);
1252              if ($1==KEEPRING_CMD)
1253              {
1254                if (h!=NULL)
1255                {
1256                  if (IDLEV(h)!=0)
1257                  {
1258                    if (iiExport(&$2,myynest-1)) YYERROR;
1259                    //if (TEST_OPT_KEEPVARS)
1260                    //{
1261                      idhdl p=IDRING(h)->idroot;
1262                      int prevlev=myynest-1;
1263                      while (p!=NULL)
1264                      {
1265                        if (IDLEV(p)==myynest) IDLEV(p)=prevlev;
1266                        p=IDNEXT(p);
1267                      }
1268                    //}
1269                  }
1270                  iiLocalRing[myynest-1]=IDRING(h);
1271                }
1272                else
1273                {
1274                  Werror("%s is no identifier",n);
1275                  $2.CleanUp();
1276                  YYERROR;
1277                }
1278              }
1279              if (h!=NULL) rSetHdl(h,TRUE);
1280              else
1281              {
1282                Werror("cannot find the name of the basering %s",n);
1283                $2.CleanUp();
1284                YYERROR;
1285              }
1286              $2.CleanUp();
1287            }
1288            else
1289            {
1290              Werror("%s is no name of a ring/qring",n);
1291              $2.CleanUp();
1292              YYERROR;
1293            }
1294          }
1295        ;
1296
1297typecmd:
1298        TYPE_CMD expr
1299          {
1300            if ($2.rtyp!=IDHDL) MYYERROR("identifier expected");
1301            idhdl h = (idhdl)$2.data;
1302            type_cmd(h);
1303          }
1304        | exprlist
1305          {
1306            //Print("typ is %d, rtyp:%d\n",$1.Typ(),$1.rtyp);
1307            #ifdef SIQ
1308            if ($1.rtyp!=COMMAND)
1309            {
1310            #endif
1311              if ($1.Typ()==UNKNOWN)
1312              {
1313                if ($1.name!=NULL)
1314                {
1315                  Werror("`%s` is undefined",$1.name);
1316                  FreeL((ADDRESS)$1.name);
1317                }
1318                YYERROR;
1319              }
1320            #ifdef SIQ
1321            }
1322            #endif
1323            $1.Print(&sLastPrinted);
1324            $1.CleanUp();
1325          }
1326        ;
1327
1328/* --------------------------------------------------------------------*/
1329/* section of flow control                                             */
1330/* --------------------------------------------------------------------*/
1331
1332ifcmd: IF_CMD '(' expr ')' BLOCKTOK
1333          {
1334            int i; TESTSETINT($3,i);
1335            if (i!=0)
1336            {
1337              newBuffer( $5, BT_if);
1338            }
1339            else
1340            {
1341              FreeL((ADDRESS)$5);
1342              currentVoice->ifsw=1;
1343            }
1344          }
1345        | ELSE_CMD BLOCKTOK
1346          {
1347            if (currentVoice->ifsw==1)
1348            {
1349              currentVoice->ifsw=0;
1350              newBuffer( $2, BT_else);
1351            }
1352            else
1353            {
1354              if (currentVoice->ifsw!=2)
1355              {
1356                char *s=$2+strlen($2)-1;
1357                while ((*s=='\0')||(*s=='\n')) s--;
1358                s[1]='\0';
1359                Warn("`else` without `if` in level %d",myynest);
1360              }
1361              FreeL((ADDRESS)$2);
1362            }
1363            currentVoice->ifsw=0;
1364          }
1365        | IF_CMD '(' expr ')' BREAK_CMD
1366          {
1367            int i; TESTSETINT($3,i);
1368            if (i)
1369            {
1370              if (exitBuffer(BT_break)) YYERROR;
1371            }
1372            currentVoice->ifsw=0;
1373          }
1374        | BREAK_CMD
1375          {
1376            if (exitBuffer(BT_break)) YYERROR;
1377            currentVoice->ifsw=0;
1378          }
1379        | CONTINUE_CMD
1380          {
1381            if (contBuffer(BT_break)) YYERROR;
1382            currentVoice->ifsw=0;
1383          }
1384      ;
1385
1386whilecmd:
1387        WHILE_CMD STRINGTOK BLOCKTOK
1388          {
1389            /* -> if(!$2) break; $3; continue;*/
1390            char * s = (char *)AllocL( strlen($2) + strlen($3) + 36);
1391            sprintf(s,"whileif (!(%s)) break;\n%scontinue;\n " ,$2,$3);
1392            newBuffer(s,BT_break);
1393            FreeL((ADDRESS)$2);
1394            FreeL((ADDRESS)$3);
1395          }
1396        ;
1397
1398forcmd:
1399        FOR_CMD STRINGTOK STRINGTOK STRINGTOK BLOCKTOK
1400          {
1401            /* $2 */
1402            /* if (!$3) break; $5; $4; continue; */
1403            char * s = (char *)AllocL( strlen($3)+strlen($4)+strlen($5)+36);
1404            sprintf(s,"forif (!(%s)) break;\n%s%s;\ncontinue;\n "
1405                   ,$3,$5,$4);
1406            FreeL((ADDRESS)$3);
1407            FreeL((ADDRESS)$4);
1408            FreeL((ADDRESS)$5);
1409            newBuffer(s,BT_break);
1410            s = (char *)AllocL( strlen($2) + 3);
1411            sprintf(s,"%s;\n",$2);
1412            FreeL((ADDRESS)$2);
1413            newBuffer(s,BT_if);
1414          }
1415        ;
1416
1417proccmd:
1418        PROC_CMD extendedid BLOCKTOK
1419          {
1420            procinfov pi;
1421            idhdl h = enterid($2,myynest,PROC_CMD,&idroot,FALSE);
1422            if (h==NULL) {FreeL((ADDRESS)$3); YYERROR;}
1423            iiInitSingularProcinfo(IDPROC(h),"", $2, 0, 0);
1424            IDPROC(h)->data.s.body = (char *)AllocL(strlen($3)+31);;
1425            sprintf(IDPROC(h)->data.s.body,"parameter list #;\n%s;return();\n\n",$3);
1426            FreeL((ADDRESS)$3);
1427          }
1428        | PROC_DEF STRINGTOK BLOCKTOK
1429          {
1430            idhdl h = enterid($1,myynest,PROC_CMD,&idroot,FALSE);
1431            if (h==NULL)
1432            {
1433              FreeL((ADDRESS)$2);
1434              FreeL((ADDRESS)$3);
1435              YYERROR;
1436            }
1437            char *args=iiProcArgs($2,FALSE);
1438            procinfov pi;
1439            FreeL((ADDRESS)$2);
1440            iiInitSingularProcinfo(IDPROC(h),"", $1, 0, 0);
1441            IDPROC(h)->data.s.body = (char *)AllocL(strlen($3)+strlen(args)+14);;
1442            sprintf(IDPROC(h)->data.s.body,"%s\n%s;return();\n\n",args,$3);
1443            FreeL((ADDRESS)args);
1444            FreeL((ADDRESS)$3);
1445            //Print(">>%s<<\n",IDPROC(h)->data.s.body);
1446          }
1447        ;
1448
1449parametercmd:
1450        PARAMETER declare_ip_variable
1451          {
1452            //Print("par:%s, %d\n",$2.Name(),$2.Typ());
1453            //yylineno--;
1454            if (iiParameter(&$2)) YYERROR;
1455          }
1456        | PARAMETER expr
1457          {
1458            //Print("par:%s, %d\n",$2.Name(),$2.Typ());
1459            sleftv tmp_expr;
1460            //yylineno--;
1461            if ((iiDeclCommand(&tmp_expr,&$2,myynest,DEF_CMD,&idroot))
1462            || (iiParameter(&tmp_expr)))
1463              YYERROR;
1464          }
1465        ;
1466
1467returncmd:
1468        RETURN '(' exprlist ')'
1469          {
1470            if(iiRETURNEXPR==NULL) YYERROR;
1471            iiRETURNEXPR[myynest].Copy(&$3);
1472            $3.CleanUp();
1473            if (exitBuffer(BT_proc)) YYERROR;
1474          }
1475        | RETURN '(' ')'
1476          {
1477            if(iiRETURNEXPR==NULL) YYERROR;
1478            iiRETURNEXPR[myynest].Init();
1479            iiRETURNEXPR[myynest].rtyp=NONE;
1480            if (exitBuffer(BT_proc)) YYERROR;
1481          }
1482        ;
Note: See TracBrowser for help on using the repository browser.