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"
<<">> Nasal interpreter ver 7.0 \n";
<<" Nasal interpreter ver 7.0 \n";
return;
}
void die(const char* stage,std::string& filename)

View File

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

View File

@ -363,7 +363,7 @@ void nasal_lexer::scanner()
void nasal_lexer::print_token()
{
for(auto tok:token_list)
for(auto& tok:token_list)
std::cout<<"("<<tok.line<<" | "<<tok.str<<")\n";
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_func)
{
if(tok_list[ptr+1].type==tok_id)
{
match(tok_func);
node=id_gen();
match(tok_id);
}
else
{
++in_function;
node=func_gen();
--in_function;
}
++in_function;
node=func_gen();
--in_function;
}
else if(tok_list[ptr].type==tok_lbracket)
node=vec_gen();

View File

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

View File

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

View File

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