Example #1
0
fortress *parse(char *tokens) {
  fortress *fort = malloc(sizeof(fortress));
  fort->dwarf_size = count(tokens, DWARF);
  fort->sub_size = count(tokens, SUB);
  fort->alive = fort->dwarf_size;
  fort->dwarves = malloc(sizeof(dwarf*) * (fort->dwarf_size + fort->sub_size));
  int cur_id = -1;
  int comment = 0;
  int prev_comment = 0;
  dwarf *cur = NULL;

  char token = *tokens;
  int step = 0;
  int c = 0;
  while(token != '\0') {
    #ifdef DEBUG
    printf("Processing Token: %c\n", token);
    #endif

    int temp_prev_comment = prev_comment;
    prev_comment = 0;
    if(token == COMMENT) {
      if(temp_prev_comment) {
        comment ^= 1;
      } else {
        prev_comment = 1;
      }
    }

    if(!comment) {
      switch(token) {
      case DWARF:
      case SUB:
        cur_id++;
        dwarf *cur = malloc(sizeof(dwarf));
        cur->id = cur_id;
        cur->pos = 1;
        int l = inst_len(tokens, c);
        cur->inst_offset = 0;
        cur->dead = (token != DWARF);
        cur->rocks = 0;
        fort->dwarves[cur_id] = cur;

        cur->instructions = malloc(sizeof(char) * l + 1);
        cur->instructions[l] = '\0';

        step = 0;
        break;
      case MINE:
      case WORK:
      case DUMP:
      case LEFT:
      case RIGHT:
        cur->instructions[step] = token;
        step++;
        break;
      }
    }
    c++;
    token = *(tokens + c);
  }
  return fort;
}
Example #2
0
File: svm.c Project: shouya/svm
int parse_jmplbl(FILE* infile) {
    int chr = 0;
    char* _inst_name = malloc(BUF_SIZE);
    int _inst_name_len = 0;
    int _state = S_NOP;

    fseek(infile, 0, SEEK_SET);
    for (;;) {
        chr = fgetc(infile);

        if (isalnum(chr) || chr == '_') {
            switch (_state) {
            case S_NOP: /* name from new line */
                _state = S_INST_NAME;
                _inst_name[0] = chr;
                _inst_name_len = 1;
                break;
            case S_INST_NAME: /* store new char into name buffer */
                if (_inst_name_len == BUF_SIZE - 1) {
                    fputs("the jump point's name is too long.\n", stderr);
                    free(_inst_name);
                    return FAILURE;
                }
                _inst_name[_inst_name_len++] = chr;
                break;
            case S_UNKNOWN:
                break;
            }
        } else if (chr == '\n') {
            if (_state == S_INST_NAME) {
                _state = S_UNKNOWN;
                _inst_name[_inst_name_len] = '\0';
                __program_len += inst_len(_inst_name);
                _inst_name[0] = '\0';
                _inst_name_len = 0;
            }
            _state = S_NOP;
        } else if (isspace(chr)) {
            switch (_state) {
            case S_INST_NAME:
                _state = S_UNKNOWN;
                _inst_name[_inst_name_len] = '\0';
                __program_len += inst_len(_inst_name);
                _inst_name[0] = '\0';
                _inst_name_len = 0;
                break;
            case S_UNKNOWN:
            case S_NOP:
                break;
            }
        } else if (chr == ':') {
            switch (_state) {
            case S_INST_NAME:
                _state = S_NOP;
                _inst_name[_inst_name_len] = '\0';
                ++__jmp_tbl_size;

                __jmp_tbl = realloc(
                    __jmp_tbl, sizeof(struct jmplbl_t) * __jmp_tbl_size);
                __jmp_tbl[__jmp_tbl_size-1].no = __jmp_tbl_size - 1;
                __jmp_tbl[__jmp_tbl_size-1].name =
                    malloc(_inst_name_len);
                strcpy(__jmp_tbl[__jmp_tbl_size-1].name, _inst_name);
                __jmp_tbl[__jmp_tbl_size-1].jmppos = __program_len;
/* DEBUG
                printf("jp[%d]: [%s] -- [%d]\n", __jmp_tbl_size - 1,
                       __jmp_tbl[__jmp_tbl_size-1].name,
                    __jmp_tbl[__jmp_tbl_size-1].jmppos);
*/
                
                _inst_name[0] = '\0';
                _inst_name_len = 0;
                break;
            case S_NOP:
            case S_UNKNOWN:
                break;
            } /* switch */
        } else if (chr == EOF) {
            break;
        } else {
            _state = S_UNKNOWN;
        }/* if */
    } /* for (;;) */
    return SUCCESS;
}