📄 gs_token.c
字号:
while (isspace(character)) { character = get_next_character(); } /* ---------------------------- */ /* Identifier */ /* ---------------------------- */ if (is_valid_identifier_character(character)) { str_ptr = str; while (is_valid_identifier_character(character)) { *str_ptr++ = character; character = get_next_character(); } *str_ptr = '\0'; push_character(character); lprintf(LOG_VERYVERBOSE, "Found Identifier '%s'\n", str); token_id = add_token(heap, str, T_IDENTIFIER, NULL); } else if (character == '"') { character = get_next_character(); str_ptr = str; while ((character != '"') && (character != EOF)) { if (character == '\\') { *str_ptr++ = escape_char(); } else { *str_ptr++ = character; } character = get_next_character(); } *str_ptr = '\0'; if (character == '"') { lprintf(LOG_VERYVERBOSE, "Found Identifier '%s'\n", str); token_id = add_token(heap, str, T_STRING, NULL); } else { token_id = null_tok; } } else if (character == '\'') { character = get_next_character(); str_ptr = str; while ((character != '\'') && (character != EOF)) { *str_ptr++ = character; character = get_next_character(); } *str_ptr = '\0'; if (character == '\'') { lprintf(LOG_VERYVERBOSE, "Found Identifier '%s'\n", str); token_id = add_token(heap, str, T_STRING, NULL); } else { token_id = null_tok; } } else if (character == '=') { token_id = assignment_tok; } else if (character == '(') { token_id = lparen_tok; } else if (character == ')') { token_id = rparen_tok; } else if (character == '{') { token_id = lcurly_tok; } else if (character == '}') { token_id = rcurly_tok; } else if (character == ',') { token_id = comma_tok; } else if (character == '.') { token_id = dot_tok; } else if (character == ';') { token_id = semicolon_tok; } else if (character == EOF) { token_id = eof_tok; } else { token_id = null_tok; } lprintf(LOG_VERYVERBOSE, "Token id = %p\n", token_id); return token_id;}/* -------------------------------------------------------------------- *//* Add token searches the token heap for 'str' *//* -------------------------------------------------------------------- */TOKEN_ID add_token(TOKEN_HEAP *heap, char *str, int type, void *ptr){ TOKEN_ID token; lprintf(LOG_VERYVERBOSE, "Entering add_token()\n"); heap->list = add_token_list_item(heap->list, &token, str, type, 0, ptr); lprintf(LOG_VERYVERBOSE, "returning %p\n", token); return token;}/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */char *get_token_strvalue(TOKEN_HEAP *heap, TOKEN_ID token_id){ return token_id->str;}/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */char *get_token_strtype(TOKEN_HEAP *heap, TOKEN_ID token_id){ return "UNKNOWN";}/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */int get_token_type(TOKEN_HEAP *heap, TOKEN_ID token_id){ return token_id->type;}/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */int get_token_indirect_type(TOKEN_HEAP *heap, TOKEN_ID token_id){ return token_id->ptr_type;}/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */char *get_token_indirect_string(TOKEN_HEAP *heap, TOKEN_ID token_id){ return token_id->ptr;}/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */TOKEN_HEAP *get_token_indirect_dictionary(TOKEN_HEAP *heap, TOKEN_ID token_id){ return (TOKEN_HEAP *)token_id->ptr;}/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */TOKEN_HEAP *get_token_indirect_list(TOKEN_HEAP *heap, TOKEN_ID token_id){ return (TOKEN_HEAP *)token_id->ptr;}/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */void token_assign_value(TOKEN_HEAP *heap, TOKEN_ID token_id, char *string){ token_id->ptr_type = TP_STRING; token_id->ptr = string;}/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */void token_assign_dictionary(TOKEN_HEAP *heap, TOKEN_ID token_id, TOKEN_HEAP *dictionary){ token_id->ptr_type = TP_DICTIONARY; token_id->ptr = dictionary;}/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */void token_assign_list(TOKEN_HEAP *heap, TOKEN_ID token_id, TOKEN_HEAP *list){ token_id->ptr_type = TP_LIST; token_id->ptr = list;}/* -------------------------------------------------------------------- *//* Add our reserved tokens to the reserved builtin heap. *//* if builtin heap has been filled by an earlier call just *//* return from this function. *//* -------------------------------------------------------------------- */void init_builtin_heap(void){ int i; if (builtin.list == NULL) { for (i=0; init_token_table[i].var != NULL; i++) { *(init_token_table[i].var) = add_token(&builtin, init_token_table[i].str, init_token_table[i].type, init_token_table[i].ptr); } }}/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */void init_heap(TOKEN_HEAP *heap){ heap->list = NULL;}/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */TOKEN_HEAP *generate_new_heap(void){ TOKEN_HEAP *heap; heap = (TOKEN_HEAP *)malloc(sizeof(TOKEN_HEAP)); if (heap == NULL) { return heap; } init_heap(heap); return heap;}/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */void dump_heap(TOKEN_HEAP *heap, char *name){ TOKEN_LIST *item; TOKEN_ID token; fprintf(stdout, " %s = { \n", name); item = heap->list; while(item != NULL) { token = item->token; if (get_token_type(heap, token) == T_IDENTIFIER) { if (get_token_indirect_type(heap, token) == TP_STRING) { fprintf(stdout, "%s = %s\n", get_token_strvalue(heap, token), get_token_indirect_string(heap, token)); } else if (get_token_indirect_type(heap, token) == TP_DICTIONARY) { dump_heap(get_token_indirect_dictionary(heap, token), get_token_strvalue(heap, token)); } else if (get_token_indirect_type(heap, token) == TP_LIST) { dump_heap(get_token_indirect_list(heap, token), get_token_strvalue(heap, token)); } } item = item->next; } fprintf(stdout, " }\n");}/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */void free_heap(TOKEN_HEAP *heap){ TOKEN_LIST *item, *prev_item; TOKEN_ID token; item = heap->list; while(item != NULL) { token = item->token; if (get_token_type(heap, token) == T_IDENTIFIER) { if (get_token_indirect_type(heap, token) == TP_DICTIONARY) { free_heap(get_token_indirect_dictionary(heap, token)); } else if (get_token_indirect_type(heap, token) == TP_LIST) { free_heap(get_token_indirect_list(heap, token)); } } prev_item = item; item = item->next; free(prev_item->token->str); free(prev_item->token); free(prev_item); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -