Free Hero Mesh

Diff
Login
This is a mirror of the main repository for Free Hero Mesh. New tickets and changes will not be accepted at this mirror.

Differences From Artifact [e332480536]:

To Artifact [8c2ce69ad5]:


56
57
58
59
60
61
62

63
64
65
66
67
68
69
typedef struct {
  Uint16 id;
  char*txt;
} Hash;

/*
  Global hash:

    2800-2FFF = Variables
    8000-BFFF = Functions
    C000-FFFF = Macros
  Local hash:
    2000-27FF = Variables
    8000-FFFF = Labels
*/







>







56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
typedef struct {
  Uint16 id;
  char*txt;
} Hash;

/*
  Global hash:
    1000-10FF = User flags
    2800-2FFF = Variables
    8000-BFFF = Functions
    C000-FFFF = Macros
  Local hash:
    2000-27FF = Variables
    8000-FFFF = Labels
*/
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
static TokenList**macros;
static LabelStack*labelstack;
static Uint16*labelptr;

#define ParseError(a,...) fatal("On line %d: " a,linenum,##__VA_ARGS__)

static const unsigned char chkind[256]={
  ['$']=1, ['!']=1, ['\'']=1, ['#']=1, ['@']=1, ['%']=1, ['&']=1, [':']=1,
  ['0'...'9']=2, ['-']=2, ['+']=2,
  ['A'...'Z']=3, ['a'...'z']=3, ['_']=3, ['?']=3, ['.']=3, ['*']=3, ['/']=3,
};

#define MIN_VERSION 0
#define MAX_VERSION 0








|







113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
static TokenList**macros;
static LabelStack*labelstack;
static Uint16*labelptr;

#define ParseError(a,...) fatal("On line %d: " a,linenum,##__VA_ARGS__)

static const unsigned char chkind[256]={
  ['$']=1, ['!']=1, ['\'']=1, ['#']=1, ['@']=1, ['%']=1, ['&']=1, [':']=1, ['^']=1,
  ['0'...'9']=2, ['-']=2, ['+']=2,
  ['A'...'Z']=3, ['a'...'z']=3, ['_']=3, ['?']=3, ['.']=3, ['*']=3, ['/']=3,
};

#define MIN_VERSION 0
#define MAX_VERSION 0

550
551
552
553
554
555
556




557
558
559
560
561
562
563
        if(heromesh_key_names[i] && !strcmp(heromesh_key_names[i],tokenstr)) {
          tokenv=i;
          return;
        }
      }
      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);







>
>
>
>







551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
        if(heromesh_key_names[i] && !strcmp(heromesh_key_names[i],tokenstr)) {
          tokenv=i;
          return;
        }
      }
      ParseError("Invalid Hero Mesh key name: %s\n",tokenstr);
      break;
    case '^':
      tokent=TF_NAME|TF_ABNORMAL|fl;
      tokenv=OP_USERFLAG;
      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);
924
925
926
927
928
929
930










931
932
933
934
935
936
937
  y=tokenv;
  if(x<1 || x>64 || y<1 || y>1024) ParseError("Array dimension out of range\n");
  z=array_size;
  if(z+x*y>0xFFFE) ParseError("Out of array memory\n");
  array_size+=x*y;
  return z|((y-1)<<16)|((x-1)<<26);
}











static void begin_label_stack(void) {
  labelstack=0;
  labelptr=malloc(0x8000*sizeof(Uint16));
  if(!labelptr) fatal("Allocation failed\n");
  memset(labelptr,255,0x8000*sizeof(Uint16));
  *labelptr=0x8001;







>
>
>
>
>
>
>
>
>
>







929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
  y=tokenv;
  if(x<1 || x>64 || y<1 || y>1024) ParseError("Array dimension out of range\n");
  z=array_size;
  if(z+x*y>0xFFFE) ParseError("Out of array memory\n");
  array_size+=x*y;
  return z|((y-1)<<16)|((x-1)<<26);
}

static void define_user_flags(Uint16 ca,Uint16 cb) {
  for(;;) {
    nxttok();
    if(tokent==TF_CLOSE) break;
    if(!Tokenf(TF_NAME) || tokenv!=OP_USERFLAG) ParseError("User flag or close parenthesis expected\n");
    if(ca>cb) ParseError("Too many user flags of this kind\n");
    if(look_hash(glohash,HASH_SIZE,0x1000,0x10FF,ca++,"user flags")) ParseError("Duplicate definition of ^%s\n",tokenstr);
  }
}

static void begin_label_stack(void) {
  labelstack=0;
  labelptr=malloc(0x8000*sizeof(Uint16));
  if(!labelptr) fatal("Allocation failed\n");
  memset(labelptr,255,0x8000*sizeof(Uint16));
  *labelptr=0x8001;
1114
1115
1116
1117
1118
1119
1120





























1121
1122
1123
1124
1125
1126
1127
          break;
        case OP_MBEGIN:
          FlowPush(OP_BEGIN);
          AddInst(OP_TMARK);
          AddInst(OP_IF);
          FlowPush(OP_WHILE);
          peep=++ptr;





























          break;
        default:
          if(Tokenf(TF_ABNORMAL)) ParseError("Invalid instruction token\n");
          if(compat && Tokenf(TF_COMPAT) && Tokenf(TF_EQUAL)) ++tokenv;
          AddInstF(tokenv,tokent);
      }
    } else if(Tokenf(TF_FUNCTION)) {







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







1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
          break;
        case OP_MBEGIN:
          FlowPush(OP_BEGIN);
          AddInst(OP_TMARK);
          AddInst(OP_IF);
          FlowPush(OP_WHILE);
          peep=++ptr;
          break;
        case OP_USERFLAG:
          // Opcodes 0x1F00-0x1F1F read a bit; opcodes 0x1F20-0x1F3F set/clear a bit
          x=look_hash(glohash,HASH_SIZE,0x1000,0x10FF,0,"user flags");
          if(!x) ParseError("User flag ^%s not defined\n",tokenstr);
          if(x<0x1020) y=OP_MISC4;
          else if(x<0x1040) y=OP_MISC5;
          else if(x<0x1060) y=OP_MISC6;
          else if(x<0x1080) y=OP_MISC7;
          else y=OP_COLLISIONLAYERS;
          if(Tokenf(TF_EQUAL)) {
            if(x>=0x1080) ParseError("Flag ^%s is read-only\n",tokenstr);
            if(Tokenf(TF_COMMA)) {
              AddInst(OP_OVER);
              AddInst(y+0x0800);
              AddInst(0x1F20|(x&0x1F));
              AddInst(y+0x1800);
            } else {
              AddInst(y);
              AddInst(0x1F20|(x&0x1F));
              AddInst(y+0x1000);
            }
          } else if(Tokenf(TF_COMMA)) {
            AddInst(y+0x0800);
            AddInst(0x1F00|(x&0x1F));
          } else {
            AddInst(y);
            AddInst(0x1F00|(x&0x1F));
          }
          break;
        default:
          if(Tokenf(TF_ABNORMAL)) ParseError("Invalid instruction token\n");
          if(compat && Tokenf(TF_COMPAT) && Tokenf(TF_EQUAL)) ++tokenv;
          AddInstF(tokenv,tokent);
      }
    } else if(Tokenf(TF_FUNCTION)) {
1418
1419
1420
1421
1422
1423
1424










1425
1426
1427
1428
1429
1430
1431
      if(tokenv<0 || tokenv>=cl->nimages) ParseError("Image number out of range\n");
      cl->images[tokenv]|=0x8000;
    } else {
      ParseError("Expected ( or ) or number\n");
    }
  }
}











static void class_definition(int cla,sqlite3_stmt*vst) {
  Hash*hash=calloc(LOCAL_HASH_SIZE,sizeof(Hash));
  Class*cl=classes[cla];
  int ptr=0;
  int compat=0;
  int i;







>
>
>
>
>
>
>
>
>
>







1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
      if(tokenv<0 || tokenv>=cl->nimages) ParseError("Image number out of range\n");
      cl->images[tokenv]|=0x8000;
    } else {
      ParseError("Expected ( or ) or number\n");
    }
  }
}

static void class_user_flag(Class*cl) {
  int x=look_hash(glohash,HASH_SIZE,0x1000,0x10FF,0,"user flags");
  if(!x) ParseError("User flag ^%s not defined\n",tokenstr);
  if(x<0x1020) cl->misc4|=1L<<(x&0x1F);
  else if(x<0x1040) cl->misc5|=1L<<(x&0x1F);
  else if(x<0x1060) cl->misc6|=1L<<(x&0x1F);
  else if(x<0x1080) cl->misc7|=1L<<(x&0x1F);
  else cl->collisionLayers|=1<<(x&0x1F);
}

static void class_definition(int cla,sqlite3_stmt*vst) {
  Hash*hash=calloc(LOCAL_HASH_SIZE,sizeof(Hash));
  Class*cl=classes[cla];
  int ptr=0;
  int compat=0;
  int i;
1565
1566
1567
1568
1569
1570
1571

1572
1573
1574
1575
1576
1577
1578
        case OP_COMPATIBLE_C: cl->cflags|=CF_COMPATIBLE; break;
        case OP_QUIZ: cl->cflags|=CF_QUIZ; break;
        case OP_INVISIBLE: cl->oflags|=OF_INVISIBLE; break;
        case OP_VISUALONLY: cl->oflags|=OF_VISUALONLY; break;
        case OP_STEALTHY: cl->oflags|=OF_STEALTHY; break;
        case OP_USERSTATE: cl->oflags|=OF_USERSTATE; break;
        case OP_SHOVABLE: cl->shovable=0x55; break;

        default: ParseError("Invalid directly inside of a class definition\n");
      }
    } else if(Tokenf(TF_CLOSE)) {
      break;
    } else {
      ParseError("Invalid directly inside of a class definition\n");
    }







>







1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
        case OP_COMPATIBLE_C: cl->cflags|=CF_COMPATIBLE; break;
        case OP_QUIZ: cl->cflags|=CF_QUIZ; break;
        case OP_INVISIBLE: cl->oflags|=OF_INVISIBLE; break;
        case OP_VISUALONLY: cl->oflags|=OF_VISUALONLY; break;
        case OP_STEALTHY: cl->oflags|=OF_STEALTHY; break;
        case OP_USERSTATE: cl->oflags|=OF_USERSTATE; break;
        case OP_SHOVABLE: cl->shovable=0x55; break;
        case OP_USERFLAG: class_user_flag(cl); break;
        default: ParseError("Invalid directly inside of a class definition\n");
      }
    } else if(Tokenf(TF_CLOSE)) {
      break;
    } else {
      ParseError("Invalid directly inside of a class definition\n");
    }
1753
1754
1755
1756
1757
1758
1759





1760
1761
1762
1763
1764
1765
1766
        case OP_VOLUME:
          nxttok();
          if(tokent!=TF_INT) ParseError("Number expected\n");
          max_volume=tokenv;
          nxttok();
          if(tokent!=TF_CLOSE) ParseError("Expected close parenthesis\n");
          break;





        default:
          ParseError("Invalid top level definition: %s\n",tokenstr);
      }
    } else {
      ParseError("Invalid top level definition\n");
    }
  }







>
>
>
>
>







1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
        case OP_VOLUME:
          nxttok();
          if(tokent!=TF_INT) ParseError("Number expected\n");
          max_volume=tokenv;
          nxttok();
          if(tokent!=TF_CLOSE) ParseError("Expected close parenthesis\n");
          break;
        case OP_MISC4: define_user_flags(0x1000,0x101F); break;
        case OP_MISC5: define_user_flags(0x1020,0x103F); break;
        case OP_MISC6: define_user_flags(0x1040,0x105F); break;
        case OP_MISC7: define_user_flags(0x1060,0x107F); break;
        case OP_COLLISIONLAYERS: define_user_flags(0x1C80,0x1C87); break;
        default:
          ParseError("Invalid top level definition: %s\n",tokenstr);
      }
    } else {
      ParseError("Invalid top level definition\n");
    }
  }