00001
00002
00003
00004
00005
00006 #ifndef lint
00007 static char vcid[] = "$Id: types.c,v 1.7 1994/12/15 22:28:56 duchier Exp $";
00008 #endif
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "extern.h"
00018 #include "login.h"
00019 #include "trees.h"
00020 #include "print.h"
00021 #include "memory.h"
00022 #include "error.h"
00023 #include "token.h"
00024
00025 long types_modified;
00026 long type_count;
00027
00028 ptr_definition *gamma_table;
00029
00030 ptr_int_list adults,children;
00031
00032 typedef struct wl_pair_def{
00033 ptr_definition car;
00034 ptr_definition cdr;
00035 } pair_def;
00036
00037
00038 void make_type_link();
00039
00040
00041
00042
00043
00044
00045 void print_def_type(t)
00046 def_type t;
00047 {
00048 switch (t) {
00049 case predicate:
00050 perr("predicate");
00051 break;
00052 case function:
00053 perr("function");
00054 break;
00055 case type:
00056 perr("sort");
00057 break;
00058 case global:
00059 perr("global variable");
00060 break;
00061 default:
00062 perr("undefined");
00063 }
00064 }
00065
00066
00067
00068 long yes_or_no()
00069 {
00070 char *old_prompt;
00071 long c,d;
00072 ptr_psi_term old_state;
00073
00074 perr("*** Are you really sure you want to do that ");
00075 old_prompt=prompt;
00076 prompt="(y/n)?";
00077 old_state=input_state;
00078 open_input_file("stdin");
00079
00080 do {
00081 do {
00082 c=read_char();
00083 } while (c!=EOLN && c>0 && c<=32);
00084 } while (c!='y' && c!='n');
00085
00086 d=c;
00087 while (d!=EOLN && d!=EOF) d=read_char();
00088
00089 prompt=old_prompt;
00090 input_state=old_state;
00091 restore_state(old_state);
00092 return (c=='y');
00093 }
00094
00095
00096
00097 remove_cycles(d, dl)
00098 ptr_definition d;
00099 ptr_int_list *dl;
00100 {
00101 while (*dl) {
00102 if (((ptr_definition)(*dl)->value)==d)
00103 *dl = (*dl)->next;
00104 else
00105 dl= &((*dl)->next);
00106 }
00107 }
00108
00109
00110
00111
00112
00113
00114
00115 long redefine(t)
00116 ptr_psi_term t;
00117 {
00118 ptr_definition d,d2;
00119 ptr_int_list l,*l2;
00120 long success=TRUE;
00121
00122 deref_ptr(t);
00123 d=t->type;
00124 if (d->date<file_date) {
00125 if (d->type==type) {
00126
00127 if (FALSE ) {
00128 Errorline("the top sort '@' may not be extended.\n");
00129 success=FALSE;
00130 }
00131
00132
00133
00134
00135 }
00136 else if (d->protected && d->type!=undef) {
00137 if (d->date>0) {
00138
00139
00140 Errorline("the %T '%s' may not be changed.\n",
00141 d->type, d->keyword->combined_name);
00142 success=FALSE;
00143 }
00144 else {
00145 if (d->rule && (unsigned long)d->rule<=MAX_BUILT_INS ) {
00146
00147 Errorline("the built-in %T '%s' may not be extended.\n",
00148 d->type, d->keyword->symbol);
00149 success=FALSE;
00150 }
00151 else {
00152
00153 Warningline("extending the %T '%s'.\n",d->type,d->keyword->symbol);
00154 if (warningflag) if (!yes_or_no()) success=FALSE;
00155 }
00156 }
00157 }
00158
00159 if (success) {
00160 if (d->type==type) {
00161
00162
00163
00164 remove_cycles(d, &(d->children));
00165 remove_cycles(d, &(d->parents));
00166
00167
00168 }
00169 if (d->date==0) d->date=file_date;
00170
00171
00172
00173
00174
00175
00176 }
00177 }
00178
00179 return success;
00180 }
00181
00182
00183
00184
00185
00186
00187 ptr_int_list cons(v,l)
00188 GENERIC v;
00189 ptr_int_list l;
00190 {
00191 ptr_int_list n;
00192
00193 n=HEAP_ALLOC(int_list);
00194 n->value=v;
00195 n->next=l;
00196
00197 return n;
00198 }
00199
00200
00201
00202
00203
00204
00205
00206 long assert_less(t1,t2)
00207 ptr_psi_term t1,t2;
00208 {
00209 ptr_definition d1,d2;
00210 long ok=FALSE;
00211 deref_ptr(t1);
00212 deref_ptr(t2);
00213
00214 if (t1->type==top) {
00215 Errorline("the top sort '@' may not be a subsort.\n");
00216 return FALSE;
00217 }
00218 if (t1->value || t2->value) {
00219 Errorline("the declaration '%P <| %P' is illegal.\n",t1,t2);
00220 return FALSE;
00221 }
00222
00223 if (t1->type==t2->type) {
00224 Errorline("cyclic sort declarations are not allowed.\n");
00225 return FALSE;
00226 }
00227
00228 if (!redefine(t1)) return FALSE;
00229 if (!redefine(t2)) return FALSE;
00230 d1=t1->type;
00231 d2=t2->type;
00232 if (d1->type==predicate || d1->type==function) {
00233 Errorline("the %T '%s' may not be redefined as a sort.\n",
00234 d1->type, d1->keyword->symbol);
00235 }
00236 else if (d2->type==predicate || d2->type==function) {
00237 Errorline("the %T '%s' may not be redefined as a sort.\n",
00238 d2->type, d2->keyword->symbol);
00239 }
00240 else {
00241 d1->type=type;
00242 d2->type=type;
00243 types_modified=TRUE;
00244 make_type_link(d1, d2);
00245
00246
00247 ok=TRUE;
00248 }
00249
00250 return ok;
00251 }
00252
00253
00254
00255
00256
00257
00258 void assert_protected(n,prot)
00259 ptr_node n;
00260 long prot;
00261 {
00262 ptr_psi_term t;
00263
00264 if (n) {
00265 assert_protected(n->left,prot);
00266
00267 t=(ptr_psi_term)n->data;
00268 deref_ptr(t);
00269 if (t->type) {
00270 if (t->type->type==type) {
00271 Warningline("'%s' is a sort. It can be extended without a declaration.\n",
00272 t->type->keyword->symbol);
00273 }
00274 else if ((unsigned long)t->type->rule<MAX_BUILT_INS &&
00275 (unsigned long)t->type->rule>0) {
00276 if (!prot)
00277 Warningline("'%s' is a built-in--it has not been made dynamic.\n",
00278 t->type->keyword->symbol);
00279 }
00280 else {
00281 t->type->protected=prot;
00282 if (prot) t->type->date&=(~1); else t->type->date|=1;
00283 }
00284 }
00285
00286 assert_protected(n->right,prot);
00287 }
00288 }
00289
00290
00291
00292
00293
00294
00295
00296 void assert_args_not_eval(n)
00297 ptr_node n;
00298 {
00299 ptr_psi_term t;
00300
00301 if (n) {
00302 assert_args_not_eval(n->left);
00303
00304 t=(ptr_psi_term)n->data;
00305 deref_ptr(t);
00306 if (t->type) {
00307 if (t->type->type==type) {
00308 Warningline("'%s' is a sort--only functions and predicates\
00309 can have unevaluated arguments.\n",t->type->keyword->symbol);
00310 }
00311 else
00312 t->type->evaluate_args=FALSE;
00313 }
00314
00315 assert_args_not_eval(n->right);
00316 }
00317 }
00318
00319
00320
00321
00322
00323
00324
00325
00326 void assert_delay_check(n)
00327 ptr_node n;
00328 {
00329 if (n) {
00330 ptr_psi_term t;
00331 assert_delay_check(n->left);
00332
00333 t=(ptr_psi_term)n->data;
00334 deref_ptr(t);
00335 if (t->type) {
00336 t->type->always_check=FALSE;
00337 }
00338
00339 assert_delay_check(n->right);
00340 }
00341 }
00342
00343
00344
00345
00346
00347
00348
00349 void clear_already_loaded(n)
00350 ptr_node n;
00351 {
00352 ptr_definition d;
00353
00354 if (n) {
00355 d=((ptr_keyword)n->data)->definition;
00356 d->already_loaded=FALSE;
00357 clear_already_loaded(n->left);
00358 clear_already_loaded(n->right);
00359 }
00360 }
00361
00362
00363
00364
00365
00366
00367
00368 void assert_type(t)
00369 ptr_psi_term t;
00370 {
00371 ptr_psi_term arg1,arg2;
00372
00373 get_two_args(t->attr_list,&arg1,&arg2);
00374 if(arg1==NULL || arg2==NULL) {
00375 Errorline("bad sort declaration '%P' (%E).\n",t);
00376 }
00377 else
00378 assert_ok=assert_less(arg1,arg2);
00379 }
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398 void assert_complicated_type(t)
00399 ptr_psi_term t;
00400 {
00401 ptr_psi_term arg2,typ1,typ2,pred=NULL;
00402 ptr_list lst;
00403 long eqflag = equ_tok((*t),":=");
00404 long ok, any_ok=FALSE;
00405
00406 get_two_args(t->attr_list,&typ1,&arg2);
00407
00408 if (typ1 && arg2) {
00409 deref_ptr(typ1);
00410 deref_ptr(arg2);
00411 typ2=arg2;
00412 if (!strcmp(arg2->type->keyword->symbol,"|")) {
00413 typ2=NULL;
00414 get_two_args(arg2->attr_list,&arg2,&pred);
00415 if (arg2) {
00416 deref_ptr(arg2);
00417 typ2=arg2;
00418 }
00419 }
00420 if (typ2) {
00421 if (typ2->type==disjunction) {
00422
00423 if (typ1->attr_list && eqflag) {
00424 Warningline("attributes ignored left of ':=' declaration (%E).\n");
00425 }
00426 while(typ2 && typ2->type!=nil) {
00427 get_two_args(typ2->attr_list,&arg2,&typ2);
00428 if(typ2)
00429 deref_ptr(typ2);
00430 if (arg2) {
00431 deref_ptr(arg2);
00432 if (eqflag) {
00433 ok=assert_less(arg2,typ1);
00434 if (ok) any_ok=TRUE;
00435 if (ok && (arg2->attr_list || pred!=NULL)) {
00436 add_rule(arg2,pred,type);
00437 }
00438 }
00439 else {
00440 ok=assert_less(typ1,arg2);
00441 if (ok) any_ok=TRUE;
00442 if (ok && arg2->attr_list) {
00443 Warningline("attributes ignored in sort declaration (%E).\n");
00444 }
00445 }
00446 }
00447 }
00448 assert_ok=TRUE;
00449 }
00450 else if (eqflag) {
00451 if (typ1->attr_list) {
00452 Warningline("attributes ignored left of ':=' declaration (%E).\n");
00453 }
00454 ok=assert_less(typ1,typ2);
00455 if (ok) any_ok=TRUE;
00456 typ2->type=typ1->type;
00457 if (ok && (typ2->attr_list || pred!=NULL))
00458 add_rule(typ2,pred,type);
00459 else
00460 assert_ok=TRUE;
00461 }
00462 else {
00463 if (typ2->attr_list) {
00464 Warningline("attributes ignored right of '<|' declaration (%E).\n");
00465 }
00466 ok=assert_less(typ1,typ2);
00467 if (ok) any_ok=TRUE;
00468 if (ok && (typ1->attr_list || pred!=NULL))
00469 add_rule(typ1,pred,type);
00470 else
00471 assert_ok=TRUE;
00472 }
00473 }
00474 else {
00475 Errorline("argument missing in sort declaration (%E).\n");
00476 }
00477 }
00478 else {
00479 Errorline("argument missing in sort declaration (%E).\n");
00480 }
00481 if (!any_ok) assert_ok=FALSE;
00482 }
00483
00484
00485
00486
00487
00488
00489
00490
00491 void assert_attributes(t)
00492 ptr_psi_term t;
00493 {
00494 ptr_psi_term arg1,arg2,pred=NULL,typ;
00495 ptr_definition d;
00496
00497 get_two_args(t->attr_list,&arg1,&arg2);
00498
00499 if (arg1) {
00500 typ=arg1;
00501 deref_ptr(arg1);
00502 if (!strcmp(arg1->type->keyword->symbol,"|")) {
00503 get_two_args(arg1->attr_list,&arg1,&pred);
00504 if (arg1) {
00505 typ=arg1;
00506 deref_ptr(arg1);
00507 }
00508 }
00509
00510 if (arg1 && wl_const(*arg1)) {
00511
00512 d=arg1->type;
00513 if (d->type==predicate || d->type==function) {
00514 Errorline("the %T '%s' may not be redefined as a sort.\n",
00515 d->type, d->keyword->symbol);
00516 }
00517 else {
00518 d->type=type;
00519 types_modified=TRUE;
00520 add_rule(typ,pred,type);
00521 }
00522 }
00523 else {
00524 Errorline("bad argument in sort declaration '%P' (%E).\n",t);
00525 }
00526 }
00527 else {
00528 Errorline("argument missing in sort declaration (%E).\n");
00529 }
00530 }
00531
00532
00533
00534
00535
00536
00537
00538
00539 void find_adults()
00540
00541 {
00542 ptr_definition d;
00543 ptr_int_list l;
00544
00545 for(d=first_definition;d;d=d->next)
00546 if(d->type==type && d->parents==NULL) {
00547 l=HEAP_ALLOC(int_list);
00548 l->value=(GENERIC)d;
00549 l->next=adults;
00550 adults=l;
00551 }
00552 }
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562 void insert_own_prop(d)
00563 ptr_definition d;
00564 {
00565 ptr_int_list l;
00566 ptr_pair_list rule;
00567 ptr_triple_list *t;
00568 long flag;
00569
00570 l=HEAP_ALLOC(int_list);
00571 l->value=(GENERIC)d;
00572 l->next=children;
00573 children=l;
00574
00575 rule = d->rule;
00576 while (rule) {
00577 t= &(d->properties);
00578 flag=TRUE;
00579
00580 while (flag) {
00581 if (*t)
00582 if ((*t)->a==rule->a && (*t)->b==rule->b && (*t)->c==d)
00583 flag=FALSE;
00584 else
00585 t= &((*t)->next);
00586 else {
00587 *t = HEAP_ALLOC(triple_list);
00588 (*t)->a=rule->a;
00589 (*t)->b=rule->b;
00590 (*t)->c=d;
00591 (*t)->next=NULL;
00592 flag=FALSE;
00593 }
00594 }
00595 rule=rule->next;
00596 }
00597 }
00598
00599
00600
00601
00602
00603 void insert_prop(d,prop)
00604 ptr_definition d;
00605 ptr_triple_list prop;
00606 {
00607 ptr_int_list l;
00608 ptr_triple_list *t;
00609 long flag;
00610
00611 l=HEAP_ALLOC(int_list);
00612 l->value=(GENERIC)d;
00613 l->next=children;
00614 children=l;
00615
00616 while (prop) {
00617 t= &(d->properties);
00618 flag=TRUE;
00619
00620 while (flag) {
00621 if (*t)
00622 if ((*t)->a==prop->a && (*t)->b==prop->b && (*t)->c==prop->c)
00623 flag=FALSE;
00624 else
00625 t= &((*t)->next);
00626 else {
00627 *t = HEAP_ALLOC(triple_list);
00628 (*t)->a=prop->a;
00629 (*t)->b=prop->b;
00630 (*t)->c=prop->c;
00631 (*t)->next=NULL;
00632 flag=FALSE;
00633 }
00634 }
00635 prop=prop->next;
00636 }
00637 }
00638
00639
00640
00641
00642
00643
00644
00645 void propagate_definitions()
00646 {
00647 ptr_int_list kids;
00648 ptr_definition d;
00649
00650 adults=NULL;
00651 find_adults();
00652
00653 while (adults) {
00654
00655 children=NULL;
00656
00657 while (adults) {
00658 d=(ptr_definition)adults->value;
00659
00660 insert_own_prop(d);
00661 children=children->next;
00662
00663 kids=d->children;
00664
00665 while(kids) {
00666 insert_prop(kids->value,d->properties);
00667
00668
00669 kids=kids->next;
00670 }
00671 adults=adults->next;
00672 }
00673 adults=children;
00674 }
00675 }
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692 long count_sorts(c0)
00693 long c0;
00694 {
00695 ptr_definition d;
00696
00697 for(d=first_definition;d;d=d->next)
00698 if (d->type==type) c0++;
00699
00700 return c0;
00701 }
00702
00703
00704
00705
00706
00707
00708 void clear_coding()
00709
00710 {
00711 ptr_definition d;
00712
00713 for(d=first_definition;d;d=d->next)
00714 if (d->type==type) d->code=NOT_CODED;
00715 }
00716
00717
00718
00719
00720
00721
00722
00723 void least_sorts()
00724
00725 {
00726 ptr_definition d;
00727
00728 for(d=first_definition;d;d=d->next)
00729 if (d->type==type && d->children==NULL && d!=nothing)
00730 nothing->parents=cons(d,nothing->parents);
00731 }
00732
00733
00734
00735
00736
00737
00738
00739 void all_sorts()
00740
00741 {
00742 ptr_definition d;
00743
00744 for(d=first_definition;d;d=d->next)
00745 if (d->type==type && d!=nothing)
00746 nothing->parents=cons(d,nothing->parents);
00747 }
00748
00749
00750
00751
00752
00753
00754 ptr_int_list two_to_the(p)
00755 long p;
00756 {
00757 ptr_int_list result,code;
00758 long v=1;
00759
00760 code=HEAP_ALLOC(int_list);
00761 code->value=0;
00762 code->next=NULL;
00763 result=code;
00764
00765 while (p>=INT_SIZE) {
00766 code->next=HEAP_ALLOC(int_list);
00767 code=code->next;
00768 code->value=0;
00769 code->next=NULL;
00770 p=p-INT_SIZE;
00771 }
00772
00773 v= v<<p ;
00774 code->value=(GENERIC)v;
00775
00776 return result;
00777 }
00778
00779
00780
00781
00782
00783 ptr_int_list copyTypeCode(u)
00784 ptr_int_list u;
00785 {
00786 ptr_int_list code;
00787
00788 code = HEAP_ALLOC(int_list);
00789 code->value=0;
00790 code->next=NULL;
00791
00792 or_codes(code, u);
00793
00794 return code;
00795 }
00796
00797
00798
00799
00800
00801
00802
00803 void or_codes(u,v)
00804 ptr_int_list u,v;
00805 {
00806 while (v) {
00807 u->value= (GENERIC)(((unsigned long)(u->value)) | ((unsigned long)(v->value)));
00808 v=v->next;
00809 if (u->next==NULL && v) {
00810 u->next=HEAP_ALLOC(int_list);
00811 u=u->next;
00812 u->value=0;
00813 u->next=NULL;
00814 }
00815 else
00816 u=u->next;
00817 }
00818 }
00819
00820
00821
00822
00823
00824
00825
00826
00827
00828
00829 equalize_codes(len)
00830 int len;
00831 {
00832 ptr_definition d;
00833 ptr_int_list c,*ci;
00834 long i;
00835 int w;
00836
00837 for(d=first_definition;d;d=d->next)
00838 if (d->type==type) {
00839 c = d->code;
00840 ci = &(d->code);
00841 w=len;
00842
00843
00844 while (c) {
00845 ci= &(c->next);
00846 c=c->next;
00847 w--;
00848 }
00849 assert(w>=0);
00850
00851 for (i=0; i<w; i++) {
00852 *ci = HEAP_ALLOC(int_list);
00853 (*ci)->value=0;
00854 ci= &((*ci)->next);
00855 }
00856 (*ci)=NULL;
00857 }
00858 }
00859
00860
00861
00862 long type_member();
00863
00864
00865
00866
00867
00868
00869
00870
00871 void make_type_link(t1,t2)
00872 ptr_definition t1, t2;
00873 {
00874 #ifdef OS2_PORT
00875 if (t1)
00876 {
00877 #endif
00878 if (t2!=top && !type_member(t2,t1->parents))
00879 t1->parents=cons(t2,t1->parents);
00880 if (t2!=top && !type_member(t1,t2->children))
00881 t2->children=cons(t1,t2->children);
00882 #ifdef OS2_PORT
00883 }
00884 #endif
00885 }
00886
00887
00888
00889
00890
00891
00892
00893
00894 long type_member(t,tlst)
00895 ptr_definition t;
00896 ptr_int_list tlst;
00897 {
00898 while (tlst) {
00899 if (t==(ptr_definition)tlst->value) return TRUE;
00900 tlst=tlst->next;
00901 }
00902 return FALSE;
00903 }
00904
00905
00906 void perr_sort(d)
00907 ptr_definition d;
00908 {
00909 perr_s("%s",d->keyword->symbol);
00910 }
00911
00912 void perr_sort_list(anc)
00913 ptr_int_list anc;
00914 {
00915 if (anc) {
00916 perr_sort_list(anc->next);
00917 if (anc->next) perr(" <| ");
00918 perr_sort((ptr_definition)anc->value);
00919 }
00920 }
00921
00922 void perr_sort_cycle(anc)
00923 ptr_int_list anc;
00924 {
00925 perr_sort((ptr_definition)anc->value);
00926 perr(" <| ");
00927 perr_sort_list(anc);
00928 }
00929
00930
00931
00932
00933
00934
00935
00936
00937 long type_cyclicity(d,anc)
00938 ptr_definition d;
00939 ptr_int_list anc;
00940 {
00941 ptr_int_list p=d->parents;
00942 ptr_definition pd;
00943 long errflag;
00944 int_list anc2;
00945
00946 while (p) {
00947 pd=(ptr_definition)p->value;
00948
00949 if (pd->code==NOT_CODED) {
00950 pd->code = (ptr_int_list)TRUE;
00951 anc2.value=(GENERIC)pd;
00952 anc2.next=anc;
00953 errflag=type_cyclicity(pd,&anc2);
00954 if (errflag) return TRUE;
00955 }
00956
00957 else {
00958 if (type_member(pd,anc)) {
00959 Errorline("there is a cycle in the sort hierarchy\n");
00960 perr("*** Cycle: [");
00961 perr_sort_cycle(anc);
00962 perr("]\n");
00963 exit_life(TRUE);
00964 return TRUE;
00965 }
00966 }
00967 p=p->next;
00968 }
00969 return FALSE;
00970 }
00971
00972
00973
00974
00975
00976
00977
00978
00979
00980 void propagate_always_check(d,ch)
00981 ptr_definition d;
00982 long *ch;
00983 {
00984 ptr_int_list child_list;
00985 ptr_definition child;
00986
00987 child_list = d->children;
00988 while (child_list) {
00989 child = (ptr_definition)child_list->value;
00990 if (child->always_check) {
00991 child->always_check = FALSE;
00992 *ch = TRUE;
00993 propagate_always_check(child,ch);
00994 }
00995 child_list = child_list->next;
00996 }
00997 }
00998
00999
01000
01001
01002
01003
01004
01005
01006 void one_pass_always_check(ch)
01007 long *ch;
01008 {
01009 ptr_definition d;
01010
01011
01012 for(d=first_definition;d;d=d->next)
01013 if (d->type==type && !d->always_check)
01014 propagate_always_check(d,ch);
01015 }
01016
01017
01018
01019
01020
01021
01022
01023
01024 void inherit_always_check()
01025 {
01026 long change;
01027
01028 do {
01029 change=FALSE;
01030 one_pass_always_check(&change);
01031 } while (change);
01032 }
01033
01034
01035
01036
01037
01038
01039
01040
01041
01042
01043
01044
01045 void encode_types()
01046 {
01047 long p=0,i,possible,ok=TRUE;
01048 ptr_int_list layer,l,kids,dads,code;
01049 ptr_definition xdef,kdef,ddef,err;
01050
01051 if (types_modified) {
01052
01053 nothing->parents=NULL;
01054 nothing->children=NULL;
01055
01056 top->parents=NULL;
01057 top->children=NULL;
01058
01059
01060 make_type_link(integer,real);
01061 make_type_link(true,boolean);
01062 make_type_link(false,boolean);
01063
01064
01065 make_type_link(quoted_string,built_in);
01066 make_type_link(boolean,built_in);
01067 make_type_link(real,built_in);
01068
01069 make_sys_type_links();
01070
01071 type_count=count_sorts(-1);
01072 clear_coding();
01073 nothing->parents=NULL;
01074 all_sorts();
01075 if (type_cyclicity(nothing,NULL)) {
01076 clear_coding();
01077 return;
01078 }
01079 clear_coding();
01080 nothing->parents=NULL;
01081 least_sorts();
01082
01083 nothing->code=NULL;
01084
01085
01086 Traceline("*** Codes:\n%C= %s\n", NULL, nothing->keyword->symbol);
01087
01088 gamma_table=(ptr_definition *) heap_alloc(type_count*sizeof(definition));
01089
01090 layer=nothing->parents;
01091
01092 while (layer) {
01093 l=layer;
01094 do {
01095 xdef=(ptr_definition)l->value;
01096 if (xdef->code==NOT_CODED && xdef!=top) {
01097
01098 kids=xdef->children;
01099 code=two_to_the(p);
01100
01101 while (kids) {
01102 kdef=(ptr_definition)kids->value;
01103 or_codes(code,kdef->code);
01104 kids=kids->next;
01105 }
01106
01107 xdef->code=code;
01108 gamma_table[p]=xdef;
01109
01110
01111 Traceline("%C = %s\n", code, xdef->keyword->symbol);
01112 p=p+1;
01113 }
01114
01115 l=l->next;
01116
01117 } while (l);
01118
01119 l=layer;
01120 layer=NULL;
01121
01122 do {
01123 xdef=(ptr_definition)l->value;
01124 dads=xdef->parents;
01125
01126 while (dads) {
01127 ddef=(ptr_definition)dads->value;
01128 if(ddef->code==NOT_CODED) {
01129
01130 possible=TRUE;
01131 kids=ddef->children;
01132
01133 while(kids && possible) {
01134 kdef=(ptr_definition)kids->value;
01135 if(kdef->code==NOT_CODED)
01136 possible=FALSE;
01137 kids=kids->next;
01138 }
01139 if(possible)
01140 layer=cons(ddef,layer);
01141 }
01142 dads=dads->next;
01143 }
01144 l=l->next;
01145 } while(l);
01146 }
01147
01148 top->code=two_to_the(p);
01149 for (i=0;i<p;i++)
01150 or_codes(top->code,two_to_the(i));
01151
01152 gamma_table[p]=top;
01153
01154
01155
01156
01157
01158
01159 type_count=p+1;
01160 for(i=type_count;i<type_count;i++)
01161 gamma_table[i]=NULL;
01162
01163 Traceline("%C = @\n\n", top->code);
01164 equalize_codes(p/32+1);
01165
01166 propagate_definitions();
01167
01168
01169 inherit_always_check();
01170
01171 Traceline("*** Encoding done, %d sorts\n",type_count);
01172
01173 if (overlap_type(real,quoted_string)) {
01174 Errorline("the sorts 'real' and 'string' are not disjoint.\n");
01175 ok=FALSE;
01176 }
01177
01178
01179
01180
01181
01182
01183
01184
01185
01186
01187
01188
01189
01190
01191
01192 if (!ok) {
01193 perr("*** Internal problem:\n");
01194 perr("*** Wild_Life may behave abnormally because some basic types\n");
01195 perr("*** have been defined incorrectly.\n\n");
01196 }
01197
01198 types_modified=FALSE;
01199 types_done=TRUE;
01200 }
01201 }
01202
01203
01204
01205
01206
01207
01208 void print_codes()
01209 {
01210 long i;
01211
01212 for (i=0; i<type_count; i++) {
01213 outputline("%C = %s\n",
01214 gamma_table[i]->code,
01215 gamma_table[i]->keyword->combined_name);
01216 }
01217 }
01218
01219
01220 long sub_CodeType();
01221
01222
01223
01224
01225
01226
01227
01228
01229
01230
01231
01232
01233
01234
01235
01236 long glb_value(result,f,c,value1,value2,value)
01237 long result;
01238 long f;
01239 GENERIC c;
01240 GENERIC value1,value2,*value;
01241 {
01242 ptr_int_list code;
01243
01244 if (!result) return FALSE;
01245 if (value1==NULL) {
01246 *value=value2;
01247 return TRUE;
01248 }
01249 if (value2==NULL) {
01250 *value=value1;
01251 return TRUE;
01252 }
01253
01254
01255
01256
01257 code = f ? ((ptr_definition)c)->code : (ptr_int_list)c;
01258
01259
01260
01261
01262 if (sub_CodeType(code,real->code)) {
01263 *value=value1;
01264 return (*(REAL *)value1 == *(REAL *)value2);
01265 }
01266 else if (sub_CodeType(code,quoted_string->code)) {
01267 *value=value1;
01268 return (!strcmp((char *)value1,(char *)value2));
01269 }
01270 else {
01271
01272
01273 *value=value1;
01274 return TRUE;
01275 }
01276 }
01277
01278
01279
01280
01281
01282
01283
01284
01285
01286
01287
01288
01289
01290
01291
01292
01293 long glb_code(f1,c1,f2,c2,f3,c3)
01294 long f1,f2,*f3;
01295 GENERIC c1,c2,*c3;
01296 {
01297 long result=0;
01298 unsigned long v1,v2,v3;
01299 ptr_int_list cd1,cd2,*cd3;
01300
01301
01302 if (f1 && f2) {
01303 if ((ptr_definition)c1==(ptr_definition)c2) {
01304 *c3=c1;
01305 result=1;
01306 }
01307 else if ((ptr_definition)c1==top) {
01308 *c3=c2;
01309 if ((ptr_definition)c2==top)
01310 result=1;
01311 else
01312 result=3;
01313 }
01314 else if ((ptr_definition)c2==top) {
01315 *c3=c1;
01316 result=2;
01317 }
01318
01319
01320 if (result) {
01321 *f3=TRUE;
01322 return result;
01323 }
01324 }
01325
01326 cd1=(ptr_int_list)(f1?(GENERIC)((ptr_definition)c1)->code:c1);
01327 cd2=(ptr_int_list)(f2?(GENERIC)((ptr_definition)c2)->code:c2);
01328 cd3=(ptr_int_list*)c3;
01329 *f3=FALSE;
01330 if (cd1==NOT_CODED) {
01331 if (cd2==NOT_CODED) {
01332 if (c1==c2) {
01333 *cd3=cd1;
01334 result=1;
01335 }
01336 else
01337 result=0;
01338 }
01339 else if (cd2==top->code) {
01340 *cd3=cd1;
01341 result=2;
01342 }
01343 else
01344 result=0;
01345 }
01346 else if (cd1==top->code) {
01347 if (cd2==top->code) {
01348 *cd3=cd1;
01349 result=1;
01350 }
01351 else {
01352 *cd3=cd2;
01353 result=3;
01354 }
01355 }
01356 else if (cd2==NOT_CODED)
01357 result=0;
01358 else if (cd2==top->code) {
01359 *cd3=cd1;
01360 result=2;
01361 }
01362 else while (cd1 && cd2) {
01363
01364 *cd3 = STACK_ALLOC(int_list);
01365 (*cd3)->next=NULL;
01366
01367 v1=(unsigned long)(cd1->value);
01368 v2=(unsigned long)(cd2->value);
01369 v3=v1 & v2;
01370 (*cd3)->value=(GENERIC)v3;
01371
01372 if (v3) {
01373 if (v3<v1 && v3<v2)
01374 result=4;
01375 else if (result!=4)
01376 if (v1<v2)
01377 result=2;
01378 else if (v1>v2)
01379 result=3;
01380 else
01381 result=1;
01382 }
01383 else if (result)
01384 if (v1 || v2)
01385 result=4;
01386
01387 cd1=cd1->next;
01388 cd2=cd2->next;
01389 cd3= &((*cd3)->next);
01390 }
01391
01392 return result;
01393 }
01394
01395
01396
01397
01398
01399
01400
01401
01402
01403
01404
01405
01406
01407
01408
01409
01410
01411
01412
01413
01414
01415
01416
01417
01418 long glb(t1,t2,t3,c3)
01419 ptr_definition t1;
01420 ptr_definition t2;
01421 ptr_definition *t3;
01422 ptr_int_list *c3;
01423 {
01424 ptr_int_list c1,c2;
01425 long result=0;
01426 unsigned long v1,v2,v3;
01427 int e1,e2,b;
01428
01429
01430
01431 *c3=NULL;
01432
01433 if (t1==t2) {
01434 result=1;
01435 *t3= t1;
01436 }
01437 else if (t1==top) {
01438 *t3= t2;
01439 if (t2==top)
01440 result=1;
01441 else
01442 result=3;
01443 }
01444 else if (t2==top) {
01445 result=2;
01446 *t3= t1;
01447 }
01448 else {
01449
01450
01451
01452
01453 c1=t1->code;
01454 c2=t2->code;
01455
01456 e1=TRUE;e2=TRUE;b=TRUE;
01457
01458 if (c1!=NOT_CODED && c2!=NOT_CODED) {
01459 result=0;
01460 while (c1 && c2) {
01461
01462 *c3 = STACK_ALLOC(int_list);
01463 (*c3)->next=NULL;
01464
01465 v1=(unsigned long)(c1->value);
01466 v2=(unsigned long)(c2->value);
01467 v3=v1 & v2;
01468
01469
01470
01471 (*c3)->value=(GENERIC)v3;
01472
01473 if(v3!=v1)
01474 e1=FALSE;
01475 if(v3!=v2)
01476 e2=FALSE;
01477 if(v3)
01478 b=FALSE;
01479
01480 c1=c1->next;
01481 c2=c2->next;
01482 c3= &((*c3)->next);
01483 }
01484 *t3=NULL;
01485
01486 if(b)
01487 result=0;
01488 else
01489 if(e1)
01490 if(e2)
01491 result=1;
01492 else
01493 result=2;
01494 else
01495 if(e2)
01496 result=3;
01497 else
01498 result=4;
01499 }
01500 }
01501
01502 if (!result) *t3=nothing;
01503
01504
01505
01506 return result;
01507 }
01508
01509
01510
01511
01512
01513
01514
01515
01516 long overlap_type(t1,t2)
01517 ptr_definition t1;
01518 ptr_definition t2;
01519 {
01520 ptr_int_list c1,c2;
01521 long result=TRUE;
01522
01523 if (t1!=t2 && t1!=top && t2!=top) {
01524
01525 c1=t1->code;
01526 c2=t2->code;
01527 result=FALSE;
01528
01529 if (c1!=NOT_CODED && c2!=NOT_CODED) {
01530 while (!result && c1 && c2) {
01531 result=(((unsigned long)(c1->value)) & ((unsigned long)(c2->value)));
01532 c1=c1->next;
01533 c2=c2->next;
01534 }
01535 }
01536 }
01537
01538
01539
01540
01541
01542 return result;
01543 }
01544
01545
01546
01547
01548
01549
01550
01551
01552 long sub_CodeType(c1,c2)
01553 ptr_int_list c1;
01554 ptr_int_list c2;
01555 {
01556 if (c1!=NOT_CODED && c2!=NOT_CODED) {
01557 while (c1 && c2) {
01558 if ((unsigned long)c1->value & ~(unsigned long)c2->value) return FALSE;
01559 c1=c1->next;
01560 c2=c2->next;
01561 }
01562 }
01563 else
01564 return FALSE;
01565
01566 return TRUE;
01567 }
01568
01569
01570
01571
01572
01573
01574 long sub_type(t1,t2)
01575 ptr_definition t1;
01576 ptr_definition t2;
01577 {
01578 if (t1!=t2)
01579 if (t2!=top)
01580 {
01581 if (t1==top)
01582 return FALSE;
01583 else
01584 return sub_CodeType(t1->code, t2->code);
01585 }
01586 return TRUE;
01587 }
01588
01589
01590
01591
01592
01593
01594
01595 long matches(t1,t2,smaller)
01596 ptr_definition t1;
01597 ptr_definition t2;
01598 long *smaller;
01599 {
01600 ptr_int_list c1,c2;
01601 long result=TRUE;
01602
01603 *smaller=TRUE;
01604
01605 if (t1!=t2)
01606 if (t2!=top)
01607 if (t1==top)
01608 *smaller=FALSE;
01609 else {
01610 c1=t1->code;
01611 c2=t2->code;
01612 result=FALSE;
01613
01614 if (c1!=NOT_CODED && c2!=NOT_CODED) {
01615 while (c1 && c2) {
01616 if ((unsigned long)c1->value & (unsigned long)c2->value) result=TRUE;
01617 if ((unsigned long)c1->value & ~(unsigned long)c2->value) *smaller=FALSE;
01618 c1=c1->next;
01619 c2=c2->next;
01620 }
01621 }
01622 else
01623 *smaller=FALSE;
01624 }
01625
01626 return result;
01627 }
01628
01629
01630
01631
01632
01633
01634
01635
01636
01637
01638 long strict_matches(t1,t2,smaller)
01639 ptr_psi_term t1;
01640 ptr_psi_term t2;
01641 long *smaller;
01642 {
01643 long result,sm;
01644
01645 result=matches(t1->type,t2->type,&sm);
01646
01647 if (sm) {
01648
01649 if (t1->type==t2->type) {
01650
01651 if (t1->value!=NULL && t2->value==NULL)
01652 sm=TRUE;
01653 else
01654 sm=FALSE;
01655 }
01656 else {
01657
01658 sm=TRUE;
01659 }
01660 }
01661
01662 *smaller=sm;
01663 return result;
01664 }
01665
01666
01667
01668
01669
01670
01671
01672
01673
01674
01675
01676
01677
01678 long bit_length(c)
01679 ptr_int_list c;
01680 {
01681 unsigned long p=0,dp=0,v=0,dv=0;
01682
01683 while (c) {
01684 v=(unsigned long)c->value;
01685 if(v) {
01686 dp=p;
01687 dv=v;
01688 }
01689 c=c->next;
01690 p=p+INT_SIZE;
01691 }
01692
01693 while (dv) {
01694 dp++;
01695 dv=dv>>1;
01696 }
01697
01698 return dp;
01699 }
01700
01701
01702
01703
01704
01705
01706
01707
01708 ptr_int_list decode(c)
01709 ptr_int_list c;
01710 {
01711 ptr_int_list c2,c3,c4,result=NULL,*prev;
01712 long p;
01713
01714 p=bit_length(c);
01715
01716 while (p) {
01717 p--;
01718 c2=gamma_table[p]->code;
01719 result=cons(gamma_table[p],result);
01720 prev= &c4;
01721 *prev=NULL;
01722
01723 while (c2) {
01724 c3=STACK_ALLOC(int_list);
01725 *prev=c3;
01726 prev= &(c3->next);
01727 *prev=NULL;
01728
01729 c3->value=(GENERIC)(((unsigned long)(c->value)) & ~((unsigned long)(c2->value)));
01730
01731 c=c->next;
01732 c2=c2->next;
01733 }
01734
01735 c=c4;
01736 p=bit_length(c);
01737 }
01738
01739 return result;
01740 }