2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) ????-2008 - INRIA
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
15 #include "intersci-n.h"
23 static int removeEOL(char *_str)
27 int len = (int)strlen(_str);
30 if (_str[len - 1] == CR)
39 /**********************************************************
40 *Reading the intersci description file
41 **********************************************************/
43 int ReadFunction(FILE *f)
45 int i = 0, j = 0, l = 0, type = 0, ftype = 0;
48 char *optwords[MAXLINE];
50 int nwords = 0, line1 = 0, inbas = 0, fline1 = 0, infor = 0, nopt = 0, out1 = 0;
55 basfun->NewMaxOpt = 0;
64 while (fgets(s, MAXLINE, f) != NULL)
67 /* ignoring comments */
68 if (s[0] == '/' && s[1] == '/' ) continue;
70 /* analysis of one line */
72 nwords = ParseLine(s,words);
74 nwords = ParseScilabLine(s,words);
75 /* empty definition at end of file */
76 if (line1 == 1 && nwords == 0)
80 /* end of description */
81 if (words[0][0] == '*') return(1);
84 /* SCILAB function description */
85 if ((int)strlen(words[0]) > nlgh)
87 printf("SCILAB function name too long: \"%s\"\n",words[0]);
90 basfun->name = (char *)malloc((unsigned)(strlen(words[0])+1));
91 strcpy(basfun->name,words[0]);
92 printf("**************************\n");
93 printf("processing SCILAB function \"%s\"\n",words[0]);
94 funNames[nFun] = basfun->name;
98 printf("too may input arguments for SCILAB function\"%s\"\n",
100 printf(" augment constant \"MAXARG\" and recompile intersci\n");
104 for (i = 0; i < basfun->nin ; i++)
106 if (words[i+1][0] == '{')
109 nopt = ParseLine(words[i+1]+1,optwords);
111 printf("Bad syntax for optional argument. Two variables needed\n");
114 ivar = GetVar(optwords[0],1);
115 basfun->in[i] = ivar;
116 variables[ivar-1]->opt_type = NAME;
117 variables[ivar-1]->opt_name =
118 (char *)malloc((unsigned)(strlen(optwords[1])+1));
119 variables[ivar-1]->stack_position = icre++;
120 strcpy(variables[ivar-1]->opt_name,optwords[1]);
121 variables[ivar-1]->is_sciarg = 1;
123 else if (words[i+1][0] == '[')
126 nopt = ParseLine(words[i+1]+1,optwords);
129 printf("Bad syntax for optional argument. Two variables needed\n");
132 ivar = GetVar(optwords[0],1);
133 basfun->in[i] = ivar;
134 variables[ivar-1]->opt_type = VALUE;
135 variables[ivar-1]->opt_name =
136 (char *)malloc((unsigned)(strlen(optwords[1])+1));
137 strcpy(variables[ivar-1]->opt_name,optwords[1]);
138 variables[ivar-1]->stack_position = icre++;
139 variables[ivar-1]->is_sciarg = 1;
143 basfun->in[i] = GetVar(words[i+1],1);
144 variables[basfun->in[i]-1]->stack_position = icre++;
145 variables[basfun->in[i]-1]->is_sciarg = 1;
155 /* end of SCILAB variable description */
161 /* SCILAB variable description */
162 ivar = GetVar(words[0],1);
164 if ( variables[i]->is_sciarg == 0)
166 /** we only fix stack_position for remaining arguments**/
167 variables[i]->stack_position = icre++;
171 printf("type missing for variable \"%s\"\n",words[0]);
174 type = GetBasType(words[1]);
175 variables[i]->type = type;
193 printf("bad type specification for variable \"%s\"\n", words[0]);
194 printf("only %d argument given and %d are expected\n", nwords,3);
197 variables[i]->el[0] = GetVar(words[2],1);
198 variables[i]->length++;
204 printf("bad type specification for variable \"%s\"\n", words[0]);
205 printf("only %d argument given and %d are expected\n", nwords,3);
208 ReadListFile(words[2],words[0],i,
209 variables[i]->stack_position);
217 printf("bad type specification for variable \"%s\"\n",words[0]);
218 printf("%d argument given and %d are expected\n", nwords,4);
221 variables[i]->el[0] = GetVar(words[2],1);
222 variables[i]->el[1] = GetVar(words[3],1);
223 variables[i]->length = 2;
228 printf("bad type specification for variable \"%s\"\n",words[0]);
229 printf("%d argument given and %d are expected\n", nwords,4);
232 variables[i]->el[0] = GetVar(words[2],1);
233 variables[i]->el[1] = GetVar(words[3],1);
234 variables[i]->el[2] = GetVar(words[4],1);
235 variables[i]->length = 3;
240 printf("bad type specification for variable \"%s\"\n",words[0]);
241 printf("%d argument given and %d are expected\n", nwords,6);
242 printf("name sparse m n nel it\n");
245 variables[i]->el[0] = GetVar(words[2],1);
246 variables[i]->el[1] = GetVar(words[3],1);
247 variables[i]->el[2] = GetVar(words[4],1);
248 variables[i]->el[3] = GetVar(words[5],1);
249 variables[i]->length = 4;
252 printf("variable \"%s\" cannot have type \"SEQUENCE\"\n",
257 printf("variable \"%s\" cannot have type \"EMPTY\"\n",
264 else if (fline1 == 1)
266 /* FORTRAN subroutine description */
267 forsub->name = (char *)malloc((unsigned)(strlen(words[0])+1));
268 strcpy(forsub->name,words[0]);
272 printf("too many argument for FORTRAN subroutine \"%s\"\n",
274 printf(" augment constant \"MAXARG\" and recompile intersci\n");
278 for (i = 0; i < nwords - 1; i++)
280 forsub->arg[i] = GetExistVar(words[i+1]);
289 /* end of FORTRAN subroutine description */
295 /* FORTRAN variable description */
298 printf("type missing for FORTRAN argument \"%s\"\n",
302 ivar = GetExistVar(words[0]);
303 ftype = GetForType(words[1]);
304 variables[ivar-1]->for_type = ftype;
305 if (ftype == EXTERNAL)
307 strcpy((char *)(variables[ivar-1]->fexternal),words[1]);
308 switch (variables[ivar-1]->type)
322 printf("FORTRAN argument \"%s\" with external type \"%s\"\n",
323 variables[ivar-1]->name,words[1]);
324 printf(" cannot have a variable type of \"%s\"\n",SGetSciType(variables[ivar-1]->type));
333 /* output variable description */
337 printf("type missing for output variable \"out\"\n");
340 ivar = GetOutVar(words[0]);
343 type = GetBasType(words[1]);
344 variables[i]->type = type;
353 printf("list or sequence too long for output variable \"out\"\n");
354 printf(" augment constant \"MAXEL\" and recompile intersci\n");
357 for (j = 0; j < l; j++)
359 int k = GetExistVar(words[j+2]);
360 variables[i]->el[j] = k;
361 variables[k-1]->out_position = j+1;
363 variables[i]->length = l;
368 printf("output variable \"out\" of SCILAB function\n");
369 printf(" must have type \"LIST\", \"TLIST\", \"SEQUENCE\" or\n");
370 printf(" \"EMPTY\"\n");
378 /* possibly equal variables */
379 ivar = GetExistVar(words[0]);
381 variables[i]->equal = GetExistVar(words[1]);
385 /* end of description file */
389 /***********************************************************************
390 * put the words of SCILAB function description line "s" in "words" and
391 * return the number of words with checking syntax of optional variables:
392 * "{g the_g }" => 1 word "{g the_g\n"
393 * "[f v]" => 1 word "[f v\n"
394 **************************************************************************/
396 int ParseScilabLine(char *s,char *words[])
401 int inopt1 = 0; /* { } */
402 int inopt2 = 0; /* [ ] */
404 if (*s == ' ' || *s == '\t') inword = 0;
405 if (*s == '{') inopt1 = 1;
406 if (*s == '[') inopt2 = 1;
410 if (*s == '{' || *s == '[' || *s == ']' || *s == '\n') {
411 printf("Bad syntax for optional argument. No matching \"}\"\n");
414 else if (*s == '}') {
417 words[nwords] = (char *)malloc((unsigned)(i+1));
418 strcpy(words[nwords],w);
426 if (*s == '[' || *s == '{' || *s == '}' || *s == '\n') {
427 printf("Bad syntax for optional argument. No matching \"]\"\n");
430 else if (*s == ']') {
433 words[nwords] = (char *)malloc((unsigned)(i+1));
434 strcpy(words[nwords],w);
442 if (*s == ' ' || *s == '\t' || *s == '\n') {
444 words[nwords] = (char *)malloc((unsigned)(i+1));
445 strcpy(words[nwords],w);
452 if (*s != ' ' && *s != '\t') {
453 /* beginning of a word */
456 if (*s == '{') inopt1 = 1;
457 if (*s == '[') inopt2 = 1;
464 /* put the words of line "s" in "words" and return the number of words */
466 int ParseLine(char *s,char *words[])
472 if(*s == ' ' || *s == '\t') inword = 0;
476 if (*s == ' ' || *s == '\t' || *s == '\n') {
478 words[nwords] = (char *)malloc((unsigned)(i+1));
479 strcpy(words[nwords],w);
486 if (*s != ' ' && *s != '\t') {
495 /***********************************************************************
496 * Read a List description file
497 **************************************************************************/
500 void ReadListFile(char *listname,char *varlistname,IVAR ivar,int stack_position)
506 sprintf(filin,"%s.list",listname);
507 fin = fopen(filin,"rt");
510 printf("description file for list or tlist \"%s\" does not exist\n",
514 printf("reading description file for list or tlist \"%s\"\n", listname);
517 while(ReadListElement(fin,varlistname,ivar,nel,stack_position))
525 int ReadListElement(FILE *f,char *varlistname,IVAR iivar,int nel,int stack_position)
528 char *words[MAXLINE];
529 int i = 0, nline = 0, nwords = 0, type = 0;
535 while (fgets(s, MAXLINE, f) != NULL)
538 /* analyse of one line */
545 /* SCILAB variable description */
546 nwords = ParseLine(s,words);
547 sprintf(str,"%s(%s)",words[0],varlistname);
548 ivar = GetVar(str,0);
550 variables[ivar-1]->stack_position =stack_position;
553 printf("type missing for variable \"%s\"\n",words[0]);
556 type = GetBasType(words[1]);
557 variables[i]->type = type;
558 variables[i]->list_name = (char *)malloc((unsigned)(strlen(varlistname)+1));
559 strcpy(variables[i]->list_name,varlistname);
560 variables[i]->list_el = nel+1;
572 printf("bad type for variable \"%s\"\n",
576 if (isdigit(words[2][0]))
578 variables[i]->el[0] = GetVar(words[2],0);
579 variables[i]->length = 1;
583 sprintf(str,"%s(%s)",words[2],varlistname);
584 variables[i]->el[0] = GetVar(str,0);
585 variables[i]->length = 1;
594 printf("bad type for variable \"%s\"\n",
598 if (isdigit(words[2][0]))
600 variables[i]->el[0] = GetVar(words[2],0);
601 variables[i]->length = 1;
605 sprintf(str,"%s(%s)",words[2],varlistname);
606 variables[i]->el[0] = GetVar(str,0);
607 variables[i]->length = 1;
609 if (isdigit(words[3][0]))
611 variables[i]->el[1] = GetVar(words[3],0);
612 variables[i]->length = 2;
616 sprintf(str,"%s(%s)",words[3],varlistname);
617 variables[i]->el[1] = GetVar(str,0);
618 variables[i]->length = 2;
624 printf("bad type for variable \"%s\"\n",
628 if (isdigit(words[2][0]))
630 variables[i]->el[0] = GetVar(words[2],0);
631 variables[i]->length = 1;
635 sprintf(str,"%s(%s)",words[2],varlistname);
636 variables[i]->el[0] = GetVar(str,0);
637 variables[i]->length = 1;
639 if (isdigit(words[3][0]))
641 variables[i]->el[1] = GetVar(words[3],0);
642 variables[i]->length = 2;
646 sprintf(str,"%s(%s)",words[3],varlistname);
647 variables[i]->el[1] = GetVar(str,0);
648 variables[i]->length = 2;
650 sprintf(str,"%s(%s)",words[4],varlistname);
651 variables[i]->el[2] = GetVar(str,0);
652 variables[i]->length = 3;
657 printf("bad type for variable \"%s\"\n",
661 if (isdigit(words[2][0]))
663 variables[i]->el[0] = GetVar(words[2],0);
664 variables[i]->length = 1;
668 sprintf(str,"%s(%s)",words[2],varlistname);
669 variables[i]->el[0] = GetVar(str,0);
670 variables[i]->length = 1;
672 if (isdigit(words[3][0]))
674 variables[i]->el[1] = GetVar(words[3],0);
675 variables[i]->length = 2;
679 sprintf(str,"%s(%s)",words[3],varlistname);
680 variables[i]->el[1] = GetVar(str,0);
681 variables[i]->length = 2;
683 if (isdigit(words[4][0]))
685 variables[i]->el[2] = GetVar(words[4],0);
686 variables[i]->length = 3;
690 sprintf(str,"%s(%s)",words[4],varlistname);
691 variables[i]->el[2] = GetVar(str,0);
692 variables[i]->length = 3;
694 sprintf(str,"%s(%s)",words[5],varlistname);
695 variables[i]->el[3] = GetVar(str,0);
696 variables[i]->length = 4;
703 printf("variable \"%s\" cannot have type \"%s\"\n",
704 words[0],SGetSciType(type));
707 printf("variable \"%s\" has unknown type \"%s\"\n",
708 words[0],SGetSciType(type));
712 /* end of description */
719 printf("bad description file for list or tlist \"%s\"\n",