change enum tok to enum class tok

This commit is contained in:
ValKmjolnir 2022-11-27 23:47:58 +08:00
parent 7e91c273f3
commit be318abb2e
4 changed files with 427 additions and 420 deletions

View File

@ -1,4 +1,4 @@
# __Nasal Script Language__ # __Nasal Script__
<img src="./doc/pic/header.png" style="width:800px"></img> <img src="./doc/pic/header.png" style="width:800px"></img>

BIN
doc/pic/social.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 173 KiB

View File

@ -8,65 +8,65 @@
#define S_ISREG(m) (((m)&0xF000)==0x8000) #define S_ISREG(m) (((m)&0xF000)==0x8000)
#endif #endif
enum tok:u32 { enum class tok:u32 {
tok_null=0, // null token (default token type) null=0, // null token (default token type)
tok_num, // number literal num, // number literal
tok_str, // string literal str, // string literal
tok_id, // identifier id, // identifier
tok_for, // loop keyword for rfor, // loop keyword for
tok_forindex,// loop keyword forindex forindex, // loop keyword forindex
tok_foreach, // loop keyword foreach foreach, // loop keyword foreach
tok_while, // loop keyword while rwhile, // loop keyword while
tok_var, // keyword for definition var, // keyword for definition
tok_func, // keyword for definition of function func, // keyword for definition of function
tok_break, // loop keyword break brk, // loop keyword break
tok_continue,// loop keyword continue cont, // loop keyword continue
tok_ret, // function keyword return ret, // function keyword return
tok_if, // condition expression keyword if rif, // condition expression keyword if
tok_elsif, // condition expression keyword elsif elsif, // condition expression keyword elsif
tok_else, // condition expression keyword else relse, // condition expression keyword else
tok_nil, // nil literal nil, // nil literal
tok_lcurve, // ( lcurve, // (
tok_rcurve, // ) rcurve, // )
tok_lbracket,// [ lbracket, // [
tok_rbracket,// ] rbracket, // ]
tok_lbrace, // { lbrace, // {
tok_rbrace, // } rbrace, // }
tok_semi, // ; semi, // ;
tok_and, // operator and opand, // operator and
tok_or, // operator or opor, // operator or
tok_comma, // , comma, // ,
tok_dot, // . dot, // .
tok_ellipsis,// ... ellipsis, // ...
tok_quesmark,// ? quesmark, // ?
tok_colon, // : colon, // :
tok_add, // operator + add, // operator +
tok_sub, // operator - sub, // operator -
tok_mult, // operator * mult, // operator *
tok_div, // operator / div, // operator /
tok_link, // operator ~ link, // operator ~
tok_not, // operator ! opnot, // operator !
tok_eq, // operator = eq, // operator =
tok_addeq, // operator += addeq, // operator +=
tok_subeq, // operator -= subeq, // operator -=
tok_multeq, // operator *= multeq, // operator *=
tok_diveq, // operator /= diveq, // operator /=
tok_lnkeq, // operator ~= lnkeq, // operator ~=
tok_cmpeq, // operator == cmpeq, // operator ==
tok_neq, // operator != neq, // operator !=
tok_less, // operator < less, // operator <
tok_leq, // operator <= leq, // operator <=
tok_grt, // operator > grt, // operator >
tok_geq, // operator >= geq, // operator >=
tok_eof // <eof> end of token list eof // <eof> end of token list
}; };
struct token { struct token {
u32 line; u32 line;
u32 col; u32 col;
u32 type; tok type;
string str; string str;
token(u32 l=0,u32 c=0,u32 t=tok_null,const string& s="") token(u32 l=0,u32 c=0,tok t=tok::null,const string& s="")
: line(l),col(c),type(t),str(s) {} : line(l),col(c),type(t),str(s) {}
}; };
@ -78,55 +78,55 @@ private:
string res; string res;
error& err; error& err;
std::vector<token> toks; std::vector<token> toks;
std::unordered_map<string,u32> typetbl { std::unordered_map<string,tok> typetbl {
{"for" ,tok_for }, {"for" ,tok::rfor },
{"forindex",tok_forindex}, {"forindex",tok::forindex},
{"foreach" ,tok_foreach }, {"foreach" ,tok::foreach },
{"while" ,tok_while }, {"while" ,tok::rwhile },
{"var" ,tok_var }, {"var" ,tok::var },
{"func" ,tok_func }, {"func" ,tok::func },
{"break" ,tok_break }, {"break" ,tok::brk },
{"continue",tok_continue}, {"continue",tok::cont },
{"return" ,tok_ret }, {"return" ,tok::ret },
{"if" ,tok_if }, {"if" ,tok::rif },
{"elsif" ,tok_elsif }, {"elsif" ,tok::elsif },
{"else" ,tok_else }, {"else" ,tok::relse },
{"nil" ,tok_nil }, {"nil" ,tok::nil },
{"(" ,tok_lcurve }, {"(" ,tok::lcurve },
{")" ,tok_rcurve }, {")" ,tok::rcurve },
{"[" ,tok_lbracket}, {"[" ,tok::lbracket},
{"]" ,tok_rbracket}, {"]" ,tok::rbracket},
{"{" ,tok_lbrace }, {"{" ,tok::lbrace },
{"}" ,tok_rbrace }, {"}" ,tok::rbrace },
{";" ,tok_semi }, {";" ,tok::semi },
{"and" ,tok_and }, {"and" ,tok::opand },
{"or" ,tok_or }, {"or" ,tok::opor },
{"," ,tok_comma }, {"," ,tok::comma },
{"." ,tok_dot }, {"." ,tok::dot },
{"..." ,tok_ellipsis}, {"..." ,tok::ellipsis},
{"?" ,tok_quesmark}, {"?" ,tok::quesmark},
{":" ,tok_colon }, {":" ,tok::colon },
{"+" ,tok_add }, {"+" ,tok::add },
{"-" ,tok_sub }, {"-" ,tok::sub },
{"*" ,tok_mult }, {"*" ,tok::mult },
{"/" ,tok_div }, {"/" ,tok::div },
{"~" ,tok_link }, {"~" ,tok::link },
{"!" ,tok_not }, {"!" ,tok::opnot },
{"=" ,tok_eq }, {"=" ,tok::eq },
{"+=" ,tok_addeq }, {"+=" ,tok::addeq },
{"-=" ,tok_subeq }, {"-=" ,tok::subeq },
{"*=" ,tok_multeq }, {"*=" ,tok::multeq },
{"/=" ,tok_diveq }, {"/=" ,tok::diveq },
{"~=" ,tok_lnkeq }, {"~=" ,tok::lnkeq },
{"==" ,tok_cmpeq }, {"==" ,tok::cmpeq },
{"!=" ,tok_neq }, {"!=" ,tok::neq },
{"<" ,tok_less }, {"<" ,tok::less },
{"<=" ,tok_leq }, {"<=" ,tok::leq },
{">" ,tok_grt }, {">" ,tok::grt },
{">=" ,tok_geq } {">=" ,tok::geq }
}; };
u32 get_type(const string&); tok get_type(const string&);
bool skip(char); bool skip(char);
bool is_id(char); bool is_id(char);
bool is_hex(char); bool is_hex(char);
@ -205,8 +205,8 @@ void lexer::open(const string& file) {
res=ss.str(); res=ss.str();
} }
u32 lexer::get_type(const string& str) { tok lexer::get_type(const string& str) {
return typetbl.count(str)?typetbl.at(str):tok_null; return typetbl.count(str)?typetbl.at(str):tok::null;
} }
string lexer::utf8_gen() { string lexer::utf8_gen() {
@ -386,19 +386,19 @@ const error& lexer::scan(const string& file) {
} }
if (is_id(res[ptr])) { if (is_id(res[ptr])) {
str=id_gen(); str=id_gen();
u32 type=get_type(str); tok type=get_type(str);
toks.push_back({line,column,type?type:tok_id,str}); toks.push_back({line,column,(type!=tok::null)?type:tok::id,str});
} else if (is_dec(res[ptr])) { } else if (is_dec(res[ptr])) {
str=num_gen(); // make sure column is correct str=num_gen(); // make sure column is correct
toks.push_back({line,column,tok_num,str}); toks.push_back({line,column,tok::num,str});
} else if (is_str(res[ptr])) { } else if (is_str(res[ptr])) {
str=str_gen(); // make sure column is correct str=str_gen(); // make sure column is correct
toks.push_back({line,column,tok_str,str}); toks.push_back({line,column,tok::str,str});
} else if (is_single_opr(res[ptr])) { } else if (is_single_opr(res[ptr])) {
str=res[ptr]; str=res[ptr];
++column; ++column;
u32 type=get_type(str); tok type=get_type(str);
if (!type) { if (type==tok::null) {
err.err("lexer",line,column,str.length(),"invalid operator `"+str+"`"); err.err("lexer",line,column,str.length(),"invalid operator `"+str+"`");
} }
toks.push_back({line,column,type,str}); toks.push_back({line,column,type,str});
@ -428,7 +428,7 @@ const error& lexer::scan(const string& file) {
err.fatal("lexer","fatal error occurred, stop"); err.fatal("lexer","fatal error occurred, stop");
} }
} }
toks.push_back({line,column,tok_eof,"<eof>"}); toks.push_back({line,column,tok::eof,"<eof>"});
res=""; res="";
return err; return err;
} }

File diff suppressed because it is too large Load Diff