示例#1
0
void init_any(void)
{
    SYMBOL *sym;
    int i;

    for (i = 0; af[i].name; i++) {
	sym = putsym(af[i].name);
	sym->type = sym->itype = st_afunc;
	sym->v.p = af[i].func;
	sym->proto = af[i].proto;
	sym->rettype = st_any;
    }
}
示例#2
0
void
cgetret(char *s)
{
	putsym('\n');
	if(flags.standout)
		standoutbeg();
	putstr("Hit ");
	putstr(flags.cbreak ? "space" : "return");
	putstr(" to continue: ");
	if(flags.standout)
		standoutend();
	xwaitforspace(s);
}
示例#3
0
void addtopl(char *s)
{
    curs(tlx, tly);

    if((tlx + strlen(s)) > COLNO) {
        putsym('\n');
    }

    putstr(s);
    tlx = curx;
    tly = cury;
    flags.topl = 1;
}
示例#4
0
文件: map.c 项目: caomw/grass
void init_map(void)
{
    SYMBOL *sym;
    int i;

    for (i = 0; mf[i].name; i++) {
	sym = putsym(mf[i].name);
	sym->type = sym->itype = st_mfunc;
	sym->v.p = mf[i].func;
	sym->proto = mf[i].proto;
	sym->rettype = st_map;
    }

    find_maps();
}
示例#5
0
/*
   Constructor for declaration syntax node.
*/
SyntaxNode* makeDeclaration(char *name, char *type) {
    SyntaxNode *newNode;
    newNode = make(DECLARATION);

    if(getsym(name) != 0) {
        fprintf(stderr, "Error: %s declared previously.\n", name);
        exit(1);
    }

    symRecord *sr; // NOTE: putsym creates the memory block for the new record
    DataType dataType = getTypeFromString(type);
    sr = putsym(name, dataType);
    newNode->dataType = dataType;
    newNode->record = sr;
    return newNode;
}
示例#6
0
int open_exodus_file(char *filename)
{
  int   cpu = sizeof(double);
  int   io  = 0;
  int   exo;
  float version; 

 exo=ex_open(filename,EX_READ,&cpu,&io,&version);
 if (exo < 0) {
   yyerror("Error opening exodusII file.");
 } else {
  symrec *ptr;
  ptr = putsym("ex_version", VAR, 0);
  ptr->value.var = version;
 }
 return exo;
}
示例#7
0
void init_table(char *comment)
{
  int i;
  symrec *ptr;
  for (i = 0; arith_fncts[i].fname != 0; i++)
    {
      ptr = putsym(arith_fncts[i].fname, FNCT, 1);
      ptr->value.fnctptr = arith_fncts[i].fnct;
      ptr->info = arith_fncts[i].description;
      ptr->syntax = arith_fncts[i].syntax;
    }
  for (i = 0; string_fncts[i].fname != 0; i++)
    {
      ptr = putsym(string_fncts[i].fname, SFNCT, 1);
      ptr->value.strfnct = string_fncts[i].fnct;
      ptr->info = string_fncts[i].description;
      ptr->syntax = string_fncts[i].syntax;
    }
  for (i = 0; array_fncts[i].fname != 0; i++)
    {
      ptr = putsym(array_fncts[i].fname, AFNCT, 1);
      ptr->value.arrfnct = array_fncts[i].fnct;
      ptr->info = array_fncts[i].description;
      ptr->syntax = array_fncts[i].syntax;
    }
  for (i = 0; variables[i].vname != 0; i++)
    {
      ptr = putsym(variables[i].vname, VAR, 1);
      ptr->value.var = variables[i].value;
    }
  for (i = 0; svariables[i].vname != 0; i++)
    {
      ptr = putsym(svariables[i].vname, SVAR, 1);
      ptr->value.svar = svariables[i].value;
    }
  sprintf(comm_string, "%s", comment);
  ptr = putsym("_C_", SVAR, 1);
  ptr->value.svar = comm_string;
  {
    char *version_string = (char *)malloc(8);
    version(version_string);
    ptr = putsym("VERSION", SVAR, 1);
    ptr->value.svar = version_string;
  }
}
示例#8
0
void
putsym(char c)
{
	switch(c) {
	case '\b':
		backsp();
		return;
	case '\n':
		curx = 1;
		cury++;
		if(cury > tly) tly = cury;
		break;
	default:
		if(curx == CO)
			putsym('\n');	/* 1 <= curx <= CO; avoid CO */
		else
			curx++;
	}
	putchar(c);
}
示例#9
0
static void
xmore(const char *s)	/* allowed chars besides space/return */
{
	if(flags.toplin) {
		curs(tlx, tly);
		if(tlx + 8 > CO) putsym('\n'), tly++;
	}

	if(flags.standout)
		standoutbeg();
	putstr("--More--");
	if(flags.standout)
		standoutend();

	xwaitforspace(s);
	if(flags.toplin && tly > 1) {
		home();
		cl_end();
		docorner(1, tly-1);
	}
	flags.toplin = 0;
}
示例#10
0
/* spaceflag: TRUE if space required */
void xmore(boolean spaceflag)
{
    if(flags.topl != 0) {
        curs(tlx, tly);
        
        if((tlx + 8) > COLNO) {
            putsym('\n');
            ++tly;
        }
    }

    putstr("--More--");
    xwaitforspace(spaceflag);

    if((flags.topl != 0) && (tly > 1)) {
        home();
        cl_end();
        docorner(1, tly - 1);
    }

    flags.topl = 0;
}
示例#11
0
int
yylex (void)
{
  int c;

  /* Ignore white space, get first nonwhite character.  */
  while ((c = getchar ()) == ' ' || c == '\t')
    continue;

  if (c == EOF)
    return 0;

  /* Char starts a number => parse the number.         */
  if (c == '.' || isdigit (c))
    {
      ungetc (c, stdin);
      scanf ("%lf", &yylval.NUM);
      return NUM;
    }



  /* Char starts an identifier => read the name.       */
  if (isalpha (c))
    {
      /* Initially make the buffer long enough
         for a 40-character symbol name.  */
      static size_t length = 40;
      static char *symbuf = 0;
      symrec *s;
      int i;

      if (!symbuf)
        symbuf = (char *) malloc (length + 1);

      i = 0;
      do

        {
          /* If buffer is full, make it bigger.        */
          if (i == length)
            {
              length *= 2;
              symbuf = (char *) realloc (symbuf, length + 1);
            }
          /* Add this character to the buffer.         */
          symbuf[i++] = c;
          /* Get another character.                    */
          c = getchar ();
        }

      while (isalnum (c));

      ungetc (c, stdin);
      symbuf[i] = '\0';

      s = getsym (symbuf);
      if (s == 0)
        s = putsym (symbuf, VAR);
      *((symrec**) &yylval) = s;
      return s->type;
    }

  /* Any other character is a token by itself.        */
  return c;
}
示例#12
0
static int oct_parser_lex (){
  int c;
  char *symbuf = 0;
  int length = 0;
  
  /* Ignore whitespace, get first nonwhite character.  */
  while ((c = par_string[par_pos++]) == ' ' || c == '\t');
	
  if (c == '\0')
    return '\n';
	
  /* Char starts a number => parse the number.         */
  if (c == '.' || isdigit (c)){
    par_pos--;
    par_pos += get_real(&par_string[par_pos], &GSL_REAL(yylval.val));
    return NUM;
  }

  /* get the logical operators */
  if (c == '<' && par_string[par_pos] == '='){
    par_pos++;
    return LE;
  }

  if (c == '>' && par_string[par_pos] == '='){
    par_pos++;
    return GE;
  }

  if (c == '=' && par_string[par_pos] == '='){
    par_pos++;
    return EQUAL;
  }

  /*
  if (c == ':' && par_string[par_pos] == '='){
    par_pos++;
    return SET;
  }
  */

  if (c == '&' && par_string[par_pos] == '&'){
    par_pos++;
    return LAND;
  }

  if (c == '|' && par_string[par_pos] == '|'){
    par_pos++;
    return LOR;
  }
     
  /* Char starts an identifier => read the name.       */
  if (isalpha (c) || c == '\'' || c == '\"'){
    symrec *s;
    char startc = c;
    int i;
		
    /* Initially make the buffer long enough
       for a 40-character symbol name.  */
    if (length == 0)
      length = 40, symbuf = (char *)malloc (length + 1);
    
    if(startc == '\'' || startc == '\"')
      c = par_string[par_pos++];
    else
      startc = 0; /* false */
    
    i = 0;
    do{
      /* If buffer is full, make it larger.        */
      if (i == length){
	length *= 2;
	symbuf = (char *)realloc (symbuf, length + 1);
      }
      /* Add this character to the buffer.         */
      symbuf[i++] = c;
      /* Get another character.                    */
      c = par_string[par_pos++];
    }while (c != '\0' && 
	    ((startc && c!=startc) || (!startc && (isalnum(c) || c == '_' ))));
    
    if(!startc) par_pos--;
    symbuf[i] = '\0';
    
    if(!startc){
      s = getsym (symbuf);
      if (s == 0){
	int jj;
	for (jj = 0; reserved_symbols[jj] != 0; jj++){
	  if(strcmp(symbuf, reserved_symbols[jj]) == 0){
	    fprintf(stderr, "Error: trying to redefine reserved symbol '%s'", symbuf);
	    exit(1);
	  }
	}
	
	s = putsym (symbuf, S_CMPLX);
      }
      yylval.tptr = s;

      free(symbuf);
      if(s->type == S_CMPLX)
	return VAR;
      else
	return FNCT;
    }else{
      yylval.str = strdup(symbuf);

      free(symbuf);
      return STR;
    }
  }
	
  /* Any other character is a token by itself.        */
  return c;
}
示例#13
0
文件: yy.c 项目: davidhoover/kent
int yylex (void)
{
int c;

/* Skip white space.  */
while ((c = getchar ()) == ' ' || c == '\t')
     ++yylloc.last_column;

/* Store starting location . */
yylloc.first_line = yylloc.last_line;
yylloc.first_column = yylloc.last_column;

/* Process numbers.  */
if (isdigit (c))
    {
    yylval.val = c - '0';
    ++yylloc.last_column;
    while (isdigit(c = getchar()))
        {
	++yylloc.last_column;
	yylval.val = yylval.val * 10 + c - '0';
	}
    if (c == '.')
        {
	double tens = 1.0;
	while (isdigit(c = getchar()))
	    {
	    ++yylloc.last_column;
	    tens *= 0.1;
	    yylval.val += tens * (c - '0');
	    }
	}
    ungetc (c, stdin);
    return NUM;
    }
if (isalpha(c))
    {
    symrec *s;
    static char *symbuf = 0;
    static int length = 0;
    int i;

    /* Initially make buffer long enough for 50 char name. */
    if (length == 0)
        {
	length = 50;
	symbuf = (char *)malloc(length+1);
	}
    i = 0;
    do
       {
       ++yylloc.last_column;
       if (i == length)
           {
	   length *= 2;
	   symbuf = (char *)realloc(symbuf, length+1);
	   }
	symbuf[i++] = c;
	c = getchar();
	}
    while (isalnum(c));
    ungetc(c, stdin);
    symbuf[i] = 0;

    s = getsym(symbuf);
    if (s == NULL)
        s = putsym(symbuf, VAR);
    yylval.tptr = s;
    return s->type;
    }

/* Return end-of-input.  */
if (c == EOF)
    return 0;

/* Handle single character stuff including location. */
if (c == '\n')
    {
    ++yylloc.last_line;
    yylloc.last_column = 0;
    }
else
    ++yylloc.last_column;
return c;
}
示例#14
0
int main (int argc, char *argv[])
{
  char *version_string = "Algebraic Preprocessor (Aprepro)";
  int c;
  time_t time_val;
  struct tm *time_structure;
  char *asc_time = NULL;
  char *include_file = NULL;
  
#define NO_ARG 0
#define IS_ARG 1
#define OP_ARG 2

  static struct option long_options[] =
    {
      {"debug",       NO_ARG, 0, 'd'},
      {"statistics",  NO_ARG, 0, 's'},
      {"copyright",   NO_ARG, 0, 'C'},
      {"comment",     IS_ARG, 0, 'c'},
      {"version",     NO_ARG, 0, 'v'},
      {"interactive", NO_ARG, 0, 'i'},
      {"include",     IS_ARG, 0, 'I'},
      {"exit_on",     NO_ARG, 0, 'e'},
      {"help",        NO_ARG, 0, 'h'},
      {"nowarning",   NO_ARG, 0, 'W'},
      {"messages",    NO_ARG, 0, 'M'},
      {"quiet",       NO_ARG, 0, 'q'},
      {"immutable",   NO_ARG, 0, 'X'},
      {"one_based_index", NO_ARG, 0, '1'},
      {NULL,          NO_ARG, NULL, 0}
    };

  int  option_index = 0;
  extern int optind;
  extern char *optarg;

  myname = strrchr (argv[0], '/');
  if (myname == NULL)
    myname = argv[0];
  else
    myname++;

  /* Process command line options */
  initialize_options(&ap_options);
  
  ap_options.end_on_exit = False;
  while ((c = getopt_long (argc, argv, "c:dDsSvViI:eEwWmMhHCqX1",
			   long_options, &option_index)) != EOF)
    {
      switch (c)
	{
	case 'c':
	  NEWSTR(optarg, ap_options.comment);
	  break;

	case 'd':
	case 'D':
	  ap_options.debugging = True;
	  ap_options.info_msg = True;
	  ap_options.warning_msg = True;
	  break;

	case 's':
	case 'S':		/* Print hash statistics */
	  ap_options.statistics = True;
	  break;

	case 'C':		/* Print copyright message */
	  ap_options.copyright = True;
	  break;

	case 'v':
	case 'V':
	  fprintf (stderr, "%s: (%s) %s\n", version_string, qainfo[2], qainfo[1]);
	  break;

	case 'i':
	  ap_options.interactive = True;
	  break;

	case 'I':
	  /*
	   * Check whether optarg specifies a file or a directory
	   * If a file, it is an include file,
	   * If a directory, it is an include_path
	   */
	  if (is_directory(optarg)) {
	    NEWSTR(optarg, ap_options.include_path);
	  } else {
	    NEWSTR(optarg, include_file);
	  }
	  break;
	  
	case 'e':
	case 'E':
	  ap_options.end_on_exit = True;
	  break;

	case 'W':
	  ap_options.warning_msg = False;
	  break;

	case 'q':
	  ap_options.quiet = True;
	  break;

	case 'M':
	  ap_options.info_msg = True;
	  break;

	case 'X':
	  ap_options.immutable = True;
	  break;

	case '1':
	  ap_options.one_based_index = True;
	  break;
	  
	case 'h':
	case 'H':
	  usage();
	  exit(EXIT_SUCCESS);
	  break;
	  
	case '?':
	default:
	  /* getopt will print a message for us */
	  usage ();
	  exit(EXIT_FAILURE);
	  break;
	}
    }

  /* Process remaining options.  If '=' in word, then it is of the form
   * var=value.  Set the value.  If '=' not found, process remaining
   * options as input and output files
   */
  while (optind < argc && strchr(argv[optind], '=') && !strchr(argv[optind], '/')) {
    char *var, *val;
    double value;
    symrec *s;

    var = argv[optind++];
    val = strchr (var, '=');
    if (val == NULL) {
      fprintf(stderr, "ERROR: '%s' is not a valid form for assiging a variable; it will not be defined\n", var);
    } else {
      *val++ = '\0';
      if (!check_valid_var(var)) {
	fprintf(stderr, "ERROR: '%s' is not a valid form for a variable; it will not be defined\n", var);
      } else {
	if (strchr(val, '"') != NULL) { /* Should be a string variable */
	  char *pt = strrchr(val, '"');
	  if (pt != NULL) {
	    val++;
	    *pt = '\0';
	    if (var[0] == '_')
	      s = putsym(var, SVAR, 0);
	    else
	      s = putsym(var, IMMSVAR, 0);
	    NEWSTR(val, s->value.svar);
	  }
	  else {
	    fprintf(stderr, "ERROR: Missing trailing \" in definition of variable '%s'; it will not be defined\n", var);
	  }
	}
	else {
	  int err = sscanf (val, "%lf", &value);
	  if (err <= 0) {
	    fprintf(stderr, "ERROR: Could not parse value in assignment of variable '%s'; it will not be defined\n", var);
	  }
	  else {
	    if (var[0] == '_')
	      s = putsym (var, VAR, 0);
	    else
	      s = putsym (var, IMMVAR, 0);
	    s->value.var = value;
	  }
	}
      }
    }
  }

  if (ap_options.copyright == True)
    copyright_output();
  /* Assume stdin, recopy if and when it is changed */
  yyin = stdin;
  yyout = stdout;

  if (argc > optind) {
    add_input_file(argv[optind]);
  }
  else {
    NEWSTR ("stdin", ap_file_list[nfile].name);
    SET_FILE_LIST (nfile, 0, False, 1);
  }
  if (argc > ++optind) {
    yyout = open_file(argv[optind], "w");
  }
  else {  /* Writing to stdout */
    if (ap_options.interactive)
      setbuf (yyout, (char *) NULL);
  }

  state_immutable = ap_options.immutable;

  
  time_val = time ((time_t*)NULL);
  time_structure = localtime (&time_val);
  asc_time = asctime (time_structure);

  /* NOTE: asc_time includes \n at end of string */
  if (!ap_options.quiet) {
    if (state_immutable) {
      fprintf (yyout, "%s Aprepro (%s) [immutable mode] %s", ap_options.comment, qainfo[2], asc_time);
    } else {
      fprintf (yyout, "%s Aprepro (%s) %s", ap_options.comment, qainfo[2], asc_time);
    }
  }

  if (include_file) {
    nfile++;
    add_input_file(include_file);
    /* Include file specified on command line is processed in immutable
     * state. Reverts back to global immutable state at end of file.
     */
    state_immutable = True;
    echo = False;
  }

  srand((unsigned)time_val);
  
  init_table (ap_options.comment);
  yyparse ();
  if (ap_options.debugging > 0)
    dumpsym (VAR, 0);
  if (ap_options.statistics > 0)
    pstats ();
  add_to_log(myname, 0);
  return (EXIT_SUCCESS);
}				/* NOTREACHED */
示例#15
0
void parse_putsym_int(char *s, int i)
{
  symrec *rec = putsym(s, S_CMPLX);
  GSL_SET_COMPLEX(&rec->value.c, (double)i, 0);
}
示例#16
0
void parse_putsym_double(char *s, double d)
{
  symrec *rec =  putsym(s, S_CMPLX);
  GSL_SET_COMPLEX(&rec->value.c, d, 0);
}
示例#17
0
文件: map.c 项目: caomw/grass
static void find_maps(void)
{
    char *gisdbase, *location, *mapset;
    char basepath[4096], subdirpath[4096], path[4096];
    DIR *dir, *subdir;
    struct dirent *ent, *subent;

    gisdbase = getenv("GISDBASE");
    if (!gisdbase)
	gisdbase = ".";

    location = getenv("LOCATION_NAME");
    if (!location)
	location = ".";

    mapset = getenv("MAPSET");
    if (!mapset)
	mapset = ".";

    /*
     * Now, if I'm not in grass, I can simulate the existence of a vector
     * map creating a directory vector with one subdirectory for each `map'
     * having a file `head'
     */

    sprintf(basepath, "%s/%s/%s/vector", gisdbase, location, mapset);
    dir = opendir(basepath);
    if (!dir)
	return;

    while ((ent = readdir(dir)) != NULL) {
	struct stat buf;

	if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
	    continue;

	strcpy(path, basepath);
	strcat(path, "/");
	strcat(path, ent->d_name);

	if (stat(path, &buf))
	    continue;
	if (S_ISDIR(buf.st_mode)) {
	    strcpy(subdirpath, path);
	    subdir = opendir(subdirpath);
	    if (!subdir)
		continue;
	    while ((subent = readdir(subdir)) != NULL) {
		if (!strcmp(subent->d_name, ".") ||
		    !strcmp(subent->d_name, ".."))
		    continue;
		if (!strcmp(subent->d_name, "head")) {
		    MAP *map;
		    SYMBOL *sym;

		    map = (MAP *) listitem(sizeof(MAP));
		    map->name = strdup(ent->d_name);
		    map->refcnt++;
		    sym = putsym(map->name);
		    sym->type = sym->itype = st_map;
		    sym->v.p = map;
		}
	    }
	    closedir(subdir);
	}
    }
    closedir(dir);
}
示例#18
0
void parse_putsym_complex(char *s, gsl_complex c)
{
  symrec *rec =  putsym(s, S_CMPLX);
  rec->value.c = c;	
}
示例#19
0
int parse_input(char *file_in)
{
  FILE *f;
  char *s;
  int c, length = 0;
  
  if(strcmp(file_in, "-") == 0)
    f = stdin;
  else
    f = fopen(file_in, "r");
  
  if(!f)
    return -1; /* error opening file */
  
  /* we now read in the file and parse */
  length = 40;
  s = (char *)malloc(length + 1);
  do{
    c = parse_get_line(f, &s, &length);
    if(*s){
      if(*s == '%'){ /* we have a block */
	*s = ' ';
	str_trim(s);
	if(getsym(s) != NULL){ /* error */
	  fprintf(stderr, "%s \"%s\" %s", "Block", s, "already defined");
	  do{ /* skip block */
	    c = parse_get_line(f, &s, &length);
	  }while(c != EOF && *s != '%');
	}else{ /* parse block */
	  symrec *rec;
	  rec = putsym(s, S_BLOCK);
	  rec->value.block = (sym_block *)malloc(sizeof(sym_block));
	  rec->value.block->n = 0;
	  rec->value.block->lines = NULL;
	  do{
	    c = parse_get_line(f, &s, &length);
	    if(*s && *s != '%'){
	      char *s1, *tok;
	      int l, col;
	      
	      l = rec->value.block->n;
	      rec->value.block->n++;
	      rec->value.block->lines = (sym_block_line *)
		realloc((void *)rec->value.block->lines, sizeof(sym_block_line)*(l+1));
	      rec->value.block->lines[l].n = 0;
	      rec->value.block->lines[l].fields = NULL;
	      
	      /* parse columns */
	      for(s1 = s; (tok = strtok(s1, "|\t")) != NULL; s1 = NULL){
		char *tok2 = strdup(tok);
		str_trim(tok2);
		
		col = rec->value.block->lines[l].n;
		rec->value.block->lines[l].n++;
		rec->value.block->lines[l].fields = (char **)
		  realloc((void *)rec->value.block->lines[l].fields, sizeof(char *)*(col+1));
		rec->value.block->lines[l].fields[col] = tok2;
	      }
	    }
	  }while(c != EOF && *s != '%');
	}
      }else{ /* we can parse it np */
	parse_result c;
	parse_exp(s, &c);
      }
    }
  }while(c != EOF);
  
  free(s);
  if(f != stdin)
    fclose(f);
  
#define OCT_ENV_HEADER "OCT_"

  /*now read options from environment variables (by X) */

  if( getenv("OCT_PARSE_ENV")!=NULL ) {
    
    /* environ is an array of C strings with all the environment
       variables, the format of the string is NAME=VALUE, which
       is directly recognized by parse_exp */
    
    char **env = environ;    
    while(*env) {
      /* Only consider variables that begin with OCT_ */
      if( strncmp(OCT_ENV_HEADER, *env, strlen(OCT_ENV_HEADER)) == 0 ){	
	parse_result c;
	parse_exp( (*env) + strlen(OCT_ENV_HEADER), &c);
      }
      
      env++;
    }
  }
  
  sym_clear_reserved();

  return 0;
}
示例#20
0
int parse_input(const char *file_in, int set_used)
{
  FILE *f;
  char *s;
  int c, length = 0;
  parse_result pc;

  if(strcmp(file_in, "-") == 0)
    f = stdin;
  else
    f = fopen(file_in, "r");
  
  if(!f)
    return -1; /* error opening file */
  
  /* we now read in the file and parse */
  /* note: 40 is just a starter length, it is not a maximum */
  length = 40;
  s = (char *)malloc(length + 1);
  do{
    c = parse_get_line(f, &s, &length);
    if(*s){
      if(strncmp("include ", s, 8) == 0 ){ /* include another file */
	/* wipe out leading 'include' with blanks */
	strncpy(s, "       ", 7);
	str_trim(s);
	if(!disable_write)
	  fprintf(fout, "# including file '%s'\n", s);
	c = parse_input(s, 0);
	if(c != 0) {
	  fprintf(stderr, "Parser error: cannot open included file '%s'.\n", s);
	  exit(1);
	}
      } else if(*s == '%'){ /* we have a block */
	*s = ' ';
	str_trim(s);
	if(getsym(s) != NULL){ /* error */
	  fprintf(stderr, "Parser warning: %s \"%s\" %s.\n", "Block", s, "already defined");
	  do{ /* skip block */
	    c = parse_get_line(f, &s, &length);
	  }while(c != EOF && *s != '%');
	}else{ /* parse block */
	  symrec *rec;
	  rec = putsym(s, S_BLOCK);
	  rec->value.block = (sym_block *)malloc(sizeof(sym_block));
	  rec->value.block->n = 0;
	  rec->value.block->lines = NULL;
	  do{
	    c = parse_get_line(f, &s, &length);
	    if(*s && *s != '%'){
	      char *s1, *tok;
	      int l, col;
	      
	      l = rec->value.block->n;
	      rec->value.block->n++;
	      rec->value.block->lines = (sym_block_line *)
		realloc((void *)rec->value.block->lines, sizeof(sym_block_line)*(l+1));
	      rec->value.block->lines[l].n = 0;
	      rec->value.block->lines[l].fields = NULL;
	      
	      /* parse columns */
	      for(s1 = s; (tok = strtok(s1, "|\t")) != NULL; s1 = NULL){
		char *tok2 = strdup(tok);
		str_trim(tok2);
		
		col = rec->value.block->lines[l].n;
		rec->value.block->lines[l].n++;
		rec->value.block->lines[l].fields = (char **)
		  realloc((void *)rec->value.block->lines[l].fields, sizeof(char *)*(col+1));
		rec->value.block->lines[l].fields[col] = tok2;
	      }
	    }
	  }while(c != EOF && *s != '%');
	}
      }else{ /* we can parse it np */
	parse_exp(s, &pc);
      }
    }
  }while(c != EOF);
  
  free(s);
  if(f != stdin)
    fclose(f);

  if(set_used == 1)
    sym_mark_table_used();

  return 0;
}
示例#21
0
void
putstr(const char *s)
{
	while(*s) putsym(*s++);
}
示例#22
0
ret * ex(nodeType *p) {
    // Early exit on EOTree.
    if (!p) return 0;

    switch(p->type) {
        case nodeBlock:
            // This is support for scoping.
            /* NOTE: if function implementation is needed
            // just store dic_list and run it after saved EBP
            */
            {
                symrec * bk_EBP = EBP; // Backup EBP
                EBP = symTable; // new EBP from ESP

                // IMPROVEMENT: if func: ex(dec_list)
                ex(p->blk);

                symTable = EBP; // Reset ESP
                EBP = bk_EBP; // Reset EBP
                return 0;
            }
        case nodeDic:
            if (isdefsym(p->dic.name, EBP)) {
                fprintf(stderr, "[ERROR] Variable %s was previously declared.\n", p->dic.name);
                exit(1);
            }
            putsym(p->dic.name, p->dic.type);
            return 0;

        case nodeCon:
            {
                ret * r = xmalloc(sizeof(ret));
                memcpy(r, &(p->con), sizeof(ret));
                return r;
            }

        case nodeId:
            {
                symrec * s = getsym(p->id.name);
                if(!s) {
                    fprintf(stderr, "[ERROR] There is not such '%s' variable in the symtable\n", p->id.name);
                    exit(1);
                }
                return ex(con(s->value, s->type));
            }

        case nodeOpr:
            {
                // Used for expr
                mappable f = NULL;
                ret * a = NULL , * b = NULL;
                int flag = 0;

                switch(p->opr.oper) {
                    case WHILE:
                        while(coercion(ex(p->opr.op[0]), BOOLTYPE)->value)
                            ex(p->opr.op[1]);

                        return 0;

                    case FOR:
                        {
                            /*
                            * 0: var
                            * 1: initial value
                            * 2: upper boundary
                            * 3: body
                            */

                            ret * c;

                            symrec * s = getsym(p->opr.op[0]->id.name);

                            // DO first assign via opr in order to ensure
                            // type checking and coercion.
                            ex(opr(EQ, 2, p->opr.op[0], p->opr.op[1]));

                            // iterator < boundary
                            while(coercion(ex(opr(LT, 2, p->opr.op[0], p->opr.op[2])), BOOLTYPE)->value) {
                                // exec
                                ex(p->opr.op[3]);

                                // Speed up (no need for other checks)
                                s->value += 1;
                            }

                            return 0;
                        }

                    case IF:
                        {
                            if(coercion(ex(p->opr.op[0]), BOOLTYPE)->value)
                                ex(p->opr.op[1]); // IF
                            else if (p->opr.nops > 2)
                                ex(p->opr.op[2]); // ELSE (if any)
                            return 0;
                        }

                    case PRINTINT:
                    case PRINTREAL:
                    case PRINTBOOL:

                    case PRINT: // HERE NO COERCION !
                        {
                            int cmd = p->opr.oper;
                            ret * to_print = ex(p->opr.op[0]);

                            switch(to_print->type){
                                case INTTYPE:
                                    if (cmd != PRINT && cmd != PRINTINT) yyerror("Type error.");
                                    printf("%d\n", (int)(to_print->value));
                                    break;
                                case REALTYPE:
                                    if (cmd != PRINT && cmd != PRINTREAL) yyerror("Type error.");
                                    {
                                        char * fstr = (char*)xmalloc(46 + 1); // len(print(FLT_MAX);
                                        sprintf(fstr, "%f", to_print->value);

                                        // substitute comma with dot
                                        char * c = fstr;
                                        for(; *c != '.'; c++);
                                        *c = ',';
                                        printf("%s\n", fstr);

                                        free(fstr);
                                    }
                                        break;

                                case BOOLTYPE:
                                    if (cmd != PRINT && cmd != PRINTBOOL) yyerror("Type error.");
                                    if (to_print->value)
                                        printf("true\n");
                                    else
                                        printf("false\n");
                                    break;
                                default:
                                    yyerror("Unrecognized type.");
                            }
                            return 0;
                        }

                    case SEMICOLON:
                        ex(p->opr.op[0]);
                        return ex(p->opr.op[1]);

                    case EQ:
                        {
                            symrec * s = getsym(p->opr.op[0]->id.name);
                            if(s == NULL){
                                fprintf(stderr, "[ERROR] There is not such '%s' varibale in the symtable\n", p->opr.op[0]->id.name);
                                exit(1);
                            }

                            ret * val = coercion(ex(p->opr.op[1]), s->type);
                            s->value = val->value;
                            return 0;
                        }


                    case UMINUS: f = f != NULL ? f : &neg;
                    case PLUS: f = f != NULL ? f : &sum;
                    case MIN:  f = f != NULL ? f : &mni;
                    case MUL:  f = f != NULL ? f : &mul;
                    case DIV:  f = f != NULL ? f : &dvi;
                        flag = 3;

                    case LT:   f = f != NULL ? f : &lt;
                    case GT:   f = f != NULL ? f : &gt;
                    case GTE:  f = f != NULL ? f : &gte;
                    case LTE:  f = f != NULL ? f : &lte;
                        flag = max(flag, 2);

                    case NE:  f = f != NULL ? f : &neq;
                    case DEQ: f = f != NULL ? f : &deq;
                        flag = max(flag, 1);

                    case AND: f = f != NULL ? f : &and;
                    case OR:  f = f != NULL ? f : &or;
                    case NOT: f = f != NULL ? f : &not;
                        flag = max(flag, 0);
                        {
                            varTypeEnum
                                retType = BOOLTYPE,
                                valType = BOOLTYPE;

                            a = ex(p->opr.op[0]);
                            b = p->opr.nops == 2 ? ex(p->opr.op[1]) : NULL;

                            switch (flag) {
                                case 3:
                                    valType = retType = max(b ? max(a->type, b->type) : a->type, INTTYPE);
                                    break;
                                case 2:
                                    valType = max(b ? max(a->type, b->type) : a->type, INTTYPE);
                                    break;
                                case 1:
                                    valType = b ? max(a->type, b->type) : a->type;
                                    break;
                            }

                            return ex(con((*f)(
                                coercion(a, valType)->value,
                                b ? coercion(b, valType)->value : 0), retType));
                        }
                    default:
                        yyerror("Operator not matched.");
                }
                break;
            }
        default:
            yyerror("Node was not matched.");
    }

    yyerror("WTF! This should be DEAD CODE.");
    return 0;
}
示例#23
0
/*
 * Flexible pager: feed it with a number of lines and it will decide
 * whether these should be fed to the pager above, or displayed in a
 * corner.
 * Call:
 *	cornline(0, title or 0)	: initialize
 *	cornline(1, text)	: add text to the chain of texts
 *	cornline(2, morcs)	: output everything and cleanup
 *	cornline(3, 0)		: cleanup
 */
void
cornline(int mode, char *text)
{
	static struct line {
		struct line *next_line;
		char *line_text;
	} *texthead, *texttail;
	static int maxlen;
	static int linect;
	struct line *tl;

	if(mode == 0) {
		texthead = 0;
		maxlen = 0;
		linect = 0;
		if(text) {
			cornline(1, text);	/* title */
			cornline(1, "");	/* blank line */
		}
		return;
	}

	if(mode == 1) {
	    int len;

	    if(!text) return;	/* superfluous, just to be sure */
	    linect++;
	    len = strlen(text);
	    if(len > maxlen)
		maxlen = len;
	    tl = (struct line *)
		alloc((unsigned)(len + sizeof(struct line) + 1));
	    tl->next_line = 0;
	    tl->line_text = (char *)(tl + 1);
	    (void) strlcpy(tl->line_text, text, len + 1);
	    if(!texthead)
		texthead = tl;
	    else
		texttail->next_line = tl;
	    texttail = tl;
	    return;
	}

	/* --- now we really do it --- */
	if(mode == 2 && linect == 1)			    /* topline only */
		pline(texthead->line_text);
	else
	if(mode == 2) {
	    int curline, lth;

	    if(flags.toplin == 1) more();	/* ab@unido */
	    remember_topl();

	    lth = CO - maxlen - 2;		   /* Use full screen width */
	    if (linect < LI && lth >= 10) {		     /* in a corner */
		home();
		cl_end();
		flags.toplin = 0;
		curline = 1;
		for (tl = texthead; tl; tl = tl->next_line) {
		    curs(lth, curline);
		    if(curline > 1)
			cl_end();
		    putsym(' ');
		    putstr (tl->line_text);
		    curline++;
		}
		curs(lth, curline);
		cl_end();
		cmore(text);
		home();
		cl_end();
		docorner(lth, curline-1);
	    } else {					/* feed to pager */
		set_pager(0);
		for (tl = texthead; tl; tl = tl->next_line) {
		    if (page_line (tl->line_text)) {
			set_pager(2);
			goto cleanup;
		    }
		}
		if(text) {
			cgetret(text);
			set_pager(2);
		} else
			set_pager(1);
	    }
	}

cleanup:
	while ((tl = texthead)) {
		texthead = tl->next_line;
		free((char *) tl);
	}
}
示例#24
0
char *do_exodus_meta(char *filename)
{
  int exoid;
  int ndim, nnodes, nelems, nblks, nnsets, nssets;
  char *title;
  symrec *ptr;

  int *ids = NULL;
  
  /*
   * Open the specified exodusII file, read the metadata and set
   * variables for each item.
   * Examples include "node_count", "element_count", ...
   */
  exoid = open_exodus_file(filename);
  if (exoid < 0) return "";

  /* read database paramters */
  title = (char *)calloc ((MAX_LINE_LENGTH+1),sizeof(char *));
  ex_get_init(exoid,title,&ndim,&nnodes,&nelems,&nblks,&nnsets,&nssets);
  
  ptr = putsym("ex_title", SVAR, 0);
  ptr->value.svar = title;

  ptr = putsym("ex_dimension", VAR, 0);
  ptr->value.var = ndim;

  ptr = putsym("ex_node_count", VAR, 0);
  ptr->value.var = nnodes;

  ptr = putsym("ex_element_count", VAR, 0);
  ptr->value.var = nelems;

  ptr = putsym("ex_block_count", VAR, 0);
  ptr->value.var = nblks;

  ptr = putsym("ex_nset_count", VAR, 0);
  ptr->value.var = nnsets;

  ptr = putsym("ex_sset_count", VAR, 0);
  ptr->value.var = nssets;
  
  { /* Nemesis Information */
    int proc_count;
    int proc_in_file;
    char file_type[MAX_STR_LENGTH+1];

    int global_nodes;
    int global_elements;
    int global_blocks;
    int global_nsets;
    int global_ssets;
    int error;
    
    error = ex_get_init_info(exoid, &proc_count, &proc_in_file, file_type);

    if (error >= 0) {
      ptr = putsym("ex_processor_count", VAR, 0);
      ptr->value.var = proc_count;
      
      ex_get_init_global(exoid, &global_nodes, &global_elements,
			 &global_blocks, &global_nsets, &global_ssets);
      
      ptr = putsym("ex_node_count_global", VAR, 0);
      ptr->value.var = global_nodes;
      
      ptr = putsym("ex_element_count_global", VAR, 0);
      ptr->value.var = global_elements;
    }
  }
    
  /*
   * Read The Element Blocks And Set Variables For Those Also.
   * The Scheme Is:
   * -- 'Ex_Block_Ids' Is A List Of Ids.  Due To Aprepro Limitations,
   *     This List Is A String, Not An Integer List...
   * -- Each Block Is Named 'Ex_Block_X' Where X Is Replaced By The
   *    Blocks Position In The List. For Example, The First Block Will
   *    Be Named 'Ex_Block_1'
   *
   * -- Each Block Will Have The Following Symbols:
   *    -- Ex_Block_X_Id = Id Of This Element Block
   *    -- Ex_Block_X_Name = Composed Name "Block_" + Id
   *    -- Ex_Block_X_Element_Count = Number Of Elements In Block
   *    -- Ex_Block_X_Nodes_Per_Element = Number Of Nodes Per Element
   *    -- Ex_Block_X_Topology = Type Of Elements In Block
   *      (Lowercased)
   *    -- Ex_Block_X_Attribute_Count = Number Of Attributes.
   */

  ids = malloc(nblks * sizeof(int));
  ex_get_elem_blk_ids (exoid, ids);

  {
    int i;
    char *buffer = NULL;
    char cid[33];     /* arbitrary size, large enough for INT_MAX */
    int size = 2048;
    char *tmp = NULL;
    
    buffer = calloc(size, sizeof(char));
    if (buffer != NULL) {
      for (i=0; i < nblks; i++) {
	sprintf(cid, "%d ", ids[i]);
	if (strlen(buffer) + strlen(cid) +1 > size) {
	  if (realloc(buffer, size *=2) == NULL) {
	    free(buffer);
	    yyerror("Error allocating memory.");
	  }
	  memset(&buffer[size/2], 0, size/2);
	}
	strcat(buffer, cid);
      }
      NEWSTR(buffer, tmp);
      ptr = putsym("ex_block_ids", SVAR, 0);
      ptr->value.svar = tmp;
      free(buffer);
    }
  }
    
  {
    int i;
    char var[128];
    char type[MAX_STR_LENGTH+1];
    char *tmp = NULL;
    int nel;
    int nnel;
    int natr;
    
    for (i=0; i < nblks; i++) {
      ex_get_elem_block(exoid, ids[i], type, &nel, &nnel, &natr);

      sprintf(var, "ex_block_seq_%d_id", i+1);
      ptr = putsym(var, VAR, 0);
      ptr->value.var = ids[i];

      sprintf(var, "ex_block_%d_name", ids[i]);
      ptr = putsym(var, SVAR, 0);
      sprintf(var, "block_%d", ids[i]);
      NEWSTR(var, tmp);
      ptr->value.svar = tmp;

      sprintf(var, "ex_block_%d_element_count", ids[i]);
      ptr = putsym(var, VAR, 0);
      ptr->value.var = nel;

      sprintf(var, "ex_block_%d_nodes_per_element", ids[i]);
      ptr = putsym(var, VAR, 0);
      ptr->value.var = nnel;

      sprintf(var, "ex_block_%d_topology", ids[i]);
      ptr = putsym(var, SVAR, 0);
      NEWSTR(type, tmp);

      /* lowercase the string */
      LowerCaseTrim(tmp);
      ptr->value.svar = tmp;

      sprintf(var, "ex_block_%d_attribute_count", ids[i]);
      ptr = putsym(var, VAR, 0);
      ptr->value.var = natr;
    }
  }
  if (ids != NULL) free(ids);

  {
    /* Get timestep count */
    int ts_count = ex_inquire_int(exoid, EX_INQ_TIME);
    ptr = putsym("ex_timestep_count", VAR, 0);
    ptr->value.var = ts_count;
    
    if (ts_count > 0) {
      int i;
      symrec *format = getsym("_FORMAT");
      char *buffer = NULL;
      char cid[33];     /* arbitrary size, large enough for double... */
      int size = 2048;
      char *tmp = NULL;
      double *timesteps = malloc(ts_count * sizeof(double));

      ex_get_all_times(exoid, timesteps);

      buffer = calloc(size, sizeof(char));
      if (buffer != NULL) {

	for (i=0; i < ts_count; i++) {
	  sprintf(cid, format->value.svar, timesteps[i]);
	  if (strlen(buffer) + strlen(cid) +2 > size) {
	    if (realloc(buffer, size *=2) == NULL) {
	      free(buffer);
	      yyerror("Error allocating memory.");
	    }
	    memset(&buffer[size/2], 0, size/2);
	  }
	  strcat(buffer, cid);
	  strcat(buffer, " ");
	}
	NEWSTR(buffer, tmp);
	ptr = putsym("ex_timestep_times", SVAR, 0);
	ptr->value.svar = tmp;

	free(buffer);
      }
    }
  }
  
  ex_close(exoid);
  return "";
}