示例#1
0
/**
 * Cons: Concatenate two s-expressions together, without references to the
 * originals.
 */
sexp_t *cons_sexp(sexp_t *r, sexp_t *l) {
  sexp_t *cr, *cl, *t;

  cr = copy_sexp(r);
  if (cr->ty == SEXP_VALUE) {
    fprintf(stderr,"Cannot cons non-lists.\n");
    destroy_sexp(cr);
    return NULL;
  } else {
    t = cr->list;
    while (t != NULL && t->next != NULL) t = t->next;
  }

  cl = copy_sexp(l);

  if (cl->ty == SEXP_LIST) {
    if (t != NULL && cl != NULL) {
      t->next = cl->list;
      /* free(cl); */ /* memory leak fix: SMJ, 4/24/2002 */
      sexp_t_deallocate(cl);
    }
  } else {
    fprintf(stderr,"Cannot cons non-lists.\n");
    destroy_sexp(cr);
    destroy_sexp(cl);
    return NULL;
  }

  return cr;
}
示例#2
0
/****
 * main
 ****/
int main(int argc, char **argv) {
  char buf[256]; /* string to sprintf to */
  float vals[3]; /* place to put data */
  sexp_t *sx;

  /*** method #1: create expression as string on one side, extract data
       on the other. ***/

  printf("===>> PART 1 <<===\n");
  sprintf(buf,"(thetag (1.0 2.0 3.0))");
  sx = parse_sexp(buf,strlen(buf));

  extract(sx,vals,3);

  printf("Extracted V1=%f V2=%f V3=%f\n",vals[0],vals[1],vals[2]);

  destroy_sexp(sx);

  /*** method #2: packing function creates expression, same extract
                  function extracts data.  print in between to show expression.
   ***/
  printf("\n===>> PART 2 <<===\n");
  sx = pack("part2tag",4.0,5.0,6.0);
  print_sexp(buf,256,sx);
  printf("SX=%s\n",buf);

  extract(sx,vals,3);
  printf("Extracted V1=%f V2=%f V3=%f\n",vals[0],vals[1],vals[2]);

  destroy_sexp(sx);

  return 0;
}
示例#3
0
bool parseFile(Game& game, const char* filename)
{
  //bool value;
  FILE* in = fopen(filename, "r");
  //int size;
  if(!in)
    return false;

  parseFile(in);

  sexp_t* st = NULL;

  while((st = parse()))
  {
    if( !parseSexp(game, st) )
    {
      while(parse()); //empty the file, keep Lex happy.
      fclose(in);
      return false;
    }
    destroy_sexp(st);
  }

  fclose(in);

  return true;
}
示例#4
0
文件: ml_eval.c 项目: lafka/my-lang
void ml_purge_env(dict_t *env) {
	dict_t *_env = env;
	dict_t *next;

	while (_env != NULL) {
		next  = _env->next;
		destroy_sexp(_env->valexp);
		free(_env);
		_env = next;
	}
}
示例#5
0
bool parseGameFromString(Game& game, const char* string)
{

  parseString( string );

  sexp_t* st = NULL;

  while((st = parse()))
  {
    if( !parseSexp(game, st) )
    {
      while(parse()); //empty the file, keep Lex happy.
      return false;
    }
    destroy_sexp(st);
  }

  return true;
}
示例#6
0
文件: ml_eval.c 项目: lafka/my-lang
dict_t *insert(char *ref, sexp_t *val, dict_t *env) {
	dict_t *_env = env;

	while (1) {
		if (strcmp(_env->varname, ref) == 0) {
			destroy_sexp(_env->valexp);
			_env->valexp = val;
			return env;
		}

		if (_env->next == NULL)
			break;

		_env = _env->next;
	}

	_env->next = (dict_t *) malloc(sizeof(dict_t));
	_env->next->valexp = val;
	strcpy(_env->next->varname, ref);
	_env->next->next = NULL;

	return env;
}
示例#7
0
int main(int argc, char **argv) {
  sexp_t *sx_in, *sx_out;
  int fd;
  char *b;
  size_t l = 0;
  CSTRING *s = NULL;
  pcont_t *pc;

  /* read data */
  fd = open("testdata",O_RDONLY);
  if (fd <= 0) {
    printf("Error opening test data file ``testdata''\n");
    exit(1);
  }
  b = readfile(fd,&l,1024,256);
  close(fd);
  
  /* report */
  printf("Read %lu bytes of data.\n",(unsigned long)l);

  sx_in = (sexp_t *)malloc(sizeof(sexp_t));
  assert(sx_in != NULL);
  sx_in->ty = SEXP_VALUE;
  sx_in->next = sx_in->list = NULL;
  sx_in->aty = SEXP_BINARY;
  sx_in->bindata = b;
  sx_in->binlength = l;
  sx_in = new_sexp_list(sx_in);

  printf("Created expression.\n");

  print_sexp_cstr(&s,sx_in,l+1024);
  destroy_sexp(sx_in);

  b = NULL;
  sx_in = NULL;

  printf("Destroyed AST and buffer.\n");

  pc = init_continuation(NULL);
  pc->mode = PARSER_INLINE_BINARY;
  pc = cparse_sexp(s->base,s->len,pc);
  sx_out = pc->last_sexp;

  printf("Parsed unparsed version back to AST.\n");

  assert(sx_out != NULL);

  b = sx_out->list->bindata;
  l = sx_out->list->binlength;

  fd = open("testdata_out",O_RDWR|O_CREAT);
  if (fd <= 0) {
    printf("Error opening ``testdata_out'': Create empty file to write to.\n");
    exit(1);
  }
  write(fd,b,l);
  close(fd);

  printf("Extracted and wrote bindata from AST.\n");

  exit(1);
}
示例#8
0
int main(int argc, char **argv) {
  int fd;
  sexp_t *sx;
  sexp_iowrap_t *iow;
  int diff;
  unsigned int i;
  int ch;
  char fname[BUFSIZ];
  CSTRING *s1,*s2;
  int passes, failures;
  char outbuf1[8192], outbuf2[8192];
  
  s1 = s2 = NULL;
  passes = failures = 0;

  strcpy(fname,TESTFILE);

  while ((ch = getopt(argc,argv,"f:")) != -1) {
    switch ((char)ch) {
    case 'f':
      strcpy(fname,optarg);
      break;
    default:
      break;
    }
  }

  fd = open(fname,O_RDONLY);

  iow = init_iowrap(fd);

  printf("TESTING CSTRING BASED UNPARSE:\n");
  
  sx = read_one_sexp(iow);

  while (sx != NULL) {

    print_sexp_cstr(&s1,sx,8);
    destroy_sexp(sx);
    
    sx = parse_sexp(s1->base,s1->curlen);
    if (sx == NULL) {
      fprintf(stderr,"ERROR: parser error state of %d\n",
	      sexp_errno);
      exit(1);
    }
    print_sexp_cstr(&s2,sx,8);
    destroy_sexp(sx);
    
    sexp_cleanup();

    diff = 0;
    for (i=0;i<s1->curlen;i++) {
      diff += abs((s1->base[i] - s2->base[i]));
      if (s1->base[i] == '\0')
        break;
    }
        
    /**
     * diff is the lexical difference between the first unparsing
     * of the original buffer and the unparsed version of the parsed
     * version of the first unparsed string.  In other words, does:
     * 
     *   orig->parse->unparse == orig->parse->unparse->parse->unparse
     *
     * This catches issues with print and parse to make sure the meaning
     * of the original is kept (or at least, "bugs" in the parser have
     * matching "bugs" in the printer.)
     */
    if (diff != 0) {
      printf("FIXED POINT MISSED (diff=%d): \nS1: %s\nS2: %s\n",diff,
             s1->base,s2->base);
      failures++;
    } else {
      passes++;
    }

    /** clean up strings **/
    sdestroy(s1);
    sdestroy(s2);
    s1 = s2 = NULL;

    sx = read_one_sexp(iow);
  }
  
  destroy_iowrap(iow);
  close(fd);
  
  printf("TOTAL TESTS: %d  PASS=%d FAIL=%d\n\n",
          passes+failures,passes,failures);

  passes = failures = 0;

  /***
   *** now do normal fixed length buffer unparse testing
   ***/
  fd = open(fname,O_RDONLY);

  iow = init_iowrap(fd);

  printf("TESTING FIXED SIZE BUFFER BASED UNPARSE:\n");
  
  sx = read_one_sexp(iow);

  while (sx != NULL) {
    print_sexp(outbuf1,8192,sx);
    destroy_sexp(sx);
    
    sx = parse_sexp(outbuf1,8192);
    if (sx == NULL) {
      fprintf(stderr,"ERROR: parser error state of %d\n",
	      sexp_errno);
      exit(1);
    }
    print_sexp(outbuf2,8192,sx);
    destroy_sexp(sx);
    
    sexp_cleanup();

    diff = 0;
    for (i=0;i<8192;i++) {
      diff += abs((outbuf1[i] - outbuf2[i]));
      if (outbuf1[i] == '\0' || outbuf2[i] == '\0')
        break;
    }
        
    /**
     * diff is the lexical difference between the first unparsing
     * of the original buffer and the unparsed version of the parsed
     * version of the first unparsed string.  In other words, does:
     * 
     *   orig->parse->unparse == orig->parse->unparse->parse->unparse
     *
     * This catches issues with print and parse to make sure the meaning
     * of the original is kept (or at least, "bugs" in the parser have
     * matching "bugs" in the printer.)
     */
    if (diff != 0) {
      printf("FIXED POINT MISSED (diff=%d): \nS1: %s\nS2: %s\n",diff,
             outbuf1,outbuf2);
      failures++;
    } else {
      passes++;
    }

    sx = read_one_sexp(iow);
  }
  
  destroy_iowrap(iow);
  close(fd);

  printf("TOTAL TESTS: %d  PASS=%d FAIL=%d\n",
          passes+failures,passes,failures);

  exit(0);
}