fixed bug in nasal_parse

This commit is contained in:
Li Haokun 2021-07-21 17:38:11 +08:00 committed by GitHub
parent 884b56ac09
commit 9ebabfe737
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 27 additions and 34 deletions

View File

@ -42,7 +42,7 @@ void logo()
<<" / \\/ / _` / __|/ _` | | \n" <<" / \\/ / _` / __|/ _` | | \n"
<<" / /\\ / (_| \\__ \\ (_| | | \n" <<" / /\\ / (_| \\__ \\ (_| | | \n"
<<" \\_\\ \\/ \\__,_|___/\\__,_|_|\n" <<" \\_\\ \\/ \\__,_|___/\\__,_|_|\n"
<<">> Nasal interpreter ver 7.0 \n"; <<" Nasal interpreter ver 7.0 \n";
return; return;
} }
void die(const char* stage,std::string& filename) void die(const char* stage,std::string& filename)

View File

@ -20,7 +20,7 @@ hashmember::=
id|string ':' calculation id|string ':' calculation
; ;
function::= function::=
func argument_list expressions func {argument_list} exprs|expr
; ;
argument_list::= argument_list::=
'(' [{id ','} ([id '...']|{id '=' scalar ','})] ')' '(' [{id ','} ([id '...']|{id '=' scalar ','})] ')'
@ -35,7 +35,7 @@ expr::=
|continue_expr |continue_expr
|break_expr |break_expr
; ;
expressions::= exprs::=
'{' {expr} '}' '{' {expr} '}'
; ;
calculation::= calculation::=
@ -63,7 +63,6 @@ unary::=
; ;
scalar::= scalar::=
function {call_scalar} function {call_scalar}
|[func] identifier {call_scalar}
|vector {call_scalar} |vector {call_scalar}
|hash {call_scalar} |hash {call_scalar}
|number |number
@ -109,18 +108,18 @@ loop::=
|forei_loop |forei_loop
; ;
while_loop::= while_loop::=
while '(' calculation ')' expressions while '(' calculation ')' exprs
; ;
for_loop::= for_loop::=
for '(' [definition|calculation] ';' [calculation] ';' [calculation] ')' expressions for '(' [definition|calculation] ';' [calculation] ';' [calculation] ')' exprs
; ;
forei_loop::= forei_loop::=
(forindex | foreach) '(' (definition | calculation) ';' calculation ')' expressions (forindex | foreach) '(' (definition | calculation) ';' calculation ')' exprs
; ;
conditional::= conditional::=
if '(' calculation ')' expressions if '(' calculation ')' exprs
{elsif '(' calculation ')' expressions} {elsif '(' calculation ')' exprs}
[else expressions] [else exprs]
; ;
continue_expr::= continue_expr::=
continue continue

View File

@ -363,7 +363,7 @@ void nasal_lexer::scanner()
void nasal_lexer::print_token() void nasal_lexer::print_token()
{ {
for(auto tok:token_list) for(auto& tok:token_list)
std::cout<<"("<<tok.line<<" | "<<tok.str<<")\n"; std::cout<<"("<<tok.line<<" | "<<tok.str<<")\n";
return; return;
} }

View File

@ -628,18 +628,9 @@ nasal_ast nasal_parse::scalar()
else if(tok_list[ptr].type==tok_id) {node=id_gen(); match(tok_id); } else if(tok_list[ptr].type==tok_id) {node=id_gen(); match(tok_id); }
else if(tok_list[ptr].type==tok_func) else if(tok_list[ptr].type==tok_func)
{ {
if(tok_list[ptr+1].type==tok_id) ++in_function;
{ node=func_gen();
match(tok_func); --in_function;
node=id_gen();
match(tok_id);
}
else
{
++in_function;
node=func_gen();
--in_function;
}
} }
else if(tok_list[ptr].type==tok_lbracket) else if(tok_list[ptr].type==tok_lbracket)
node=vec_gen(); node=vec_gen();

View File

@ -152,12 +152,12 @@ var (ptr,pc)=(0,0);
var (code,inum,stack)=(nil,nil,nil); var (code,inum,stack)=(nil,nil,nil);
var (add,mov,jt,jf,in,out)=(0,1,2,3,4,5); var (add,mov,jt,jf,in,out)=(0,1,2,3,4,5);
var func_table=[ var func_table=[
func(){paper[ptr]+=inum[pc];return;}, func paper[ptr]+=inum[pc],
func(){ptr+=inum[pc];return;}, func ptr+=inum[pc],
func(){if(paper[ptr])pc=inum[pc];return;}, func if(paper[ptr])pc=inum[pc],
func(){if(!paper[ptr])pc=inum[pc];return;}, func if(!paper[ptr])pc=inum[pc],
func(){paper[ptr]=input()[0];return;}, func paper[ptr]=input()[0],
func(){print(chr(paper[ptr]));return;} func print(chr(paper[ptr]))
]; ];
var bf=func(program) var bf=func(program)
@ -179,7 +179,7 @@ var bf=func(program)
inum[-1]+=1; inum[-1]+=1;
elsif(chr(program[i])=='-') elsif(chr(program[i])=='-')
inum[-1]-=1; inum[-1]-=1;
else elsif(chr(program[i])!='\n')
{ {
i-=1; i-=1;
break; break;
@ -196,7 +196,7 @@ var bf=func(program)
inum[-1]+=1; inum[-1]+=1;
elsif(chr(program[i])=='<') elsif(chr(program[i])=='<')
inum[-1]-=1; inum[-1]-=1;
else elsif(chr(program[i])!='\n')
{ {
i-=1; i-=1;
break; break;
@ -230,7 +230,10 @@ var bf=func(program)
} }
} }
if(size(stack)) if(size(stack))
{
die("lack ]"); die("lack ]");
return;
}
len=size(code); len=size(code);
for(pc=0;pc<len;pc+=1) for(pc=0;pc<len;pc+=1)
code[pc](); code[pc]();

View File

@ -164,7 +164,7 @@ var bf=func(program)
cnt+=1; cnt+=1;
elsif(chr(program[i])=='-') elsif(chr(program[i])=='-')
cnt-=1; cnt-=1;
else elsif(chr(program[i])!='\n')
{ {
i-=1; i-=1;
break; break;
@ -187,7 +187,7 @@ var bf=func(program)
cnt+=1; cnt+=1;
elsif(chr(program[i])=='<') elsif(chr(program[i])=='<')
cnt-=1; cnt-=1;
else elsif(chr(program[i])!='\n')
{ {
i-=1; i-=1;
break; break;

View File

@ -49,6 +49,6 @@ println(f);#//func(...){...}
f();#//f is called f();#//f is called
println(z.funcc);#//func(...){...} println(z.funcc);#//func(...){...}
z.funcc();#//f is called z.funcc();#//f is called
println(z.funcccall);#//nil println(z.funcccall);#//func(...){...}
z2.listt2[3].hashh.funcc();#//f is called z2.listt2[3].hashh.funcc();#//f is called
println(y1[f2()][w]);#//hello println(y1[f2()][w]);#//hello