This commit is contained in:
Valk Richard Li 2019-09-05 20:43:36 +08:00 committed by GitHub
parent 225b079fc7
commit 493b09acfe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 77 deletions

View File

@ -11,14 +11,7 @@
#define NUMBER 3 // number #define NUMBER 3 // number
#define RESERVEWORD 4 // reserve word #define RESERVEWORD 4 // reserve word
#define STRING 5 // string #define STRING 5 // string
#define CHAR 6 // char #define DYNAMIC_ID 6 // id...
#define CALL_LIST 7 // id[
#define CALL_FUNC 8 // id(
#define FUNC_HEAD 9 // func(
#define DYNAMIC_ID 10 // id...
#define IF_HEAD 11 // if (
#define ELSIF_HEAD 12 // elsif (
#define WHILE_HEAD 13 // while (
#define FAIL -1 //fail #define FAIL -1 //fail
#define SCANEND -2 //complete scanning #define SCANEND -2 //complete scanning
#define ERRORFOUND -3 //error occurred #define ERRORFOUND -3 //error occurred
@ -176,33 +169,11 @@ class nasal_lexer
syn=IDENTIFIER; syn=IDENTIFIER;
else else
syn=RESERVEWORD; syn=RESERVEWORD;
if((syn==IDENTIFIER) && ((source[ptr]=='(') || (source[ptr]=='[') || (source[ptr]=='.' && source[ptr+1]=='.' && source[ptr+2]=='.'))) if((syn==IDENTIFIER) && source[ptr]=='.' && source[ptr+1]=='.' && source[ptr+2]=='.')
{ {
__token+=source[ptr]; __token+="...";
if(source[ptr]=='(') syn=DYNAMIC_ID;
syn=CALL_FUNC; ptr+=3;
else if(source[ptr]=='[')
syn=CALL_LIST;
else if(source[ptr]=='.' && source[ptr+1]=='.' && source[ptr+2]=='.')
{
__token+="..";
syn=DYNAMIC_ID;
ptr+=2;
}
++ptr;
}
else if((syn==RESERVEWORD) && ((__token=="func") || (__token=="if") || (__token=="elsif") || (__token=="while")) && (source[ptr]=='('))
{
if(__token=="func")
syn=FUNC_HEAD;
else if(__token=="if")
syn=IF_HEAD;
else if(__token=="elsif")
syn=ELSIF_HEAD;
else if(__token=="while")
syn=WHILE_HEAD;
__token+=source[ptr];
++ptr;
} }
} }
else if(isNumber(temp)) else if(isNumber(temp))
@ -263,17 +234,40 @@ class nasal_lexer
} }
else if(temp=='\'') else if(temp=='\'')
{ {
syn=STRING;
__token+=temp; __token+=temp;
++ptr; ++ptr;
temp=source[ptr]; temp=source[ptr];
__token+=temp; while(temp!='\'')
++ptr; {
temp=source[ptr]; if(temp=='\\')
__token+=temp; {
++ptr; __token+=temp;
if(temp!='\'')
std::cout<<">>[Lexer] Abnormal char type detected: "<<__token<<" ."<<std::endl; ++ptr;
syn=CHAR; temp=source[ptr];
__token+=temp;
++ptr;
temp=source[ptr];
}
else
{
__token+=temp;
++ptr;
temp=source[ptr];
}
if(temp==0 || temp=='\n')
break;
}
//add the last char \"
if(temp=='\'')
{
__token+=temp;
++ptr;
}
else
__token+=" __missing_end_of_string";
} }
else if(temp=='=' || temp=='+' || temp=='-' || temp=='*' || temp=='!' || temp=='/' || temp=='<' || temp=='>' || temp=='~') else if(temp=='=' || temp=='+' || temp=='-' || temp=='*' || temp=='!' || temp=='/' || temp=='<' || temp=='>' || temp=='~')
{ {
@ -396,22 +390,8 @@ class nasal_lexer
std::cout<<"( ReserveWord | "; std::cout<<"( ReserveWord | ";
else if(temp.type==STRING) else if(temp.type==STRING)
std::cout<<"( String | "; std::cout<<"( String | ";
else if(temp.type==CHAR)
std::cout<<"( Char | ";
else if(temp.type==CALL_LIST)
std::cout<<"( Call list head | ";
else if(temp.type==CALL_FUNC)
std::cout<<"( Call func head | ";
else if(temp.type==FUNC_HEAD)
std::cout<<"( Func head | ";
else if(temp.type==DYNAMIC_ID) else if(temp.type==DYNAMIC_ID)
std::cout<<"( Identifier | "; std::cout<<"( Identifier | ";
else if(temp.type==IF_HEAD)
std::cout<<"( If head | ";
else if(temp.type==ELSIF_HEAD)
std::cout<<"( Elsif head | ";
else if(temp.type==WHILE_HEAD)
std::cout<<"( While head | ";
std::cout<<temp.content<<" )"<<std::endl; std::cout<<temp.content<<" )"<<std::endl;
} }
return; return;

View File

@ -8,30 +8,28 @@ enum token_type
{ {
__stack_end=1, __stack_end=1,
__equal,// = __equal,// =
__cmp_equal,// == __cmp_equal,__cmp_not_equal,// == !=
__cmp_not_equal,// !=
__cmp_less,__cmp_less_or_equal,// < <= __cmp_less,__cmp_less_or_equal,// < <=
__cmp_more,__cmp_more_or_equal,// > >= __cmp_more,__cmp_more_or_equal,// > >=
__and_operator,__or_operator,__nor_operator,// and or ! __and_operator,__or_operator,__nor_operator,// and or !
__add_operator,__sub_operator,__mul_operator,__div_operator,__link_operator,// + - * / ~ __add_operator,__sub_operator,// + -
__add_equal,__sub_equal,__mul_equal,__div_equal,__link_equal,// += -= *= /= ~= __mul_operator,__div_operator,__link_operator,// * / ~
__add_equal,__sub_equal,// += -=
__mul_equal,__div_equal,__link_equal,// *= /= ~=
__left_brace,__right_brace,// {} __left_brace,__right_brace,// {}
__left_bracket,__right_bracket,// [] __left_bracket,__right_bracket,// []
__left_curve,__right_curve,// () __left_curve,__right_curve,// ()
__semi,// ; __semi,__comma,__colon,__dot,// ; , : .
__comma,// ,
__colon,// : __var,__func,__return,
__dot,// .
__var,
__func,
__id,__dynamic_id,
__return,
__if,__elsif,__else, __if,__elsif,__else,
__continue,__break,__for,__forindex,__foreach,__while, __id,__dynamic_id,
__continue,__break,
__for,__forindex,__foreach,__while,
//end of operators & reserve words //end of operators & reserve words
__two_operator, __two_operator,
__scalar,__data_list, __scalar,__data_list,
__number,__string,__char, __number,__string,
__list, __list,
__hash, __hash,
__hash_member,__hash_member_list, __hash_member,__hash_member_list,
@ -81,7 +79,6 @@ cmp_seq par[]=
{{__number},__scalar}, {{__number},__scalar},
{{__string},__scalar}, {{__string},__scalar},
{{__char},__scalar},
{{__calculation},__scalar}, {{__calculation},__scalar},
{{__call_list},__call}, {{__call_list},__call},
@ -442,9 +439,6 @@ void print_token(int type)
case __string: case __string:
context="string"; context="string";
break; break;
case __char:
context="char";
break;
case __continue: case __continue:
context="continue"; context="continue";
break; break;
@ -818,14 +812,12 @@ class nasal_parser
else if((*i).content==".") else if((*i).content==".")
temp_parse.type=__dot; temp_parse.type=__dot;
} }
else if(((*i).type==NUMBER) || ((*i).type==STRING) || ((*i).type==CHAR)) else if(((*i).type==NUMBER) || ((*i).type==STRING))
{ {
if((*i).type==NUMBER) if((*i).type==NUMBER)
temp_parse.type=__number; temp_parse.type=__number;
else if((*i).type==STRING) else if((*i).type==STRING)
temp_parse.type=__string; temp_parse.type=__string;
else if((*i).type==CHAR)
temp_parse.type=__char;
} }
else if(((*i).content=="+") || ((*i).content=="-") || ((*i).content=="*") || ((*i).content=="/") || ((*i).content=="~") || ((*i).content=="!")) else if(((*i).content=="+") || ((*i).content=="-") || ((*i).content=="*") || ((*i).content=="/") || ((*i).content=="~") || ((*i).content=="!"))
{ {