Ejemplo n.º 1
0
int
_readx(Rune *p, int n, int nmode, int line)
{
	int c, omode;
	Rune *e;

	while((c = getrune()) == ' ' || c == '\t')
		;
	ungetrune(c);
	omode = inputmode;
	inputmode = nmode;
	e = p+n-1;
	for(c=getnext(); p<e; c=getnext()){
		if(c < 0)
			break;
		if(!line && (c == ' ' || c == '\t'))
			break;
		if(c == '\n'){
			if(!line)
				ungetnext(c);
			break;
		}
		*p++ = c;
	}
	inputmode = omode;
	*p = 0;
	if(c < 0)
		return -1;
	return 0;
}
Ejemplo n.º 2
0
Archivo: t7.c Proyecto: 00001/plan9port
int
e_dollar(void)
{
	int c;

	c = getnext();
	if(c < '1' || c > '9'){
		ungetnext(c);
		return 0;
	}
	c -= '0';
	if(nmstack <= 0 || mstack[nmstack-1].argc <= c)
		return 0;
	pushinputstring(mstack[nmstack-1].argv[c]);
	return 0;
}
Ejemplo n.º 3
0
Archivo: t16.c Proyecto: aahud/harvey
int
ifeval(void)
{
	int c, cc, neg, nc;
	Rune line[MaxLine], *p, *e, *q;
	Rune *a;
	
	while((c = getnext()) == ' ' || c == '\t')
		;
	neg = 0;
	while(c == '!'){
		neg = !neg;
		c = getnext();
	}

	if('0' <= c && c <= '9'){
		ungetnext(c);
		a = copyarg();
		c = (eval(a)>0) ^ neg;
		free(a);
		return c;
	}
	
	switch(c){
	case ' ':
	case '\n':
		ungetnext(c);
		return !neg;
	case 'o':	/* odd page */
	case 't':	/* troff */
	case 'h':	/* htmlroff */
		while((c = getrune()) != ' ' && c != '\t' && c != '\n' && c >= 0)
			;
		return 1 ^ neg;
	case 'n':	/* nroff */
	case 'e':	/* even page */
		while((c = getnext()) != ' ' && c != '\t' && c != '\n' && c >= 0)
			;
		return 0 ^ neg;
	}

	/* string comparison 'string1'string2' */
	p = line;
	e = p+nelem(line);
	nc = 0;
	q = nil;
	while((cc=getnext()) >= 0 && cc != '\n' && p<e){
		if(cc == c){
			if(++nc == 2)
				break;
			q = p;
		}
		*p++ = cc;
	}
	if(cc != c){
		ungetnext(cc);
		return 0;
	}
	if(nc < 2){
		return 0;
	}
	*p = 0;
	return (q-line == p-(q+1)
		&& memcmp(line, q+1, (q-line)*sizeof(Rune))==0) ^ neg;
}
Ejemplo n.º 4
0
static void getprimarytoken(void)
{
  static int wildcard = 0;
  int count = 0, ch;

  while(isspace(ch=getnext()));
  tokenindent = current->columnnr;
  if(tokenindent < tokenoffside)
  {
    ungetnext(ch);
    tokentype = offside;
    tokenval[count++] = '\0';
    return;
  }
  if(isdigit(ch))
  {
    tokentype = NUMBER;
    while(isdigit(ch))
    {
      tokenval[count++] = ch;
      ch = getnext();
    }
    if(ch == '.')
    {
      ch = getnext();
      if(isdigit(ch))
        tokenval[count++] = '.';
      else
      {
        ungetnext(ch);
        ch = '.';
      }
      while(isdigit(ch))
      {
        tokenval[count++] = ch;
        ch = getnext();
      }
    }
    tokenval[count++] = '\0';
    ungetnext(ch);
    return;
  }
  if(isalpha(ch) || ch == '_')
  {
    if('A' <= ch && ch <= 'Z')
      tokentype = TYPEID;
    else
      tokentype = IDENTIFIER;
    while(isalpha(ch) || isdigit(ch) || ch == '_')
    {
      tokenval[count++] = ch;
      ch = getnext();
    }
    tokenval[count++] = '\0';
    ungetnext(ch);
    if(strcmp(tokenval, "where")     == 0) tokentype = WHERE;
    if(strcmp(tokenval, "otherwise") == 0) tokentype = OTHERWISE;
    if(strcmp(tokenval, "abstype")   == 0) tokentype = ABSTYPE;
    if(strcmp(tokenval, "with")      == 0) tokentype = WITH;
    if(strcmp(tokenval, "generic")   == 0) tokentype = GENERIC;
    if(strcmp(tokenval, "True")      == 0) tokentype = IDENTIFIER;
    if(strcmp(tokenval, "False")     == 0) tokentype = IDENTIFIER;
    if(strcmp(tokenval, "Nil")       == 0) tokentype = IDENTIFIER;
    if(strcmp(tokenval, "_")         == 0) sprintf(tokenval, "_??_%d", wildcard++);
    return;
  }
  if(ch == '$')
  {
    ch = getnext();
    if(('a' <= ch && ch <= 'z') || ch == '_')
    {
      tokentype = OPERATOR;
      while(isalpha(ch) || isdigit(ch) || ch == '_')
      {
        tokenval[count++] = ch;
        ch = getnext();
      }
      tokenval[count++] = '\0';
      ungetnext(ch);
    }
    else
      parseerror(25);
    return;
  }
  if(ch == '\'')
  {
    tokentype = CHARACTER;
    ch = getnext();
    if(!isprint(ch)) parseerror(26);
    if(ch == '\\')
    {
      ch = getnext();
      switch(ch)
      {
        case 'a':
          ch = '\a';
          break;
        case 'b':
          ch = '\b';
          break;
        case 'f':
          ch = '\f';
          break;
        case 'n':
          ch = '\n';
          break;
        case 'r':
          ch = '\r';
          break;
        case 't':
          ch = '\t';
          break;
        case 'v':
          ch = '\v';
          break;
        case 'e':
          ch = 27;
          break;
        case 'x':
          ch = 16 * makehex(getnext()) + makehex(getnext());
          break;
      }
    }
    tokenval[count++] = ch;
    ch = getnext();
    if(ch != '\'') parseerror(27);
    tokenval[count++] = '\0';
    return;
  }
  if(ch == '\"')
  {
    tokentype = STRING;
    ch = getnext();
    while(isprint(ch) && ch != '\"')
    {
      if(ch == '\\')
      {
        ch = getnext();
        switch(ch)
        {
          case 'a':
            ch = '\a';
            break;
          case 'b':
            ch = '\b';
            break;
          case 'f':
            ch = '\f';
            break;
          case 'n':
            ch = '\n';
            break;
          case 'r':
            ch = '\r';
            break;
          case 't':
            ch = '\t';
            break;
          case 'v':
            ch = '\v';
            break;
          case 'e':
            ch = 27;
            break;
          case 'x':
            ch = 16 * makehex(getnext()) + makehex(getnext());
            break;
        }
      }
      tokenval[count++] = ch;
      ch = getnext();
    }
    if(ch != '\"') parseerror(28);
    tokenval[count++] = '\0';
    return;
  }
  if(ch == '(')
  {
    tokentype = LPAR;
    tokenval[count++] = ch;
    tokenval[count++] = '\0';
    return;
  }
  if(ch == ')')
  {
    tokentype = RPAR;
    tokenval[count++] = ch;
    tokenval[count++] = '\0';
    return;
  }
  if(ch == ';')
  {
    tokentype = SEP;
    tokenval[count++] = ch;
    tokenval[count++] = '\0';
    return;
  }
  if(ch == ',')
  {
    tokentype = COMMA;
    tokenval[count++] = ch;
    tokenval[count++] = '\0';
    return;
  }
  if(ch == '[')
  {
    tokentype = LBRACK;
    tokenval[count++] = ch;
    tokenval[count++] = '\0';
    return;
  }
  if(ch == ']')
  {
    tokentype = RBRACK;
    tokenval[count++] = ch;
    tokenval[count++] = '\0';
    return;
  }
  if(ch == '{')
  {
    tokentype = LACC;
    tokenval[count++] = ch;
    tokenval[count++] = '\0';
    return;
  }
  if(ch == '}')
  {
    tokentype = RACC;
    tokenval[count++] = ch;
    tokenval[count++] = '\0';
    return;
  }
  if(ch == '|')
  {
    tokentype = BAR;
    tokenval[count++] = ch;
    ch = getnext();
    if(ch == '|')
    {
      while(ch != '\n')
      {
        ch = getnext();
        if(ch == EOF)
        {
          tokentype = empty;
          tokenval[count++] = '\0';
          return;
        }
      }
      gettoken();
      return;
    }
    tokenval[count++] = '\0';
    ungetnext(ch);
    return;
  }
  if(ch == '.')
  {
    tokentype = OPERATOR;
    tokenval[count++] = ch;
    ch = getnext();
    if(ch == '.')
    {
      tokentype = POINTS;
      tokenval[count++] = ch;
      ch = getnext();
    }
    tokenval[count++] = '\0';
    ungetnext(ch);
    return;
  }
  if(ch == '/')
  {
    ch = getnext();
    if(ch == '*')
    {
      int comments = 1;
      while(comments > 0)
      {
        ch = getnext();
        if(ch == EOF) parseerror(29);
        else if(ch == '/')
        {
          ch = getnext();
          if(ch == '*') comments++;
          else ungetnext(ch);
        }
        else if(ch == '*')
        {
          ch = getnext();
          if(ch == '/') comments--;
          else ungetnext(ch);
        }
      }
      gettoken();
      return;
    }
    tokentype = OPERATOR;
    tokenval[count++] = '/';
    while(ispunct(ch) && ch!='('  && ch!=')'  && ch!='['
                      && ch!=']'  && ch!='{'  && ch!='}'
                      && ch!='|'  && ch!=';'  && ch!=','
                      && ch!='\'' && ch!='\"' && ch!='.'
                      && ch!='_')
    {
      tokenval[count++] = ch;
      ch = getnext();
    }
    tokenval[count++] = '\0';
    ungetnext(ch);
    return;
  }
  if(ispunct(ch))
  {
    tokentype = OPERATOR;
    while(ispunct(ch) && ch!='('  && ch!=')'  && ch!='['
                      && ch!=']'  && ch!='{'  && ch!='}'
                      && ch!='|'  && ch!=';'  && ch!=','
                      && ch!='\'' && ch!='\"' && ch!='.'
                      && ch!='_')
    {
      tokenval[count++] = ch;
      ch = getnext();
    }
    tokenval[count++] = '\0';
    ungetnext(ch);
    if     (strcmp(tokenval, "<-")  == 0) tokentype = GENER;
    else if(strcmp(tokenval, ":=")  == 0) tokentype = ASSIGNMENT;
    else if(strcmp(tokenval, "->")  == 0) tokentype = ARROW;
    else if(strcmp(tokenval, "::")  == 0) tokentype = COLONS;
    else if(strcmp(tokenval, "::=") == 0) tokentype = DEF;
    else if(strcmp(tokenval, "==")  == 0) tokentype = SYN;
    else if(strcmp(tokenval, "~")   == 0) tokentype = IDENTIFIER;
    else if(strcmp(tokenval, "#")   == 0) tokentype = IDENTIFIER;
    return;
  }
  tokentype = empty;
  tokenval[count++] = '\0';
  ungetnext(ch);
}