int pbc_param_init_i(pbc_param_ptr p, struct symtab_s *tab) { param_init(p); params *param = p->data; int err = 0; err += lookup_int(¶m->m, tab, "m"); err += lookup_int(¶m->t, tab, "t"); err += lookup_mpz(param->n, tab, "n"); err += lookup_mpz(param->n2, tab, "n2"); return err; }
int pbc_param_init_e(pbc_param_ptr par, struct symtab_s *tab) { e_init(par); e_param_ptr p = par->data; int err = 0; err += lookup_mpz(p->q, tab, "q"); err += lookup_mpz(p->r, tab, "r"); err += lookup_mpz(p->h, tab, "h"); err += lookup_mpz(p->a, tab, "a"); err += lookup_mpz(p->b, tab, "b"); err += lookup_int(&p->exp2, tab, "exp2"); err += lookup_int(&p->exp1, tab, "exp1"); err += lookup_int(&p->sign1, tab, "sign1"); err += lookup_int(&p->sign0, tab, "sign0"); return err; }
int pbc_param_init_d(pbc_param_ptr par, struct symtab_s *tab) { d_param_init(par); d_param_ptr p = par->data; char s[80]; int i, d; int err = 0; err += lookup_mpz(p->q, tab, "q"); err += lookup_mpz(p->n, tab, "n"); err += lookup_mpz(p->h, tab, "h"); err += lookup_mpz(p->r, tab, "r"); err += lookup_mpz(p->a, tab, "a"); err += lookup_mpz(p->b, tab, "b"); err += lookup_int(&p->k, tab, "k"); err += lookup_mpz(p->nk, tab, "nk"); err += lookup_mpz(p->hk, tab, "hk"); err += lookup_mpz(p->nqr, tab, "nqr"); d = p->k / 2; p->coeff = pbc_realloc(p->coeff, sizeof(mpz_t) * d); for (i=0; i<d; i++) { sprintf(s, "coeff%d", i); mpz_init(p->coeff[i]); err += lookup_mpz(p->coeff[i], tab, s); } return err; }
void eval_expr(expr *in, expr *out, symtab *table) { switch (in->type) { case NOTHING: break; case INTEGER: case REAL: case STRING: out->type = in->type; out->val = in->val; return; case INT_VAR: { int n; if (lookup_int(in->val.string, table, &n)) { out->type = INTEGER; out->val.integer = n; return; } else { fprintf(stderr, "undefined integer variable '%s'\n", in->val.string); exit(1); } } break; case REAL_VAR: { double d; if (lookup_real(in->val.string, table, &d)) { out->type = REAL; out->val.real = d; return; } else { fprintf(stderr, "undefined real variable '%s'\n", in->val.string); exit(1); } } break; case STR_VAR: { char *s = (char *)malloc(MAX_LINE); if (lookup_str(in->val.string, table, &s)) { out->type = STRING; out->val.string = s; return; } else { fprintf(stderr, "undefined string variable '%s'\n", in->val.string); exit(1); } } break; } switch (in->op) { case NOTHING: fprintf(stderr, "No operation for non-constant expression\n"); exit(1); case ADD: add_expr(in, out, table); break; case SUB: sub_expr(in, out, table); break; case EXPT: pow_expr(in, out, table); break; case MUL: mul_expr(in, out, table); break; case DIV: div_expr(in, out, table); break; case LT: eval_expr(in->arg1, out->arg1, table); eval_expr(in->arg2, out->arg2, table); out->type = INTEGER; out->val.integer = compare(out->arg1, out->arg2, LT); break; case LE: eval_expr(in->arg1, out->arg1, table); eval_expr(in->arg2, out->arg2, table); out->type = INTEGER; out->val.integer = compare(out->arg1, out->arg2, LE); break; case EQ: eval_expr(in->arg1, out->arg1, table); eval_expr(in->arg2, out->arg2, table); out->type = INTEGER; out->val.integer = compare(out->arg1, out->arg2, EQ); break; case GE: eval_expr(in->arg1, out->arg1, table); eval_expr(in->arg2, out->arg2, table); out->type = INTEGER; out->val.integer = compare(out->arg1, out->arg2, GE); break; case GT: eval_expr(in->arg1, out->arg1, table); eval_expr(in->arg2, out->arg2, table); out->type = INTEGER; out->val.integer = compare(out->arg1, out->arg2, GT); break; case NE: eval_expr(in->arg1, out->arg1, table); eval_expr(in->arg2, out->arg2, table); out->type = INTEGER; out->val.integer = compare(out->arg1, out->arg2, NE); break; /* Numerical functions */ case RAND: { expr arg; eval_expr(in->arg1, &arg, table); out->type = INTEGER; out->val.integer = rand() % arg.val.integer; } break; case SQRT: { expr arg; eval_expr(in->arg1, &arg, table); out->type = REAL; out->val.real = sqrt(arg.type == REAL ? arg.val.real : (double)arg.val.integer); } break; /* Casting */ case CEIL: { expr arg; eval_expr(in->arg1, &arg, table); out->type = INTEGER; out->val.integer = (int)ceil(arg.val.real); } break; case FLOOR: { expr arg; eval_expr(in->arg1, &arg, table); out->type = INTEGER; out->val.integer = (int)floor(arg.val.real); } break; case REAL_CAST: { expr arg; eval_expr(in->arg1, &arg, table); out->type = REAL; out->val.real = (double)arg.val.integer; } break; case ROUND: { expr arg; eval_expr(in->arg1, &arg, table); out->type = INTEGER; out->val.integer = (int)round(arg.val.real); } break; default: fprintf(stderr, "unrecognised operation\n"); exit(1); } }