Free Hero Mesh

Check-in [c4bb0e3270]
Login
This is a mirror of the main repository for Free Hero Mesh. New tickets and changes will not be accepted at this mirror.
Overview
Comment:Implement the {append} macro; also start to implement arrays in the conversion
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: c4bb0e3270f18c7cde8da89239d50387f218d8e0
User & Date: user on 2021-02-25 03:28:57
Original Comment: Implement the {append} macro
Other Links: manifest | tags
Context
2021-02-25
05:36
Correct and complete the implementation of arrays in the converter check-in: 33593e7ab7 user: user tags: trunk
03:28
Implement the {append} macro; also start to implement arrays in the conversion check-in: c4bb0e3270 user: user tags: trunk
2021-02-24
07:12
Start to implement arrays (there is no way to use them yet, and the converter does not yet handle arrays). check-in: f7d9c45b1e user: user tags: trunk
Changes

Modified class.c from [88b2dee7a7] to [f9db23fff0].

127
128
129
130
131
132
133

134
135
136
137
138
139
140
#define MAC_BNOT 0xFFC8
#define MAC_CAT 0xFFC9
#define MAC_BIT 0xFFCA
#define MAC_VERSION 0xFFE0
#define MAC_DEFINE 0xFFE1
#define MAC_INCLUDE 0xFFE2
#define MAC_CALL 0xFFE3

#define MAC_UNDEFINED 0xFFFF

static TokenList*add_macro(void) {
  TokenList*o=malloc(sizeof(TokenList));
  if(!o) fatal("Allocation failed\n");
  o->t=tokent;
  o->v=tokenv;







>







127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#define MAC_BNOT 0xFFC8
#define MAC_CAT 0xFFC9
#define MAC_BIT 0xFFCA
#define MAC_VERSION 0xFFE0
#define MAC_DEFINE 0xFFE1
#define MAC_INCLUDE 0xFFE2
#define MAC_CALL 0xFFE3
#define MAC_APPEND 0xFFE4
#define MAC_UNDEFINED 0xFFFF

static TokenList*add_macro(void) {
  TokenList*o=malloc(sizeof(TokenList));
  if(!o) fatal("Allocation failed\n");
  o->t=tokent;
  o->v=tokenv;
541
542
543
544
545
546
547
548
549
550

551
552


553
554

555

556
557
558
559
560

561
562
563









564
565
566
567
568

569
570
571
572
573
574
575
        }
      }
      ParseError("Invalid Hero Mesh key name: %s\n",tokenstr);
      break;
  }
}

static void define_macro(Uint16 name) {
  int i;
  TokenList**t;

  if(glohash[name].id<0xC000) fatal("Confusion\n");
  if(glohash[name].id<MAX_MACRO+0xC000) {


    free_macro(macros[i=glohash[name].id-0xC000]);
    macros[i]=0;

  } else {

    for(i=0;i<MAX_MACRO;i++) if(!macros[i]) break;
    if(i==MAX_MACRO) ParseError("Too many macro definitions\n");
  }
  glohash[name].id=i+0xC000;
  t=macros+i;

  i=1;
  for(;;) {
    nxttok1();









    if(tokent==TF_MACRO+TF_OPEN && ++i>65000) ParseError("Too much macro nesting\n");
    if(tokent==TF_MACRO+TF_CLOSE && !--i) break;
    *t=add_macro();
    t=&(*t)->next;
  }

}

static void begin_include_file(const char*name) {
  InputStack*nxt=inpstack;
  inpstack=malloc(sizeof(InputStack));
  if(!inpstack) fatal("Allocation failed\n");
  inpstack->classfp=classfp;







|


>


>
>
|
|
>

>





>



>
>
>
>
>
>
>
>
>





>







542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
        }
      }
      ParseError("Invalid Hero Mesh key name: %s\n",tokenstr);
      break;
  }
}

static void define_macro(Uint16 name,Uint8 q) {
  int i;
  TokenList**t;
  if(main_options['M']) printf("M< %04X %04X \"%s\" %d\n",name,glohash[name].id,glohash[name].txt,q);
  if(glohash[name].id<0xC000) fatal("Confusion\n");
  if(glohash[name].id<MAX_MACRO+0xC000) {
    i=glohash[name].id-0xC000;
    if(q) {
      free_macro(macros[i]);
      macros[i]=0;
    }
  } else {
    q=1;
    for(i=0;i<MAX_MACRO;i++) if(!macros[i]) break;
    if(i==MAX_MACRO) ParseError("Too many macro definitions\n");
  }
  glohash[name].id=i+0xC000;
  t=macros+i;
  if(!q) while(*t) t=&(*t)->next;
  i=1;
  for(;;) {
    nxttok1();
    if(main_options['L'] && main_options['M']) {
      int j;
      printf("*: %5d %04X %08X \"",i,tokent,tokenv);
      for(j=0;tokenstr[j];j++) {
        if(tokenstr[j]<32 || tokenstr[j]>126) printf("<%02X>",tokenstr[j]&255);
        else putchar(tokenstr[j]);
      }
      printf("\"\n");
    }
    if(tokent==TF_MACRO+TF_OPEN && ++i>65000) ParseError("Too much macro nesting\n");
    if(tokent==TF_MACRO+TF_CLOSE && !--i) break;
    *t=add_macro();
    t=&(*t)->next;
  }
  if(main_options['M']) printf("M> %04X %04X %p\n",name,glohash[name].id,macros[glohash[name].id-0xC000]);
}

static void begin_include_file(const char*name) {
  InputStack*nxt=inpstack;
  inpstack=malloc(sizeof(InputStack));
  if(!inpstack) fatal("Allocation failed\n");
  inpstack->classfp=classfp;
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837









838
839
840
841
842
843
844
        case MAC_DEFINE:
          if(!macros) {
            macros=calloc(MAX_MACRO,sizeof(TokenList*));
            if(!macros) fatal("Allocation failed\n");
          }
          nxttok();
          if(!(tokent&TF_NAME) || tokenv!=OP_STRING) ParseError("String literal expected\n");
          define_macro(look_hash_mac());
          goto again;
        case MAC_INCLUDE:
          if(macstack) ParseError("Cannot use {include} inside of a macro\n");
          nxttok1();
          if(tokent==TF_MACRO && tokenv==1) ReturnToken(TF_EOF,0);
          if(!(tokent&TF_NAME) || tokenv!=OP_STRING) ParseError("String literal expected\n");
          n=look_hash_mac();
          nxttok1();
          if(tokent!=TF_MACRO+TF_CLOSE) ParseError("Too many macro arguments\n");
          begin_include_file(glohash[n].txt);
          goto again;
        case MAC_CALL:
          nxttok();
          if(!(tokent&TF_NAME) || tokenv!=OP_STRING) ParseError("String literal expected\n");
          tokenv=look_hash_mac();
          goto call;









        case MAC_UNDEFINED:
          ParseError("Undefined macro: {%s}\n",tokenstr);
          break;
        default:
          ParseError("Strange macro token: 0x%04X\n",glohash[tokenv].id);
      }
      if(main_options['M']) printf("M-\n");







|
















>
>
>
>
>
>
>
>
>







831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
        case MAC_DEFINE:
          if(!macros) {
            macros=calloc(MAX_MACRO,sizeof(TokenList*));
            if(!macros) fatal("Allocation failed\n");
          }
          nxttok();
          if(!(tokent&TF_NAME) || tokenv!=OP_STRING) ParseError("String literal expected\n");
          define_macro(look_hash_mac(),1);
          goto again;
        case MAC_INCLUDE:
          if(macstack) ParseError("Cannot use {include} inside of a macro\n");
          nxttok1();
          if(tokent==TF_MACRO && tokenv==1) ReturnToken(TF_EOF,0);
          if(!(tokent&TF_NAME) || tokenv!=OP_STRING) ParseError("String literal expected\n");
          n=look_hash_mac();
          nxttok1();
          if(tokent!=TF_MACRO+TF_CLOSE) ParseError("Too many macro arguments\n");
          begin_include_file(glohash[n].txt);
          goto again;
        case MAC_CALL:
          nxttok();
          if(!(tokent&TF_NAME) || tokenv!=OP_STRING) ParseError("String literal expected\n");
          tokenv=look_hash_mac();
          goto call;
        case MAC_APPEND:
          if(!macros) {
            macros=calloc(MAX_MACRO,sizeof(TokenList*));
            if(!macros) fatal("Allocation failed\n");
          }
          nxttok();
          if(!(tokent&TF_NAME) || tokenv!=OP_STRING) ParseError("String literal expected\n");
          define_macro(look_hash_mac(),0);
          goto again;
        case MAC_UNDEFINED:
          ParseError("Undefined macro: {%s}\n",tokenstr);
          break;
        default:
          ParseError("Strange macro token: 0x%04X\n",glohash[tokenv].id);
      }
      if(main_options['M']) printf("M-\n");
1609
1610
1611
1612
1613
1614
1615

1616
1617
1618
1619
1620
1621
1622
  strcpy(tokenstr,"bnot"); glohash[look_hash_mac()].id=MAC_BNOT;
  strcpy(tokenstr,"cat"); glohash[look_hash_mac()].id=MAC_CAT;
  strcpy(tokenstr,"bit"); glohash[look_hash_mac()].id=MAC_BIT;
  strcpy(tokenstr,"version"); glohash[look_hash_mac()].id=MAC_VERSION;
  strcpy(tokenstr,"define"); glohash[look_hash_mac()].id=MAC_DEFINE;
  strcpy(tokenstr,"include"); glohash[look_hash_mac()].id=MAC_INCLUDE;
  strcpy(tokenstr,"call"); glohash[look_hash_mac()].id=MAC_CALL;

  if(main_options['L']) {
    for(;;) {
      nxttok();
      printf("** %5d %04X %08X \"",linenum,tokent,tokenv);
      for(i=0;tokenstr[i];i++) {
        if(tokenstr[i]<32 || tokenstr[i]>126) printf("<%02X>",tokenstr[i]&255);
        else putchar(tokenstr[i]);







>







1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
  strcpy(tokenstr,"bnot"); glohash[look_hash_mac()].id=MAC_BNOT;
  strcpy(tokenstr,"cat"); glohash[look_hash_mac()].id=MAC_CAT;
  strcpy(tokenstr,"bit"); glohash[look_hash_mac()].id=MAC_BIT;
  strcpy(tokenstr,"version"); glohash[look_hash_mac()].id=MAC_VERSION;
  strcpy(tokenstr,"define"); glohash[look_hash_mac()].id=MAC_DEFINE;
  strcpy(tokenstr,"include"); glohash[look_hash_mac()].id=MAC_INCLUDE;
  strcpy(tokenstr,"call"); glohash[look_hash_mac()].id=MAC_CALL;
  strcpy(tokenstr,"append"); glohash[look_hash_mac()].id=MAC_APPEND;
  if(main_options['L']) {
    for(;;) {
      nxttok();
      printf("** %5d %04X %08X \"",linenum,tokent,tokenv);
      for(i=0;tokenstr[i];i++) {
        if(tokenstr[i]<32 || tokenstr[i]>126) printf("<%02X>",tokenstr[i]&255);
        else putchar(tokenstr[i]);

Modified class.doc from [30af8774af] to [31119da0f4].

127
128
129
130
131
132
133




134
135
136
137
138
139
140
  Subtraction.

{* <numbers...>}
  Multiplication. The result is 1 if no arguments are specified.

{/ <number> <number>}
  Division.





{band <numbers...>}
  Bitwise AND. The result is -1 if no arguments are specified.

{bit <numbers...>}
  The numbers are in range 0 to 31 and denote bit positions; the result is
  a number with only those bits set.







>
>
>
>







127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
  Subtraction.

{* <numbers...>}
  Multiplication. The result is 1 if no arguments are specified.

{/ <number> <number>}
  Division.

{append <string> <tokens...>}
  This works like {define} but adds to an existing definition rather than
  replacing the definition.

{band <numbers...>}
  Bitwise AND. The result is -1 if no arguments are specified.

{bit <numbers...>}
  The numbers are in range 0 to 31 and denote bit positions; the result is
  a number with only those bits set.

Modified mbtofhm.c from [443bde1bde] to [86b6c496cf].

410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
    }
  }
}

#define SubOpcode(...) do{ static const char*const ss[]={__VA_ARGS__}; if(*op>=sizeof(ss)/sizeof(*ss)) goto unknown; fprintf(fp," %s",ss[*op]); }while(0)
#define PushFlowControl() do{ flowblock[flowptr]=ofs+2+(op[1]<<1)+(op[2]<<9); if(++flowptr==64) fatal("Too many nested flow controls\n"); len+=2; ind+=2; }while(0)

static void class_codes(FILE*fp,const unsigned char*op,int ofs,const unsigned char*lbl,int nlbl,const unsigned char*subs,int nsubs,unsigned char*vars) {
  static const char*const stdvars[256]={
    [0]="Class","Temperature","Shape",
    [4]="Xloc","Yloc","Dir","Image","Inertia","Misc1","Misc2","Misc3","Misc4","Misc5","Misc6","Misc7",
    [16]="Arrived","Departed","Arrivals","Departures",
    [32]="Busy","Invisible","UserSignal","UserState","KeyCleared","Player","Destroyed","Stealthy","VisualOnly",
    [48]="Msg","From","Arg1","Arg2",
    [64]="Density","Volume","Strength","Weight","Distance","Height","Climb",







|







410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
    }
  }
}

#define SubOpcode(...) do{ static const char*const ss[]={__VA_ARGS__}; if(*op>=sizeof(ss)/sizeof(*ss)) goto unknown; fprintf(fp," %s",ss[*op]); }while(0)
#define PushFlowControl() do{ flowblock[flowptr]=ofs+2+(op[1]<<1)+(op[2]<<9); if(++flowptr==64) fatal("Too many nested flow controls\n"); len+=2; ind+=2; }while(0)

static void class_codes(FILE*fp,const unsigned char*op,int ofs,const unsigned char*lbl,int nlbl,const unsigned char*subs,int nsubs,unsigned char*vars,const char*cname) {
  static const char*const stdvars[256]={
    [0]="Class","Temperature","Shape",
    [4]="Xloc","Yloc","Dir","Image","Inertia","Misc1","Misc2","Misc3","Misc4","Misc5","Misc6","Misc7",
    [16]="Arrived","Departed","Arrivals","Departures",
    [32]="Busy","Invisible","UserSignal","UserState","KeyCleared","Player","Destroyed","Stealthy","VisualOnly",
    [48]="Msg","From","Arg1","Arg2",
    [64]="Density","Volume","Strength","Weight","Distance","Height","Climb",
558
559
560
561
562
563
564









565
566
567
568
569
570
571
        break;
      case 39:
        fprintf(fp," Self");
        break;
      case 40:
        fprintf(fp," ObjClassAt");
        break;









      case 48:
        fprintf(fp," Key");
        break;
      case 49:
        SubOpcode("STOP","ONCE","LOOP","3","4","5","6","7","OSC","9","OSCLOOP");
        break;
      case 50:







>
>
>
>
>
>
>
>
>







558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
        break;
      case 39:
        fprintf(fp," Self");
        break;
      case 40:
        fprintf(fp," ObjClassAt");
        break;
      case 47:
        z=-1;
        for(x=0;x<nsubs;x++) {
          if(op[1]==subs[x*10+8] && op[2]==subs[x*10+9]) {
            z=x; break;
          }
        }
        if(z==-1) fprintf(fp," @???"); else fprintf(fp," @%s.%s",cname,subs+z*10);
        len+=2; break;
      case 48:
        fprintf(fp," Key");
        break;
      case 49:
        SubOpcode("STOP","ONCE","LOOP","3","4","5","6","7","OSC","9","OSCLOOP");
        break;
      case 50:
604
605
606
607
608
609
610




611
612
613
614
615
616
617
      case 59:
        fprintf(fp," bit%d",*op);
        break;
      case 60:
        if(*op<50) fprintf(fp," %s",standard_sound_names[*op]);
        else goto unknown;
        break;




      case 64:
        fprintf(fp," Send");
        break;
      case 65:
        fprintf(fp," ,Send");
        break;
      case 66:







>
>
>
>







613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
      case 59:
        fprintf(fp," bit%d",*op);
        break;
      case 60:
        if(*op<50) fprintf(fp," %s",standard_sound_names[*op]);
        else goto unknown;
        break;
      case 61:
        if(*op!=1) goto unknown;
        fprintf(fp," GetArray");
        break;
      case 64:
        fprintf(fp," Send");
        break;
      case 65:
        fprintf(fp," ,Send");
        break;
      case 66:
722
723
724
725
726
727
728
















729
730
731
732
733
734
735
        fprintf(fp," JumpTo .");
        st=0; break;
      case 111:
        fprintf(fp," ,JumpTo .");
        st=0; break;
      case 112: case 113:
        fprintf(fp," Sound");
















        st=0; break;
      case 126:
        fprintf(fp," Animate");
        st=0; break;
      case 127: case 128:
        fprintf(fp," LoseLevel");
        st=0; break;







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
        fprintf(fp," JumpTo .");
        st=0; break;
      case 111:
        fprintf(fp," ,JumpTo .");
        st=0; break;
      case 112: case 113:
        fprintf(fp," Sound");
        st=0; break;
      case 114:
        if(op[1]) {
          SubOpcode(";","SetArray","InitArray");
        } else {
          x=0;
          y=nlbl;
          z=-1;
          while(y) {
            if(lbl[x+8]|(lbl[x+9]<<8))==ofs>>1) break;
            y--;
            x+=10;
          }
          if(z!=-1) fprintf(fp,"{Array @%s.%s %d %d}",cname,lbl+z*10,op[1],op[3]);
          len+=4*(op[1]*op[3]+1);
        }
        st=0; break;
      case 126:
        fprintf(fp," Animate");
        st=0; break;
      case 127: case 128:
        fprintf(fp," LoseLevel");
        st=0; break;
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
        fprintf(fp,")\n");
      } else {
        fprintf(fp,"  (Sharp %d)\n",c->attr[38]|(c->attr[39]<<8));
      }
    }
    if(c->subscode[1] || c->subscode[0]>2) {
      fprintf(fp,"  (SUBS");
      class_codes(fp,c->subscode+2,2,c->subslbl,c->nsubslbl,c->subslbl,c->nsubslbl,c->vars);
      fprintf(fp,"\n  )\n");
    }
    o=4;
    for(i=0;i<c->nmsgs;i++) {
      fprintf(fp,"  (");
      j=c->msgscode[i][0]|(c->msgscode[i][1]<<8);
      if(j<20) fprintf(fp,"%s",standard_message_names[j]);
      else if(j-20<nusermsg && usermsg[j-20]) fprintf(fp,"#%s",usermsg[j-20]);
      else fprintf(fp,"#???%d",j);
      class_codes(fp,c->msgscode[i]+4,o,c->msgslbl,c->nmsgslbl,c->subslbl,c->nsubslbl,c->vars);
      o+=(c->msgscode[i][2]<<1)|(c->msgscode[i][3]<<9);
      fprintf(fp,"\n  )\n");
    }
    fprintf(fp,")\n");
  }
  fclose(fp);
}







|









|







910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
        fprintf(fp,")\n");
      } else {
        fprintf(fp,"  (Sharp %d)\n",c->attr[38]|(c->attr[39]<<8));
      }
    }
    if(c->subscode[1] || c->subscode[0]>2) {
      fprintf(fp,"  (SUBS");
      class_codes(fp,c->subscode+2,2,c->subslbl,c->nsubslbl,c->subslbl,c->nsubslbl,c->vars,c->name);
      fprintf(fp,"\n  )\n");
    }
    o=4;
    for(i=0;i<c->nmsgs;i++) {
      fprintf(fp,"  (");
      j=c->msgscode[i][0]|(c->msgscode[i][1]<<8);
      if(j<20) fprintf(fp,"%s",standard_message_names[j]);
      else if(j-20<nusermsg && usermsg[j-20]) fprintf(fp,"#%s",usermsg[j-20]);
      else fprintf(fp,"#???%d",j);
      class_codes(fp,c->msgscode[i]+4,o,c->msgslbl,c->nmsgslbl,c->subslbl,c->nsubslbl,c->vars,c->name);
      o+=(c->msgscode[i][2]<<1)|(c->msgscode[i][3]<<9);
      fprintf(fp,"\n  )\n");
    }
    fprintf(fp,")\n");
  }
  fclose(fp);
}