~ubuntu-branches/ubuntu/quantal/gclcvs/quantal

« back to all changes in this revision

Viewing changes to gmp3/demos/expr/exprza.c

  • Committer: Bazaar Package Importer
  • Author(s): Camm Maguire
  • Date: 2004-06-24 15:13:46 UTC
  • Revision ID: james.westby@ubuntu.com-20040624151346-xh0xaaktyyp7aorc
Tags: 2.7.0-26
C_GC_OFFSET is 2 on m68k-linux

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* mpz expression evaluation */
 
2
 
 
3
/*
 
4
Copyright 2000, 2001 Free Software Foundation, Inc.
 
5
 
 
6
This file is part of the GNU MP Library.
 
7
 
 
8
The GNU MP Library is free software; you can redistribute it and/or modify
 
9
it under the terms of the GNU Lesser General Public License as published by
 
10
the Free Software Foundation; either version 2.1 of the License, or (at your
 
11
option) any later version.
 
12
 
 
13
The GNU MP Library is distributed in the hope that it will be useful, but
 
14
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 
15
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 
16
License for more details.
 
17
 
 
18
You should have received a copy of the GNU Lesser General Public License
 
19
along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
 
20
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 
21
MA 02111-1307, USA.
 
22
*/
 
23
 
 
24
#include <ctype.h>
 
25
#include <stdio.h>
 
26
#include "gmp.h"
 
27
#include "expr-impl.h"
 
28
 
 
29
 
 
30
/* No need to parse '-' since that's handled as an operator.
 
31
   This function also by mpq_expr_a, so it's not static.  */
 
32
size_t
 
33
mpexpr_mpz_number (mpz_ptr res, __gmp_const char *e, size_t elen, int base)
 
34
{
 
35
  char    *edup;
 
36
  size_t  i, ret;
 
37
  int     base_effective = (base == 0 ? 10 : base);
 
38
 
 
39
  i = 0;
 
40
  if (e[i] == '0')
 
41
    {
 
42
      i++;
 
43
      if (e[i] == 'x' || e[i] == 'b')
 
44
        i++;
 
45
    }
 
46
 
 
47
  for ( ; i < elen; i++)
 
48
    if (! isasciidigit_in_base (e[i], base_effective))
 
49
      break;
 
50
 
 
51
  edup = (*__gmp_allocate_func) (i+1);
 
52
  memcpy (edup, e, i);
 
53
  edup[i] = '\0';
 
54
 
 
55
  if (mpz_set_str (res, edup, base) == 0)
 
56
    ret = i;
 
57
  else
 
58
    ret = 0;
 
59
 
 
60
  (*__gmp_free_func) (edup, i+1);
 
61
  return ret;
 
62
}
 
63
 
 
64
 
 
65
int
 
66
mpz_expr_a (__gmp_const struct mpexpr_operator_t *table,
 
67
            mpz_ptr res, int base,
 
68
            __gmp_const char *e, size_t elen,
 
69
            mpz_srcptr var[26])
 
70
{
 
71
  struct mpexpr_parse_t  p;
 
72
 
 
73
  p.table = table;
 
74
  p.res = (mpX_ptr) res;
 
75
  p.base = base;
 
76
  p.e = e;
 
77
  p.elen = elen;
 
78
  p.var = (mpX_srcptr *) var;
 
79
 
 
80
  p.mpX_clear       = (mpexpr_fun_one_t)      mpz_clear;
 
81
  p.mpX_ulong_p     = (mpexpr_fun_i_unary_t)  mpz_fits_ulong_p;
 
82
  p.mpX_get_ui      = (mpexpr_fun_get_ui_t)   mpz_get_ui;
 
83
  p.mpX_init        = (mpexpr_fun_unary_ui_t) mpz_init;
 
84
  p.mpX_number      = (mpexpr_fun_number_t)   mpexpr_mpz_number;
 
85
  p.mpX_set         = (mpexpr_fun_unary_t)    mpz_set;
 
86
  p.mpX_set_or_swap = (mpexpr_fun_unary_t)    mpz_swap;
 
87
  p.mpX_set_si      = (mpexpr_fun_set_si_t)   mpz_set_si;
 
88
  p.mpX_swap        = (mpexpr_fun_swap_t)     mpz_swap;
 
89
 
 
90
  return mpexpr_evaluate (&p);
 
91
}