Ejemplo n.º 1
0
/* Helper to replace a connection's expression only if the given string
 * parses successfully. Returns 0 on success, non-zero on error. */
static int replace_expression_string(mapper_connection c,
                                     const char *expr_str,
                                     int *input_history_size,
                                     int *output_history_size)
{
    mapper_expr expr = mapper_expr_new_from_string(
        expr_str, c->props.src_type, c->props.dest_type,
        c->props.src_length, c->props.dest_length);

    if (!expr)
        return 1;

    *input_history_size = mapper_expr_input_history_size(expr);
    *output_history_size = mapper_expr_output_history_size(expr);

    if (c->expr)
        mapper_expr_free(c->expr);

    c->expr = expr;

    if (c->props.expression == expr_str)
        return 0;

    int len = strlen(expr_str);
    if (!c->props.expression || len > strlen(c->props.expression))
        c->props.expression = realloc(c->props.expression, len+1);

    /* Using strncpy() here causes memory profiling errors due to possible
     * overlapping memory (e.g. expr_str == c->props.expression). */
    memcpy(c->props.expression, expr_str, len);
    c->props.expression[len] = '\0';

    return 0;
}
Ejemplo n.º 2
0
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;
}
Ejemplo n.º 3
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;
}
Ejemplo n.º 4
0
/* Helper to replace a connection's expression only if the given string
 * parses successfully. Returns 0 on success, non-zero on error. */
static int replace_expression_string(mapper_connection c,
                                     mapper_signal s,
                                     const char *expr_str)
{
    mapper_expr expr = mapper_expr_new_from_string(
        expr_str, s->props.type=='f', c->props.dest_type=='f',
        s->props.length);

    if (!expr)
        return 1;

    if (c->expr)
        mapper_expr_free(c->expr);

    c->expr = expr;
    int len = strlen(expr_str)+1;
    if (!c->props.expression || len > strlen(c->props.expression))
        c->props.expression = realloc(c->props.expression, len);
    strncpy(c->props.expression, expr_str, len);
    return 0;
}
Ejemplo n.º 5
0
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;
}
Ejemplo n.º 6
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;
}