File: context.c

package info (click to toggle)
gp2c 0.0.7pl3-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 1,728 kB
  • sloc: ansic: 7,730; sh: 1,420; lex: 336; yacc: 208; makefile: 143
file content (151 lines) | stat: -rw-r--r-- 3,424 bytes parent folder | download
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
Copyright (C) 2000  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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "header.h"

/* return hashing value for variable s*/
long
varkey(const char *s)
{
  long  n = 0;
  while (*s) { n = (n<<1) ^ *s; s++; }
  return n;
}

int newctx(int flag)
{
  int n;
  n=stack_new(&s_ctx);
  ctxstack[n].flag=flag;
  return n;
}

int newaff(enum AFenum func, int idx)
{
  int n=stack_new(&s_aff);
  if (idx<0)
    die(-1,"Internal error: unknown var");
  affstack[n].f=func;
  affstack[n].idx=idx;
  return n;
}

int newblock(void)
{
  int n=stack_new(&s_bloc);
  stack_init(&block[n].s,sizeof(*block[n].c),(void **)&block[n].c);
  block[n].ret=-1;
  stack_init(&block[n].v,sizeof(*block[n].var),(void **)&block[n].var);
  block[n].gc=0;
  block[n].egc=-1;
  stack_init(&block[n].g,sizeof(*block[n].gcvar),(void **)&block[n].gcvar);
  return n;
}

int fillvar(int n, int flag, int t, int initval)
{
  int c=getvarerr(n);
  ctxvar *v=ctxstack+c;
  v->flag=flag;
  vartype(*v)=t;
  v->initval=initval;
  return c;
}
int pushvar(int n, int flag, int t, int initval)
{
  const char *s=entryname(n);
  int c=newctx(flag);
  ctxvar *v=ctxstack+c;
  if (flag&(1<<Cuser))
    v->cvar=usercname(s);
  else
    v->cvar=s;
  v->key=varkey(s);
  v->node=newnode(Fentry,newentry(s),-1);
  vartype(*v)=t;
  v->initval=initval;
  v->val=-1;
  return c;
}
int getvarinstack(const char *s, int n, const ctxvar *ctx)
{
  int i,key;
  key=varkey(s);
  for(i=n-1;i>=0 && (ctx[i].key!=key || strcmp(varstr(ctx[i]),s));i--);
  return i;
}
int getvarbyname(const char *s)
{
  return getvarinstack(s,s_ctx.n,ctxstack);
}
int getvar(int n)
{
  if(tree[n].f!=Fentry && tree[n].f!=Ffunction)
    die(n,"Internal error : Not a Fentry/Ffunction in getvar");
  return getvarbyname(value[tree[n].x].val.str);
}
ctxvar *getvarinblock(int n, context *fc)
{
  int v;
  if (tree[n].f!=Fentry)
    die(n,"Internal error : %s(%d) in getvarinblock",funcname(tree[n].f),tree[n].f);
  v=getvarinstack(value[tree[n].x].val.str,fc->s.n,fc->c);
  if ( v<0 )
    die(n,"Internal error : var not found in getvarinblock");
  return fc->c+v;
}
int getvarerr(int n)
{
  int v=getvar(n);
  if (v==-1)
    die(n,"Internal error: variable %s appears from nowhere",
        value[tree[n].x].val.str);
  return v;
}
void restorectx(int c)
{
  int i,j;
  for(i=c,j=c;i<s_ctx.n;i++)
    if (ctxstack[i].flag&(1<<Cglobal))
    {
      if (i!=j)
        ctxstack[j]=ctxstack[i];
      /*      ctxstack[j].flag=ctxstack[i].flag&~(1<<Cparent);*/
      j++;
    }
  if(j>s_ctx.n) die(-1,"Internal error: restorectx");
  s_ctx.n=j;
}

void pushctx(context *fc)
{
  stack_push(&s_ctx,&fc->s);
}

void copyctx(int c, context *fc)
{
  int i;
  fc->s.n=0;
  for (i=c; i<s_ctx.n ;i++)
    if (!(ctxstack[i].flag&(1<<Cglobal)))
    {
      int n=stack_new(&fc->s);
      fc->c[n]=ctxstack[i];
    }
}