Skip to content

Commit

Permalink
eliminate remaining (V)(L) double-casting
Browse files Browse the repository at this point in the history
  • Loading branch information
tavmem committed Sep 3, 2013
1 parent ebceb58 commit ba42a2c
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 34 deletions.
12 changes: 6 additions & 6 deletions k.c
Expand Up @@ -85,7 +85,7 @@ C vc[]="+-*%|&^!<>=~@?_,#$.:";// was "!#$%&*+,-.<=>?@^_|~:";
//V vd[] = {_VERB2};

V offsetSSR, offsetWhat, offsetAt, offsetDot, offsetColon;
I offsetOver, offsetScan, offsetEach, offsetEachright, offsetEachleft, offsetEachpair;
L offsetOver, offsetScan, offsetEach, offsetEachright, offsetEachleft, offsetEachpair;

S IFS[3] = {"x","y","z"};
S IFP[3]; //Implicit function parameters sp(x),...
Expand All @@ -94,11 +94,11 @@ I stringHasChar(S s,C c){I i=0;while(s[i])if(c==s[i++])R 1;R 0;} //string never
I charpos(S s,C c){I i=0;while(s[i] && c!=s[i])i++; R i;}

I isCharVerb(C c) {R stringHasChar(vc,c);}
I charsVerb(C c) {R charpos(vc,c);}
L charsVerb(C c) {R charpos(vc,c);}
Z C verbsChar(V p) {R ((L)p>=DT_VERB_OFFSET && (L)p < DT_SPECIAL_VERB_OFFSET)?vc[((L)p-DT_VERB_OFFSET)/2]:'\0';}

Z C adverbsChar(V p){R ((L)p>=DT_ADVERB_OFFSET)?ac[((L)p-DT_ADVERB_OFFSET)%3]:'\0';}
I charsAdverb(C c) {R charpos(ac,c);}
L charsAdverb(C c) {R charpos(ac,c);}

I sva(V p) //simpleVerbArity: Use boundaries of arrays to determine verb class in O(1) constant time
{
Expand Down Expand Up @@ -511,9 +511,9 @@ TR DT[] = //Dispatch table is append-only. Reorder/delete/insert breaks backwar
};

K TABLE_END(){R 0;}
I DT_SIZE=0;
I DT_END_OFFSET, DT_ADVERB_OFFSET, DT_VERB_OFFSET, DT_SPECIAL_VERB_OFFSET;
I DT_OFFSET(V v){I i=0; while(v!=DT[i].func)i++; R i;} //init only
L DT_SIZE=0;
L DT_END_OFFSET, DT_ADVERB_OFFSET, DT_VERB_OFFSET, DT_SPECIAL_VERB_OFFSET;
L DT_OFFSET(V v){I i=0; while(v!=DT[i].func)i++; R i;} //init only

I kreci=0; //should be inside DEBUG case but needed in r.c cached verbs, at least until caching method changes
#ifdef DEBUG
Expand Down
10 changes: 5 additions & 5 deletions k.h
Expand Up @@ -25,8 +25,8 @@ I valence(V p);
I adverbClass(V p);
extern I vn_ct;
I sva(V p);
I charsAdverb(C c);
I charsVerb(C c);
L charsAdverb(C c);
L charsVerb(C c);
I isCharVerb(C c);
I charpos(S s,C c);
I stringHasChar(S s,C c);
Expand Down Expand Up @@ -107,7 +107,7 @@ K ex(K a);
K X(S s);
I min(I a,I b);
I max(I a,I b);
I DT_OFFSET(V v);
L DT_OFFSET(V v);
extern C PPON;
extern I PPMAX;
extern I PP;
Expand All @@ -120,7 +120,7 @@ extern K KTREE;
extern N SYMBOLS;
K kerr(cS s);
extern C errmsg[256];
extern I DT_SIZE, DT_END_OFFSET, DT_ADVERB_OFFSET, DT_VERB_OFFSET, DT_SPECIAL_VERB_OFFSET;
extern L DT_SIZE, DT_END_OFFSET, DT_ADVERB_OFFSET, DT_VERB_OFFSET, DT_SPECIAL_VERB_OFFSET;
extern TR DT[];
extern I offsetOver, offsetScan, offsetEach, offsetEachright, offsetEachleft, offsetEachpair;
extern L offsetOver, offsetScan, offsetEach, offsetEachright, offsetEachleft, offsetEachpair;
K TABLE_END();
10 changes: 5 additions & 5 deletions kc.c
Expand Up @@ -241,11 +241,11 @@ I kinit() //oom (return bad)
offsetEachpair = DT_OFFSET(eachpair);

//could probably delete these variables and create func if(x<DT_SIZE) && DT[x].func == what
offsetWhat = (V)(L)DT_OFFSET(what); //equiv: DT_VERB_OFFSET+1+2*charpos(vc,'?');
offsetAt = (V)(L)DT_OFFSET(at);
offsetDot = (V)(L)DT_OFFSET(dot);
offsetColon = (V)(L)DT_OFFSET(colon_dyadic);
offsetSSR = (V)(L)DT_OFFSET(_ssr);
offsetWhat = (V)DT_OFFSET(what); //equiv: DT_VERB_OFFSET+1+2*charpos(vc,'?');
offsetAt = (V)DT_OFFSET(at);
offsetDot = (V)DT_OFFSET(dot);
offsetColon = (V)DT_OFFSET(colon_dyadic);
offsetSSR = (V)DT_OFFSET(_ssr);

kerr("undescribed");//initialize errmsg string to be non-null for more useful reporting
SYMBOLS=newN(); //Initialize intern pool
Expand Down
16 changes: 8 additions & 8 deletions kx.c
Expand Up @@ -257,11 +257,11 @@ Z K dv_ex(K a, V *p, K b)
if ((L)adverb == offsetScan) R scanDyad(a, p, b);
if ((L)adverb == offsetEach)
{
if(!a) adverb = (V)(L)offsetEachright;
if(!a) adverb = (V)offsetEachright;
else if(a->t <= 0 && b->t <= 0 && a->n != b->n) R LE;
else if(a->t > 0 && b->t > 0) R dv_ex(a,p-1,b);
else if (a->t > 0) adverb = (V)(L)offsetEachright;
else if(b->t > 0) adverb = (V)(L)offsetEachleft;
else if (a->t > 0) adverb = (V)offsetEachright;
else if(b->t > 0) adverb = (V)offsetEachleft;
else
{
//a and b both lists/vectors of size an
Expand Down Expand Up @@ -299,7 +299,7 @@ Z K dv_ex(K a, V *p, K b)
if(gn > 0) kK(g)[0]=a?a:b;

K tmp; I flag=0;
if(*p>(V)(L)DT_SIZE && 0!=b->n) {V*p1=*p; if(*p1>(V)(L)DT_SIZE) {K p2=*p1; if(7!=p2->t) flag=1;}}
if(*p>(V)DT_SIZE && 0!=b->n) {V*p1=*p; if(*p1>(V)DT_SIZE) {K p2=*p1; if(7!=p2->t) flag=1;}}
if(flag) tmp = vf_ex(*p,b);
else {if(stk>2e6){R kerr("stack"); GC;} stk++; tmp=vf_ex(*p,g); stk--;}

Expand Down Expand Up @@ -487,7 +487,7 @@ Z V ex_(V a, I r)//Expand wd()->7-0 types, expand and evaluate brackets

if(kV(x)[CONJ])
{
if((tmp=*(K*)(kV(x)+CONJ))) if(offsetColon==*kW(tmp) && *(kW(tmp)+1)>(V)(L)DT_SIZE)fer=1;
if((tmp=*(K*)(kV(x)+CONJ))) if(offsetColon==*kW(tmp) && *(kW(tmp)+1)>(V)DT_SIZE)fer=1;
y=ex_(kV(x)+CONJ,2); //Use 0-type with NULLS if passing to function
U(y);
if(y->t == 0 && y->n==0){cd(y); y=_n();}
Expand All @@ -499,7 +499,7 @@ Z V ex_(V a, I r)//Expand wd()->7-0 types, expand and evaluate brackets
R z;
}

K ex(K a){U(a); if(7==a->t)if(*(kW(a))>(V)(L)DT_SIZE){K tmp=*(K*)*(kW(a)); if(7==tmp->t)if(6==tmp->n)fwh=1;} K z=ex_(&a,0); cd(a); fer=0; fwh=0; stk=0; proj=0; R z;} //Input is 7-0 type from wd()
K ex(K a){U(a); if(7==a->t)if(*(kW(a))>(V)DT_SIZE){K tmp=*(K*)*(kW(a)); if(7==tmp->t)if(6==tmp->n)fwh=1;} K z=ex_(&a,0); cd(a); fer=0; fwh=0; stk=0; proj=0; R z;} //Input is 7-0 type from wd()

Z K ex0(V*v,K k,I r) //r: {0,1,2} -> {code, (code), [code]} Reverse execution/return multiple (paren not function or script) "list notation" {4,5,6,7} -> {:,if,while,do}
{
Expand All @@ -514,7 +514,7 @@ Z K ex0(V*v,K k,I r) //r: {0,1,2} -> {code, (code), [code]} Reverse execution/re
CS(0, for(i=-1;i<n;i++)if(-1==i||bk(v[i])){cd(z); U(x=ex1(v+1+i,0,&i,n,1)) z=bk(x)?_n():x; if(fer)R z;})// c:9;a+b;c:1
CS(4, for(i=-1;i<n;i++)if(-1==i||bk(v[i])){U(x=ex1(v+1+i,0,&i,n,1)) if(fer)R x; x=bk(x)?_n():x; while(++i<n&&!bk(v[i])); if(i==n) R x; z=delist(x); if(ABS(z->t)!=1 || z->n!=1){cd(z);R TE;}a=*kI(z);cd(z); if(a)R ex1(v+i+1,0,&i,n,1); else while(i<n&&!bk(v[i]))i++; } R _n())
CSR(5,)CSR(6,)CS(7, do{I i=0; U(x=ex1(v,0,&i,0,1)) if(fer)R x; x=bk(x)?_n():x; z=delist(x); if(ABS(z->t)!=1 || z->n!=1){cd(z);R TE;}a=*kI(z);cd(z);i=0;if(b){while(++i<n&&!bk(v[i])); if(i>=n)break;}SW(r){CSR(5,)CS(6,if(a&&b){x=ex0(v+i+1,0,0); if(fer)R x; cd(x);})CS(7,DO2(a, x=ex0(v+i+1,0,0); if(fer)R x; cd(x);))}}while(6==r && a); R _n())
CD: z=newK(0,n?e:0); if(n)for(i=n-1;i>=-1;i--)if(-1==i||bk(v[i])){if(offsetColon==(v+1+i)[0] && (v+1+i)[1]>(V)(L)DT_SIZE)fer=1; x=ex1(v+1+i,0,&i,n,0); if(fer){cd(z); R x;} M(x,z) kK(z)[--e]=bk(x)?2==r?0:_n():x;}// (c:9;a+b;c:1) oom
CD: z=newK(0,n?e:0); if(n)for(i=n-1;i>=-1;i--)if(-1==i||bk(v[i])){if(offsetColon==(v+1+i)[0] && (v+1+i)[1]>(V)DT_SIZE)fer=1; x=ex1(v+1+i,0,&i,n,0); if(fer){cd(z); R x;} M(x,z) kK(z)[--e]=bk(x)?2==r?0:_n():x;}// (c:9;a+b;c:1) oom
}

//Note on brackets: [] is _n, not (). Expression [1;1] (0-type with two atoms) is different from [1 1] (integer vector)
Expand Down Expand Up @@ -633,7 +633,7 @@ K ex1(V*w,K k,I*i,I n,I f)//convert verb pieces (eg 1+/) to seven-types, default

if(!c || !VA(w[c-1]) || (c>1 && offsetColon==w[c-1] ) ) R ex2(w,k); //typical list for execution

if(w[0]==offsetColon && w[1]>(V)(L)DT_SIZE){
if(w[0]==offsetColon && w[1]>(V)DT_SIZE){
if(w[-1]!=offsetColon) fer=1;
I d=0; while(w[d] && !bk(w[d])){d++;}
K a=Kv(); a->n=0; K kb=newK(-4,d); M(a,kb) V*b=(V*)kK(kb); DO(d-1, b[i]=w[i+1];) b[d-1]=0; kV(a)[CODE]=kb;
Expand Down
16 changes: 8 additions & 8 deletions p.c
Expand Up @@ -533,19 +533,19 @@ I capture(S s,I n,I k,I*m,V*w,I*d,K*locals,K*dict,K func) //IN string, string le
{
if(k+r<n && ':'==s[k+r] && -MARK_VERB==m[k+r]) R (L)PE;
u=strdupn(s+k,r); P(!u,(L)PE)
I i;
L i;

i=DT_SPECIAL_VERB_OFFSET;
while(i < DT_SIZE && (!DT[i].text || SC(u, DT[i].text)))i++;
if(i < DT_SIZE){z=(V)(L)i;} //faster is sp()/hash-table (compared to SC())
if(i < DT_SIZE){z=(V)i;} //faster is sp()/hash-table (compared to SC())
free(u);
P(!z,(L)kerr("reserved"))// _invalidsystemverbname
break; // _verb does not grab monadic ':' following
}

//The code for verb arity is hard to follow. I suspect this is because I could not find the factorization
I grab=0;
I i=0;
L i=0;

I is_colon=':'==s[k];
I name_bracket_assign=0;
Expand Down Expand Up @@ -575,13 +575,13 @@ I capture(S s,I n,I k,I*m,V*w,I*d,K*locals,K*dict,K func) //IN string, string le
else if(modifier_colon){m[k+r]*=-1; r++; a=1; grab=1;} //grab monad ':' sign

i=0;
if(r-grab==1) {z=(V)(L)(DT_VERB_OFFSET+2*charsVerb(s[k])+(1==a?0:1));}
if(r-grab==1) {z=(V)(DT_VERB_OFFSET+2*charsVerb(s[k])+(1==a?0:1));}

else
{ j=(V)(L)atol(s+k);
{ j=(V)atol(s+k);
i=DT_SPECIAL_VERB_OFFSET;
while(i < DT_SIZE && (!DT[i].text || (L)j != atol(DT[i].text)))i++;
if(i<DT_SIZE){z=(V)(L)(i+(1==a?0:1));}
if(i<DT_SIZE){z=(V)(i+(1==a?0:1));}
else R (L)PE; //no matching 0: 1: style verb. (if exists, we also allow eg 123: and -2: )
}

Expand All @@ -592,8 +592,8 @@ I capture(S s,I n,I k,I*m,V*w,I*d,K*locals,K*dict,K func) //IN string, string le
//what passes for y? <--- anything that isn't an end/\0, except colon verb will go on to ex1 to the right and assign _n

)
CS(MARK_ADVERB , z=(V)(L)(DT_ADVERB_OFFSET+charsAdverb(s[k])+(r>1?3:0)))
CS(MARK_END , z=(V)(L)(DT_OFFSET(end)))
CS(MARK_ADVERB , z=(V)(DT_ADVERB_OFFSET+charsAdverb(s[k])+(r>1?3:0)))
CS(MARK_END , z=(V)(DT_OFFSET(end)))
}

if(!z) ; //TODO: handle null z, which can happen
Expand Down
4 changes: 2 additions & 2 deletions p.h
@@ -1,6 +1,6 @@
I charsAdverb(C c);
L charsAdverb(C c);
extern V adverbs[];
I charsVerb(C c);
L charsVerb(C c);
I SC(S a,S b);
K *denameS(S dir_string,S t,I create);
K EV(K e);
Expand Down

0 comments on commit ba42a2c

Please sign in to comment.