Overview
Comment: | More class loading code |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
9d448767e434ec04a33a4335cbda3918 |
User & Date: | user on 2018-05-27 22:33:21 |
Other Links: | manifest | tags |
Context
2018-06-03
| ||
22:43 | The syntax of for/next loops has been changed; also added some codes for dealing with user cache data check-in: 8b26cd42e7 user: user tags: trunk | |
2018-05-27
| ||
22:33 | More class loading code check-in: 9d448767e4 user: user tags: trunk | |
2018-05-19
| ||
22:31 | Add shell script to compile everything; support $CFLAGS and $EXE environment variables check-in: f833310c79 user: user tags: trunk | |
Changes
Modified class.c from [8167e45e03] to [d60d73f095].
︙ | ︙ | |||
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 | typedef struct MacroStack { TokenList*tok; Sint16 n; TokenList**args; struct MacroStack*next; } MacroStack; static FILE*classfp; static Uint16 tokent; static Uint32 tokenv; static int linenum=1; static char tokenstr[0x2000]; static int undef_class=1; static int undef_message=0; static int num_globals=0; static int num_functions=0; static int num_strings=0; static char pushback=0; static Hash*glohash; static InputStack*inpstack; static MacroStack*macstack; static TokenList**macros; #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, | > > > > > > > > | 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 | typedef struct MacroStack { TokenList*tok; Sint16 n; TokenList**args; struct MacroStack*next; } MacroStack; typedef struct LabelStack { Uint16 id; Uint16 addr; struct LabelStack*next; } LabelStack; static FILE*classfp; static Uint16 tokent; static Uint32 tokenv; static int linenum=1; static char tokenstr[0x2000]; static int undef_class=1; static int undef_message=0; static int num_globals=0; static int num_functions=0; static int num_strings=0; static char pushback=0; static Hash*glohash; static InputStack*inpstack; static MacroStack*macstack; 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, |
︙ | ︙ | |||
228 229 230 231 232 233 234 235 236 237 238 239 240 241 | cl->temperature=cl->misc4=cl->misc5=cl->misc6=cl->misc7=0; cl->uservars=cl->hard[0]=cl->hard[1]=cl->hard[2]=cl->hard[3]=cl->nmsg=0; cl->oflags=cl->sharp[0]=cl->sharp[1]=cl->sharp[2]=cl->sharp[3]=0; cl->shape=cl->shovable=cl->collisionLayers=cl->nimages=0; cl->cflags=f; if(undef_class<=n) undef_class=n+1; } static int look_class_name(void) { int i; int u=undef_class; for(i=1;i<undef_class;i++) { if(classes[i]) { if(!strcmp(classes[i]->name,tokenstr)) { | > > > > > > > > > > > > > > > | 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 | cl->temperature=cl->misc4=cl->misc5=cl->misc6=cl->misc7=0; cl->uservars=cl->hard[0]=cl->hard[1]=cl->hard[2]=cl->hard[3]=cl->nmsg=0; cl->oflags=cl->sharp[0]=cl->sharp[1]=cl->sharp[2]=cl->sharp[3]=0; cl->shape=cl->shovable=cl->collisionLayers=cl->nimages=0; cl->cflags=f; if(undef_class<=n) undef_class=n+1; } Uint16 get_message_ptr(int c,int m) { if(!classes[c]) return 0xFFFF; if(classes[c]->nmsg<=m) return 0xFFFF; return classes[c]->messages[m]; } static void set_message_ptr(int c,int m,int p) { if(m>classes[c]->nmsg) { classes[c]->messages=realloc(classes[c]->messages,(m+1)*sizeof(Uint16)); if(!classes[c]->messages) fatal("Allocation failed\n"); while(m>classes[c]->nmsg) classes[c]->messages[classes[c]->nmsg++]=0xFFFF; } classes[c]->messages[m]=p; } static int look_class_name(void) { int i; int u=undef_class; for(i=1;i<undef_class;i++) { if(classes[i]) { if(!strcmp(classes[i]->name,tokenstr)) { |
︙ | ︙ | |||
487 488 489 490 491 492 493 | case ':': tokent=TF_NAME|TF_ABNORMAL|fl; tokenv=OP_LABEL; break; case '&': if(fl) ParseError("Invalid use of , and = in token\n"); tokent=TF_FUNCTION|TF_ABNORMAL; | | | 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 | case ':': tokent=TF_NAME|TF_ABNORMAL|fl; tokenv=OP_LABEL; break; case '&': if(fl) ParseError("Invalid use of , and = in token\n"); tokent=TF_FUNCTION|TF_ABNORMAL; tokenv=look_hash(glohash,HASH_SIZE,0x8000,0xBFFF,num_functions+0x8000,"user functions")?:(num_functions++)+0x8000; break; case '\'': if(fl) ParseError("Invalid use of , and = in token\n"); tokent=TF_NAME|TF_KEY; for(i=8;i<256;i++) { if(heromesh_key_names[i] && !strcmp(heromesh_key_names[i],tokenstr)) { tokenv=i; |
︙ | ︙ | |||
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 | case OP_STRING: return UVALUE(pool_string(tokenstr),TY_STRING); case OP_BITCONSTANT ... OP_BITCONSTANT_LAST: return NVALUE(1<<(tokenv&31)); case 0xC000 ... 0xFFFF: return MVALUE(tokenv-0xBF00); } } ParseError("Constant value expected\n"); } #define AddInst(x) (cl->codes[ptr++]=(x),prflag=0) #define AddInst2(x,y) (cl->codes[ptr++]=(x),cl->codes[ptr++]=(y),prflag=0,peep=ptr) #define AddInstF(x,y) (cl->codes[ptr++]=(x),prflag=(y)) #define ChangeInst(x) (cl->codes[ptr-1]x,prflag=0) #define InstFlag(x) (peep<ptr && (prflag&(x))) #define Inst7bit() (peep<ptr && cl->codes[ptr-1]<0x0080) #define Inst8bit() (peep<ptr && cl->codes[ptr-1]<0x0100) #define AbbrevOp(x,y) case x: if(Inst7bit()) ChangeInst(+=0x1000|((y)<<4)); else AddInstF(x,tokent); break static int parse_instructions(int cla,int ptr,Hash*hash) { int peep=ptr; int prflag=0; Class*cl=classes[cla]; cl->codes=realloc(cl->codes,0x10000*sizeof(Uint16)); if(!cl->codes) fatal("Allocation failed\n"); for(;;) { nxttok(); if(Tokenf(TF_MACRO)) ParseError("Unexpected macro\n"); if(Tokenf(TF_INT)) { | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | > > > > > | 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 871 872 873 874 875 876 877 878 879 880 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 906 | case OP_STRING: return UVALUE(pool_string(tokenstr),TY_STRING); case OP_BITCONSTANT ... OP_BITCONSTANT_LAST: return NVALUE(1<<(tokenv&31)); case 0xC000 ... 0xFFFF: return MVALUE(tokenv-0xBF00); } } ParseError("Constant value expected\n"); } 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; } static void end_label_stack(Uint16*codes,Hash*hash) { LabelStack*s; while(labelstack) { s=labelstack; if(labelptr[s->id-0x8000]==0xFFFF) { int i; for(i=0;i<LOCAL_HASH_SIZE;i++) { if(hash[i].id==s->id) ParseError("Label :%s mentioned but never defined\n",hash[i].txt); } ParseError("Label mentioned but never defined\n"); } codes[s->addr]=labelptr[s->id-0x8000]; labelstack=s->next; free(s); } free(labelptr); labelstack=0; labelptr=0; } #define AddInst(x) (cl->codes[ptr++]=(x),prflag=0) #define AddInst2(x,y) (cl->codes[ptr++]=(x),cl->codes[ptr++]=(y),prflag=0,peep=ptr) #define AddInstF(x,y) (cl->codes[ptr++]=(x),prflag=(y)) #define ChangeInst(x) (cl->codes[ptr-1]x,prflag=0) #define InstFlag(x) (peep<ptr && (prflag&(x))) #define Inst7bit() (peep<ptr && cl->codes[ptr-1]<0x0080) #define Inst8bit() (peep<ptr && cl->codes[ptr-1]<0x0100) #define AbbrevOp(x,y) case x: if(Inst7bit()) ChangeInst(+=0x1000|((y)<<4)); else AddInstF(x,tokent); break #define FlowPush(x) do{ if(flowdepth==64) ParseError("Too much flow control nesting\n"); flowop[flowdepth]=x; flowptr[flowdepth++]=ptr; }while(0) #define FlowPop(x) do{ if(!flowdepth || flowop[--flowdepth]!=(x)) ParseError("Flow control mismatch\n"); }while(0) static int parse_instructions(int cla,int ptr,Hash*hash) { int peep=ptr; int prflag=0; Class*cl=classes[cla]; int flowdepth=0; Uint16 flowop[64]; Uint16 flowptr[64]; int x,y; cl->codes=realloc(cl->codes,0x10000*sizeof(Uint16)); if(!cl->codes) fatal("Allocation failed\n"); for(;;) { nxttok(); if(Tokenf(TF_MACRO)) ParseError("Unexpected macro\n"); if(Tokenf(TF_INT)) { numeric: if(!(tokenv&~0xFF)) AddInst(tokenv); else if(!((tokenv-1)&tokenv)) AddInst(0x87E0+__builtin_ctz(tokenv)); else if(!(tokenv&~0xFFFF)) AddInst2(OP_INT16,tokenv); else if(-256<(Sint32)tokenv) AddInst(tokenv&0x01FF); else AddInst(OP_INT32),AddInst2(tokenv>>16,tokenv); } else if(Tokenf(TF_NAME)) { switch(tokenv) { AbbrevOp(OP_ADD,0x00); AbbrevOp(OP_SUB,0x08); AbbrevOp(OP_MUL,0x10); AbbrevOp(OP_DIV,0x18); AbbrevOp(OP_MOD,0x20); |
︙ | ︙ | |||
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 | AbbrevOp(OP_GT,0x88); AbbrevOp(OP_LE,0x90); AbbrevOp(OP_GE,0x98); AbbrevOp(OP_LT_C,0xA0); AbbrevOp(OP_GT_C,0xA8); AbbrevOp(OP_LE_C,0xB0); AbbrevOp(OP_GE_C,0xB8); case OP_DROP: if(InstFlag(TF_DROP)) ChangeInst(+=0x2000); else AddInst(OP_DROP); break; default: if(Tokenf(TF_ABNORMAL)) ParseError("Invalid instruction token\n"); AddInstF(tokenv,tokent); } } else if(Tokenf(TF_FUNCTION)) { AddInst2(OP_FUNCTION,tokenv&0x3FFF); } else if(tokent==TF_OPEN) { nxttok(); if(Tokenf(TF_MACRO) || !Tokenf(TF_NAME)) ParseError("Invalid parenthesized instruction\n"); switch(tokenv) { | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | > > > > > > > | 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 | AbbrevOp(OP_GT,0x88); AbbrevOp(OP_LE,0x90); AbbrevOp(OP_GE,0x98); AbbrevOp(OP_LT_C,0xA0); AbbrevOp(OP_GT_C,0xA8); AbbrevOp(OP_LE_C,0xB0); AbbrevOp(OP_GE_C,0xB8); AbbrevOp(OP_RET,0xE0); case OP_DROP: if(InstFlag(TF_DROP)) ChangeInst(+=0x2000); else AddInst(OP_DROP); break; case OP_LOCAL: if(!cla) ParseError("Cannot use local variable in a global definition\n"); tokenv=look_hash(hash,LOCAL_HASH_SIZE,0x2000,0x27FF,cl->uservars+0x2000,"user local variables")?:(cl->uservars++)+0x2000; if(Tokenf(TF_EQUAL)) tokenv+=0x1000; AddInst(tokenv); break; case OP_LABEL: tokenv=look_hash(hash,LOCAL_HASH_SIZE,0x8000,0xFFFF,*labelptr,"labels"); if(!tokenv) tokenv=*labelptr,++*labelptr; tokenv-=0x8000; if(Tokenf(TF_COMMA)) { AddInst2(OP_CALLSUB,labelptr[tokenv]); } else if(Tokenf(TF_EQUAL)) { AddInst2(OP_GOTO,labelptr[tokenv]); } else { if(labelptr[tokenv]!=0xFFFF) ParseError("Duplicate definition of label :%s\n",tokenstr); labelptr[tokenv]=ptr; peep=ptr; break; } if(labelptr[tokenv]==0xFFFF) { LabelStack*s=malloc(sizeof(LabelStack)); if(!s) fatal("Allocation failed\n"); s->id=tokenv|0x8000; s->addr=ptr-1; s->next=labelstack; labelstack=s; } break; case OP_IF: AddInst(OP_IF); FlowPush(OP_IF); peep=++ptr; break; case OP_THEN: FlowPop(OP_IF); cl->codes[flowptr[flowdepth]]=peep=ptr; break; case OP_ELSE: FlowPop(OP_IF); x=flowptr[flowdepth]; AddInst(OP_GOTO); FlowPush(OP_IF); cl->codes[x]=peep=++ptr; break; case OP_EL: AddInst(OP_GOTO); y=++ptr; FlowPop(OP_IF); x=flowptr[flowdepth]; AddInst(OP_GOTO); FlowPush(OP_IF); cl->codes[x]=peep=++ptr; flowptr[flowdepth-1]=y; break; case OP_BEGIN: FlowPush(OP_BEGIN); peep=ptr; break; case OP_AGAIN: FlowPop(OP_BEGIN); AddInst2(OP_GOTO,flowptr[flowdepth]); break; case OP_UNTIL: FlowPop(OP_BEGIN); AddInst2(OP_IF,flowptr[flowdepth]); break; case OP_WHILE: AddInst(OP_IF); FlowPush(OP_WHILE); peep=++ptr; break; case OP_REPEAT: FlowPop(OP_WHILE); x=flowptr[flowdepth]; FlowPop(OP_BEGIN); AddInst2(OP_GOTO,flowptr[flowdepth]); cl->codes[x]=ptr; break; case OP_NEXT: FlowPop(OP_FOR); AddInst(OP_NEXT); cl->codes[flowptr[flowdepth]]=ptr; break; default: if(Tokenf(TF_ABNORMAL)) ParseError("Invalid instruction token\n"); AddInstF(tokenv,tokent); } } else if(Tokenf(TF_FUNCTION)) { AddInst2(OP_FUNCTION,tokenv&0x3FFF); } else if(tokent==TF_OPEN) { nxttok(); if(Tokenf(TF_MACRO) || !Tokenf(TF_NAME)) ParseError("Invalid parenthesized instruction\n"); switch(tokenv) { case OP_FOR: nxttok(); if(Tokenf(TF_MACRO|TF_EQUAL) || !Tokenf(TF_NAME)) ParseError("Global or local variable expected\n"); if(tokenv==OP_LOCAL) { if(!cla) ParseError("Cannot use local variable in a global definition\n"); tokenv=look_hash(hash,LOCAL_HASH_SIZE,0x2000,0x27FF,cl->uservars+0x2000,"user local variables")?:(cl->uservars++)+0x2000; } else if(tokenv<0x2000 || tokenv>0x2FFF) { ParseError("Global or local variable expected\n"); } AddInst2(OP_FOR,tokenv); FlowPush(OP_FOR); peep=++ptr; nxttok(); if(tokent!=TF_CLOSE) ParseError("Unterminated (for)\n"); break; case OP_POPUP: nxttok(); if(tokent!=TF_INT || tokenv<0 || tokenv>32) ParseError("Expected number from 0 to 32"); if(tokenv) AddInst2(OP_POPUPARGS,tokenv); else AddInst(OP_POPUP); nxttok(); if(tokent!=TF_CLOSE) ParseError("Unterminated (PopUp)\n"); break; case OP_BROADCAST: nxttok(); if(Tokenf(TF_MACRO) || !Tokenf(TF_NAME) || tokenv<0x4000 || tokenv>0x7FFF) ParseError("Class name expected\n"); AddInst2(OP_BROADCASTCLASS,tokenv-0x4000); nxttok(); if(tokent!=TF_CLOSE) ParseError("Unterminated (Broadcast)\n"); break; case OP_BIT: x=0; for(;;) { nxttok(); if(tokent==TF_CLOSE) break; if(Tokenf(TF_MACRO) || !Tokenf(TF_INT)) ParseError("Number or close parenthesis expected\n"); x|=1<<tokenv; } tokenv=x; goto numeric; default: ParseError("Invalid parenthesized instruction\n"); } } else if(tokent==TF_CLOSE) { if(peep<ptr && cl->codes[ptr-1]==OP_RET) break; if(Inst8bit()) ChangeInst(+=0x1E00); else AddInst(OP_RET); break; } else if(Tokenf(TF_EOF)) { ParseError("Unexpected end of file\n"); } else { ParseError("Invalid instruction token\n"); } if(ptr>=0xFFEF) ParseError("Out of code space\n"); } if(flowdepth) ParseError("Unterminated flow control blocks (%d levels)\n",flowdepth); cl->codes=realloc(cl->codes,ptr*sizeof(Uint16))?:cl->codes; return ptr; } static void class_definition(int cla) { } void load_classes(void) { int i; int gloptr=0; Hash*glolocalhash; char*nam=sqlite3_mprintf("%s.class",basefilename); if(!nam) fatal("Allocation failed\n"); |
︙ | ︙ | |||
940 941 942 943 944 945 946 | memset(functions,-1,sizeof(functions)); for(;;) { nxttok(); if(tokent==TF_EOF) goto done; if(tokent!=TF_OPEN) ParseError("Expected open parenthesis\n"); nxttok(); if(Tokenf(TF_FUNCTION)) { | | > > > | | > > > | 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 | memset(functions,-1,sizeof(functions)); for(;;) { nxttok(); if(tokent==TF_EOF) goto done; if(tokent!=TF_OPEN) ParseError("Expected open parenthesis\n"); nxttok(); if(Tokenf(TF_FUNCTION)) { functions[tokenv&0x3FFF]=gloptr; begin_label_stack(); gloptr=parse_instructions(0,gloptr,glolocalhash); end_label_stack(classes[0]->codes,glolocalhash); } else if(Tokenf(TF_NAME)) { switch(tokenv) { case 0x4000 ... 0x7FFF: // Class definition class_definition(tokenv-0x4000); break; case 0x0200 ... 0x02FF: case 0xC000 ... 0xFFFF: // Default message handler begin_label_stack(); set_message_ptr(0,tokenv&0x8000?(tokenv&0x3FFF)+256:tokenv-0x0200,gloptr); gloptr=parse_instructions(0,gloptr,glolocalhash); end_label_stack(classes[0]->codes,glolocalhash); break; case 0x2800 ... 0x2FFF: // Define initial values of global variables i=tokenv-0x2800; nxttok(); if(tokent==TF_CLOSE) break; initglobals[i]=parse_constant_value(); nxttok(); |
︙ | ︙ |
Modified heromesh.h from [6804915000] to [a6cc7a9107].
︙ | ︙ | |||
94 95 96 97 98 99 100 101 102 103 104 105 106 107 | extern const char*messages[0x4000]; // index is 256 less than message number extern Uint16 functions[0x4000]; extern int max_animation; // max steps in animation queue (default 32) extern Sint32 max_volume; // max total volume to allow moving diagonally (default 10000) extern Uint8 back_color; extern char**stringpool; void load_classes(void); // == bindings == typedef struct { char cmd; union { | > | 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | extern const char*messages[0x4000]; // index is 256 less than message number extern Uint16 functions[0x4000]; extern int max_animation; // max steps in animation queue (default 32) extern Sint32 max_volume; // max total volume to allow moving diagonally (default 10000) extern Uint8 back_color; extern char**stringpool; Uint16 get_message_ptr(int c,int m); void load_classes(void); // == bindings == typedef struct { char cmd; union { |
︙ | ︙ |