| 1 | #include <stdio.h> |
|---|
| 2 | #include <stdlib.h> |
|---|
| 3 | #include <string.h> |
|---|
| 4 | /* #include <malloc.h> */ |
|---|
| 5 | |
|---|
| 6 | #include "aisc.h" |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | CL *aisc_calc_blocks(CL * co, CL * afor, CL * aif, int up) { |
|---|
| 10 | CL *oif; |
|---|
| 11 | CL *ofor; |
|---|
| 12 | CL *aelse; |
|---|
| 13 | CL *anext; |
|---|
| 14 | |
|---|
| 15 | while (co) { |
|---|
| 16 | while (co &&(!co->command)) { |
|---|
| 17 | gl->pc = co; |
|---|
| 18 | co->IF = aif; |
|---|
| 19 | co->FOR = afor; |
|---|
| 20 | co = co->next; |
|---|
| 21 | } |
|---|
| 22 | if (!co) return 0; |
|---|
| 23 | gl->pc = co; |
|---|
| 24 | co->IF = aif; |
|---|
| 25 | co->FOR = afor; |
|---|
| 26 | gl->pc = co; |
|---|
| 27 | switch (co->command) { |
|---|
| 28 | case NEXT: |
|---|
| 29 | case ENDFOR: |
|---|
| 30 | case ELSE: |
|---|
| 31 | case ELSEIF: |
|---|
| 32 | case ENDIF: |
|---|
| 33 | return co; |
|---|
| 34 | case IF: |
|---|
| 35 | oif = aif; |
|---|
| 36 | aif = co; |
|---|
| 37 | co = aisc_calc_blocks(co->next,afor,aif,0); |
|---|
| 38 | if (!co) { |
|---|
| 39 | print_error("IF without ELSE or ENDIF"); |
|---|
| 40 | return 0; |
|---|
| 41 | } |
|---|
| 42 | if(co->command == ELSE) { |
|---|
| 43 | aif->ELSE=co; |
|---|
| 44 | aelse = co; |
|---|
| 45 | co = aisc_calc_blocks(co->next,afor,aif,0); |
|---|
| 46 | if (!co){ |
|---|
| 47 | gl->pc = aif; |
|---|
| 48 | print_error("ELSE without ENDIF"); |
|---|
| 49 | return 0; |
|---|
| 50 | } |
|---|
| 51 | if (co->command!=ENDIF){ |
|---|
| 52 | print_error("ELSE without ENDIF"); |
|---|
| 53 | return 0; |
|---|
| 54 | } |
|---|
| 55 | aif->ENDIF=co; |
|---|
| 56 | aelse->ENDIF=co; |
|---|
| 57 | if (up) return co; |
|---|
| 58 | }else if(co->command == ELSEIF) { |
|---|
| 59 | CL *cod; |
|---|
| 60 | cod = make_CL(); |
|---|
| 61 | *cod = *co; |
|---|
| 62 | cod->command = IF; |
|---|
| 63 | co->command = ELSE; |
|---|
| 64 | co ->next = cod; |
|---|
| 65 | co->str = NULL; |
|---|
| 66 | co->path = strdup(co->path); |
|---|
| 67 | aif->ELSE=co; |
|---|
| 68 | aelse = co; |
|---|
| 69 | co = aisc_calc_blocks(cod,afor,aif,1); |
|---|
| 70 | if (!co) { |
|---|
| 71 | gl->pc = aif; |
|---|
| 72 | print_error("ELSEIF without ENDIF or ELSE"); |
|---|
| 73 | return 0; |
|---|
| 74 | } |
|---|
| 75 | if (co->command!=ENDIF){ |
|---|
| 76 | print_error("ELSEIF without ENDIF"); |
|---|
| 77 | return 0; |
|---|
| 78 | } |
|---|
| 79 | aif->ENDIF=co; |
|---|
| 80 | cod->ENDIF=co; |
|---|
| 81 | aelse->ENDIF=co; |
|---|
| 82 | if (up) return co; |
|---|
| 83 | }else if (co->command == ENDIF) { |
|---|
| 84 | aif->ELSE=co; |
|---|
| 85 | aif->ENDIF=co; |
|---|
| 86 | if (up) return co; |
|---|
| 87 | }else{ |
|---|
| 88 | print_error("IF without ELSE or ENDIF"); |
|---|
| 89 | return 0; |
|---|
| 90 | } |
|---|
| 91 | aif = oif; |
|---|
| 92 | break; |
|---|
| 93 | case FOR: |
|---|
| 94 | ofor = afor; |
|---|
| 95 | afor = co; |
|---|
| 96 | co = aisc_calc_blocks(co->next,afor,aif,0); |
|---|
| 97 | if (!co) { |
|---|
| 98 | gl->pc = afor; |
|---|
| 99 | print_error("FOR without NEXT or ENDFOR"); |
|---|
| 100 | return 0; |
|---|
| 101 | } |
|---|
| 102 | if(co->command == NEXT) { |
|---|
| 103 | afor->NEXT=co; |
|---|
| 104 | anext = co; |
|---|
| 105 | co = aisc_calc_blocks(co->next,afor,aif,0); |
|---|
| 106 | if (!co){ |
|---|
| 107 | gl->pc = aif; |
|---|
| 108 | print_error("NEXT without ENDFOR"); |
|---|
| 109 | return 0; |
|---|
| 110 | } |
|---|
| 111 | if (co->command!=ENDFOR){ |
|---|
| 112 | print_error("NEXT without ENDFOR"); |
|---|
| 113 | return 0; |
|---|
| 114 | } |
|---|
| 115 | afor->ENDFOR=co; |
|---|
| 116 | anext->ENDFOR=co; |
|---|
| 117 | |
|---|
| 118 | } else if (co->command == ENDFOR) { |
|---|
| 119 | afor->ENDFOR=co; |
|---|
| 120 | afor->NEXT=co; |
|---|
| 121 | co->command = NEXT; |
|---|
| 122 | }else{ |
|---|
| 123 | print_error("FOR without NEXT or ENDFOR"); |
|---|
| 124 | return 0; |
|---|
| 125 | } |
|---|
| 126 | afor = ofor; |
|---|
| 127 | break; |
|---|
| 128 | default: |
|---|
| 129 | break; |
|---|
| 130 | |
|---|
| 131 | } |
|---|
| 132 | co = co->next; |
|---|
| 133 | } |
|---|
| 134 | return 0; |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | int aisc_calc_special_commands(void) |
|---|
| 138 | { |
|---|
| 139 | CL *co; |
|---|
| 140 | char *buf1,*buf2; |
|---|
| 141 | for (co=gl->prg;co;co=co->next) { |
|---|
| 142 | if (!strncmp(co->str,"IF",2)) { |
|---|
| 143 | buf1 = co->str+2; |
|---|
| 144 | co->command = IF; |
|---|
| 145 | READ_SPACES(buf1); |
|---|
| 146 | buf2 = strdup(buf1); |
|---|
| 147 | free(co->str); |
|---|
| 148 | co->str = buf2; |
|---|
| 149 | continue; |
|---|
| 150 | } |
|---|
| 151 | if (!strncmp(co->str,"ELSEIF",6)) { |
|---|
| 152 | buf1 = co->str+6; |
|---|
| 153 | co->command = ELSEIF; |
|---|
| 154 | READ_SPACES(buf1); |
|---|
| 155 | buf2 = strdup(buf1); |
|---|
| 156 | free(co->str); |
|---|
| 157 | co->str = buf2; |
|---|
| 158 | continue; |
|---|
| 159 | } |
|---|
| 160 | if (!strncmp(co->str,"ELSE",4)) { |
|---|
| 161 | buf1 = co->str+4; |
|---|
| 162 | co->command = ELSE; |
|---|
| 163 | READ_SPACES(buf1); |
|---|
| 164 | buf2 = strdup(buf1); |
|---|
| 165 | free(co->str); |
|---|
| 166 | co->str = buf2; |
|---|
| 167 | continue; |
|---|
| 168 | } |
|---|
| 169 | if (!strncmp(co->str,"ENDIF",5)) { |
|---|
| 170 | buf1 = co->str+5; |
|---|
| 171 | co->command = ENDIF; |
|---|
| 172 | READ_SPACES(buf1); |
|---|
| 173 | buf2 = strdup(buf1); |
|---|
| 174 | free(co->str); |
|---|
| 175 | co->str = buf2; |
|---|
| 176 | continue; |
|---|
| 177 | } |
|---|
| 178 | if (!strncmp(co->str,"FOR",3)) { |
|---|
| 179 | buf1 = co->str+3; |
|---|
| 180 | co->command = FOR; |
|---|
| 181 | READ_SPACES(buf1); |
|---|
| 182 | buf2 = strdup(buf1); |
|---|
| 183 | free(co->str); |
|---|
| 184 | co->str = buf2; |
|---|
| 185 | continue; |
|---|
| 186 | } |
|---|
| 187 | if (!strncmp(co->str,"ENDFOR",6)) { |
|---|
| 188 | buf1 = co->str+6; |
|---|
| 189 | co->command = ENDFOR; |
|---|
| 190 | READ_SPACES(buf1); |
|---|
| 191 | buf2 = strdup(buf1); |
|---|
| 192 | free(co->str); |
|---|
| 193 | co->str = buf2; |
|---|
| 194 | continue; |
|---|
| 195 | } |
|---|
| 196 | if (!strncmp(co->str,"NEXT",4)) { |
|---|
| 197 | buf1 = co->str+4; |
|---|
| 198 | co->command = NEXT; |
|---|
| 199 | READ_SPACES(buf1); |
|---|
| 200 | buf2 = strdup(buf1); |
|---|
| 201 | free(co->str); |
|---|
| 202 | co->str = buf2; |
|---|
| 203 | continue; |
|---|
| 204 | } |
|---|
| 205 | if (!strncmp(co->str,"FUNCTION",8)) { |
|---|
| 206 | char *s,*s2; |
|---|
| 207 | buf1 = co->str+8; |
|---|
| 208 | co->command = FUNCTION; |
|---|
| 209 | READ_SPACES(buf1); |
|---|
| 210 | for (s=buf1;!gl->b_tab[(int)(*s)];s++) ; |
|---|
| 211 | if (*s) { |
|---|
| 212 | *s = 0; |
|---|
| 213 | s++; |
|---|
| 214 | READ_SPACES(s); |
|---|
| 215 | s2 = strdup(s); |
|---|
| 216 | }else{ |
|---|
| 217 | s2 = strdup(""); |
|---|
| 218 | } |
|---|
| 219 | buf2 = strdup(buf1); |
|---|
| 220 | free(co->str); |
|---|
| 221 | co->str = s2; |
|---|
| 222 | sprintf(string_buf,"%li",(long)co); |
|---|
| 223 | write_hash(gl->fns,buf2,string_buf); |
|---|
| 224 | continue; |
|---|
| 225 | } |
|---|
| 226 | if (!strncmp(co->str,"LABEL",5)) { |
|---|
| 227 | buf1 = co->str+5; |
|---|
| 228 | co->command = LABEL; |
|---|
| 229 | READ_SPACES(buf1); |
|---|
| 230 | buf2 = strdup(buf1); |
|---|
| 231 | free(co->str); |
|---|
| 232 | co->str = buf2; |
|---|
| 233 | sprintf(string_buf,"%li",(long)co); |
|---|
| 234 | write_hash(gl->fns,buf2,string_buf); |
|---|
| 235 | continue; |
|---|
| 236 | } |
|---|
| 237 | } |
|---|
| 238 | return 0; |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | static int hash_index(const char *key, int size) |
|---|
| 242 | { |
|---|
| 243 | int x; |
|---|
| 244 | const char *p; |
|---|
| 245 | char c; |
|---|
| 246 | |
|---|
| 247 | p = key; |
|---|
| 248 | x = 1; |
|---|
| 249 | while ( (c=*(p++))){ |
|---|
| 250 | x = (x<<1) ^ c; |
|---|
| 251 | } |
|---|
| 252 | x %= size; |
|---|
| 253 | if (x<0) x+= size; |
|---|
| 254 | return x; |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | |
|---|
| 258 | struct hash_struct *create_hash(int size) |
|---|
| 259 | { |
|---|
| 260 | struct hash_struct *hs; |
|---|
| 261 | hs = (struct hash_struct *)calloc(sizeof(struct hash_struct),1); |
|---|
| 262 | hs->size = size; |
|---|
| 263 | hs->entries = (struct hash_entry **)calloc(sizeof(struct hash_entry *),size); |
|---|
| 264 | return hs; |
|---|
| 265 | } |
|---|
| 266 | |
|---|
| 267 | char *read_hash_local(char *key,struct hash_struct **hs) |
|---|
| 268 | { |
|---|
| 269 | struct stack_struct *ss; |
|---|
| 270 | int i; |
|---|
| 271 | struct hash_entry *e; |
|---|
| 272 | i = hash_index(key,gl->st->hs->size); |
|---|
| 273 | for (ss = gl->st;ss;ss=ss->next) { |
|---|
| 274 | for(e=ss->hs->entries[i];e;e=e->next) |
|---|
| 275 | { |
|---|
| 276 | if (!strcmp(e->key,key)) { |
|---|
| 277 | if (hs) *hs = ss->hs; |
|---|
| 278 | return e->val; |
|---|
| 279 | } |
|---|
| 280 | } |
|---|
| 281 | } |
|---|
| 282 | return 0; |
|---|
| 283 | } |
|---|
| 284 | |
|---|
| 285 | |
|---|
| 286 | char *read_hash(struct hash_struct *hs,char *key) |
|---|
| 287 | { |
|---|
| 288 | struct hash_entry *e; |
|---|
| 289 | int i; |
|---|
| 290 | i = hash_index(key,hs->size); |
|---|
| 291 | for(e=hs->entries[i];e;e=e->next) |
|---|
| 292 | { |
|---|
| 293 | if (!strcmp(e->key,key)) return e->val; |
|---|
| 294 | } |
|---|
| 295 | return 0; |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | char *write_hash(struct hash_struct *hs,const char *key,const char *val) |
|---|
| 299 | { |
|---|
| 300 | struct hash_entry *e; |
|---|
| 301 | char *str2; |
|---|
| 302 | int i; |
|---|
| 303 | i = hash_index(key,hs->size); |
|---|
| 304 | for(e=hs->entries[i];e;e=e->next) |
|---|
| 305 | { |
|---|
| 306 | if (!strcmp(e->key,key)) { |
|---|
| 307 | str2 = e->val; |
|---|
| 308 | if (e->val) free(e->val); |
|---|
| 309 | if (val) { |
|---|
| 310 | e->val = strdup(val); |
|---|
| 311 | } |
|---|
| 312 | else { |
|---|
| 313 | e->val = 0; |
|---|
| 314 | } |
|---|
| 315 | |
|---|
| 316 | return str2; |
|---|
| 317 | } |
|---|
| 318 | } |
|---|
| 319 | e = (struct hash_entry *)calloc(sizeof(struct hash_entry),1); |
|---|
| 320 | e->next = hs->entries[i]; |
|---|
| 321 | e->key = strdup(key); |
|---|
| 322 | if (val) e->val = strdup(val); |
|---|
| 323 | hs->entries[i] = e; |
|---|
| 324 | |
|---|
| 325 | return 0; |
|---|
| 326 | } |
|---|
| 327 | int free_hash(struct hash_struct *hs) |
|---|
| 328 | { |
|---|
| 329 | int i; |
|---|
| 330 | int e2; |
|---|
| 331 | struct hash_entry *e,*ee; |
|---|
| 332 | e2 = hs->size; |
|---|
| 333 | for (i=0;i<e2;i++) { |
|---|
| 334 | for (e=hs->entries[i];e;e=ee) { |
|---|
| 335 | if (e->val) free(e->val); |
|---|
| 336 | free(e->key); |
|---|
| 337 | ee = e->next; |
|---|
| 338 | free((char *)e); |
|---|
| 339 | } |
|---|
| 340 | } |
|---|
| 341 | free ((char *)hs); |
|---|
| 342 | return 0; |
|---|
| 343 | } |
|---|