添加双字符符号的解析

This commit is contained in:
2024-10-30 23:30:35 +08:00
parent 01a7999d55
commit ef7a4fdf4b
2 changed files with 58 additions and 6 deletions

4
main.c
View File

@@ -13,8 +13,8 @@
const char g_str[ ] = "int main(){\n"
"int index;\n"
"while(1){\n"
"int index=1000;\n"
"while(index--){\n"
"printf(index)\n"
"}\n"
"}";

View File

@@ -23,7 +23,17 @@ typedef enum {
TOKEN_INT=263,
TOKEN_VOID=264,
TOKEN_NAME = 265 ,
TOKEN_NUM = 266 ,
TOKEN_NUM = 266 ,// 数字
TOKEN_INC = 267,// 自增
TOKEN_DEC = 268,// 自减
TOKEN_EQ = 269,// 相等
TOKEN_NEQ = 270,// 不相等
TOKEN_LSH = 271,// 左移
TOKEN_RSH = 272,// 右移
TOKEN_LEQ = 273,// 小于等于
TOKEN_GEQ = 274,// 大于等于
TOKEN_ELSE = 275,
TOKEN_CONTINUE = 276,
}token_index_def;
@@ -37,6 +47,7 @@ typedef struct {
const keywork_item_def g_keyword_table[ ] = {
TOKEN_DEF(if,TOKEN_IF),
TOKEN_DEF(else,TOKEN_ELSE),
TOKEN_DEF(break,TOKEN_IF),
TOKEN_DEF(while,TOKEN_WHILE),
TOKEN_DEF(switch,TOKEN_SWITCH),
@@ -45,7 +56,8 @@ const keywork_item_def g_keyword_table[ ] = {
TOKEN_DEF(char,TOKEN_CHAR),
TOKEN_DEF(int,TOKEN_INT),
TOKEN_DEF(void,TOKEN_VOID),
{NULL } ,
TOKEN_DEF(continue,TOKEN_CONTINUE),
{NULL},
};
@@ -242,12 +254,10 @@ int lex_analysis(const char *text,int len){
case '[':
case ']':
case '~':
case '!':
case ',':
case ';':
case ':':
{
DBG_LOG("enter %c" , lex.current_c);
lex_save_char(&lex);
lex.token_buff.token = lex.current_c;
lex_save_token(&lex);
@@ -274,6 +284,48 @@ int lex_analysis(const char *text,int len){
}
break;
}
case '+':{
lex_save_char(&lex);
lex_get_next(&lex);
if(lex.current_c=='+'){
lex_save_char(&lex);
lex_get_next(&lex);
lex.token_buff.token = TOKEN_INC;
}else{
lex.token_buff.token = lex.current_c;
}
lex_save_token(&lex);
break;
}
case '-':{
lex_save_char(&lex);
lex_get_next(&lex);
if(lex.current_c=='-'){
lex_save_char(&lex);
lex_get_next(&lex);
lex.token_buff.token = TOKEN_DEC;
}else{
lex.token_buff.token = lex.current_c;
}
lex_save_token(&lex);
break;
}
case '!':
case '>':
case '<':
case '=':{
lex_save_char(&lex);
lex_get_next(&lex);
if(lex.current_c=='='){
lex_save_char(&lex);
lex_get_next(&lex);
lex.token_buff.token = TOKEN_EQ;
}else{
lex.token_buff.token = lex.current_c;
}
lex_save_token(&lex);
break;
}
default:
if (cislalpha(lex.current_c)) {
do {