示例#1
0
文件: parse.c 项目: guptaak2/CSC209
int printexpr(struct expr *e, char **s, int ssize)
{
    if (e->subexpr) {
	if (ssize > 0) {
	    ssize--;
	    *(*s)++ = '(';
	}
	ssize = printexpr(e->subexpr->a, s, ssize);
	if (ssize > 0) {
	    ssize--;
	    *(*s)++ = printop(e->subexpr->op);
	}
	ssize = printexpr(e->subexpr->b, s, ssize);
	if (ssize > 0) {
	    ssize--;
	    *(*s)++ = ')';
	}
	*(*s) = '\0';
	return(ssize);
    } else {
	char buf[40];
	int len;
	sprintf(buf, "%d", e->val);
	len = strlen(buf);
	if (len >= ssize)
	    return(0);
	strcpy(*s, buf);
	*s += len;
	return(ssize - len);
    }
}
示例#2
0
void ppexpr(TOKEN tok)              /* print an expression in prefix form */
  { if ( (long) tok <= 0 )
      { printf("ppexpr called with bad pointer %ld\n", (long)tok);
	return; };
    printexpr(tok, 0);
    printf("\n");
  }
示例#3
0
void printexpr(TOKEN tok, int col)     /* print an expression in prefix form */
  { TOKEN opnds; int nextcol, start, i;
    if (PRINTEXPRDEBUG != 0)
      { printf ("printexpr: col %d\n", col);
        dbugprinttok(tok);
      };
    if (tok->tokentype == OPERATOR)
      { printf ("(%s", opprint[tok->whichval]);
        nextcol = col + 2 + opsize[tok->whichval];
        opnds = tok->operands;
	start = 0;
	while (opnds != NULL)
	  { if (start == 0) 
	       printf(" ");
	       else { printf("\n");
		      for (i = 0; i < nextcol; i++) printf(" ");
		    }
	    printexpr(opnds, nextcol);
	    if ( opnds->tokentype == IDENTIFIERTOK && nextcol < 60 )
	       nextcol = nextcol + 1 + strlength(opnds->stringval);
	       else start = 1;
	    opnds = opnds->link;
	  }
        printf (")");
      }
      else printtok(tok);
  }
示例#4
0
文件: test.c 项目: radarsat1/expr
int test2()
{
    const char str[] = "y=26*2/2+x*30/(20*1)";
    mapper_expr e = mapper_expr_new_from_string(str, 0, 0, 1);
    printf("\nParsing %s\n", str);
    if (!e) { printf("Test FAILED.\n"); return 1; }
#ifdef DEBUG
    printexpr("Parser returned: ", e);
#endif
    printf("\n");

    int inp, outp;
    inp = 3;
    mapper_expr_evaluate(e, &inp, &outp);

    printf("Evaluate with x=%d: %d (expected: %d)\n",
           inp, outp,
           26*2/2+inp*30/(20*1));

    inp = 321;
    mapper_expr_evaluate(e, &inp, &outp);

    printf("Evaluate with x=%d: %d (expected: %d)\n",
           inp, outp,
           26*2/2+inp*30/(20*1));

    mapper_expr_free(e);

    return 0;
}
示例#5
0
int main()
{
    const char str[] = "y=26*2/2+log10(pi)+2.*pow(2,1*(3+7*.1)*1.1+x{0}[0])*3*4+cos(2.)";
    //const char str[] = "y=x?1:2";
    int input_history_size, output_history_size;
    mapper_expr e = mapper_expr_new_from_string(str, 'f', 'f', 1, 1,
                                                &input_history_size,
                                                &output_history_size);
    printf("Parsing %s\n", str);
    if (!e) { printf("Test FAILED.\n"); return 1; }
#ifdef DEBUG
    printexpr("Parser returned: ", e);
#endif
    printf("\n");

    float inp = 3.0, outp;

    // create signal_history structures
    mapper_signal_history_t inh, outh;
    inh.type = 'f';
    inh.size = 1;
    inh.length = 1;
    inh.value = &inp;
    inh.timetag = calloc(1, sizeof(mapper_timetag_t));
    inh.position = 0;

    outh.type = 'f';
    outh.size = 1;
    outh.length = 1;
    outh.value = &outp;
    outh.timetag = calloc(1, sizeof(mapper_timetag_t));
    outh.position = -1;

    int iterations = 1000000;
    int results = 0;
    double then = get_current_time();
    printf("Calculate expression %i times... ", iterations);
    while (iterations--) {
        results += mapper_expr_evaluate(e, &inh, &outh);
    }
    double now = get_current_time();
    printf("%f seconds.\n", now-then);

    if (results) {
        printf("Evaluate with x=%f: %f (expected: %f)\n",
               inp, outp,
               26*2/2+log10f(M_PI)+2.f*powf(2,1*(3+7*.1f)*1.1f+inp)*3*4+cosf(2.0f));
    }
    else
        printf("NO results.\n");

    mapper_expr_free(e);
    free(inh.timetag);
    free(outh.timetag);

    return 0;
}
示例#6
0
文件: test.c 项目: radarsat1/expr
int test1()
{
    const char str[] = "y=26*2/2+log10(pi)+2.*pow(2,1*(3+7*.1)*1.1+x{-6*2+12})*3*4+cos(2.)";
    mapper_expr e = mapper_expr_new_from_string(str, 1, 1, 1);
    printf("Parsing %s\n", str);
    if (!e) { printf("Test FAILED.\n"); return 1; }
#ifdef DEBUG
    printexpr("Parser returned: ", e);
#endif
    printf("\n");

    float inp, outp;
    inp = 3.0;
    mapper_expr_evaluate(e, &inp, &outp);

    printf("Evaluate with x=%f: %f (expected: %f)\n",
           inp, outp,
           26*2/2+log10f(M_PI)+2.f*powf(2,1*(3+7*.1f)*1.1f+inp)*3*4+cosf(2.0f));

    mapper_expr_free(e);

    return 0;
}
示例#7
0
/*
 * parse_header
 *
 * parse the header entry of the GNU MO file and
 * extract the src encoding and the plural information of the MO file
 */
static int
parse_header(const char *header, Msg_g_node *gmnp)
{
	char	*charset = NULL;
	char	*charset_str;
	size_t	len;
	char	*nplurals_str, *plural_str;
	plural_expr_t	plural;
	char	*p, *q;
	unsigned int	nplurals;
	int	ret;

#ifdef GETTEXT_DEBUG
	(void) printf("*************** parse_header(\"%s\", 0x%p)\n",
		header ? header : "(null)", (void *)gmnp);
	printgnumsg(gmnp, 0);
#endif

	if (!header) {
		gmnp->src_encoding = (char *)nullstr;
		gmnp->nplurals = 2;
		gmnp->plural = NULL;
#ifdef GETTEXT_DEBUG
		(void) printf("*************** exiting parse_header\n");
		(void) printf("no header\n");
#endif

		return (0);
	}

	charset_str = strstr(header, CHARSET_MOD);
	if (!charset_str) {
		gmnp->src_encoding = (char *)nullstr;
	} else {
		p = charset_str + CHARSET_LEN;
		q = p;
		while ((*q != ' ') && (*q != '\t') &&
			(*q != '\n')) {
			q++;
		}
		len = q - p;
		if (len > 0) {
			charset = (char *)malloc(len + 1);
			if (!charset) {
				gmnp->src_encoding = (char *)nullstr;
				gmnp->nplurals = 2;
				gmnp->plural = NULL;
				return (-1);
			}
			(void) memcpy(charset, p, len);
			charset[len] = '\0';
			gmnp->src_encoding = charset;
		} else {
			gmnp->src_encoding = (char *)nullstr;
		}
	}

	nplurals_str = strstr(header, NPLURALS_MOD);
	plural_str = strstr(header, PLURAL_MOD);
	if (!nplurals_str || !plural_str) {
		/* no valid plural specification */
		gmnp->nplurals = 2;
		gmnp->plural = NULL;
#ifdef GETTEXT_DEBUG
		(void) printf("*************** exiting parse_header\n");
		(void) printf("no plural entry\n");
#endif
		return (0);
	} else {
		p = nplurals_str + NPLURALS_LEN;
		while (*p && isspace((unsigned char)*p)) {
			p++;
		}
		nplurals = (unsigned int)strtol(p, &q, 10);
		if (p != q) {
			gmnp->nplurals = nplurals;
		} else {
			gmnp->nplurals = 2;
		}

		p = plural_str + PLURAL_LEN;
#ifdef GETTEXT_DEBUG
		(void) printf("plural_str: \"%s\"\n", p);
#endif

		ret = plural_expr(&plural, (const char *)p);
		if (ret == 0) {
			/* parse succeeded */
			gmnp->plural = plural;
#ifdef GETTEXT_DEBUG
		(void) printf("*************** exiting parse_header\n");
		(void) printf("charset: \"%s\"\n",
			charset ? charset : "(null)");
		printexpr(plural, 0);
#endif
			return (0);
		} else if (ret == 1) {
			/* parse error */
			gmnp->nplurals = 2;
			gmnp->plural = NULL;
			return (0);
		} else {
			/* fatal error */
			if (charset)
				free(charset);
			gmnp->src_encoding = (char *)nullstr;
			gmnp->nplurals = 2;
			gmnp->plural = NULL;
			return (-1);
		}
	}
	/* NOTREACHED */
}
示例#8
0
int parse_and_eval(int expectation)
{
    // clear output arrays
    int i;
    for (i = 0; i < DEST_ARRAY_LEN; i++) {
        dest_int[i] = 0;
        dest_float[i] = 0.0f;
        dest_double[i] = 0.0;
    }

    eprintf("***************** Expression %d *****************\n",
            expression_count++);
    eprintf("Parsing string '%s'\n", str);
    e = mapper_expr_new_from_string(str, num_sources, src_types, src_lengths,
                                    outh.type, outh.length);
    if (!e) {
        eprintf("Parser FAILED.\n");
        goto fail;
    }
    for (i = 0; i < num_sources; i++) {
        inh[i].size = mapper_expr_input_history_size(e, i);
    }
    outh.size = mapper_expr_output_history_size(e);

    if (mapper_expr_num_variables(e) > MAX_VARS) {
        eprintf("Maximum variables exceeded.\n");
        goto fail;
    }

    // reallocate variable value histories
    for (i = 0; i < e->num_variables; i++) {
        eprintf("user_var[%d]: %p\n", i, &user_vars[i]);
        mhist_realloc(&user_vars[i], e->variables[i].history_size,
                      sizeof(double), 0);
    }
    user_vars_p = user_vars;

#ifdef DEBUG
    if (verbose) {
        char str[128];
        snprintf(str, 128, "Parser returned %d tokens:", e->length);
        printexpr(str, e);
    }
#endif

    token_count += e->length;

    eprintf("Try evaluation once... ");
    if (!mapper_expr_evaluate(e, inh_p, &user_vars_p, &outh, &tt_in, typestring)) {
        eprintf("FAILED.\n");
        goto fail;
    }
    eprintf("OK\n");

    then = current_time();
    eprintf("Calculate expression %i times... ", iterations);
    i = iterations-1;
    while (i--) {
        mapper_expr_evaluate(e, inh_p, &user_vars_p, &outh, &tt_in, typestring);
    }
    now = current_time();
    eprintf("%g seconds.\n", now-then);
    total_elapsed_time += now-then;

    if (verbose) {
        printf("Got:      ");
        print_value(typestring, outh.length, outh.value, outh.position);
        printf(" \n");
    }
    else
        printf(".");

    mapper_expr_free(e);

    return expectation != EXPECT_SUCCESS;

fail:
    return expectation != EXPECT_FAILURE;
}
示例#9
0
void printexpr(FILE *out, const struct expr *expr, int lvl) {
	int i;
	switch (expr->type) {
		case EXPR_NUM:
			switch (expr->special) {
				case 1:
					fprintf(out, "%s", cbtarg);
					break;
				case 2:
					fprintf(out, "%s", cctarg);
					break;
				case 3:
					fprintf(out, "%s", cmem);
					break;
				default:
					fprintf(out, "%s", cnum);
					break;
			}
			if (expr->num1 & 1ull << 63)
				fprintf(out, "-%#llx", -expr->num1);
			else
				fprintf(out, "%#llx", expr->num1);
			return;
		case EXPR_ID:
			switch (expr->special) {
				case 1:
					fprintf(out, "%s%s", cnum, expr->str);
					break;
				case 2:
					fprintf(out, "%s%s", cunk, expr->str);
					break;
				default:
					fprintf(out, "%s%s", cname, expr->str);
					break;
			}
			return;
		case EXPR_LABEL:
			switch (expr->special) {
				case 2:
					fprintf(out, "%s#%s", cunk, expr->str);
					break;
				default:
					fprintf(out, "%s#%s", cnum, expr->str);
					break;
			}
			return;
		case EXPR_REG:
			switch (expr->special) {
				case 1:
					fprintf(out, "%s$%s", creg1, expr->str);
					break;
				case 2:
					fprintf(out, "%s$%s", cunk, expr->str);
					break;
				default:
					fprintf(out, "%s$%s", creg0, expr->str);
					break;
			}
			return;
		case EXPR_MEM:
			if (expr->str)
				fprintf(out, "%s%s[", cmem, expr->str);
			printexpr(out, expr->expr1, 0);
			if (expr->str)
				fprintf(out, "%s]", cmem);
			return;
		case EXPR_VEC:
			fprintf(out, "%s{", cnorm);
			for (i = 0; i < expr->vexprsnum; i++) {
				fprintf(out, " ");
				printexpr(out, expr->vexprs[i], 0);
			}
			fprintf(out, " %s}", cnorm);
			return;
		case EXPR_DISCARD:
			fprintf(out, "%s#", creg0);
			return;
		case EXPR_BITFIELD:
			fprintf(out, "%s%lld:%lld", cnum, expr->num1, expr->num2);
			return;
		case EXPR_NEG:
			fprintf(out, "%s-", cmem);
			printexpr(out, expr->expr1, 7);
			return;
		case EXPR_NOT:
			fprintf(out, "%s~", cmem);
			printexpr(out, expr->expr1, 7);
			return;
		case EXPR_MUL:
			if (lvl >= 7)
				fprintf(out, "%s(", cmem);
			printexpr(out, expr->expr1, 6);
			fprintf(out, "%s*", cmem);
			printexpr(out, expr->expr2, 7);
			if (lvl >= 7)
				fprintf(out, "%s)", cmem);
			return;
		case EXPR_DIV:
			if (lvl >= 7)
				fprintf(out, "%s(", cmem);
			printexpr(out, expr->expr1, 6);
			fprintf(out, "%s/", cmem);
			printexpr(out, expr->expr2, 7);
			if (lvl >= 7)
				fprintf(out, "%s)", cmem);
			return;
		case EXPR_ADD:
			if (lvl >= 6)
				fprintf(out, "%s(", cmem);
			printexpr(out, expr->expr1, 5);
			fprintf(out, "%s+", cmem);
			printexpr(out, expr->expr2, 6);
			if (lvl >= 6)
				fprintf(out, "%s)", cmem);
			return;
		case EXPR_SUB:
			if (lvl >= 6)
				fprintf(out, "%s(", cmem);
			printexpr(out, expr->expr1, 5);
			fprintf(out, "%s-", cmem);
			printexpr(out, expr->expr2, 6);
			if (lvl >= 6)
				fprintf(out, "%s)", cmem);
			return;
		case EXPR_SHL:
			if (lvl >= 5)
				fprintf(out, "%s(", cmem);
			printexpr(out, expr->expr1, 4);
			fprintf(out, "%s<<", cmem);
			printexpr(out, expr->expr2, 5);
			if (lvl >= 5)
				fprintf(out, "%s)", cmem);
			return;
		case EXPR_SHR:
			if (lvl >= 5)
				fprintf(out, "%s(", cmem);
			printexpr(out, expr->expr1, 4);
			fprintf(out, "%s>>", cmem);
			printexpr(out, expr->expr2, 5);
			if (lvl >= 5)
				fprintf(out, "%s)", cmem);
			return;
		case EXPR_AND:
			if (lvl >= 4)
				fprintf(out, "%s(", cmem);
			printexpr(out, expr->expr1, 3);
			fprintf(out, "%s&", cmem);
			printexpr(out, expr->expr2, 4);
			if (lvl >= 4)
				fprintf(out, "%s)", cmem);
			return;
		case EXPR_XOR:
			if (lvl >= 3)
				fprintf(out, "%s(", cmem);
			printexpr(out, expr->expr1, 2);
			fprintf(out, "%s^", cmem);
			printexpr(out, expr->expr2, 3);
			if (lvl >= 3)
				fprintf(out, "%s)", cmem);
			return;
		case EXPR_OR:
			if (lvl >= 2)
				fprintf(out, "%s(", cmem);
			printexpr(out, expr->expr1, 1);
			fprintf(out, "%s|", cmem);
			printexpr(out, expr->expr2, 2);
			if (lvl >= 2)
				fprintf(out, "%s)", cmem);
			return;
		case EXPR_PIADD:
			if (lvl >= 1)
				fprintf(out, "%s(", cmem);
			printexpr(out, expr->expr1, 0);
			fprintf(out, "%s++", cmem);
			printexpr(out, expr->expr2, 1);
			if (lvl >= 1)
				fprintf(out, "%s)", cmem);
			return;
		case EXPR_PISUB:
			if (lvl >= 1)
				fprintf(out, "%s(", cmem);
			printexpr(out, expr->expr1, 0);
			fprintf(out, "%s--", cmem);
			printexpr(out, expr->expr2, 1);
			if (lvl >= 1)
				fprintf(out, "%s)", cmem);
			return;
		case EXPR_SESTART:
			fprintf(out, "%s(", cmem);
			return;
		case EXPR_SEEND:
			fprintf(out, "%s)", cmem);
			return;
		default:
			assert(0);
	}
}