Example #1
0
Static Stmt *proc_fwritebytes()
{
    Expr *ex, *ex2, *vex, *fex;
    Type *type;

    if (!skipopenparen())
	return NULL;
    fex = p_expr(tp_text);
    if (!skipcomma())
	return NULL;
    vex = p_expr(NULL);
    if (!skipcomma())
	return NULL;
    ex2 = p_expr(tp_integer);
    skipcloseparen();
    type = vex->val.type;
    ex = makeexpr_bicall_4("fwrite", tp_integer,
                           makeexpr_addr(vex),
                           convert_size(type, ex2, "FWRITEBYTES"),
                           makeexpr_long(1),
                           filebasename(copyexpr(fex)));
    if (checkfilewrite) {
        ex = makeexpr_bicall_2(name_SETIO, tp_void,
                               makeexpr_rel(EK_EQ, ex, makeexpr_long(1)),
                               makeexpr_long(3));
    }
    return wrapopencheck(makestmt_call(ex), fex);
}
Example #2
0
int
p_assign(const char *name, struct value *v, int flag)
{
	(void) s_gettok();

	if (p_expr(v, flag) < 0) {
		p_synerror();
		return -1;
	}
	switch (v->v_type) {
	case V_STR:
	case V_NUM:
		if (name && flag && var_set(name, v) == 0) {
			p_memerror();
			val_free(*v);
			return -1;
		}
		break;
	}
	return 0;
}
Example #3
0
Parser::stmt_t Parser::p_stmt_toks(Lexer &lex,const Context &ctx,std::vector<Lexer::Token> *toks){
  Lexer::Token tok,tok1,tok2;
  lex >> tok;

  stmt_t res_stmt = stmt_t::nop();
  std::vector<Lexer::Token> mytoks;

  switch(tok.type){
  case Lexer::NOP:
    ppush(&mytoks,tok);
    res_stmt = stmt_t::nop(tok.pos,mytoks);
    break;
  case Lexer::READ:
    {
      ppush(&mytoks,tok);
      force_toks(lex,Lexer::COLON,&mytoks);
      lex >> tok1;
      if(tok1.type == Lexer::REG){
        ppush(&mytoks,tok1);
        force_toks(lex,Lexer::ASSIGNMENT,&mytoks);
        memloc_or_pointer_t ml = p_memloc_toks(lex,ctx,&mytoks);
        res_stmt = resolve_pointer(ml,[&tok1,&tok](const memloc_t &ml){
            return stmt_t::read_assign(tok1.value,ml,tok.pos);
          },ctx,mytoks);
      }else{
        lex.putback(tok1);
        Parser::memloc_or_pointer_t ml = p_memloc_toks(lex,ctx,&mytoks);
        force_toks(lex,Lexer::EQ,&mytoks);
        expr_t e = p_expr_toks(lex,&mytoks);
        res_stmt = resolve_pointer(ml,[&e,&tok](const memloc_t &ml){
            return stmt_t::read_assert(ml,e,tok.pos);
          },ctx,mytoks);
      }
      break;
    }
  case Lexer::SYNCRD:
    {
      ppush(&mytoks,tok);
      force_toks(lex,Lexer::COLON,&mytoks);
      lex >> tok1;
      if(tok1.type == Lexer::REG){
        ppush(&mytoks,tok1);
        force_toks(lex,Lexer::ASSIGNMENT,&mytoks);
        memloc_or_pointer_t ml = p_memloc_toks(lex,ctx,&mytoks);
        res_stmt = resolve_pointer(ml,[&tok1,&tok](const memloc_t &ml){
            return stmt_t::syncrd_assign(tok1.value,ml,tok.pos);
          },ctx,mytoks);
      }else{
        lex.putback(tok1);
        Parser::memloc_or_pointer_t ml = p_memloc_toks(lex,ctx,&mytoks);
        force_toks(lex,Lexer::EQ,&mytoks);
        expr_t e = p_expr_toks(lex,&mytoks);
        res_stmt = resolve_pointer(ml,[&e,&tok](const memloc_t &ml){
            return stmt_t::syncrd_assert(ml,e,tok.pos);
          },ctx,mytoks);
      }
      break;
    }
  case Lexer::WRITE:
    {
      ppush(&mytoks,tok);
      force_toks(lex,Lexer::COLON,&mytoks);
      Parser::memloc_or_pointer_t ml = p_memloc_toks(lex,ctx,&mytoks);
      force_toks(lex,Lexer::ASSIGNMENT,&mytoks);
      expr_t e = p_expr_toks(lex,&mytoks);
      res_stmt = resolve_pointer(ml,[&e,&tok](const memloc_t &ml){
          return stmt_t::write(ml,e,tok.pos);
        },ctx,mytoks);
      break;
    }
  case Lexer::SYNCWR:
    {
      ppush(&mytoks,tok);
      force_toks(lex,Lexer::COLON,&mytoks);
      Parser::memloc_or_pointer_t ml = p_memloc_toks(lex,ctx,&mytoks);
      force_toks(lex,Lexer::ASSIGNMENT,&mytoks);
      expr_t e = p_expr_toks(lex,&mytoks);
      res_stmt = resolve_pointer(ml,[&e,&tok](const memloc_t &ml){
          return stmt_t::syncwr(ml,e,tok.pos);
        },ctx,mytoks);
      break;
    }
  case Lexer::FENCE:
    {
      ppush(&mytoks,tok);
      res_stmt = stmt_t::full_fence(tok.pos,mytoks);
      break;
    }
  case Lexer::SSFENCE:
    {
      ppush(&mytoks,tok);
      res_stmt = stmt_t::ss_fence(tok.pos,mytoks);
      break;
    }
  case Lexer::LLFENCE:
    {
      ppush(&mytoks,tok);
      res_stmt = stmt_t::ll_fence(tok.pos,mytoks);
      break;
    }
  case Lexer::CAS:
    {
      ppush(&mytoks,tok);
      force_toks(lex,Lexer::LPAREN,&mytoks);
      Parser::memloc_or_pointer_t ml = p_memloc_toks(lex,ctx,&mytoks);
      force_toks(lex,Lexer::COMMA,&mytoks);
      expr_t v0(p_expr_toks(lex,&mytoks));
      force_toks(lex,Lexer::COMMA,&mytoks);
      expr_t v1(p_expr_toks(lex,&mytoks));
      force_toks(lex,Lexer::RPAREN,&mytoks);
      res_stmt = resolve_pointer(ml,[&v0,&v1,&tok](const memloc_t &ml){
          return stmt_t::cas(ml,v0,v1,tok.pos);
        },ctx,mytoks);
      break;
    }
  case Lexer::REG:
    {
      ppush(&mytoks,tok);
      force_toks(lex,Lexer::ASSIGNMENT,&mytoks);
      expr_t e = p_expr_toks(lex,&mytoks);
      res_stmt = stmt_t::assignment(tok.value,e,tok.pos,mytoks);
      break;
    }
  case Lexer::IF:
    {
      ppush(&mytoks,tok);
      bexpr_t b(p_bexpr_toks(lex,&mytoks));

      force_toks(lex,Lexer::THEN,&mytoks);
      stmt_t::labeled_stmt_t s0 = p_lstmt(lex,ctx,&mytoks);
      lex >> tok1;
      if(tok1.type == Lexer::ELSE){
        ppush(&mytoks,tok1);
        stmt_t::labeled_stmt_t s1 = p_lstmt(lex,ctx,&mytoks);
        res_stmt = stmt_t::if_stmt(b,s0,s1,tok.pos,mytoks);
      }else{
        lex.putback(tok1);
        res_stmt = stmt_t::if_stmt(b,s0,tok.pos,mytoks);
      }
      break;
    }
  case Lexer::WHILE:
    {
      ppush(&mytoks,tok);
      bexpr_t b(p_bexpr_toks(lex,&mytoks));
      force_toks(lex,Lexer::DO,&mytoks);
      stmt_t::labeled_stmt_t s = p_lstmt(lex,ctx,&mytoks);
      res_stmt = stmt_t::while_stmt(b,s,tok.pos,mytoks);
      break;
    }
  case Lexer::GOTO:
    {
      ppush(&mytoks,tok);
      Lang::label_t lbl = p_label(lex,&mytoks);
      res_stmt = stmt_t::goto_stmt(lbl,tok.pos,mytoks);
      break;
    }
  case Lexer::EITHER:
    {
      ppush(&mytoks,tok);
      Lexer::TokenPos pos0 = tok.pos;
      std::vector<stmt_t> seq;
      Lexer::Token lctok;
      Lexer::Token tok;
      lex >> lctok;
      lex.putback(lctok);
      force_toks(lex,Lexer::LCURL,&mytoks);

      seq.push_back(p_stmt_list(lex,ctx,&mytoks));
      lex >> tok;
      while(tok.type == Lexer::EITHER_OR){
        ppush(&mytoks,tok);
        seq.push_back(p_stmt_list(lex,ctx,&mytoks));
        lex >> tok;
      }
      lex.putback(tok);
      force_toks(lex,Lexer::RCURL,&mytoks,"Expected '}' at "," to match '{' at "+
                 lctok.pos.to_long_string()+".");

      res_stmt = stmt_t::either(seq,pos0);
      break;
    }
  case Lexer::LOCKED:
    {
      ppush(&mytoks,tok);
      Lexer::TokenPos pos0 = tok.pos;
      std::vector<stmt_t> seq;
      Lexer::Token lctok;
      Lexer::Token tok;
      lex >> lctok;
      if(lctok.type == Lexer::WRITE){
        ppush(&mytoks,lctok);
        force_toks(lex,Lexer::COLON,&mytoks);
        Parser::memloc_or_pointer_t ml = p_memloc_toks(lex,ctx,&mytoks);
        force_toks(lex,Lexer::ASSIGNMENT,&mytoks);
        expr_t e = p_expr_toks(lex,&mytoks);
        res_stmt = resolve_pointer(ml,[&e,&pos0](const memloc_t &ml){
            return stmt_t::locked_write(ml,e,pos0);
          },ctx,mytoks);
      }else{
        lex.putback(lctok);
        force_toks(lex,Lexer::LCURL,&mytoks);

        stmt_t s = p_stmt_list(lex,ctx,&mytoks);
        std::string cmt;
        if(!stmt_t::check_locked_invariant(s,&cmt)){
          throw new SyntaxError("Error in locked block at "+lctok.pos.to_long_string()+": "+cmt,lctok.pos);
        }
        seq.push_back(s);
        lex >> tok;
        while(tok.type == Lexer::EITHER_OR){
          ppush(&mytoks,tok);
          s = p_stmt_list(lex,ctx,&mytoks);
          if(!stmt_t::check_locked_invariant(s,&cmt)){
            throw new SyntaxError("Error in locked block at "+tok.pos.to_long_string()+": "+cmt,tok.pos);
          }
          seq.push_back(s);
          lex >> tok;
        }
        lex.putback(tok);
        force_toks(lex,Lexer::RCURL,&mytoks,"Expected '}' at "," to match '{' at "+
                   lctok.pos.to_long_string()+".");

        res_stmt = stmt_t::locked_block(seq,pos0);
      }
      break;
    }
 case Lexer::SLOCKED:
    {
      Lexer::TokenPos pos0 = tok.pos;
      force(lex,Lexer::WRITE);
      force(lex,Lexer::COLON);
      Parser::memloc_or_pointer_t ml = p_memloc(lex,ctx);
      force(lex,Lexer::ASSIGNMENT);
      expr_t e = p_expr(lex);
      return resolve_pointer(ml,[&e,&pos0](const memloc_t &ml){
          return stmt_t::slocked_write(ml,e,pos0);
        },ctx,mytoks);
    }
  case Lexer::LCURL:
    {
      ppush(&mytoks,tok);
      stmt_t sl = p_stmt_list(lex,ctx,&mytoks);
      force_toks(lex,Lexer::RCURL,&mytoks,"Expected '}' at "," to match '{' at "+
                 tok.pos.to_long_string()+".");
      res_stmt = sl;
      break;
    }
  case Lexer::ASSUME:
    {
      ppush(&mytoks,tok);
      force_toks(lex,Lexer::COLON,&mytoks);
      bexpr_t cnd = p_bexpr_toks(lex,&mytoks);
      res_stmt = stmt_t::assume(cnd,tok.pos,mytoks);
      break;
    }
  default:
    throw new SyntaxError("Expected statement at "+tok.pos.to_long_string()+".",tok.pos);
  }
  if(toks) toks->insert(toks->end(),mytoks.begin(),mytoks.end());
  return res_stmt;
}
Example #4
0
/*
 * name == 0 means we don't have a function name but
 * want to parse the arguments anyway.  flag == 0 in this case.
 */
int
p_function(const char *name, struct value *v, int flag)
{
	struct value t;
	struct lcmd_tab *c = NULL;
	struct alias *a = NULL;
	struct lcmd_arg *ap;			/* this arg */
	struct lcmd_arg *lp = NULL;		/* list arg */
	int i;
	struct value av[LCMD_NARG + 1];
	struct value *vp;

	if (name != NULL) {
		if ((c = lcmd_lookup(name)))
			name = c->lc_name;
		else if ((a = alias_lookup(name)))
			name = a->a_name;
		else {
			p_error("%s: No such command or alias.", name);
			flag = 0;
		}
	}
	for (vp = av; vp < &av[LCMD_NARG + 1]; vp++)
		vp->v_type = V_ERR;

	if (token == T_LP)
		(void) s_gettok();
	i = 0;
	for (;;) {
		ap = NULL;
		vp = NULL;
		if (token == T_COMMA)		/* null argument */
			t.v_type = V_ERR;
		else {
			if (p_expr0(&t, flag) < 0)
				break;
			if (t.v_type == V_ERR)
				flag = 0;
		}
		if (token != T_ASSIGN) {
			if (i >= LCMD_NARG ||
			    (c != NULL && (ap = lp) == NULL &&
			    (ap = c->lc_arg + i)->arg_name == 0)) {
				p_error("%s: Too many arguments.", name);
				flag = 0;
			} else
				vp = &av[i++];
		} else {
			char *tmp;
			if (p_convstr(&t) < 0)
				goto abort;
			tmp = t.v_type == V_STR ? t.v_str : 0;
			(void) s_gettok();
			if (p_expr(&t, flag) < 0) {
				if (tmp)
					str_free(tmp);
				p_synerror();
				goto abort;
			}
			if (t.v_type == V_ERR)
				flag = 0;
			if (tmp) {
				if (c == NULL) {
					/* an aliase */
					p_error("%s: Bad alias syntax.", name);
					flag = 0;
				} else {
					for (ap = c->lc_arg, vp = av;
					     ap != NULL && ap->arg_name != 0 &&
					     (*ap->arg_name == '\0' ||
					      !str_match(tmp, ap->arg_name,
							ap->arg_minlen));
					     ap++, vp++)
						;
					if (ap == NULL || ap->arg_name == 0) {
						p_error("%s: Unknown argument \"%s\".",
							name, tmp);
						flag = 0;
						ap = NULL;
						vp = NULL;
					}
				}
				str_free(tmp);
			}
		}
		if (ap != NULL) {
			if (ap->arg_flags & ARG_LIST) {
				i = vp - av + 1;
				lp = ap;
			}
			if (vp && vp->v_type != V_ERR) {
				if (*ap->arg_name)
					p_error("%s: Argument %d (%s) duplicated.",
						name, (int)(vp - av + 1),
						ap->arg_name);
				else
					p_error("%s: Argument %d duplicated.",
						name, (int)(vp - av + 1));
				flag = 0;
				vp = NULL;
			} else if (t.v_type == V_ERR) {
				/* do nothing */
			} else if (((ap->arg_flags&ARG_TYPE) == ARG_NUM &&
				    t.v_type != V_NUM) ||
				   ((ap->arg_flags&ARG_TYPE) == ARG_STR &&
				   t.v_type != V_STR)) {
				if (*ap->arg_name)
					p_error("%s: Argument %d (%s) type mismatch.",
						name, (int)(vp - av + 1),
						ap->arg_name);
				else
					p_error("%s: Argument %d type mismatch.",
						name, (int)(vp - av + 1));
				flag = 0;
				vp = NULL;
			}
		}
		if (vp != NULL)
			*vp = t;
		else
			val_free(t);
		if (token == T_COMMA)
			(void) s_gettok();
	}

	if (p_erred())
		flag = 0;
	if (token == T_RP)
		(void) s_gettok();
	else if (token != T_EOL && token != T_EOF)
		flag = 0;		/* look for legal follow set */
	v->v_type = V_ERR;
	if (flag) {
		if (c != NULL)
			(*c->lc_func)(v, av);
		else {
			if (a->a_flags & A_INUSE)
				p_error("%s: Recursive alias.", a->a_name);
			else {
				a->a_flags |= A_INUSE;
				if (dolongcmd(a->a_buf, av, i) < 0)
					p_memerror();
				a->a_flags &= ~A_INUSE;
			}
		}
	}
	if (p_abort()) {
		val_free(*v);
		v->v_type = V_ERR;
		goto abort;
	}
	for (vp = av; vp < &av[LCMD_NARG]; vp++)
		val_free(*vp);
	return 0;
abort:
	for (vp = av; vp < &av[LCMD_NARG]; vp++)
		val_free(*vp);
	return -1;
}