improve error output info generated by codegen.

This commit is contained in:
ValKmjolnir 2022-09-05 00:41:36 +08:00
parent a13e419518
commit 972ad49a4f
5 changed files with 88 additions and 67 deletions

View File

@ -129,12 +129,13 @@ class nasal_ast
{ {
private: private:
u32 _line; u32 _line;
u32 _column;
u32 _type; u32 _type;
f64 _num; f64 _num;
string _str; string _str;
std::vector<nasal_ast> _child; std::vector<nasal_ast> _child;
public: public:
nasal_ast(const u32 l=0,const u32 t=ast_null):_line(l),_type(t),_num(0){} nasal_ast(const u32 l,const u32 c,const u32 t):_line(l),_column(c),_type(t),_num(0){}
nasal_ast(const nasal_ast&); nasal_ast(const nasal_ast&);
nasal_ast(nasal_ast&&); nasal_ast(nasal_ast&&);
void tree(); void tree();
@ -155,6 +156,7 @@ public:
void set_num(const f64 n){_num=n;} void set_num(const f64 n){_num=n;}
inline u32 line() const {return _line;} inline u32 line() const {return _line;}
inline u32 col() const {return _column;}
inline u32 type() const {return _type;} inline u32 type() const {return _type;}
inline f64 num() const {return _num;} inline f64 num() const {return _num;}
inline const string& str() const {return _str;} inline const string& str() const {return _str;}
@ -166,6 +168,7 @@ nasal_ast::nasal_ast(const nasal_ast& tmp):
_str(tmp._str),_child(tmp._child) _str(tmp._str),_child(tmp._child)
{ {
_line=tmp._line; _line=tmp._line;
_column=tmp._column;
_type=tmp._type; _type=tmp._type;
_num =tmp._num; _num =tmp._num;
} }
@ -173,6 +176,7 @@ nasal_ast::nasal_ast(const nasal_ast& tmp):
nasal_ast::nasal_ast(nasal_ast&& tmp) nasal_ast::nasal_ast(nasal_ast&& tmp)
{ {
_line=tmp._line; _line=tmp._line;
_column=tmp._column;
_type=tmp._type; _type=tmp._type;
_num =tmp._num; _num =tmp._num;
_str.swap(tmp._str); _str.swap(tmp._str);
@ -182,6 +186,7 @@ nasal_ast::nasal_ast(nasal_ast&& tmp)
nasal_ast& nasal_ast::operator=(const nasal_ast& tmp) nasal_ast& nasal_ast::operator=(const nasal_ast& tmp)
{ {
_line=tmp._line; _line=tmp._line;
_column=tmp._column;
_type=tmp._type; _type=tmp._type;
_num=tmp._num; _num=tmp._num;
_str=tmp._str; _str=tmp._str;
@ -192,6 +197,7 @@ nasal_ast& nasal_ast::operator=(const nasal_ast& tmp)
nasal_ast& nasal_ast::operator=(nasal_ast&& tmp) nasal_ast& nasal_ast::operator=(nasal_ast&& tmp)
{ {
_line=tmp._line; _line=tmp._line;
_column=tmp._column;
_type=tmp._type; _type=tmp._type;
_num=tmp._num; _num=tmp._num;
_str.swap(tmp._str); _str.swap(tmp._str);
@ -201,7 +207,7 @@ nasal_ast& nasal_ast::operator=(nasal_ast&& tmp)
void nasal_ast::clear() void nasal_ast::clear()
{ {
_line=0; _line=_column=0;
_num=0; _num=0;
_str=""; _str="";
_type=ast_null; _type=ast_null;

View File

@ -209,7 +209,7 @@ private:
std::stack<u32> fbstk; std::stack<u32> fbstk;
std::stack<u32> festk; std::stack<u32> festk;
void die(const string&,const u32); void die(const string&,const u32,const u32);
void regist_num(const f64); void regist_num(const f64);
void regist_str(const string&); void regist_str(const string&);
void find_symbol(const nasal_ast&); void find_symbol(const nasal_ast&);
@ -260,10 +260,13 @@ public:
const std::vector<opcode>& codes() const {return code;} const std::vector<opcode>& codes() const {return code;}
}; };
void nasal_codegen::die(const string& info,const u32 line) void nasal_codegen::die(const string& info,const u32 line,const u32 col)
{ {
nerr.load(file[fileindex]); nerr.load(file[fileindex]);
nerr.err("code",line,info); if(col)
nerr.err("code",line,col,info);
else
nerr.err("code",line,info);
} }
void nasal_codegen::regist_num(const f64 num) void nasal_codegen::regist_num(const f64 num)
@ -409,7 +412,7 @@ void nasal_codegen::func_gen(const nasal_ast& ast)
{ {
const string& str=tmp.str(); const string& str=tmp.str();
if(str=="me") if(str=="me")
die("\"me\" should not be a parameter",tmp.line()); die("\"me\" should not be a parameter",tmp.line(),tmp.col());
regist_str(str); regist_str(str);
switch(tmp.type()) switch(tmp.type())
{ {
@ -436,7 +439,7 @@ void nasal_codegen::func_gen(const nasal_ast& ast)
in_iterloop.pop(); in_iterloop.pop();
code[lsize].num=local.back().size(); code[lsize].num=local.back().size();
if(local.back().size()>=STACK_DEPTH) if(local.back().size()>=STACK_DEPTH)
die("too many local variants: "+std::to_string(local.back().size()),block.line()); die("too many local variants: "+std::to_string(local.back().size()),block.line(),0);
local.pop_back(); local.pop_back();
if(!block.size() || block.child().back().type()!=ast_ret) if(!block.size() || block.child().back().type()!=ast_ret)
@ -472,7 +475,7 @@ void nasal_codegen::call_id(const nasal_ast& ast)
{ {
gen(op_callb,i,ast.line()); gen(op_callb,i,ast.line());
if(local.empty()) if(local.empty())
die("should warp native functions in local scope",ast.line()); die("should warp native functions in local scope",ast.line(),ast.col());
return; return;
} }
i32 index; i32 index;
@ -491,7 +494,7 @@ void nasal_codegen::call_id(const nasal_ast& ast)
gen(op_callg,index,ast.line()); gen(op_callg,index,ast.line());
return; return;
} }
die("undefined symbol \""+str+"\"",ast.line()); die("undefined symbol \""+str+"\"",ast.line(),ast.col());
} }
void nasal_codegen::call_hash(const nasal_ast& ast) void nasal_codegen::call_hash(const nasal_ast& ast)
@ -588,7 +591,7 @@ void nasal_codegen::mcall_id(const nasal_ast& ast)
for(u32 i=0;builtin[i].name;++i) for(u32 i=0;builtin[i].name;++i)
if(builtin[i].name==str) if(builtin[i].name==str)
{ {
die("cannot modify native function",ast.line()); die("cannot modify native function",ast.line(),ast.col());
return; return;
} }
i32 index; i32 index;
@ -607,7 +610,7 @@ void nasal_codegen::mcall_id(const nasal_ast& ast)
gen(op_mcallg,index,ast.line()); gen(op_mcallg,index,ast.line());
return; return;
} }
die("undefined symbol \""+str+"\"",ast.line()); die("undefined symbol \""+str+"\"",ast.line(),ast.col());
} }
void nasal_codegen::mcall_vec(const nasal_ast& ast) void nasal_codegen::mcall_vec(const nasal_ast& ast)
@ -1235,7 +1238,7 @@ void nasal_codegen::compile(const nasal_parse& parse,const nasal_import& import)
block_gen(parse.ast()); // generate main block block_gen(parse.ast()); // generate main block
gen(op_exit,0,0); gen(op_exit,0,0);
if(global.size()>=STACK_DEPTH) if(global.size()>=STACK_DEPTH)
die("too many global variants: "+std::to_string(global.size()),0); die("too many global variants: "+std::to_string(global.size()),0,0);
nerr.chkerr(); nerr.chkerr();
} }

View File

@ -123,8 +123,13 @@ public:
std::cerr<<bold_red<<stage<<": " std::cerr<<bold_red<<stage<<": "
<<bold_white<<info<<reset<<"\n" <<bold_white<<info<<reset<<"\n"
<<bold_cyan<<" --> "<<reset <<bold_cyan<<" --> "<<reset
<<bold_orange<<file<<":"<<line<<":"<<column<<"\n" <<bold_orange<<file<<":"<<line<<":"<<column<<"\n";
<<bold_cyan<<ident<<" | "<<reset<<"\n" if(!line)
{
std::cerr<<"\n";
return;
}
std::cerr<<bold_cyan<<ident<<" | "<<reset<<"\n"
<<bold_cyan<<line<<" | "<<reset<<code<<"\n" <<bold_cyan<<line<<" | "<<reset<<code<<"\n"
<<bold_cyan<<ident<<" | "<<reset; <<bold_cyan<<ident<<" | "<<reset;
for(i32 i=0;i<(i32)column-1;++i) for(i32 i=0;i<(i32)column-1;++i)
@ -138,8 +143,13 @@ public:
std::cerr<<bold_red<<stage<<": " std::cerr<<bold_red<<stage<<": "
<<bold_white<<info<<reset<<"\n" <<bold_white<<info<<reset<<"\n"
<<bold_cyan<<" --> "<<reset <<bold_cyan<<" --> "<<reset
<<bold_orange<<file<<":"<<line<<"\n" <<bold_orange<<file<<":"<<line<<"\n";
<<bold_cyan<<ident<<" | "<<reset<<"\n" if(!line)
{
std::cerr<<"\n";
return;
}
std::cerr<<bold_cyan<<ident<<" | "<<reset<<"\n"
<<bold_cyan<<line<<" | "<<reset<<res[line-1]<<"\n" <<bold_cyan<<line<<" | "<<reset<<res[line-1]<<"\n"
<<bold_cyan<<ident<<" | "<<reset<<"\n\n"; <<bold_cyan<<ident<<" | "<<reset<<"\n\n";
} }

View File

@ -159,7 +159,7 @@ nasal_ast nasal_import::fimpt(nasal_ast& node)
// avoid infinite loading loop // avoid infinite loading loop
filename=findf(filename); filename=findf(filename);
if(!filename.length() || exist(filename)) if(!filename.length() || exist(filename))
return {0,ast_root}; return {0,0,ast_root};
// start importing... // start importing...
lex.scan(filename); lex.scan(filename);
@ -175,11 +175,11 @@ nasal_ast nasal_import::libimpt()
nasal_parse par(nerr); nasal_parse par(nerr);
string filename=findf("lib.nas"); string filename=findf("lib.nas");
if(!filename.length()) if(!filename.length())
return {0,ast_root}; return {0,0,ast_root};
// avoid infinite loading loop // avoid infinite loading loop
if(exist(filename)) if(exist(filename))
return {0,ast_root}; return {0,0,ast_root};
// start importing... // start importing...
lex.scan(filename); lex.scan(filename);
@ -191,7 +191,7 @@ nasal_ast nasal_import::libimpt()
nasal_ast nasal_import::load(nasal_ast& root,u16 fileindex) nasal_ast nasal_import::load(nasal_ast& root,u16 fileindex)
{ {
nasal_ast new_root(0,ast_root); nasal_ast new_root(0,0,ast_root);
if(!lib_loaded) if(!lib_loaded)
{ {
linker(new_root,libimpt()); linker(new_root,libimpt());
@ -205,7 +205,7 @@ nasal_ast nasal_import::load(nasal_ast& root,u16 fileindex)
break; break;
} }
// add root to the back of new_root // add root to the back of new_root
nasal_ast file_head(0,ast_file); nasal_ast file_head(0,0,ast_file);
file_head.set_num(fileindex); file_head.set_num(fileindex);
new_root.add(std::move(file_head)); new_root.add(std::move(file_head));
linker(new_root,std::move(root)); linker(new_root,std::move(root));

View File

@ -100,7 +100,9 @@ private:
nasal_ast break_expr(); nasal_ast break_expr();
nasal_ast ret_expr(); nasal_ast ret_expr();
public: public:
nasal_parse(nasal_err& e):ptr(0),in_func(0),in_loop(0),tokens(nullptr),nerr(e){} nasal_parse(nasal_err& e):
ptr(0),in_func(0),in_loop(0),
tokens(nullptr),root(0,0,ast_root),nerr(e){}
void print(){root.tree();} void print(){root.tree();}
void compile(const nasal_lexer&); void compile(const nasal_lexer&);
nasal_ast& ast(){return root;} nasal_ast& ast(){return root;}
@ -111,7 +113,7 @@ void nasal_parse::compile(const nasal_lexer& lexer)
tokens=lexer.result().data(); tokens=lexer.result().data();
ptr=in_func=in_loop=0; ptr=in_func=in_loop=0;
root={1,ast_root}; root={0,0,ast_root};
while(tokens[ptr].type!=tok_eof) while(tokens[ptr].type!=tok_eof)
{ {
root.add(expr()); root.add(expr());
@ -255,29 +257,29 @@ void nasal_parse::check_memory_reachable(const nasal_ast& node)
} }
nasal_ast nasal_parse::null() nasal_ast nasal_parse::null()
{ {
return {tokens[ptr].line,ast_null}; return {tokens[ptr].line,tokens[ptr].col,ast_null};
} }
nasal_ast nasal_parse::nil() nasal_ast nasal_parse::nil()
{ {
return {tokens[ptr].line,ast_nil}; return {tokens[ptr].line,tokens[ptr].col,ast_nil};
} }
nasal_ast nasal_parse::num() nasal_ast nasal_parse::num()
{ {
nasal_ast node(tokens[ptr].line,ast_num); nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_num);
node.set_num(str2num(tokens[ptr].str.c_str())); node.set_num(str2num(tokens[ptr].str.c_str()));
match(tok_num); match(tok_num);
return node; return node;
} }
nasal_ast nasal_parse::str() nasal_ast nasal_parse::str()
{ {
nasal_ast node(tokens[ptr].line,ast_str); nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_str);
node.set_str(tokens[ptr].str); node.set_str(tokens[ptr].str);
match(tok_str); match(tok_str);
return node; return node;
} }
nasal_ast nasal_parse::id() nasal_ast nasal_parse::id()
{ {
nasal_ast node(tokens[ptr].line,ast_id); nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_id);
node.set_str(tokens[ptr].str); node.set_str(tokens[ptr].str);
match(tok_id); match(tok_id);
return node; return node;
@ -293,7 +295,7 @@ nasal_ast nasal_parse::vec()
tok_func,tok_var,tok_lcurve, tok_func,tok_var,tok_lcurve,
tok_lbrace,tok_lbracket,tok_null tok_lbrace,tok_lbracket,tok_null
}; };
nasal_ast node(tokens[ptr].line,ast_vec); nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_vec);
match(tok_lbracket); match(tok_lbracket);
while(tokens[ptr].type!=tok_rbracket) while(tokens[ptr].type!=tok_rbracket)
{ {
@ -310,7 +312,7 @@ nasal_ast nasal_parse::vec()
} }
nasal_ast nasal_parse::hash() nasal_ast nasal_parse::hash()
{ {
nasal_ast node(tokens[ptr].line,ast_hash); nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_hash);
match(tok_lbrace); match(tok_lbrace);
while(tokens[ptr].type!=tok_rbrace) while(tokens[ptr].type!=tok_rbrace)
{ {
@ -327,7 +329,7 @@ nasal_ast nasal_parse::hash()
} }
nasal_ast nasal_parse::pair() nasal_ast nasal_parse::pair()
{ {
nasal_ast node(tokens[ptr].line,ast_pair); nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_pair);
if(tokens[ptr].type==tok_id) if(tokens[ptr].type==tok_id)
node.add(id()); node.add(id());
else if(tokens[ptr].type==tok_str) else if(tokens[ptr].type==tok_str)
@ -341,7 +343,7 @@ nasal_ast nasal_parse::pair()
nasal_ast nasal_parse::func() nasal_ast nasal_parse::func()
{ {
++in_func; ++in_func;
nasal_ast node(tokens[ptr].line,ast_func); nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_func);
match(tok_func); match(tok_func);
if(tokens[ptr].type==tok_lcurve) if(tokens[ptr].type==tok_lcurve)
node.add(args()); node.add(args());
@ -353,14 +355,14 @@ nasal_ast nasal_parse::func()
} }
nasal_ast nasal_parse::args() nasal_ast nasal_parse::args()
{ {
nasal_ast node(tokens[ptr].line,ast_args); nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_args);
match(tok_lcurve); match(tok_lcurve);
while(tokens[ptr].type!=tok_rcurve) while(tokens[ptr].type!=tok_rcurve)
{ {
nasal_ast tmp=id(); nasal_ast tmp=id();
if(tokens[ptr].type==tok_eq || tokens[ptr].type==tok_ellipsis) if(tokens[ptr].type==tok_eq || tokens[ptr].type==tok_ellipsis)
{ {
nasal_ast special_arg(tokens[ptr].line,ast_null); nasal_ast special_arg(tokens[ptr].line,tokens[ptr].col,ast_null);
if(tokens[ptr].type==tok_eq) if(tokens[ptr].type==tok_eq)
{ {
match(tok_eq); match(tok_eq);
@ -468,7 +470,7 @@ nasal_ast nasal_parse::expr()
++ptr; ++ptr;
break; break;
} }
return {tokens[ptr].line,ast_null}; return {tokens[ptr].line,tokens[ptr].col,ast_null};
} }
nasal_ast nasal_parse::exprs() nasal_ast nasal_parse::exprs()
{ {
@ -477,7 +479,7 @@ nasal_ast nasal_parse::exprs()
die(error_line,"expected expression block"); die(error_line,"expected expression block");
return null(); return null();
} }
nasal_ast node(tokens[ptr].line,ast_block); nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_block);
if(tokens[ptr].type==tok_lbrace) if(tokens[ptr].type==tok_lbrace)
{ {
match(tok_lbrace); match(tok_lbrace);
@ -506,7 +508,7 @@ nasal_ast nasal_parse::calc()
if(tokens[ptr].type==tok_quesmark) if(tokens[ptr].type==tok_quesmark)
{ {
// trinocular calculation // trinocular calculation
nasal_ast tmp(tokens[ptr].line,ast_trino); nasal_ast tmp(tokens[ptr].line,tokens[ptr].col,ast_trino);
match(tok_quesmark); match(tok_quesmark);
tmp.add(std::move(node)); tmp.add(std::move(node));
tmp.add(calc()); tmp.add(calc());
@ -518,7 +520,7 @@ nasal_ast nasal_parse::calc()
{ {
check_memory_reachable(node); check_memory_reachable(node);
// tok_eq~tok_lnkeq is 37 to 42,ast_equal~ast_lnkeq is 21~26 // tok_eq~tok_lnkeq is 37 to 42,ast_equal~ast_lnkeq is 21~26
nasal_ast tmp(tokens[ptr].line,tokens[ptr].type-tok_eq+ast_equal); nasal_ast tmp(tokens[ptr].line,tokens[ptr].col,tokens[ptr].type-tok_eq+ast_equal);
tmp.add(std::move(node)); tmp.add(std::move(node));
match(tokens[ptr].type); match(tokens[ptr].type);
tmp.add(calc()); tmp.add(calc());
@ -531,7 +533,7 @@ nasal_ast nasal_parse::or_expr()
nasal_ast node=and_expr(); nasal_ast node=and_expr();
while(tokens[ptr].type==tok_or) while(tokens[ptr].type==tok_or)
{ {
nasal_ast tmp(tokens[ptr].line,ast_or); nasal_ast tmp(tokens[ptr].line,tokens[ptr].col,ast_or);
tmp.add(std::move(node)); tmp.add(std::move(node));
match(tok_or); match(tok_or);
tmp.add(and_expr()); tmp.add(and_expr());
@ -544,7 +546,7 @@ nasal_ast nasal_parse::and_expr()
nasal_ast node=cmp_expr(); nasal_ast node=cmp_expr();
while(tokens[ptr].type==tok_and) while(tokens[ptr].type==tok_and)
{ {
nasal_ast tmp(tokens[ptr].line,ast_and); nasal_ast tmp(tokens[ptr].line,tokens[ptr].col,ast_and);
tmp.add(std::move(node)); tmp.add(std::move(node));
match(tok_and); match(tok_and);
tmp.add(cmp_expr()); tmp.add(cmp_expr());
@ -558,7 +560,7 @@ nasal_ast nasal_parse::cmp_expr()
while(tok_cmpeq<=tokens[ptr].type && tokens[ptr].type<=tok_geq) while(tok_cmpeq<=tokens[ptr].type && tokens[ptr].type<=tok_geq)
{ {
// tok_cmpeq~tok_geq is 43~48,ast_cmpeq~ast_geq is 27~32 // tok_cmpeq~tok_geq is 43~48,ast_cmpeq~ast_geq is 27~32
nasal_ast tmp(tokens[ptr].line,tokens[ptr].type-tok_cmpeq+ast_cmpeq); nasal_ast tmp(tokens[ptr].line,tokens[ptr].col,tokens[ptr].type-tok_cmpeq+ast_cmpeq);
tmp.add(std::move(node)); tmp.add(std::move(node));
match(tokens[ptr].type); match(tokens[ptr].type);
tmp.add(additive_expr()); tmp.add(additive_expr());
@ -571,7 +573,7 @@ nasal_ast nasal_parse::additive_expr()
nasal_ast node=multive_expr(); nasal_ast node=multive_expr();
while(tokens[ptr].type==tok_add || tokens[ptr].type==tok_sub || tokens[ptr].type==tok_link) while(tokens[ptr].type==tok_add || tokens[ptr].type==tok_sub || tokens[ptr].type==tok_link)
{ {
nasal_ast tmp(tokens[ptr].line,ast_null); nasal_ast tmp(tokens[ptr].line,tokens[ptr].col,ast_null);
switch(tokens[ptr].type) switch(tokens[ptr].type)
{ {
case tok_add: tmp.set_type(ast_add); break; case tok_add: tmp.set_type(ast_add); break;
@ -590,7 +592,7 @@ nasal_ast nasal_parse::multive_expr()
nasal_ast node=(tokens[ptr].type==tok_sub || tokens[ptr].type==tok_not)?unary():scalar(); nasal_ast node=(tokens[ptr].type==tok_sub || tokens[ptr].type==tok_not)?unary():scalar();
while(tokens[ptr].type==tok_mult || tokens[ptr].type==tok_div) while(tokens[ptr].type==tok_mult || tokens[ptr].type==tok_div)
{ {
nasal_ast tmp(tokens[ptr].line,tokens[ptr].type-tok_mult+ast_mult); nasal_ast tmp(tokens[ptr].line,tokens[ptr].col,tokens[ptr].type-tok_mult+ast_mult);
tmp.add(std::move(node)); tmp.add(std::move(node));
match(tokens[ptr].type); match(tokens[ptr].type);
tmp.add((tokens[ptr].type==tok_sub || tokens[ptr].type==tok_not)?unary():scalar()); tmp.add((tokens[ptr].type==tok_sub || tokens[ptr].type==tok_not)?unary():scalar());
@ -600,7 +602,7 @@ nasal_ast nasal_parse::multive_expr()
} }
nasal_ast nasal_parse::unary() nasal_ast nasal_parse::unary()
{ {
nasal_ast node(tokens[ptr].line,ast_null); nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_null);
switch(tokens[ptr].type) switch(tokens[ptr].type)
{ {
case tok_sub:node.set_type(ast_neg);match(tok_sub);break; case tok_sub:node.set_type(ast_neg);match(tok_sub);break;
@ -611,7 +613,7 @@ nasal_ast nasal_parse::unary()
} }
nasal_ast nasal_parse::scalar() nasal_ast nasal_parse::scalar()
{ {
nasal_ast node(tokens[ptr].line,ast_null); nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_null);
if(tokens[ptr].type==tok_nil) {node=nil();match(tok_nil);} if(tokens[ptr].type==tok_nil) {node=nil();match(tok_nil);}
else if(tokens[ptr].type==tok_num) node=num(); else if(tokens[ptr].type==tok_num) node=num();
else if(tokens[ptr].type==tok_str) node=str(); else if(tokens[ptr].type==tok_str) node=str();
@ -642,7 +644,7 @@ nasal_ast nasal_parse::scalar()
if(is_call(tokens[ptr].type) && !(tokens[ptr].type==tok_lcurve && tokens[ptr+1].type==tok_var)) if(is_call(tokens[ptr].type) && !(tokens[ptr].type==tok_lcurve && tokens[ptr+1].type==tok_var))
{ {
nasal_ast tmp=std::move(node); nasal_ast tmp=std::move(node);
node={tokens[ptr].line,ast_call}; node={tokens[ptr].line,tokens[ptr].col,ast_call};
node.add(std::move(tmp)); node.add(std::move(tmp));
while(is_call(tokens[ptr].type)) while(is_call(tokens[ptr].type))
node.add(call_scalar()); node.add(call_scalar());
@ -658,11 +660,11 @@ nasal_ast nasal_parse::call_scalar()
case tok_dot: return callh(); break; case tok_dot: return callh(); break;
} }
// should never run this expression // should never run this expression
return {tokens[ptr].line,ast_nil}; return {tokens[ptr].line,tokens[ptr].col,ast_nil};
} }
nasal_ast nasal_parse::callh() nasal_ast nasal_parse::callh()
{ {
nasal_ast node(tokens[ptr].line,ast_callh); nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_callh);
match(tok_dot); match(tok_dot);
node.set_str(tokens[ptr].str); node.set_str(tokens[ptr].str);
match(tok_id,"expected hashmap key"); // get key match(tok_id,"expected hashmap key"); // get key
@ -680,7 +682,7 @@ nasal_ast nasal_parse::callv()
tok_lbrace,tok_lbracket,tok_colon, tok_lbrace,tok_lbracket,tok_colon,
tok_null tok_null
}; };
nasal_ast node(tokens[ptr].line,ast_callv); nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_callv);
match(tok_lbracket); match(tok_lbracket);
while(tokens[ptr].type!=tok_rbracket) while(tokens[ptr].type!=tok_rbracket)
{ {
@ -706,7 +708,7 @@ nasal_ast nasal_parse::callf()
tok_func,tok_var,tok_lcurve, tok_func,tok_var,tok_lcurve,
tok_lbrace,tok_lbracket,tok_null tok_lbrace,tok_lbracket,tok_null
}; };
nasal_ast node(tokens[ptr].line,ast_callf); nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_callf);
bool special_call=check_special_call(); bool special_call=check_special_call();
match(tok_lcurve); match(tok_lcurve);
while(tokens[ptr].type!=tok_rcurve) while(tokens[ptr].type!=tok_rcurve)
@ -727,7 +729,7 @@ nasal_ast nasal_parse::subvec()
nasal_ast node=tokens[ptr].type==tok_colon?nil():calc(); nasal_ast node=tokens[ptr].type==tok_colon?nil():calc();
if(tokens[ptr].type==tok_colon) if(tokens[ptr].type==tok_colon)
{ {
nasal_ast tmp(node.line(),ast_subvec); nasal_ast tmp(node.line(),node.col(),ast_subvec);
match(tok_colon); match(tok_colon);
tmp.add(std::move(node)); tmp.add(std::move(node));
tmp.add((tokens[ptr].type==tok_comma || tokens[ptr].type==tok_rbracket)?nil():calc()); tmp.add((tokens[ptr].type==tok_comma || tokens[ptr].type==tok_rbracket)?nil():calc());
@ -737,7 +739,7 @@ nasal_ast nasal_parse::subvec()
} }
nasal_ast nasal_parse::definition() nasal_ast nasal_parse::definition()
{ {
nasal_ast node(tokens[ptr].line,ast_def); nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_def);
if(tokens[ptr].type==tok_var) if(tokens[ptr].type==tok_var)
{ {
match(tok_var); match(tok_var);
@ -778,7 +780,7 @@ nasal_ast nasal_parse::outcurve_def()
} }
nasal_ast nasal_parse::multi_id() nasal_ast nasal_parse::multi_id()
{ {
nasal_ast node(tokens[ptr].line,ast_multi_id); nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_multi_id);
while(tokens[ptr].type!=tok_eof) while(tokens[ptr].type!=tok_eof)
{ {
node.add(id()); node.add(id());
@ -805,7 +807,7 @@ nasal_ast nasal_parse::multi_scalar(bool check_call_memory)
tok_func,tok_var,tok_lcurve, tok_func,tok_var,tok_lcurve,
tok_lbrace,tok_lbracket,tok_null tok_lbrace,tok_lbracket,tok_null
}; };
nasal_ast node(tokens[ptr].line,ast_multi_scalar); nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_multi_scalar);
match(tok_lcurve); match(tok_lcurve);
while(tokens[ptr].type!=tok_rcurve) while(tokens[ptr].type!=tok_rcurve)
{ {
@ -824,7 +826,7 @@ nasal_ast nasal_parse::multi_scalar(bool check_call_memory)
} }
nasal_ast nasal_parse::multi_assgin() nasal_ast nasal_parse::multi_assgin()
{ {
nasal_ast node(tokens[ptr].line,ast_multi_assign); nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_multi_assign);
node.add(multi_scalar(true)); node.add(multi_scalar(true));
match(tok_eq); match(tok_eq);
if(tokens[ptr].type==tok_eof) if(tokens[ptr].type==tok_eof)
@ -844,7 +846,7 @@ nasal_ast nasal_parse::multi_assgin()
nasal_ast nasal_parse::loop() nasal_ast nasal_parse::loop()
{ {
++in_loop; ++in_loop;
nasal_ast node; nasal_ast node(0,0,ast_null);
switch(tokens[ptr].type) switch(tokens[ptr].type)
{ {
case tok_while: node=while_loop(); break; case tok_while: node=while_loop(); break;
@ -857,7 +859,7 @@ nasal_ast nasal_parse::loop()
} }
nasal_ast nasal_parse::while_loop() nasal_ast nasal_parse::while_loop()
{ {
nasal_ast node(tokens[ptr].line,ast_while); nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_while);
match(tok_while); match(tok_while);
match(tok_lcurve); match(tok_lcurve);
node.add(calc()); node.add(calc());
@ -867,7 +869,7 @@ nasal_ast nasal_parse::while_loop()
} }
nasal_ast nasal_parse::for_loop() nasal_ast nasal_parse::for_loop()
{ {
nasal_ast node(tokens[ptr].line,ast_for); nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_for);
match(tok_for); match(tok_for);
match(tok_lcurve); match(tok_lcurve);
// first expression // first expression
@ -903,7 +905,7 @@ nasal_ast nasal_parse::for_loop()
} }
nasal_ast nasal_parse::forei_loop() nasal_ast nasal_parse::forei_loop()
{ {
nasal_ast node(tokens[ptr].line,ast_null); nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_null);
switch(tokens[ptr].type) switch(tokens[ptr].type)
{ {
case tok_forindex:node.set_type(ast_forindex);match(tok_forindex);break; case tok_forindex:node.set_type(ast_forindex);match(tok_forindex);break;
@ -925,7 +927,7 @@ nasal_ast nasal_parse::forei_loop()
} }
nasal_ast nasal_parse::iter_gen() nasal_ast nasal_parse::iter_gen()
{ {
nasal_ast node(tokens[ptr].line,ast_null); nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_null);
if(tokens[ptr].type==tok_var) if(tokens[ptr].type==tok_var)
{ {
match(tok_var); match(tok_var);
@ -944,8 +946,8 @@ nasal_ast nasal_parse::iter_gen()
} }
nasal_ast nasal_parse::conditional() nasal_ast nasal_parse::conditional()
{ {
nasal_ast node(tokens[ptr].line,ast_conditional); nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_conditional);
nasal_ast ifnode(tokens[ptr].line,ast_if); nasal_ast ifnode(tokens[ptr].line,tokens[ptr].col,ast_if);
match(tok_if); match(tok_if);
match(tok_lcurve); match(tok_lcurve);
ifnode.add(calc()); ifnode.add(calc());
@ -954,7 +956,7 @@ nasal_ast nasal_parse::conditional()
node.add(std::move(ifnode)); node.add(std::move(ifnode));
while(tokens[ptr].type==tok_elsif) while(tokens[ptr].type==tok_elsif)
{ {
nasal_ast elsifnode(tokens[ptr].line,ast_elsif); nasal_ast elsifnode(tokens[ptr].line,tokens[ptr].col,ast_elsif);
match(tok_elsif); match(tok_elsif);
match(tok_lcurve); match(tok_lcurve);
elsifnode.add(calc()); elsifnode.add(calc());
@ -964,7 +966,7 @@ nasal_ast nasal_parse::conditional()
} }
if(tokens[ptr].type==tok_else) if(tokens[ptr].type==tok_else)
{ {
nasal_ast elsenode(tokens[ptr].line,ast_else); nasal_ast elsenode(tokens[ptr].line,tokens[ptr].col,ast_else);
match(tok_else); match(tok_else);
elsenode.add(exprs()); elsenode.add(exprs());
node.add(std::move(elsenode)); node.add(std::move(elsenode));
@ -973,19 +975,19 @@ nasal_ast nasal_parse::conditional()
} }
nasal_ast nasal_parse::continue_expr() nasal_ast nasal_parse::continue_expr()
{ {
nasal_ast node(tokens[ptr].line,ast_continue); nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_continue);
match(tok_continue); match(tok_continue);
return node; return node;
} }
nasal_ast nasal_parse::break_expr() nasal_ast nasal_parse::break_expr()
{ {
nasal_ast node(tokens[ptr].line,ast_break); nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_break);
match(tok_break); match(tok_break);
return node; return node;
} }
nasal_ast nasal_parse::ret_expr() nasal_ast nasal_parse::ret_expr()
{ {
nasal_ast node(tokens[ptr].line,ast_ret); nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_ret);
match(tok_ret); match(tok_ret);
u32 type=tokens[ptr].type; u32 type=tokens[ptr].type;
if(type==tok_nil || type==tok_num || if(type==tok_nil || type==tok_num ||