File: gencast.c

package info (click to toggle)
gp2c 0.0.12-2
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 2,916 kB
  • sloc: ansic: 8,592; sh: 1,606; lex: 346; yacc: 226; makefile: 107
file content (104 lines) | stat: -rw-r--r-- 2,671 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
Copyright (C) 2002-2013  The PARI group.

This file is part of the GP2C package.

PARI/GP is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation. It is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY WHATSOEVER.

Check the License for details. You should have received a copy of it, along
with the package; see the file 'COPYING'. If not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.*/

#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "header.h"
void gencastf(FILE *fout, int n, int nt, int f)
{
  int t=tree[n].t;
  int tnt=typemax[t][nt];
  int i;
  if (t==nt)
  {
    /*bottom of the recursion*/
    if (f)
      genparens(fout,n);
    else
      gencode(fout,n);
    return;
  }
  if (gencastcode(fout,n,t,nt,f))
    return;
  /*the interesting case */
  if (is_subtype(t,nt))
    for (i=0;i<s_GPtype.n;i++)
      if (is_subtype(t,i) && is_subtype(i,nt))
        if (gencastcode(fout,n,i,nt,f))
          return;
  if (is_subtype(nt,t))
    for (i=0;i<s_GPtype.n;i++)
      if (is_subtype(i,t) && is_subtype(nt,i))
        if (gencastcode(fout,n,i,nt,f))
          return;
  if (gencastcode(fout,n,tnt,nt,f))
    return;
  if (debug)
    warning(-1,"No conversion method defined for %s->%s",GPname(t),GPname(nt));
  for(i=0;i<s_GPtype.n;i++)
    if ( is_subtype(i,tnt) && is_subtype(nt,i))
      if (gencastcode(fout,n,i,nt,f))
          return;
  die(-1,"No conversion method defined for %s->%s",GPname(t),GPname(nt));
}

void gencast(FILE *fout, int n, int nt)
{
  if (nt<s_GPtype.n)
    gencastf(fout,n,nt,0);
  else
    gencode(fout,n);
}

int gencastcode(FILE *fout, int n, int t, int nt, int f)
{
  int i;
  gpfunc *gp=lfunc+FC_cast;
  gpdesc *dsc=gp->dsc;
  for(i=0; i<dsc->nb; i++)
  {
    gpdescarg *rule=dsc->a+i;
    descargatom atom=rule->args[0];
    if (rule->type!=nt || (atom.mode&tree[n].m)!=atom.mode)
      continue;
    switch(atom.t)
    {
    case AAherevalue:
      if(tree[n].f!=Fsmall && tree[n].f!=Fconst) continue;
    case AAtype:
      if(atom.type!=t) continue;
      break;
    case AAsmall:
      if (tree[n].f!=Fsmall || atom.misc!=tree[n].x) continue;
      break;
    default:
      continue;
    }
    if (rule->cname[0]==0)
      gencastf(fout,n,t,f);
    else
    {
      if (f && rule->mode&(1<<Mparens))
        fprintf(fout,"(");
      gencodedesc(fout,1,&n,rule,n,FC_cast);
      if (f && rule->mode&(1<<Mparens))
        fprintf(fout,")");
    }
    return 1;
  }
  return 0;
}