-
Notifications
You must be signed in to change notification settings - Fork 1
/
scope.c
141 lines (130 loc) · 3 KB
/
scope.c
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
#include "scope.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "x.h"
#include "sym.h"
#include "sys.h"
#define SM 1000000
#ifdef _WIN32
#define strtok_r strtok_s
#endif
static scope *scopea[SM];
scope *ks,*gs,*cs;
dict *ktree,*C,*Z;
extern K *null;
static int reserved(char *s, char *n) {
if(!s) return 0;
if(strlen(s)>2)
if(s[0]=='.'&&s[2]=='.'&&s[1]!='k')
return 1;
if(strlen(s)==1)
if(s[0]=='.' && n && strlen(n)==1 && n[0]!='k')
return 1;
if(strlen(s)==2 && s[0]=='.' && s[1]!='k')
return 1;
return 0;
}
static scope* scope_new_(scope *p, char *k) {
int i;
scope *s = xmalloc(sizeof(scope));
s->p = p;
s->d = dnew();
s->k = k;
for(i=0;i<SM;i++) if(!scopea[i]) break;
if(i==SM) {
printf("error: scope_new() i==SM\n");
exit(1);
}
scopea[i]=s;
return s;
}
scope* scope_new(scope *p) { return scope_new_(p,0); }
scope* scope_newk(scope *p, char *k) { return scope_new_(p,k); }
void scope_free(scope *s) {
int i;
if(s->k) return;
for(i=0;i<SM;i++) {
if(!scopea[i]) break;
if(scopea[i] && scopea[i] == s) scopea[i]=0;
}
dfree(s->d);
xfree(s);
}
/* n is a global reference. ex: .k.a.b.c */
static K* ktree_get(char *n) {
K *r=null,*q;
char *s,*p,*sp;
if(!strcmp(n,".")) { ktree->r++; return k5(ktree); }
s=xstrdup(n);
p=strtok_r(s,".",&sp);
r=dget(ktree,p);
while((p=strtok_r(0,".",&sp))) {
if(!r) { xfree(s); return kerror("value"); }
if(r->t!=5) { xfree(s); kfree(r); return kerror("value"); }
q=dget(r->v,p);
kfree(r);
r=q;
}
xfree(s);
return r ? r : kerror("value");
}
K* scope_get(scope *s, char *n) {
K *r=0,*t=0,*q=0;
char *p=0,*nn,*rp;
if(!s) return 0;
if(!n) return 0;
t=t0(); dset(Z,"t",t); kfree(t);
t=tt0(); dset(Z,"T",t); kfree(t);
if(n[0]=='.') return ktree_get(n);
if(s->k) { /* refresh scope dict from ktree */
q=ktree_get(s->k);
if(q->v!=s->d) { dfree(s->d); s->d=q->v; }
kfree(q);
}
if(strchr(n,'.')) {
nn=xstrdup(n);
p=strtok_r(nn,".",&rp);
r=scope_get(s,p);
while((p=strtok_r(0,".",&rp))) {
if(!r) { xfree(nn); return kerror("value"); }
if(r->t!=5) { xfree(nn); kfree(r); return kerror("value"); }
t=dget(r->v,p); kfree(r); r=t;
}
xfree(nn);
return r ? r : kerror("value");
}
r = dget(s->d,n);
if(!r && s->p && s!=gs) r = scope_get(s->p,n);
if(!r) r = dget(C,n);
return r ? r : kerror("value");
}
K* scope_set(scope *s, char *n, K *v) {
if(reserved(s->k,n)) return kerror("reserved");
dset(s->d,n,v);
return null;
}
scope* scope_cp(scope *s) {
int i;
if(!s) return 0;
scope *s2 = xmalloc(sizeof(scope));
s2->p = s->p;
s2->d = dcp(s->d);
s2->k = s->k;
for(i=0;i<SM;i++) if(!scopea[i]) break;
if(i==SM) {
printf("error: scope_new() i==SM\n");
exit(1);
}
scopea[i]=s2;
return s2;
}
scope* scope_find(dict *d) {
int i;
for(i=0;i<SM;i++) if(!scopea[i]||scopea[i]->d==d) break;
if(i==SM) {
printf("error: scope_find() i==SM\n");
exit(1);
}
return scopea[i];
}