コード例 #1
0
ファイル: btorbtor.c プロジェクト: hellok/kint
static BtorNode *
parse_binary (BtorBTORParser * parser, int len, Binary f)
{
  BtorNode *l, *r, *res;

  assert (len);

  if (parse_space (parser))
    return 0;

  if (!(l = parse_exp (parser, len, 0)))
    return 0;

  if (parse_space (parser))
    {
    RELEASE_L_AND_RETURN_ERROR:
      btor_release_exp (parser->btor, l);
      return 0;
    }

  if (!(r = parse_exp (parser, len, 0)))
    goto RELEASE_L_AND_RETURN_ERROR;

  res = f (parser->btor, l, r);
  btor_release_exp (parser->btor, r);
  btor_release_exp (parser->btor, l);
  assert (btor_get_exp_len (parser->btor, res) == len);

  return res;
}
コード例 #2
0
ファイル: main.c プロジェクト: gabfou/RT
int			main(int argc, char **argv)
{
	int		line_number;
	int		fd;
	char	*line;
	t_list	*tokens;

	if (argc < 2)
		fatal_error(RAYTRACER, ERR_TOO_FEW_ARGS, 0);
	if (argc > 2)
		fatal_error(RAYTRACER, ERR_TOO_MANY_ARGS, 0);
	line_number = 1;
	fd = access_file(argv[1]);
	while (get_next_line(fd, &line))
	{
		ft_lstappend(&tokens, tokenize(line, line_number));
		line_number++;
		free(line);
	}
	if (parse_exp(&tokens) && tokens == NULL)
		ft_putendl("PARSE WIN");
	else
	{
		WRITE(2, "Parse fail near : '");
		ft_putstr_fd(get_token(&tokens)->lexeme, 2);
		WRITE(2, "' at line ");
		ft_putnbr_fd(get_token(&tokens)->line, 2);
		ft_putchar('\n');
	}
	return (0);
}
コード例 #3
0
ファイル: btorbtor.c プロジェクト: hellok/kint
static BtorNode *
parse_read (BtorBTORParser * parser, int len)
{
  BtorNode *array, *idx, *res;
  int idxlen;

  if (parse_space (parser))
    return 0;

  if (!(array = parse_array_exp (parser, len)))
    return 0;

  if (parse_space (parser))
    {
RELEASE_ARRAY_AND_RETURN_ERROR:
      btor_release_exp (parser->btor, array);
      return 0;
    }

  idxlen = btor_get_index_exp_len (parser->btor, array);
  if (!(idx = parse_exp (parser, idxlen, 0)))
    goto RELEASE_ARRAY_AND_RETURN_ERROR;

  res = btor_read_exp (parser->btor, array, idx);
  btor_release_exp (parser->btor, idx);
  btor_release_exp (parser->btor, array);

  return res;
}
コード例 #4
0
ファイル: btorbtor.c プロジェクト: hellok/kint
static BtorNode *
parse_redunary (BtorBTORParser * parser, int len, Unary f)
{
  BtorNode *tmp, *res;

  (void) len;
  assert (len == 1);

  if (parse_space (parser))
    return 0;

  if (!(tmp = parse_exp (parser, 0, 0)))
    return 0;

  if (btor_get_exp_len (parser->btor, tmp) == 1)
    {
      (void) btor_perr_btor (parser,
			  "argument of reduction operation of width 1");
      btor_release_exp (parser->btor, tmp);
      return 0;
    }

  res = f (parser->btor, tmp);
  btor_release_exp (parser->btor, tmp);
  assert (btor_get_exp_len (parser->btor, res) == 1);

  return res;
}
コード例 #5
0
ファイル: parse.c プロジェクト: gimunu/octopus-irECS
/* If *_PARSE_ENV is set, then environment variables beginning with prefix are read and set,
overriding input file if defined there too. */
void parse_environment(const char* prefix)
{
  char* flag;
  parse_result pc;

  flag = (char *)malloc(strlen(prefix) + 11);
  strcpy(flag, prefix);
  strcat(flag, "PARSE_ENV");
  
  if( getenv(flag)!=NULL ) {
    if(!disable_write)
      fprintf(fout, "# %s is set, parsing environment variables\n", flag);
    
    /* 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 the prefix, except the PARSE_ENV flag */
      if( strncmp(flag, *env, strlen(flag)) != 0 && strncmp(prefix, *env, strlen(prefix)) == 0 ){	
	if(!disable_write)
	  fprintf(fout, "# parsed from environment: %s\n", (*env) + strlen(prefix));
	parse_exp( (*env) + strlen(prefix), &pc);
      }
      
      env++;
    }
  }

  free(flag);
}
コード例 #6
0
ファイル: btorbtor.c プロジェクト: hellok/kint
static BtorNode *
parse_write (BtorBTORParser * parser, int len)
{
  BtorNode *array, *idx, *val, *res;
  int idxlen, vallen;

  if (parse_space (parser))
    return 0;

  if (parse_positive_int (parser, &idxlen))
    return 0;

  if (parse_space (parser))
    return 0;

  if (!(array = parse_array_exp (parser, len)))
    return 0;

  if (parse_space (parser))
    {
RELEASE_ARRAY_AND_RETURN_ERROR:
      btor_release_exp (parser->btor, array);
      return 0;
    }

  if (!(idx = parse_exp (parser, idxlen, 0)))
    goto RELEASE_ARRAY_AND_RETURN_ERROR;

  if (parse_space (parser))
    {
RELEASE_ARRAY_AND_IDX_AND_RETURN_ERROR:
      btor_release_exp (parser->btor, idx);
      goto RELEASE_ARRAY_AND_RETURN_ERROR;
    }

  vallen = btor_get_exp_len (parser->btor, array);
  if (!(val = parse_exp (parser, vallen, 0)))
    goto RELEASE_ARRAY_AND_IDX_AND_RETURN_ERROR;

  res = btor_write_exp (parser->btor, array, idx, val);

  btor_release_exp (parser->btor, array);
  btor_release_exp (parser->btor, idx);
  btor_release_exp (parser->btor, val);

  return res;
}
コード例 #7
0
ファイル: btorbtor.c プロジェクト: hellok/kint
static BtorNode *
parse_slice (BtorBTORParser * parser, int len)
{
  int arglen, upper, lower, delta;
  BtorNode *res, *arg;

  if (parse_space (parser))
    return 0;

  if (!(arg = parse_exp (parser, 0, 0)))
    return 0;

  if (parse_space (parser))
    {
    RELEASE_ARG_AND_RETURN_ERROR:
      btor_release_exp (parser->btor, arg);
      return 0;
    }

  arglen = btor_get_exp_len (parser->btor, arg);

  if (parse_non_negative_int (parser, &upper))
    goto RELEASE_ARG_AND_RETURN_ERROR;

  if (upper >= arglen)
    {
      (void) btor_perr_btor (parser, "upper index '%d' >= argument width '%d",
			  upper, arglen);
      goto RELEASE_ARG_AND_RETURN_ERROR;
    }

  if (parse_space (parser))
    goto RELEASE_ARG_AND_RETURN_ERROR;

  if (parse_non_negative_int (parser, &lower))
    goto RELEASE_ARG_AND_RETURN_ERROR;

  if (upper < lower)
    {
      (void) btor_perr_btor (parser,
			  "upper index '%d' smaller than lower index '%d'",
			  upper, lower);
      goto RELEASE_ARG_AND_RETURN_ERROR;
    }

  delta = upper - lower + 1;
  if (delta != len)
    {
      (void) btor_perr_btor (parser,
			  "slice width '%d' not equal to expected width '%d'",
			  delta, len);
      goto RELEASE_ARG_AND_RETURN_ERROR;
    }

  res = btor_slice_exp (parser->btor, arg, upper, lower);
  btor_release_exp (parser->btor, arg);

  return res;
}
コード例 #8
0
ファイル: btorbtor.c プロジェクト: hellok/kint
static BtorNode *
parse_acond (BtorBTORParser * parser, int len)
{
  BtorNode *c, *t, *e, *res;
  int idxlen;

  if (parse_space (parser))
    return 0;

  if (parse_positive_int (parser, &idxlen))
    return 0;

  if (parse_space (parser))
    return 0;

  if (!(c = parse_exp (parser, 1, 0)))
    return 0;

  if (parse_space (parser))
    {
    RELEASE_C_AND_RETURN_ERROR:
      btor_release_exp (parser->btor, c);
      return 0;
    }

  if (!(t = parse_array_exp (parser, len)))
    goto RELEASE_C_AND_RETURN_ERROR;

  if (idxlen != btor_get_index_exp_len (parser->btor, t))
    {
      (void) btor_perr_btor (parser,
			     "mismatch of index bit width of 'then' array");
    RELEASE_C_AND_T_AND_RETURN_ERROR:
      btor_release_exp (parser->btor, t);
      goto RELEASE_C_AND_RETURN_ERROR;
    }

  if (parse_space (parser))
    goto RELEASE_C_AND_T_AND_RETURN_ERROR;

  if (!(e = parse_array_exp (parser, len)))
    goto RELEASE_C_AND_T_AND_RETURN_ERROR;

  if (idxlen != btor_get_index_exp_len (parser->btor, e))
    {
      (void) btor_perr_btor (parser,
			     "mismatch of index bit width of 'else' array");
      btor_release_exp (parser->btor, e);
      goto RELEASE_C_AND_T_AND_RETURN_ERROR;
    }

  res = btor_cond_exp (parser->btor, c, t, e);
  btor_release_exp (parser->btor, e);
  btor_release_exp (parser->btor, t);
  btor_release_exp (parser->btor, c);

  return res;
}
コード例 #9
0
ファイル: btorbtor.c プロジェクト: hellok/kint
static BtorNode *
parse_logical (BtorBTORParser * parser, int len, Binary f)
{
  BtorNode * l, * r, * res;

  if (len != 1)
    {
      (void) btor_perr_btor (parser, "logical operator bit width '%d'", len);
      return 0;
    }

  if (parse_space (parser))
    return 0;

  if (!(l = parse_exp (parser, 0, 0)))
    return 0;

  if (btor_get_exp_len (parser->btor, l) != 1)
    {
BIT_WIDTH_ERROR_RELEASE_L_AND_RETURN:
      (void) btor_perr_btor (parser, "expected argument of bit width '1'");
RELEASE_L_AND_RETURN_ERROR:
      btor_release_exp (parser->btor, l);
      return 0;
    }

  if (parse_space (parser))
    goto RELEASE_L_AND_RETURN_ERROR;

  if (!(r = parse_exp (parser, 0, 0)))
    goto RELEASE_L_AND_RETURN_ERROR;

  if (btor_get_exp_len (parser->btor, r) != 1)
    {
      btor_release_exp (parser->btor, r);
      goto BIT_WIDTH_ERROR_RELEASE_L_AND_RETURN;
    }

  res = f (parser->btor, l, r);
  btor_release_exp (parser->btor, r);
  btor_release_exp (parser->btor, l);
  assert (btor_get_exp_len (parser->btor, res) == 1);

  return res;
}
コード例 #10
0
ファイル: btorbtor.c プロジェクト: hellok/kint
static BtorNode *
parse_concat (BtorBTORParser * parser, int len)
{
  BtorNode *l, *r, *res;
  int llen, rlen;

  if (parse_space (parser))
    return 0;

  if (!(l = parse_exp (parser, 0, 0)))
    return 0;

  if (parse_space (parser))
    {
    RELEASE_L_AND_RETURN_ERROR:
      btor_release_exp (parser->btor, l);
      return 0;
    }

  if (!(r = parse_exp (parser, 0, 0)))
    goto RELEASE_L_AND_RETURN_ERROR;

  llen = btor_get_exp_len (parser->btor, l);
  rlen = btor_get_exp_len (parser->btor, r);

  if (llen + rlen != len)
    {
      (void)
	btor_perr_btor (parser,
		     "operands widths %d and %d do not add up to %d",
		     llen, rlen, len);

      btor_release_exp (parser->btor, r);
      btor_release_exp (parser->btor, l);
      return 0;
    }

  res = btor_concat_exp (parser->btor, l, r);
  btor_release_exp (parser->btor, r);
  btor_release_exp (parser->btor, l);
  assert (btor_get_exp_len (parser->btor, res) == len);

  return res;
}
コード例 #11
0
ファイル: parse.cpp プロジェクト: tov/ipd
 list<shared_ptr<exp>>
 Parser::parse_until_close()
 {
   list<shared_ptr<exp>> exps;
   while (*cur != ")" && cur != end) {
     exps.push_back(parse_exp());
   }
   if (cur == end)
     throw "unmatched parens";
   cur++;
   return exps;
 }
コード例 #12
0
ファイル: parse.c プロジェクト: gimunu/octopus-irECS
static int parse_block_work(const sym_block *blk, int l, int col, parse_result *r)
{
  assert(blk!=NULL);
  assert(l>=0 && l<blk->n);

  if(col < 0 || col >= blk->lines[l].n){
    fprintf(stderr, "Parser error: column %i out of range [0,%i] when parsing block '%s'.\n", col, blk->lines[l].n-1, blk->name);
    exit(1);
  }
  
  return parse_exp(blk->lines[l].fields[col], r);
}
コード例 #13
0
ファイル: parse.c プロジェクト: gimunu/octopus_SCE
static int parse_block_work(sym_block *blk, int l, int col, parse_result *r)
{
  assert(blk!=NULL);
  assert(l>=0 && l<blk->n);
  assert(col>=0);

  if(col >= blk->lines[l].n){
    fprintf(stderr, "%s\n", "Input error: not enough columns found when parsing block.");
    exit(1);
  }
  
  return parse_exp(blk->lines[l].fields[col], r);
}
コード例 #14
0
ファイル: btorbtor.c プロジェクト: hellok/kint
static BtorNode *
parse_cond (BtorBTORParser * parser, int len)
{
  BtorNode *c, *t, *e, *res;

  if (parse_space (parser))
    return 0;

  if (!(c = parse_exp (parser, 1, 0)))
    return 0;

  if (parse_space (parser))
    {
    RELEASE_C_AND_RETURN_ERROR:
      btor_release_exp (parser->btor, c);
      return 0;
    }

  if (!(t = parse_exp (parser, len, 0)))
    goto RELEASE_C_AND_RETURN_ERROR;

  if (parse_space (parser))
    {
    RELEASE_C_AND_T_AND_RETURN_ERROR:
      btor_release_exp (parser->btor, t);
      goto RELEASE_C_AND_RETURN_ERROR;
    }

  if (!(e = parse_exp (parser, len, 0)))
    goto RELEASE_C_AND_T_AND_RETURN_ERROR;

  res = btor_cond_exp (parser->btor, c, t, e);
  btor_release_exp (parser->btor, e);
  btor_release_exp (parser->btor, t);
  btor_release_exp (parser->btor, c);

  return res;
}
コード例 #15
0
ファイル: btorbtor.c プロジェクト: hellok/kint
static BtorNode *
parse_shift (BtorBTORParser * parser, int len, Shift f)
{
  BtorNode *l, *r, *res;
  int rlen;

  for (rlen = 1; rlen <= 30 && len != (1 << rlen); rlen++)
    ;

  if (len != (1 << rlen))
    {
      (void) btor_perr_btor (parser, "length %d is not a power of two", len);
      return 0;
    }

  if (parse_space (parser))
    return 0;

  if (!(l = parse_exp (parser, len, 0)))
    return 0;

  if (parse_space (parser))
    {
    RELEASE_L_AND_RETURN_ERROR:
      btor_release_exp (parser->btor, l);
      return 0;
    }

  if (!(r = parse_exp (parser, rlen, 0)))
    goto RELEASE_L_AND_RETURN_ERROR;

  res = f (parser->btor, l, r);
  btor_release_exp (parser->btor, r);
  btor_release_exp (parser->btor, l);
  assert (btor_get_exp_len (parser->btor, res) == len);

  return res;
}
コード例 #16
0
ファイル: btorbtor.c プロジェクト: hellok/kint
static BtorNode *
parse_root (BtorBTORParser * parser, int len)
{
  BtorNode *res;

  if (parse_space (parser))
    return 0;

  if (!(res = parse_exp (parser, len, 0)))
    return 0;

  BTOR_PUSH_STACK (parser->mem, parser->outputs, res);

  return res;
}
コード例 #17
0
ファイル: btorbtor.c プロジェクト: hellok/kint
static BtorNode *
parse_next (BtorBTORParser * parser, int len)
{
  int idx;
  BtorNode * current, * next;
  Info info;

  if (parse_space (parser))
    return 0;

  if (parse_positive_int (parser, &idx))
    return 0;

  if (idx >= BTOR_COUNT_STACK (parser->exps) ||
      !(current = parser->exps.start[idx]))
    {
      (void) btor_perr_btor (parser, "invalid next index %d", idx);
      return 0;
    }

  info = parser->info.start[idx];

  if (!info.var)
    {
      (void) btor_perr_btor (parser, "next index %d is not a variable", idx);
      return 0;
    }

  if (info.next)
    {
      (void) btor_perr_btor (parser, "next index %d already used", idx);
      return 0;
    }

  if (parse_space (parser))
    return 0;

  assert (!btor_is_array_exp (parser->btor, current));
  if (!(next = parse_exp (parser, len, 0)))
    return 0;

  BTOR_PUSH_STACK (parser->mem, parser->regs, current);
  BTOR_PUSH_STACK (parser->mem, parser->nexts, next);
  parser->info.start[idx].next = 1;

  return next;
}
コード例 #18
0
ファイル: btorbtor.c プロジェクト: hellok/kint
static BtorNode *
parse_array_exp (BtorBTORParser * parser, int len)
{
  BtorNode * res;

  res = parse_exp (parser, len, 1);
  if (!res)
    return 0;

  if (btor_is_array_exp (parser->btor, res))
    return res;

  (void) btor_perr_btor (parser, "expected array expression");
  btor_release_exp (parser->btor, res);

  return 0;
}
コード例 #19
0
ファイル: btorbtor.c プロジェクト: hellok/kint
static BtorNode *
parse_unary (BtorBTORParser * parser, int len, Unary f)
{
  BtorNode *tmp, *res;

  assert (len);
  if (parse_space (parser))
    return 0;

  if (!(tmp = parse_exp (parser, len, 0)))
    return 0;

  res = f (parser->btor, tmp);
  btor_release_exp (parser->btor, tmp);
  assert (btor_get_exp_len (parser->btor, res) == len);

  return res;
}
コード例 #20
0
ファイル: parser.c プロジェクト: MgaMPKAy/m_Practice
struct expression *parse_prop()
{
    struct expression *exp = parse_exp();

    if (is_empty_expression(exp) || current_token == NULL)
        return NULL;

    switch (current_token->type) {
    case OpOr:
        advanceToken();
        struct expression *prop =
            new_op_expression(OrExp, exp, parse_prop());
        if (is_right_child_empty(prop)) {
            destroy_expression(exp);
            return NULL;
        }
        return prop;
    default:
        return exp;
    }
}
コード例 #21
0
ファイル: btorbtor.c プロジェクト: hellok/kint
static BtorNode *
parse_ext (BtorBTORParser * parser, int len, Extend f)
{
  BtorNode *res, *arg;
  int alen, elen;

  if (parse_space (parser))
    return 0;

  if (!(arg = parse_exp (parser, 0, 0)))
    return 0;

  alen = btor_get_exp_len (parser->btor, arg);

  if (parse_space (parser))
    {
    RELEASE_ARG_AND_RETURN_ERROR:
      btor_release_exp (parser->btor, arg);
      return 0;
    }

  if (parse_non_negative_int (parser, &elen))
    goto RELEASE_ARG_AND_RETURN_ERROR;

  if (alen + elen != len)
    {
      (void)
	btor_perr_btor (parser,
		     "argument length of %d plus %d does not match %d",
		     alen, elen, len);
      goto RELEASE_ARG_AND_RETURN_ERROR;
    }

  res = f (parser->btor, arg, elen);
  assert (btor_get_exp_len (parser->btor, res) == len);
  btor_release_exp (parser->btor, arg);

  return res;
}
コード例 #22
0
ファイル: parser.c プロジェクト: MgaMPKAy/m_Practice
struct expression *parse_exp()
{
    struct expression *term = parse_term();
    if (is_empty_expression(term))
        return NULL;

    if (current_token == NULL) return NULL;

    switch (current_token->type) {
    case OpAnd:
        advanceToken();
        struct expression *exp =
            new_op_expression(AndExp, term, parse_exp());
        if (is_right_child_empty(exp)) {
            destroy_expression(exp);
            return NULL;
        }
        return exp;
    default:
        return term;
    }
}
コード例 #23
0
ファイル: parse.c プロジェクト: gimunu/octopus-irECS
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;
}
コード例 #24
0
ファイル: parse.cpp プロジェクト: tov/ipd
 shared_ptr<exp> Parser::parse()
 {
   cur = tokens.begin();
   end = tokens.end();
   return parse_exp();
 }
コード例 #25
0
ファイル: parse.cpp プロジェクト: tov/ipd
  shared_ptr<exp>
  Parser::parse_exp()
  {
    if (isNumeric(*cur)) {
      return shared_ptr<exp>{new num(std::stoi(*cur++))};
    } else if (*cur == "(") {
      string first = *++cur;
      cur++;
      if (first == "+") {
	return shared_ptr<exp>{new add(parse_until_close())};
      } else if (first == "-") {
	return shared_ptr<exp>{new sub(parse_until_close())};
      } else if (first == "*") {
	return shared_ptr<exp>{new mul(parse_until_close())};
      } else if (first == "/") {
	return shared_ptr<exp>{new expressions::div(parse_until_close())};
      } else if (first == "lambda") {
	if (*cur != "(")
	  throw "ill-formed lambda";
	cur++;
	list<string> params;
	while (*cur != ")" && cur != end) {
	  params.push_back(*cur);
	  cur++;
	}
	if (*cur != ")")
	  throw "ill-formed lambda";
	cur++;
	shared_ptr<exp> body = parse_exp();
	if (*cur != ")")
	  throw "ill-formed lambda";
	cur++;
	return shared_ptr<exp>{new lambda(params, body)};
      } else if (first == "if0") {
	list<shared_ptr<exp>> exps = parse_until_close();
	if (exps.size() != 3)
	  throw "ill-formed if0";
	shared_ptr<exp> t = exps.front();
	exps.pop_front();
	shared_ptr<exp> tb = exps.front();
	exps.pop_front();
	shared_ptr<exp> fb = exps.front();
	return shared_ptr<exp>{new if0(t, tb, fb)};
      } else if (first == "let") {
	if (*cur != "(")
	  throw "ill-formed let";
	if (*++cur != "[")
	  throw "ill-formed let";
	string id = *++cur;
	cur++;
	shared_ptr<exp> bound = parse_exp();
	if (*cur != "]")
	  throw "ill-formed let";
	if (*++cur != ")")
	  throw "ill-formed let";
	cur++;
	shared_ptr<exp> body = parse_exp();
	if (*cur != ")")
	  throw "ill-formed let";
	cur++;
	return shared_ptr<exp>{new let(id, bound, body)};
      } else {
	cur--;
	return shared_ptr<exp>{new app(parse_until_close())};
      }
    } else {
      // any other token is a variable
      auto tv = *cur;
      cur++;
      return shared_ptr<exp>{new var(tv)};
    }
  }
コード例 #26
0
ファイル: tc-z8k.c プロジェクト: Manishearth/gdb
static void
get_operand (char **ptr, struct z8k_op *mode, unsigned int dst ATTRIBUTE_UNUSED)
{
  char *src = *ptr;
  char *end;

  mode->mode = 0;

  while (*src == ' ')
    src++;
  if (*src == '#')
    {
      mode->mode = CLASS_IMM;
      imm_operand = &(mode->exp);
      src = parse_exp (src + 1, &(mode->exp));
    }
  else if (*src == '@')
    {
      mode->mode = CLASS_IR;
      src = parse_reg (src + 1, &mode->regsize, &mode->reg);
    }
  else
    {
      unsigned int regn;

      end = parse_reg (src, &mode->mode, &regn);

      if (end)
	{
	  int nw;
	  unsigned int nr;

	  src = end;
	  if (*src == '(')
	    {
	      src++;
	      end = parse_reg (src, &nw, &nr);
	      if (end)
		{
		  /* Got Ra(Rb).  */
		  src = end;

		  if (*src != ')')
		    as_bad (_("Missing ) in ra(rb)"));
		  else
		    src++;

		  regaddr (mode->mode, "ra(rb) ra");
		  mode->mode = CLASS_BX;
		  mode->reg = regn;
		  mode->x_reg = nr;
		  reg[ARG_RX] = nr;
		}
	      else
		{
		  /* Got Ra(disp).  */
		  if (*src == '#')
		    src++;
		  src = parse_exp (src, &(mode->exp));
		  src = checkfor (src, ')');
		  mode->mode = CLASS_BA;
		  mode->reg = regn;
		  mode->x_reg = 0;
		  imm_operand = &(mode->exp);
		}
	    }
	  else
	    {
	      mode->reg = regn;
	      mode->x_reg = 0;
	    }
	}
      else
	{
	  /* No initial reg.  */
	  src = parse_exp (src, &(mode->exp));
	  if (*src == '(')
	    {
	      src++;
	      end = parse_reg (src, &(mode->mode), &regn);
	      regword (mode->mode, "addr(Ra) ra");
	      mode->mode = CLASS_X;
	      mode->reg = regn;
	      mode->x_reg = 0;
	      da_operand = &(mode->exp);
	      src = checkfor (end, ')');
	    }
	  else
	    {
	      /* Just an address.  */
	      mode->mode = CLASS_DA;
	      mode->reg = 0;
	      mode->x_reg = 0;
	      da_operand = &(mode->exp);
	    }
	}
    }
  *ptr = src;
}
コード例 #27
0
ファイル: parse.c プロジェクト: gimunu/octopus_SCE
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;
}
コード例 #28
0
ファイル: btorbtor.c プロジェクト: hellok/kint
static BtorNode *
parse_compare_and_overflow (BtorBTORParser * parser,
			    int len, Binary f, int can_be_array)
{
  BtorNode *l, *r, *res;
  int llen, rlen;

  if (len != 1)
    {
      (void) btor_perr_btor (parser,
			  "comparison or overflow operator returns %d bits",
			  len);
      return 0;
    }

  if (parse_space (parser))
    return 0;

  if (!(l = parse_exp (parser, 0, can_be_array)))
    return 0;

  if (parse_space (parser))
    {
    RELEASE_L_AND_RETURN_ERROR:
      btor_release_exp (parser->btor, l);
      return 0;
    }

  if (!(r = parse_exp (parser, 0, can_be_array)))
    goto RELEASE_L_AND_RETURN_ERROR;

  llen = btor_get_exp_len (parser->btor, l);
  rlen = btor_get_exp_len (parser->btor, r);

  if (llen != rlen)
    {
      (void)
	btor_perr_btor (parser,
		     "operands have different bit width %d and %d",
		     llen, rlen);
RELEASE_L_AND_R_AND_RETURN_ZERO:
      btor_release_exp (parser->btor, r);
      btor_release_exp (parser->btor, l);
      return 0;
    }

  if (can_be_array)
    {
      if (btor_is_array_exp (parser->btor, l) &&
	  !btor_is_array_exp (parser->btor, r))
	{
	  (void) btor_perr_btor (parser,
				 "first operand is array and second not");
	  goto RELEASE_L_AND_R_AND_RETURN_ZERO;
	}

      if (!btor_is_array_exp (parser->btor, l) &&
	  btor_is_array_exp (parser->btor, r))
	{
	  (void) btor_perr_btor (parser,
				 "second operand is array and first not");
	  goto RELEASE_L_AND_R_AND_RETURN_ZERO;
	}

      if (btor_is_array_exp (parser->btor, l) &&
	  btor_is_array_exp (parser->btor, r))
	{
	  llen = btor_get_index_exp_len (parser->btor, l);
	  rlen = btor_get_index_exp_len (parser->btor, r);

	  if (llen != rlen)
	    {
	      (void) btor_perr_btor (
		       parser,
		       "array operands have different index width %d and %d",
		       llen, rlen);
	      goto RELEASE_L_AND_R_AND_RETURN_ZERO;
	    }
	}
    }

  res = f (parser->btor, l, r);

  btor_release_exp (parser->btor, r);
  btor_release_exp (parser->btor, l);

  assert (btor_get_exp_len (parser->btor, res) == 1);

  return res;
}