Ejemplo n.º 1
0
/* handler for parser QUOTES state */
static const WCHAR *quotes_state( struct parser *parser, const WCHAR *pos )
{
  const WCHAR *p, *token_end = parser->start;

  for (p = pos; !is_eol( parser, p ); p++)
    {
      if (*p == '"')
        {
          if (p+1 < parser->end && p[1] == '"')  /* double quotes */
            {
              push_token( parser, p + 1 );
              parser->start = token_end = p + 2;
              p++;
            }
          else  /* end of quotes */
            {
              push_token( parser, p );
              parser->start = p + 1;
              pop_state( parser );
              return p + 1;
            }
        }
    }
  push_token( parser, p );
  pop_state( parser );
  return p;
}
Ejemplo n.º 2
0
static double eval_expr(const char *buf, int *pos) /* expr = term+expr | term-expr | term */
{
	double rv = 0.0, lhs, rhs;
	Token tok;
	
	DB(printf("-- eval_expr(\"%s\", &pos=%p pos=%d)\n", buf+*pos, pos, *pos));
	lhs = eval_term(buf, pos);
	if(G_eval_error)
		return rv;
	DB(printf("-- expr lhs=%f\n", lhs));
	tok = pull_token(buf, pos);
	if(G_eval_error)
		return rv;
	DB(printf("-- expr token type '%c' = ", tok.type));
	switch(tok.type)
	{
	case '\0':
		DB(printf("END OF BUFFER\n"));
		rv = lhs;
		break;
	case '+': /* addition */
		rhs = eval_expr(buf, pos);
		DB(printf("%f+%f", lhs, rhs));
		rv = lhs+rhs;
		DB(printf("=%f\n", rv));
		break;
	case '-': /* subtraction */
		rhs = eval_expr(buf, pos);
		DB(printf("%f-%f", lhs, rhs));
		rv = lhs-rhs;
		DB(printf("=%f\n", rv));
		break;
	case ')': /* end of group */
		DB(printf("end group"));
		push_token(tok);
		rv = lhs;
		DB(printf(" (rv=%f)\n", rv));
		break;
	case ',': /* argument delimiter */
		DB(printf("delimiter"));
		push_token(tok);
		rv = lhs;
		DB(printf(" (rv=%f)\n", rv));
		break;
	default:
		DB(printf("invalid expr token\n"));
		G_eval_error = EVAL_SYNTAX_ERROR;
		rv = lhs;
	}
	
	DB(printf("-- expr rv=%f\n", rv));
	return rv;
}
Ejemplo n.º 3
0
/*
 * Parse a file path and return a structure containing its
 * components.
 *
 * This function takes a file path (e.g. /var/tmp/myexample.txt)
 * and produces a structure containing points to the path elements.
 *
 * typedef struct _PATHINFO_STRUCT {
 *	char* fullpath;
 *	char* filename;
 *	char* dirpath;
 *	char* extension;
 *	int isdir;
 * } PATHINFO_STRUCT;
 *
 * The PATHINFO_STRUCT structure is allocated from the heap, as are
 * the strings pointed two by its members. This memory must be
 * returned to the heap by calling pathinfo_release, passing a pointer
 * to the PATHINFO_STRUCT returned by pathinfo_parse.
 */
PATHINFO_STRUCT* pathinfo_parse_filepath(const char* pathname) {
	DQHEADER deque;
	PATHINFO_STRUCT* pathinfop;
	char* pathdelim = "/";
	char* pathbuff;
	char* tokenp = NULL;
	char* token_aheadp = NULL;
	int isdir = 0;

	// Get a pathinfo structure
	pathinfop = calloc(1, sizeof(PATHINFO_STRUCT));
	pathinfop->fullpath = strdup(pathname);
	// Initialize a plenty big deque to hold path components.
	dq_init(strlen(pathname) + 1, sizeof(char), &deque);

	// Break the file path into tokens. Copy to a working buffer
	// so we don't alter path with calls to strtok
	pathbuff = strdup(pathname);

	// Check for leading /
	if (pathbuff[0] == '/') {
		push_token(&deque, "/");
	}
	isdir = pathinfo_is_dir(pathname);
	pathinfop->isdir = isdir;

	// Get first token and look ahead token
	tokenp = strtok(pathbuff, pathdelim);
	token_aheadp = strtok(NULL, pathdelim);
	while (tokenp != NULL) {
		if (token_aheadp != NULL) {
			// More tokens in path string
			push_token(&deque, tokenp);
			push_token(&deque, "/");
			tokenp = token_aheadp;
			token_aheadp = strtok(NULL, pathdelim);
		} else {
			// tokenp is the last token in the sequence
			if (!isdir) {
				parse_filename(pathinfop, tokenp);
			} else {
				push_token(&deque, tokenp);
			}
			tokenp = NULL;
		}
	}
	set_dirpath(pathinfop, &deque);
	free(pathbuff);
	return pathinfop;
}
Ejemplo n.º 4
0
Archivo: rcs.c Proyecto: johan/pike
static void tokenize( struct pike_string *s )
{
    int in_string = 0;
    unsigned int ts=0, i, len=s->len;
    const char *data = s->str;
    struct svalue *osp = Pike_sp;
    push_array( allocate_array_no_init( 0, 100 ) );
    for( i=0; i<len; i++ )
    {
        if( in_string )
        {
            if( (data[i]=='@') )
            {
                if( data[i+1]!='@' )
                {
                    push_token( data, ts, i-1 );
                    in_string=0;
                    ts=i+1;
                }
                else
                    i++;
            }
        }
        else
        {
            switch( data[i] )
            {
            case '@':
                ts=i+1;
                in_string=1;
                break;
            case ':':
            case ' ':
            case '\t':
            case '\n':
            case '\r':
                if( ts < i ) push_token( data, ts, i-1 );
                ts=i+1;
                break;
            case ';':
                if( ts < i ) push_token( data, ts, i-1 );
                ts=i+1;
                push_array( allocate_array_no_init( 0, 10 ) );
                break;
            }
        }
    }
    if( ts < len ) push_token( data, ts, len-1 );
    f_aggregate( Pike_sp-osp );
}
Ejemplo n.º 5
0
static int handle_token_colon( Token * tk, Parser * parser, FILE * out )
{
	if( !tk || !parser || !out )
		return 0;

	if( parser->empty_line
	    && !push_indent( parser->indent, out ))
		return 0;

	if( parser->in_branch
	    && !parser->parens_closed
	    && parser->empty_line ) 
	{
		int i;
		for( i = 4; i > 0; --i )
			putc( ' ', out );
	}

	if( !push_token( tk, out ) )
		return 0;

	parser->empty_line = 0;
	parser->un_op = 0;
	return 1;
}
Ejemplo n.º 6
0
static int handle_token_assign_op( Token * tk, Parser * parser, FILE * out )
{
	if( !tk || !parser || !out )
		return 0;

	if( parser->empty_line ) {

		if( !push_indent( parser->indent, out ) )
			return 0;
		int i;
		for( i = 4; i > 0; --i )
			putc( ' ', out );
	}
	else {
		putc( ' ', out );
	}
	    
	if( !push_token( tk, out ) )
		return 0;

	parser->empty_line = 0;
	parser->un_op = 0;

	return 1;
}
Ejemplo n.º 7
0
static int run_scan_toks(lua_State * L)
{
    saved_tex_scanner texstate;
    int macro_def = false, xpand = false;
    halfword t, saved_defref;
    int i = 1;
    int top = lua_gettop(L);
    if (top>0)
      macro_def = lua_toboolean(L,1); /* \\def ? */
    if (top>1)
      xpand = lua_toboolean(L,2); /* expand ? */
    save_tex_scanner(texstate);
    saved_defref = def_ref;
    (void) scan_toks(macro_def, xpand);
    t = def_ref;
    unsave_tex_scanner(texstate);
    def_ref = saved_defref;
    /* This function returns a pointer to the tail of a new token
       list, and it also makes |def_ref| point to the reference count at the
       head of that list. */
    lua_newtable(L);
    while (token_link(t)) {
        t = token_link(t);
        push_token(L,t);
        lua_rawseti(L,-2,i++);
    }
    return 1;
}
Ejemplo n.º 8
0
static int handle_token_rparen( Token * tk, Parser * parser, FILE * out )
{
	if( !tk || !parser || !out )
		return 0;

	if( parser->in_branch
	    && !parser->parens_closed )
	{
		--parser->paren_depth;

		if( parser->paren_depth == 0 ) {
			parser->parens_closed = 1;
		}

		if( parser->empty_line ) {
			int i;
			for( i = 4; i > 0; --i )
				putc( ' ', out );
		}
	}

	if( !push_token( tk, out ) )
		return 0;

	parser->empty_line = 0;
	parser->un_op = 0;

	return 1;
}
Ejemplo n.º 9
0
/*#define TOK_TO_CATCH PREPROC */
static void check_parser( Parser * parser )
{
	if( parser->paren_depth < 0
	    || parser->indent < 0
	#if defined(LINE_TO_STOP) && defined(TOK_TO_CATCH) 
	    || (parser->cur_line == LINE_TO_STOP
	        && parser->prev_tk.type == TOK_TO_CATCH) 
	#endif
	    )
	{
		fprintf( stderr, "\n**************************\n" );
		if( parser->paren_depth < 0 ) {
			fprintf( stderr, "ERROR: paren_depth < 0\n" );
		}
		if( parser->indent < 0 ) {
			fprintf( stderr, "ERROR: indent < 0\n" );
		}
		if( parser->last_brace_indent < 0 ) {
			fprintf( stderr, "ERROR: last_brace_indent < 0\n" );
		}
		fprintf( stderr, "paren_depth = %d\n", 
		         parser->paren_depth );
		fprintf( stderr, "indent = %d\n", 
		         parser->indent );
		fprintf( stderr, "last_brace_indent = %d\n",
		         parser->last_brace_indent );
		fprintf( stderr, "Current line: %d\n",
			 parser->cur_line );
		fprintf( stderr, "Current token:\n" );
		push_token( &(parser->prev_tk), stderr );
		fprintf( stderr, "\n");
		abort();
	}
}
Ejemplo n.º 10
0
static int handle_token_eol( Token * tk, Parser * parser, FILE * out )
{
	if( !tk || !parser || !out )
		return 0;

	if( !push_token( tk, out ) ) {
		return 0;
	}

	parser->empty_line = 1;
	parser->un_op = 0;
	return 1;
}
Ejemplo n.º 11
0
main()
{
	int type;
	double op2;
	char c;
	char s[MAXOP];

	while ((type = getop(s)) != EOF) {
		switch (type) {
		case NUMBER:
			push_token(NUMBER, atof(s));
			break;
		case '+': case '-': case '/': case '*': case '%':
			while (((c = top_char()) != EOF) && islesseq(type, c))
				push_token(pop_char(),  -1);
			push_char(type);
			break;
		case '(':
			push_char(type);
			break;
		case ')':
			while (((c = pop_char()) != EOF) && c != '(')
				push_token(c, -1); 
			break;
		case '\n':
			while ((c = pop_char()) != EOF)
				push_token(c, -1);
			calc();
			break;
		case 'q':
			return;
		default:
			printf("error: unknown command %s\n", s);
			break;
		}
	}
}
Ejemplo n.º 12
0
static int handle_token_rbrace( Token * tk, Parser * parser, FILE * out )
{
	if( !tk || !parser || !out )
		return 0;

	int lbi = parser->brace_indent[parser->last_brace_indent];

	if( !parser->empty_line ) {
		putc( '\n', out );
		parser->empty_line = 1;
	}
	
	parser->indent = lbi;

	if( !push_indent( parser->indent, out ) )
	{
		return 0;
	}

	if( !push_token( tk, out ) )
	{
		return 0;
	}

	--parser->last_brace_indent;

	if( parser->last_brace_indent > 0
	    && parser->last_brace_indent < parser->stack_size - 10
	    && !shrink_stack( &(parser->brace_indent),
	                       &(parser->stack_size) ) )
	{
		return 0;
	}

	lbi = parser->brace_indent[parser->last_brace_indent];

	if( !parser->last_brace_indent )
	{
		parser->indent = lbi;
	}
	else
	{
		parser->indent = lbi + 1;
	}

	parser->empty_line = 0;
	parser->un_op = 0;
	return 1;
}
Ejemplo n.º 13
0
static int handle_token_comment( Token * tk, Parser * parser, FILE * out )
{
	if( !tk || !parser || !out )
		return 0;
	if( !parser->empty_line ) { 
		putc( '\t', out );
	}
	if( parser->empty_line
	    && !push_indent( parser->indent, out ))
		return 0;
	if( !push_token( tk, out ) )
		return 0;
	parser->empty_line = 0;
	parser->un_op = 0;
	return 1;
}
Ejemplo n.º 14
0
/* handler for parser KEY_NAME state */
static
const CHAR*
key_name_state(
    struct parser *parser,
    const CHAR *pos)
{
    const CHAR *p, *token_end = parser->start;

    for (p = pos; !is_eol(parser, p); p++)
    {
        if (*p == ',') break;
        switch(*p)
        {

        case '=':
            push_token(parser, token_end);
            if (!add_field_from_token(parser, 1)) return NULL;
            parser->start = p + 1;
            push_state(parser, VALUE_NAME);
            set_state(parser, LEADING_SPACES);
            return p + 1;
        case ';':
            push_token(parser, token_end);
            if (!add_field_from_token(parser, 0)) return NULL;
            push_state(parser, LINE_START);
            set_state(parser, COMMENT);
            return p + 1;
        case '"':
            push_token(parser, token_end);
            parser->start = p + 1;
            push_state(parser, KEY_NAME);
            set_state(parser, QUOTES);
            return p + 1;
        case '\\':
            push_token(parser, token_end);
            parser->start = p;
            push_state(parser, KEY_NAME);
            set_state(parser, EOL_BACKSLASH);
            return p;
        default:
            if (!isspace((unsigned char)*p)) token_end = p + 1;
            else
            {
                push_token(parser, p);
                push_state(parser, KEY_NAME);
                set_state(parser, TRAILING_SPACES);
                return p;
            }
            break;
        }
    }
    push_token(parser, token_end);
    set_state(parser, VALUE_NAME);
    return p;
}
Ejemplo n.º 15
0
/*
 * peek_token
 *    utility function to find the type of the next token that
 * would be returned by the scanner
 *
 * returns type of token to be read next
 */
TokenType peek_token(struct ScanData *data) {
  struct Token myToken;
  
  /* if the token is on the stack, this is easy */
  if (data->tokBuf != NULL) {
    return data->tokBuf->token.type;
  }

  /* else get a copy of the next token */
  (void)get_token(&myToken,data);
  if (push_token(&myToken, data) != 0) {
    /* cannot push back to stack, very bad */
    fprintf(stderr,"Cannot push token back into buffer\n");
    exit(1);
  }
  
  /* return its type */
  return myToken.type;
}
Ejemplo n.º 16
0
/* handler for parser SECTION_NAME state */
static const WCHAR *section_name_state( struct parser *parser, const WCHAR *pos )
{
  const WCHAR *p;

  for (p = pos; !is_eol( parser, p ); p++)
    {
      if (*p == ']')
        {
          push_token( parser, p );
          if (add_section_from_token( parser ) == NULL)
            return NULL;
          push_state( parser, LINE_START );
          set_state( parser, COMMENT );  /* ignore everything else on the line */
          return p + 1;
        }
    }
  parser->error = INF_STATUS_BAD_SECTION_NAME_LINE; /* unfinished section name */
  return NULL;
}
Ejemplo n.º 17
0
/* handler for parser EOL_BACKSLASH state */
static
const CHAR*
eol_backslash_state(
    struct parser *parser,
    const CHAR *pos )
{
    const CHAR *p;

    for (p = pos; !is_eof(parser, p); p++)
    {
        switch(*p)
        {
        case '\r':
            continue;

        case '\n':
            parser->line_pos++;
            parser->start = p + 1;
            set_state(parser, LEADING_SPACES);
            return p + 1;

        case '\\':
            continue;

        case ';':
            push_state(parser, EOL_BACKSLASH);
            set_state(parser, COMMENT);
            return p + 1;

        default:
            if (isspace((unsigned char)*p))
                continue;
            push_token(parser, p);
            pop_state(parser);
            return p;
        }
    }
    parser->start = p;
    pop_state(parser);

    return p;
}
Ejemplo n.º 18
0
static int handle_token_lbrace( Token * tk, Parser * parser, FILE * out )
{
	if( !tk || !parser || !out )
		return 0;

	++parser->last_brace_indent;

	if( parser->last_brace_indent >= parser->stack_size
	    && !enlarge_stack( &(parser->brace_indent),
		                &(parser->stack_size)  ) )
	{
		return 0;	
	}

	parser->brace_indent[parser->last_brace_indent]
		= parser->indent;

	if( !parser->empty_line 
	    && parser->prev_tk.type != ASSIGN_OP )
	{
		putc( '\n', out );
		parser->empty_line = 1;
	}

	// insert a space after '=' in array initialization
	if( parser->prev_tk.type == ASSIGN_OP )
	{
		putc( ' ', out );
	}

	if( !push_indent( parser->indent, out ) )
		return 0;

	if( !push_token( tk, out ) )
		return 0;

	parser->empty_line = 0;
	parser->in_branch = 0;
	++parser->indent;
	parser->un_op = 0;
	return 1;
}
Ejemplo n.º 19
0
static int handle_token_semicolon( Token * tk, Parser * parser, FILE * out )
{
	if( !tk || !parser || !out )
		return 0;

	if( parser->empty_line
	    && !push_indent( parser->indent, out ) )
		return 0;

	if( parser->in_branch )	
	{
		if( parser->parens_closed ) {
			parser->in_branch = 0;
		}
		else if( parser->empty_line ) {
			int i;
			for( i = 4; i > 0; --i )
				putc( ' ', out );
		}
	}

	if( !push_token( tk, out ) )
		return 0;

	if( !parser->in_branch )
	{
		if( parser->last_brace_indent )
			parser->indent = parser->brace_indent[parser
				->last_brace_indent] + 1;
		else
			parser->indent = 0;
	}

	parser->empty_line = 0;
	parser->un_op = 0;

	return 1;
}
Ejemplo n.º 20
0
static int handle_token_case_kw( Token * tk, Parser * parser, FILE * out )
{
	if( !tk || !parser || !out )
		return 0;
	if( !parser->empty_line ) {
		putc( '\n', out );
		parser->empty_line = 1;
	}

	parser->indent = parser->brace_indent[parser->last_brace_indent];

	if( parser->empty_line
	    && !push_indent( parser->indent, out ))
		return 0;

	if( !push_token( tk, out ) )
		return 0;

	parser->empty_line = 0;
	++parser->indent;
	parser->un_op = 0;
	return 1;
}
Ejemplo n.º 21
0
static double eval_fact(const char *buf, int *pos) /* fact = item^fact | item */
{
	double rv = 0.0, lhs, rhs;
	Token tok;
	
	DB(printf("-- eval_fact(\"%s\", &pos=%p pos=%d)\n", buf+*pos, pos, *pos));
	lhs = eval_item(buf, pos);
	if(G_eval_error)
		return rv;
	DB(printf("-- fact lhs=%f\n", lhs));
	tok = pull_token(buf, pos);
	if(G_eval_error)
		return rv;
	DB(printf("-- fact token type '%c' = ", tok.type));
	switch(tok.type)
	{
	case '\0':
		DB(printf("END OF BUFFER\n"));
		rv = lhs;
		break;
	case '^': /* exponentiation */
		rhs = eval_fact(buf, pos);
		DB(printf("%f^%f", lhs, rhs));
		if(G_eval_error)
			return rv;
		rv = pow(lhs, rhs);
		DB(printf("=%f\n", rv));
		break;
	default:
		DB(printf("PUSHBACK\n"));
		push_token(tok);
		rv = lhs;
	}
	
	DB(printf("-- fact rv=%f\n", rv));
	return rv;
}
Ejemplo n.º 22
0
static int handle_token_cond( Token * tk, Parser * parser, FILE * out )
{
	if( !tk || !parser || !out )
		return 0;
	if( tk->type == IF_KW && parser->prev_tk.type == ELSE_KW ) {
		putc( ' ', out );	
	}
	else if( !parser->empty_line ) {
		putc( '\n', out );
		parser->empty_line = 1;
	}
	if( parser->in_branch
	    && parser->prev_tk.type != ELSE_KW )
	{
		++parser->indent;
	}
	else {
		parser->in_branch = 1;
	}
	if( tk->type != ELSE_KW )
		parser->parens_closed = 0;
	else {
		parser->parens_closed = 1;
	}
	
	if( parser->prev_tk.type != ELSE_KW
	    && parser->empty_line
	    && !push_indent( parser->indent, out ) )
		return 0;

	if( !push_token( tk, out ) )
		return 0;

	parser->empty_line = 0;
	parser->un_op = 0;
	return 1;
}
Ejemplo n.º 23
0
static double eval_item(const char *buf, int *pos) /* item = -item | +item | int | var | (expr) */
{
	double rv = 0.0;
	void *data;
	int tlen, nargs, xargs;
	DB(int i);
	FunctionPtr fn;
	DB(char *fname);
	Token tok;
	
	DB(printf("-- eval_item(\"%s\", &pos=%p pos=%d)\n", buf+*pos, pos, *pos));
	tok = pull_token(buf, pos);
	if(G_eval_error)
		return rv;
	DB(printf("-- item token type '%c' = ", tok.type));
	switch(tok.type)
	{
	case '+': /* positive */
		DB(printf("positive\n"));
		rv = eval_fact(buf, pos);
		break;
	case '-': /* negative */
		DB(printf("negative\n"));
		rv = -eval_fact(buf, pos);
		break;
	case 'v': /* variable */
		DB(printf("variable name '%s'=%f\n", tok.str, tok.value));
		rv = tok.value;
		break;
	case 'f': /* function */
		DB(printf("function name '%s'=%p(%d)\n", tok.str, tok.fn, tok.args));
		tlen = tok.args;
		nargs = tok.args;
		xargs = nargs;
		data = tok.data;
		fn = tok.fn;
		DB(fname = tok.str);
		tok = pull_token(buf, pos);
		if(G_eval_error == 0)
		{
			if(tok.type != '(')
				G_eval_error = EVAL_SYNTAX_ERROR;
			else
			{
				double *targ = NULL;
				
				if(tlen > 0)
				{
					targ = (double*)lalloc(sizeof(double)*tlen);
					if(targ == NULL)
					{
						G_eval_error = EVAL_MEM_ERROR;
						break;
					}
				}else
					tlen = 0;
				if(eval_args(buf, pos, &nargs, &targ, &tlen, 0))
					break;
				DB(printf("-- item %d arguments\n", nargs));
				if(xargs < 0)
				{
					if(nargs < 1)
					{
						DB(printf("-- item too few arguments\n"));
						G_eval_error = EVAL_ARGS_ERROR;
						break;
					}
				}else if(nargs != xargs)
				{
					DB(printf("-- item bad argument count (%d) need %d\n",
						nargs, xargs));
					G_eval_error = EVAL_ARGS_ERROR;
					break;
				}
				tok = pull_token(buf, pos);
				if(tok.type != ')')
					G_eval_error = EVAL_SYNTAX_ERROR;
				else if(G_eval_error == 0)
				{
					DB(printf("-- call function [%p] with %d args [%p]\n",
						fn, nargs, targ));
					DB(printf("-- %s(%f", fname, targ[0]));
					DB(for(i=1;i<nargs;i++)printf(",%f",targ[i]));
					DB(printf(")\n"));
					if(fn(nargs, targ, &rv, data) != 0)
						G_eval_error = EVAL_FUNCTION_ERROR;
					DB(printf("-- rv = %f\n", rv));
				}
			}
		}
		break;
	case 'n': /* number */
		DB(printf("number value '%s'=%f\n", tok.str, tok.value));
		rv = tok.value;
		break;
	case '(':
		DB(printf("start grouping\n"));
		rv = eval_expr(buf, pos);
		tok = pull_token(buf, pos);
		if(tok.type != ')')
			G_eval_error = EVAL_SYNTAX_ERROR;
		break;
	default:
		DB(printf("PUSHBACK\n"));
		push_token(tok);
	}
Ejemplo n.º 24
0
static double eval_term(const char *buf, int *pos) /* term = fact*term | fact/term | fact */
{
	double rv = 0.0, lhs, rhs;
	Token tok;
	
	DB(printf("-- eval_term(\"%s\", &pos=%p pos=%d)\n", buf+*pos, pos, *pos));
	lhs = eval_fact(buf, pos);
	if(G_eval_error)
		return rv;
	DB(printf("-- term lhs=%f\n", lhs));
	tok = pull_token(buf, pos);
	if(G_eval_error)
		return rv;
	DB(printf("-- term token type '%c' = ", tok.type));
	switch(tok.type)
	{
	case '\0':
		DB(printf("END OF BUFFER\n"));
		rv = lhs;
		break;
	case '*': /* multiplication */
		rhs = eval_term(buf, pos);
		DB(printf("%f*%f", lhs, rhs));
		rv = lhs*rhs;
		DB(printf("=%f\n", rv));
		break;
	case '/': /* division */
		rhs = eval_term(buf, pos);
		DB(printf("%f/%f", lhs, rhs));
		if(rhs == 0.0)
		{
			DB(printf(" DIVISION BY ZERO\n"));
			G_eval_error = EVAL_DIVIDE_BY_ZERO;
		}else
		{
			rv = lhs/rhs;
			DB(printf("=%f\n", rv));
		}
		break;
	case '\\': /* modulo division */
		rhs = eval_term(buf, pos);
		DB(printf("%f%%%f", lhs, rhs));
		if(rhs == 0.0)
		{
			DB(printf(" DIVISION BY ZERO\n"));
			G_eval_error = EVAL_DIVIDE_BY_ZERO;
		}else
		{
			rv = fmod(lhs, rhs);
			DB(printf("=%f\n", rv));
		}
		break;
	default:
		DB(printf("PUSHBACK\n"));
		push_token(tok);
		rv = lhs;
	}
	
	DB(printf("-- term rv=%f\n", rv));
	return rv;
}
Ejemplo n.º 25
0
static int handle_token_ident( Token * tk, Parser * parser, FILE * out )
{
	if( !tk || !parser || !out )
		return 0;

	if( !parser->in_branch ) {

		if( !parser->empty_line ) {

			if( parser->prev_tk.type == LBRACE
			    || parser->prev_tk.type == SEMICOLON )
			{
				putc( '\n', out );
				parser->empty_line = 1;
			}
			else if( parser->prev_tk.type != LPAREN
			         && parser->prev_tk.type != LBRACKET
			         && parser->prev_tk.type != STRUCT_SEP
			         && !parser->un_op )
			{
				putc( ' ', out );
			}
		}
	}
	else if( parser->parens_closed ) {

		if( parser->prev_tk.type == RPAREN
		    && !parser->empty_line )
		{
			putc( '\n', out );
			parser->empty_line = 1;
		}
		parser->in_branch = 0;
		++parser->indent;
	}


	if( parser->empty_line
	    && !push_indent( parser->indent, out ) )
	{
		return 0;
	}


	if( parser->in_branch
	    && !parser->parens_closed
	    && parser->empty_line ) 
	{
		int i;
		for( i = 4; i > 0; --i )
			putc( ' ', out );
	}
		

	if( !push_token( tk, out ) )
		return 0;

	parser->empty_line = 0;
	parser->un_op = 0;
	return 1;
}
Ejemplo n.º 26
0
static int handle_token_lparen( Token * tk, Parser * parser, FILE * out )
{
	if( !tk || !parser || !out )
		return 0;
	
	if( parser->in_branch ) {

		if( parser->parens_closed ) {

			if( !parser->empty_line ) {
				putc( '\n', out );
				parser->empty_line = 1;
			}
			++parser->indent;

			if( !push_indent( parser->indent, out ) )
				return 0;
			parser->in_branch = 0;
		}
		else {
			++parser->paren_depth;

			if( parser->empty_line ) {

				if( !push_indent( parser->indent, out ) )
					return 0;

				int i;
				for( i = 4; i > 0; --i )
					putc( ' ', out );
			}
		}
	}
	else
	{
		if( !parser->empty_line ) {

			if( parser->prev_tk.type == LBRACE
			    || parser->prev_tk.type == RBRACE
			    || parser->prev_tk.type == SEMICOLON )
			{
				putc('\n', out );
				parser->empty_line = 1;
			}
		}

		if( parser->empty_line
		    && !push_indent( parser->indent, out ) )
			return 0;
	}
	
	/* putting space before token if needed */
	if( !parser->empty_line
	    && parser->prev_tk.type != LPAREN
	    && parser->prev_tk.type != LBRACKET
	    && parser->prev_tk.type != SEMICOLON
	    && !parser->un_op )
	{
		putc( ' ', out );
	}

	if( !push_token( tk, out ) )
		return 0;

	parser->empty_line = 0;
	parser->un_op = 0;
	return 1;
}
Ejemplo n.º 27
0
static int handle_token_op( Token * tk, Parser * parser, FILE * out )
{
	if( !tk || !parser || !out )
		return 0;

	if( !parser->empty_line 
	    && (!parser->in_branch || !parser->parens_closed) )
	{
		if( is_incr_or_decr( tk ) ) {

			if( !parser->un_op
			    && parser->prev_tk.type != LPAREN
			    && parser->prev_tk.type != RPAREN
			    && parser->prev_tk.type != LBRACKET
			    && parser->prev_tk.type != RBRACKET
			    && parser->prev_tk.type != IDENT
			    && parser->prev_tk.type != NUM_CONST
			    && parser->prev_tk.type != CHR_LIT
			    && parser->prev_tk.type != STR_LIT )
			{
				putc( ' ', out );
			}
		}
		else if( is_unary_op( tk ) ) {

			if( is_incr_or_decr( &(parser->prev_tk ) ) ) {
				putc( ' ', out );
			}
			else if( !parser->un_op
			         && parser->prev_tk.type != LPAREN
			         && parser->prev_tk.type != LBRACKET
			         && !(parser->prev_tk.start[0] == '*'
			              && tk->start[0] == '*') )
			{
				putc( ' ', out );
			}
		}
		else if( parser->un_op
		         || parser->prev_tk.type == RPAREN
		         || parser->prev_tk.type == RBRACKET
		         || parser->prev_tk.type == IDENT
		         || parser->prev_tk.type == NUM_CONST
		         || parser->prev_tk.type == CHR_LIT
		         || parser->prev_tk.type == STR_LIT)
		{
			putc( ' ', out );
		}
	}

	/* find out if this is an unary op */
	if( is_incr_or_decr( tk ) ) {
		parser->un_op = 1;
	}
	else if( is_unary_op( tk )
	         && !is_incr_or_decr( &(parser->prev_tk) )
	         && parser->prev_tk.type != IDENT
	         && parser->prev_tk.type != NUM_CONST
	         && parser->prev_tk.type != CHR_LIT
	         && parser->prev_tk.type != STR_LIT
	         && parser->prev_tk.type != RBRACKET )
	{
		if( parser->prev_tk.type != RPAREN ) {
			parser->un_op = 1;
		}
		else if( parser->in_branch ) {
			parser->un_op = 1;
		}
	}
	else {
		parser->un_op = 0;
	}

	if( parser->in_branch ) {

		if( parser->parens_closed )
		{
			if( !parser->empty_line )
			{
				putc( '\n', out );
				parser->empty_line = 1;
			}

			parser->in_branch = 0;
			++parser->indent;
		}
	}
	else if( parser->prev_tk.type == SEMICOLON ) {
		putc( '\n', out );
		parser->empty_line = 1;
	}

	if( parser->empty_line
	    && !push_indent( parser->indent, out ))
		return 0;

	if( parser->in_branch
	    && !parser->parens_closed
	    && parser->empty_line ) 
	{
		int i;
		for( i = 4; i > 0; --i )
			putc( ' ', out );
	}
	if( !push_token( tk, out ) )
		return 0;
	parser->empty_line = 0;
	return 1;
}
Ejemplo n.º 28
0
static int handle_token( Token * tk, Parser * parser, FILE * out)
{
	if( tk->type == PREPROC ) {

		if( !push_token( tk, out ) ) {
			return 0;
		}
		parser->empty_line = 0;
		return 1;
	}
	else if( tk->type == COMMENT
 		 || tk->type == MUL_COMMENT )
	{
		return handle_token_comment( tk, parser, out );	
	}
	else if( tk->type == IF_KW 
	         || tk->type == ELSE_KW
	         || tk->type == FOR_KW
	         || tk->type == WHILE_KW
	         || tk->type == DO_KW
	         || tk->type == SWITCH_KW )
	{
		return handle_token_cond( tk, parser, out );
	}
	else if( tk->type == CASE_KW
	         || tk->type == DEFAULT_KW )
	{
		return handle_token_case_kw( tk, parser, out );
	}
	else if( tk->type == IDENT 
	         || tk->type == NUM_CONST
	         || tk->type == CHR_LIT
	         || tk->type == STR_LIT )
	{
		return handle_token_ident( tk, parser, out );
	}
	else if( tk->type == LPAREN ) {
		return handle_token_lparen( tk, parser, out );
	}
	else if( tk->type == RPAREN ) {
		return handle_token_rparen( tk, parser, out );
	}
	else if( tk->type == LBRACE ) {
		return handle_token_lbrace( tk, parser, out );
	}
	else if( tk->type == RBRACE ) {
		return handle_token_rbrace( tk, parser, out );
	}
	else if( tk->type == LBRACKET
	         || tk->type == RBRACKET )
	{
		return handle_token_bracket( tk, parser, out );
	}
	else if( tk->type == ASSIGN_OP ) {
		return handle_token_assign_op( tk, parser, out );
	}
	else if( tk->type == OP ) {
		return handle_token_op( tk, parser, out );
	}
	else if( tk->type == ELLIPSIS ) {
		return handle_token_ellipsis( tk, parser, out );
	}
	else if( tk->type == STRUCT_SEP
	         || tk->type == COMMA )
	{
		return handle_token_sep( tk, parser, out );
	}
	else if( tk->type == SEMICOLON ) {
		return handle_token_semicolon( tk, parser, out );
	}
	else if( tk->type == COLON
	         || tk->type == QUESTION )
	{
		return handle_token_colon( tk, parser, out );
	}
	else if( tk->type == EOL_TOK ) {
		return handle_token_eol( tk, parser, out );
	}
	else
		return 0;
	return 1;
}
Ejemplo n.º 29
0
static int
do_parse (void)
{
  int tok;
  int unit1;
  int continue_ulist;
  char *start;

  unit_count = 0;

  start = p;

  /* Parse the string.  First, let's look for a default.  */
  tok = next_token ();
  switch (tok)
    {
    case NATIVE:
      endian = GFC_CONVERT_NATIVE;
      break;

    case SWAP:
      endian = GFC_CONVERT_SWAP;
      break;

    case BIG:
      endian = GFC_CONVERT_BIG;
      break;

    case LITTLE:
      endian = GFC_CONVERT_LITTLE;
      break;

    case INTEGER:
      /* A leading digit means that we are looking at an exception.
	 Reset the position to the beginning, and continue processing
	 at the exception list.  */
      p = start;
      goto exceptions;
      break;

    case END:
      goto end;
      break;

    default:
      goto error;
      break;
    }

  tok = next_token ();
  switch (tok)
    {
    case ';':
      def = endian;
      break;

    case ':':
      /* This isn't a default after all.  Reset the position to the
	 beginning, and continue processing at the exception list.  */
      p = start;
      goto exceptions;
      break;

    case END:
      def = endian;
      goto end;
      break;

    default:
      goto error;
      break;
    }

 exceptions:

  /* Loop over all exceptions.  */
  while(1)
    {
      tok = next_token ();
      switch (tok)
	{
	case NATIVE:
	  if (next_token () != ':')
	    goto error;
	  endian = GFC_CONVERT_NATIVE;
	  break;

	case SWAP:
	  if (next_token () != ':')
	    goto error;
	  endian = GFC_CONVERT_SWAP;
	  break;

	case LITTLE:
	  if (next_token () != ':')
	    goto error;
	  endian = GFC_CONVERT_LITTLE;
	  break;

	case BIG:
	  if (next_token () != ':')
	    goto error;
	  endian = GFC_CONVERT_BIG;
	  break;

	case INTEGER:
	  push_token ();
	  break;

	case END:
	  goto end;
	  break;

	default:
	  goto error;
	  break;
	}
      /* We arrive here when we want to parse a list of
	 numbers.  */
      continue_ulist = 1;
      do
	{
	  tok = next_token ();
	  if (tok != INTEGER)
	    goto error;

	  unit1 = unit_num;
	  tok = next_token ();
	  /* The number can be followed by a - and another number,
	     which means that this is a unit range, a comma
	     or a semicolon.  */
	  if (tok == '-')
	    {
	      if (next_token () != INTEGER)
		goto error;

	      mark_range (unit1, unit_num);
	      tok = next_token ();
	      if (tok == END)
		goto end;
	      else if (tok == ';')
		continue_ulist = 0;
	      else if (tok != ',')
		goto error;
	    }
	  else
	    {
	      mark_single (unit1);
	      switch (tok)
		{
		case ';':
		  continue_ulist = 0;
		  break;

		case ',':
		  break;

		case END:
		  goto end;
		  break;

		default:
		  goto error;
		}
	    }
	} while (continue_ulist);
    }
 end:
  return 0;
 error:
  def = GFC_CONVERT_NONE;
  return -1;
}
Ejemplo n.º 30
0
/*
 * If the file contains a control character, we can get multiple
 * comments per line.
 */
static inline void push_comment(fb_parser_t *P, const char *first, const char *last)
{
    if (P->doc_mode) {
        push_token(P, tok_kw_doc_comment, first, last);
    }
}