添加一些语法

This commit is contained in:
2024-12-03 08:53:09 +08:00
parent c736ef5b1b
commit dac9dad45d
5 changed files with 399 additions and 12 deletions

View File

@@ -34,6 +34,18 @@ TOKEN_UNION = 283,
TOKEN_STRING = 284,
TOKEN_DEFAULT = 285,
TOKEN_RETURN = 286,
TOKEN_ASSIG_ADD = 287
TOKEN_ASSIG_SUB = 288
TOKEN_ASSIG_MUL = 289
TOKEN_ASSIG_DIV = 290
TOKEN_ASSIG_LSH = 291
TOKEN_ASSIG_RSH = 292
TOKEN_EXTERN = 293
TOKEN_FLOAT = 294
TOKEN_DOUBLE = 295
TOKEN_SHORT = 296
TOKEN_LONG = 297
def TOKEN(t:str):
return t.encode("utf-8")[0]
@@ -59,6 +71,11 @@ _KeyWordTable={
"union":TOKEN_UNION,
"default":TOKEN_DEFAULT,
"return":TOKEN_RETURN,
"extern":TOKEN_EXTERN,
"float":TOKEN_FLOAT,
"double":TOKEN_DOUBLE,
"short":TOKEN_SHORT,
"long":TOKEN_LONG,
}
_MarkTable={
@@ -70,6 +87,11 @@ _MarkTable={
"==":TOKEN_EQ,
"++":TOKEN_INC,
"--":TOKEN_DEC,
"+=":TOKEN_ASSIG_ADD,
"-=":TOKEN_ASSIG_SUB,
"*=":TOKEN_ASSIG_MUL,
"<<=":TOKEN_ASSIG_LSH,
">>=":TOKEN_ASSIG_RSH,
"=":TOKEN("="),
"!":TOKEN("!"),
"<":TOKEN("<"),
@@ -233,8 +255,29 @@ def lex(text:bytes):
c=lex_obj.read_operator_and_save(c)
elif isinstr(c,"\""):
c=lex_obj.read_str_and_save(c)
elif isinstr(c,"\\"):
c=lex_obj.get_next_char(c)
if(c!=TOKEN("\r") and c!=TOKEN("\n")):
raise Exception(f"符号 '\\' 必须在行末, line:{lex_obj.line} pos:{lex_obj.pos}")
elif isinstr(c,"/"):
c=lex_obj.get_next_char()
if(c==TOKEN("/")):
while c!=TOKEN("\n"):
c=lex_obj.get_next_char()
elif(c==TOKEN("*")):
c_old=lex_obj.get_next_char()
c=lex_obj.get_next_char()
while not (c_old==TOKEN("*") and c==TOKEN("/")):
c_old=c
c=lex_obj.get_next_char()
c=lex_obj.get_next_char()
elif(c==TOKEN("=")):
lex_obj.save_token(lex_token("/=",b"/=",TOKEN_ASSIG_DIV,lex_obj.line,lex_obj.pos))
c=lex_obj.get_next_char()
else:
lex_obj.save_one_char_token(TOKEN("/"))
else:
raise Exception(f"err char {bytes([c])} at line:{lex_obj.line} pos:{lex_obj.pos}")
raise Exception(f"未知的字符 {bytes([c])}, line:{lex_obj.line} pos:{lex_obj.pos}")
# for item in lex_obj.token_list:
# print(f"{item}")
return lex_obj.token_list