source: git/Singular/grammar.y @ 474652

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