🚀 add `stl/csv.nas` & ast name change

This commit is contained in:
ValKmjolnir 2022-10-19 00:54:21 +08:00
parent 7a93527948
commit 025ff49ffc
10 changed files with 531 additions and 509 deletions

View File

@ -83,45 +83,45 @@ void execute(const string& file,const std::vector<string>& argv,const u32 cmd)
{
// front end use the same error module
nasal_err nerr;
nasal_lexer lexer(nerr);
nasal_lexer lex(nerr);
nasal_parse parse(nerr);
nasal_import linker(nerr);
nasal_import ld(nerr);
nasal_codegen gen(nerr);
// back end
nasal_vm vm;
// lexer scans file to get tokens
lexer.scan(file);
lex.scan(file);
if(cmd&VM_TOKEN)
lexer.print();
lex.print();
// parser gets lexer's token list to compile
parse.compile(lexer);
parse.compile(lex);
// linker gets parser's ast and load import files to this ast
linker.link(parse,file,cmd&VM_DETAIL);
ld.link(parse,file,cmd&VM_DETAIL);
// optimizer does simple optimization on ast
if(cmd&VM_OPT)
optimize(parse.ast());
optimize(parse.tree());
if(cmd&VM_AST)
parse.print();
// code generator gets parser's ast and linker's import file list to generate code
gen.compile(parse,linker);
gen.compile(parse,ld);
if(cmd&VM_CODE)
gen.print();
// run
if(cmd&VM_DEBUG)
nasal_dbg(nerr).run(gen,linker,argv);
nasal_dbg(nerr).run(gen,ld,argv);
else if(cmd&VM_TIME)
{
auto start=std::chrono::high_resolution_clock::now();
vm.run(gen,linker,argv,cmd&VM_DETAIL);
vm.run(gen,ld,argv,cmd&VM_DETAIL);
auto end=std::chrono::high_resolution_clock::now();
std::clog<<"process exited after "<<(end-start).count()*1.0/std::chrono::high_resolution_clock::duration::period::den<<"s.\n";
}
else if(cmd&VM_EXEC)
vm.run(gen,linker,argv,cmd&VM_DETAIL);
vm.run(gen,ld,argv,cmd&VM_DETAIL);
}
i32 main(i32 argc,const char* argv[])

View File

@ -128,7 +128,7 @@ const char* ast_name[]=
"return"
};
class nasal_ast
class ast
{
private:
u32 node_line;
@ -136,24 +136,24 @@ private:
u32 node_type;
f64 node_num;
string node_str;
std::vector<nasal_ast> node_child;
std::vector<ast> node_child;
public:
nasal_ast(const u32 l,const u32 c,const u32 t):
ast(const u32 l,const u32 c,const u32 t):
node_line(l),node_col(c),node_type(t),node_num(0){}
nasal_ast(const nasal_ast&);
nasal_ast(nasal_ast&&);
void tree();
ast(const ast&);
ast(ast&&);
void print_tree();
void print(u32,bool,std::vector<string>&);
void clear();
nasal_ast& operator=(const nasal_ast&);
nasal_ast& operator=(nasal_ast&&);
nasal_ast& operator[](usize n){return node_child[n];}
const nasal_ast& operator[](usize n) const {return node_child[n];}
ast& operator=(const ast&);
ast& operator=(ast&&);
ast& operator[](usize n){return node_child[n];}
const ast& operator[](usize n) const {return node_child[n];}
usize size() const {return node_child.size();}
void add(nasal_ast&& ast){node_child.push_back(std::move(ast));}
void add(const nasal_ast& ast){node_child.push_back(ast);}
void add(ast&& node){node_child.push_back(std::move(node));}
void add(const ast& node){node_child.push_back(node);}
void set_line(const u32 l){node_line=l;}
void set_type(const u32 t){node_type=t;}
void set_str(const string& s){node_str=s;}
@ -164,11 +164,11 @@ public:
inline u32 type() const {return node_type;}
inline f64 num() const {return node_num;}
inline const string& str() const {return node_str;}
inline const std::vector<nasal_ast>& child() const {return node_child;}
inline std::vector<nasal_ast>& child(){return node_child;}
inline const std::vector<ast>& child() const {return node_child;}
inline std::vector<ast>& child(){return node_child;}
};
nasal_ast::nasal_ast(const nasal_ast& tmp):
ast::ast(const ast& tmp):
node_str(tmp.node_str),node_child(tmp.node_child)
{
node_line=tmp.node_line;
@ -177,7 +177,7 @@ nasal_ast::nasal_ast(const nasal_ast& tmp):
node_num =tmp.node_num;
}
nasal_ast::nasal_ast(nasal_ast&& tmp)
ast::ast(ast&& tmp)
{
node_line=tmp.node_line;
node_col=tmp.node_col;
@ -187,7 +187,7 @@ nasal_ast::nasal_ast(nasal_ast&& tmp)
node_child.swap(tmp.node_child);
}
nasal_ast& nasal_ast::operator=(const nasal_ast& tmp)
ast& ast::operator=(const ast& tmp)
{
node_line=tmp.node_line;
node_col=tmp.node_col;
@ -198,7 +198,7 @@ nasal_ast& nasal_ast::operator=(const nasal_ast& tmp)
return *this;
}
nasal_ast& nasal_ast::operator=(nasal_ast&& tmp)
ast& ast::operator=(ast&& tmp)
{
node_line=tmp.node_line;
node_col=tmp.node_col;
@ -209,7 +209,7 @@ nasal_ast& nasal_ast::operator=(nasal_ast&& tmp)
return *this;
}
void nasal_ast::clear()
void ast::clear()
{
node_line=node_col=0;
node_num=0;
@ -218,13 +218,13 @@ void nasal_ast::clear()
node_child.clear();
}
void nasal_ast::tree()
void ast::print_tree()
{
std::vector<string> tmp;
print(0,false,tmp);
}
void nasal_ast::print(u32 depth,bool last,std::vector<string>& indent)
void ast::print(u32 depth,bool last,std::vector<string>& indent)
{
for(auto& i:indent)
std::cout<<i;

File diff suppressed because it is too large Load Diff

View File

@ -21,10 +21,9 @@ enum vm_type:u8{
vm_func,
vm_upval,
vm_obj,
vm_co,
vm_tsize
vm_co
};
const u32 gc_tsize=vm_tsize-vm_str;
const u32 gc_tsize=vm_co-vm_str+1;
// change parameters here to make your own efficient gc
// better set bigger number on vm_vec
const u32 ini[gc_tsize]=
@ -39,11 +38,11 @@ const u32 ini[gc_tsize]=
};
const u32 incr[gc_tsize]=
{
256, // vm_str
256, // vm_vec
256, // vm_hash
128, // vm_func
128, // vm_upval
1024,// vm_str
512, // vm_vec
512, // vm_hash
512, // vm_func
512, // vm_upval
128, // vm_obj
32 // vm_co
};

View File

@ -21,14 +21,14 @@ private:
nasal_err& nerr;
std::vector<string> files;
std::vector<string> envpath;
bool imptchk(const nasal_ast&);
bool imptchk(const ast&);
bool exist(const string&);
void linker(nasal_ast&,nasal_ast&&);
string path(const nasal_ast&);
void linker(ast&,ast&&);
string path(const ast&);
string findf(const string&);
nasal_ast fimpt(nasal_ast&);
nasal_ast libimpt();
nasal_ast load(nasal_ast&,u16);
ast fimpt(ast&);
ast libimpt();
ast load(ast&,u16);
public:
nasal_import(nasal_err&);
void link(nasal_parse&,const string&,bool);
@ -55,7 +55,7 @@ nasal_import::nasal_import(nasal_err& e):lib_loaded(false),nerr(e){
envpath.push_back(PATH.substr(last));
}
string nasal_import::path(const nasal_ast& node)
string nasal_import::path(const ast& node)
{
if(node[1].type()==ast_callf)
return node[1][0].str();
@ -101,7 +101,7 @@ string nasal_import::findf(const string& fname)
return "";
}
bool nasal_import::imptchk(const nasal_ast& node)
bool nasal_import::imptchk(const ast& node)
{
// only these two kinds of node can be recognized as 'import':
/*
@ -143,14 +143,14 @@ bool nasal_import::exist(const string& file)
return false;
}
void nasal_import::linker(nasal_ast& root,nasal_ast&& add_root)
void nasal_import::linker(ast& root,ast&& add_root)
{
// add children of add_root to the back of root
for(auto& i:add_root.child())
root.add(std::move(i));
}
nasal_ast nasal_import::fimpt(nasal_ast& node)
ast nasal_import::fimpt(ast& node)
{
nasal_lexer lex(nerr);
nasal_parse par(nerr);
@ -166,12 +166,12 @@ nasal_ast nasal_import::fimpt(nasal_ast& node)
// start importing...
lex.scan(filename);
par.compile(lex);
nasal_ast tmp=std::move(par.ast());
ast tmp=std::move(par.tree());
// check if tmp has 'import'
return load(tmp,files.size()-1);
}
nasal_ast nasal_import::libimpt()
ast nasal_import::libimpt()
{
nasal_lexer lex(nerr);
nasal_parse par(nerr);
@ -186,14 +186,14 @@ nasal_ast nasal_import::libimpt()
// start importing...
lex.scan(filename);
par.compile(lex);
nasal_ast tmp=std::move(par.ast());
ast tmp=std::move(par.tree());
// check if tmp has 'import'
return load(tmp,files.size()-1);
}
nasal_ast nasal_import::load(nasal_ast& root,u16 fileindex)
ast nasal_import::load(ast& root,u16 fileindex)
{
nasal_ast new_root(0,0,ast_root);
ast new_root(0,0,ast_root);
if(!lib_loaded)
{
linker(new_root,libimpt());
@ -207,7 +207,7 @@ nasal_ast nasal_import::load(nasal_ast& root,u16 fileindex)
break;
}
// add root to the back of new_root
nasal_ast file_head(0,0,ast_file);
ast file_head(0,0,ast_file);
file_head.set_num(fileindex);
new_root.add(std::move(file_head));
linker(new_root,std::move(root));
@ -221,7 +221,7 @@ void nasal_import::link(nasal_parse& parse,const string& self,bool spath=false)
files={self};
// scan root and import files,then generate a new ast and return to import_ast
// the main file's index is 0
parse.ast()=load(parse.ast(),0);
parse.tree()=load(parse.tree(),0);
nerr.chkerr();
}

View File

@ -22,15 +22,22 @@
enum tok:u32{
tok_null=0, // null token (default token type)
tok_num, // number basic token type
tok_str, // string basic token type
tok_id, // identifier basic token type
tok_num, // number literal
tok_str, // string literal
tok_id, // identifier
tok_for, // loop keyword for
tok_forindex,// loop keyword forindex
tok_foreach, // loop keyword foreach
tok_while, // loop keyword while
tok_var,tok_func,tok_break,tok_continue,
tok_ret,tok_if,tok_elsif,tok_else,tok_nil,
tok_var, // keyword for definition
tok_func, // keyword for definition of function
tok_break, // loop keyword break
tok_continue,// loop keyword continue
tok_ret, // function keyword return
tok_if, // condition expression keyword if
tok_elsif, // condition expression keyword elsif
tok_else, // condition expression keyword else
tok_nil, // nil literal
tok_lcurve,tok_rcurve,
tok_lbracket,tok_rbracket,
tok_lbrace,tok_rbrace,

View File

@ -3,7 +3,7 @@
#include <cmath>
void const_str(nasal_ast& root)
void const_str(ast& root)
{
auto& vec=root.child();
root.set_str(vec[0].str()+vec[1].str());
@ -11,7 +11,7 @@ void const_str(nasal_ast& root)
root.set_type(ast_str);
}
void const_num(nasal_ast& root)
void const_num(ast& root)
{
auto& vec=root.child();
f64 res;
@ -34,7 +34,7 @@ void const_num(nasal_ast& root)
root.set_type(ast_num);
}
void calc_const(nasal_ast& root)
void calc_const(ast& root)
{
auto& vec=root.child();
for(auto& i:vec)
@ -62,7 +62,7 @@ void calc_const(nasal_ast& root)
vec[0].type()==ast_num && vec[1].type()==ast_num)
const_num(root);
}
void optimize(nasal_ast& root)
void optimize(ast& root)
{
for(auto& i:root.child())
calc_const(i);

View File

@ -47,66 +47,66 @@ private:
u32 in_func; // count function block
u32 in_loop; // count loop block
const token* tokens;// ref from nasal_lexer
nasal_ast root;
ast root;
nasal_err& nerr;
void die(u32,string,bool);
void match(u32 type,const char* info=nullptr);
bool check_comma(const u32*);
bool check_multi_scalar();
bool check_func_end(const nasal_ast&);
bool check_func_end(const ast&);
bool check_special_call();
bool need_semi_check(const nasal_ast&);
void check_memory_reachable(const nasal_ast&);
nasal_ast null();
nasal_ast nil();
nasal_ast num();
nasal_ast str();
nasal_ast id();
nasal_ast vec();
nasal_ast hash();
nasal_ast pair();
nasal_ast func();
nasal_ast args();
nasal_ast lcurve_expr();
nasal_ast expr();
nasal_ast exprs();
nasal_ast calc();
nasal_ast or_expr();
nasal_ast and_expr();
nasal_ast cmp_expr();
nasal_ast additive_expr();
nasal_ast multive_expr();
nasal_ast unary();
nasal_ast scalar();
nasal_ast call_scalar();
nasal_ast callh();
nasal_ast callv();
nasal_ast callf();
nasal_ast subvec();
nasal_ast definition();
nasal_ast incurve_def();
nasal_ast outcurve_def();
nasal_ast multi_id();
nasal_ast multi_scalar(bool);
nasal_ast multi_assgin();
nasal_ast loop();
nasal_ast while_loop();
nasal_ast for_loop();
nasal_ast forei_loop();
nasal_ast iter_gen();
nasal_ast conditional();
nasal_ast continue_expr();
nasal_ast break_expr();
nasal_ast ret_expr();
bool need_semi_check(const ast&);
void check_memory_reachable(const ast&);
ast null();
ast nil();
ast num();
ast str();
ast id();
ast vec();
ast hash();
ast pair();
ast func();
ast args();
ast lcurve_expr();
ast expr();
ast exprs();
ast calc();
ast or_expr();
ast and_expr();
ast cmp_expr();
ast additive_expr();
ast multive_expr();
ast unary();
ast scalar();
ast call_scalar();
ast callh();
ast callv();
ast callf();
ast subvec();
ast definition();
ast incurve_def();
ast outcurve_def();
ast multi_id();
ast multi_scalar(bool);
ast multi_assgin();
ast loop();
ast while_loop();
ast for_loop();
ast forei_loop();
ast iter_gen();
ast conditional();
ast continue_expr();
ast break_expr();
ast ret_expr();
public:
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.print_tree();}
void compile(const nasal_lexer&);
nasal_ast& ast(){return root;}
const nasal_ast& ast() const {return root;}
ast& tree(){return root;}
const ast& tree() const {return root;}
};
void nasal_parse::compile(const nasal_lexer& lexer)
{
@ -188,7 +188,7 @@ bool nasal_parse::check_multi_scalar()
}
return false;
}
bool nasal_parse::check_func_end(const nasal_ast& node)
bool nasal_parse::check_func_end(const ast& node)
{
u32 type=node.type();
if(type==ast_func)
@ -235,18 +235,18 @@ bool nasal_parse::check_special_call()
}
return false;
}
bool nasal_parse::need_semi_check(const nasal_ast& node)
bool nasal_parse::need_semi_check(const ast& node)
{
u32 type=node.type();
if(type==ast_for || type==ast_foreach || type==ast_forindex || type==ast_while || type==ast_conditional)
return false;
return !check_func_end(node);
}
void nasal_parse::check_memory_reachable(const nasal_ast& node)
void nasal_parse::check_memory_reachable(const ast& node)
{
if(node.type()==ast_call)
{
const nasal_ast& tmp=node.child().back();
const ast& tmp=node.child().back();
if(tmp.type()==ast_callf)
die(tmp.line(),"bad left-value");
if(tmp.type()==ast_callv && (tmp.size()==0 || tmp.size()>1 || tmp[0].type()==ast_subvec))
@ -255,36 +255,36 @@ void nasal_parse::check_memory_reachable(const nasal_ast& node)
else if(node.type()!=ast_id)
die(node.line(),"bad left-value");
}
nasal_ast nasal_parse::null()
ast nasal_parse::null()
{
return {tokens[ptr].line,tokens[ptr].col,ast_null};
}
nasal_ast nasal_parse::nil()
ast nasal_parse::nil()
{
return {tokens[ptr].line,tokens[ptr].col,ast_nil};
}
nasal_ast nasal_parse::num()
ast nasal_parse::num()
{
nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_num);
ast node(tokens[ptr].line,tokens[ptr].col,ast_num);
node.set_num(str2num(tokens[ptr].str.c_str()));
match(tok_num);
return node;
}
nasal_ast nasal_parse::str()
ast nasal_parse::str()
{
nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_str);
ast node(tokens[ptr].line,tokens[ptr].col,ast_str);
node.set_str(tokens[ptr].str);
match(tok_str);
return node;
}
nasal_ast nasal_parse::id()
ast nasal_parse::id()
{
nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_id);
ast node(tokens[ptr].line,tokens[ptr].col,ast_id);
node.set_str(tokens[ptr].str);
match(tok_id);
return node;
}
nasal_ast nasal_parse::vec()
ast nasal_parse::vec()
{
// panic set for this token is not ','
// this is the FIRST set of calculation
@ -295,7 +295,7 @@ nasal_ast nasal_parse::vec()
tok_func,tok_var,tok_lcurve,
tok_lbrace,tok_lbracket,tok_null
};
nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_vec);
ast node(tokens[ptr].line,tokens[ptr].col,ast_vec);
match(tok_lbracket);
while(tokens[ptr].type!=tok_rbracket)
{
@ -310,9 +310,9 @@ nasal_ast nasal_parse::vec()
match(tok_rbracket,"expected ']' when generating vector");
return node;
}
nasal_ast nasal_parse::hash()
ast nasal_parse::hash()
{
nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_hash);
ast node(tokens[ptr].line,tokens[ptr].col,ast_hash);
match(tok_lbrace);
while(tokens[ptr].type!=tok_rbrace)
{
@ -327,9 +327,9 @@ nasal_ast nasal_parse::hash()
match(tok_rbrace,"expected '}' when generating hash");
return node;
}
nasal_ast nasal_parse::pair()
ast nasal_parse::pair()
{
nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_pair);
ast node(tokens[ptr].line,tokens[ptr].col,ast_pair);
if(tokens[ptr].type==tok_id)
node.add(id());
else if(tokens[ptr].type==tok_str)
@ -340,10 +340,10 @@ nasal_ast nasal_parse::pair()
node.add(calc());
return node;
}
nasal_ast nasal_parse::func()
ast nasal_parse::func()
{
++in_func;
nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_func);
ast node(tokens[ptr].line,tokens[ptr].col,ast_func);
match(tok_func);
if(tokens[ptr].type==tok_lcurve)
node.add(args());
@ -353,16 +353,16 @@ nasal_ast nasal_parse::func()
--in_func;
return node;
}
nasal_ast nasal_parse::args()
ast nasal_parse::args()
{
nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_args);
ast node(tokens[ptr].line,tokens[ptr].col,ast_args);
match(tok_lcurve);
while(tokens[ptr].type!=tok_rcurve)
{
nasal_ast tmp=id();
ast tmp=id();
if(tokens[ptr].type==tok_eq || tokens[ptr].type==tok_ellipsis)
{
nasal_ast special_arg(tokens[ptr].line,tokens[ptr].col,ast_null);
ast special_arg(tokens[ptr].line,tokens[ptr].col,ast_null);
if(tokens[ptr].type==tok_eq)
{
match(tok_eq);
@ -430,13 +430,13 @@ nasal_ast nasal_parse::args()
}
return node;
}
nasal_ast nasal_parse::lcurve_expr()
ast nasal_parse::lcurve_expr()
{
if(tokens[ptr+1].type==tok_var)
return definition();
return check_multi_scalar()?multi_assgin():calc();
}
nasal_ast nasal_parse::expr()
ast nasal_parse::expr()
{
u32 type=tokens[ptr].type;
if((type==tok_break || type==tok_continue) && !in_loop)
@ -472,14 +472,14 @@ nasal_ast nasal_parse::expr()
}
return {tokens[ptr].line,tokens[ptr].col,ast_null};
}
nasal_ast nasal_parse::exprs()
ast nasal_parse::exprs()
{
if(tokens[ptr].type==tok_eof)
{
die(error_line,"expected expression block");
return null();
}
nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_block);
ast node(tokens[ptr].line,tokens[ptr].col,ast_block);
if(tokens[ptr].type==tok_lbrace)
{
match(tok_lbrace);
@ -502,13 +502,13 @@ nasal_ast nasal_parse::exprs()
}
return node;
}
nasal_ast nasal_parse::calc()
ast nasal_parse::calc()
{
nasal_ast node=or_expr();
ast node=or_expr();
if(tokens[ptr].type==tok_quesmark)
{
// trinocular calculation
nasal_ast tmp(tokens[ptr].line,tokens[ptr].col,ast_trino);
ast tmp(tokens[ptr].line,tokens[ptr].col,ast_trino);
match(tok_quesmark);
tmp.add(std::move(node));
tmp.add(calc());
@ -520,7 +520,7 @@ nasal_ast nasal_parse::calc()
{
check_memory_reachable(node);
// tok_eq~tok_lnkeq is 37 to 42,ast_equal~ast_lnkeq is 21~26
nasal_ast tmp(tokens[ptr].line,tokens[ptr].col,tokens[ptr].type-tok_eq+ast_equal);
ast tmp(tokens[ptr].line,tokens[ptr].col,tokens[ptr].type-tok_eq+ast_equal);
tmp.add(std::move(node));
match(tokens[ptr].type);
tmp.add(calc());
@ -528,12 +528,12 @@ nasal_ast nasal_parse::calc()
}
return node;
}
nasal_ast nasal_parse::or_expr()
ast nasal_parse::or_expr()
{
nasal_ast node=and_expr();
ast node=and_expr();
while(tokens[ptr].type==tok_or)
{
nasal_ast tmp(tokens[ptr].line,tokens[ptr].col,ast_or);
ast tmp(tokens[ptr].line,tokens[ptr].col,ast_or);
tmp.add(std::move(node));
match(tok_or);
tmp.add(and_expr());
@ -541,12 +541,12 @@ nasal_ast nasal_parse::or_expr()
}
return node;
}
nasal_ast nasal_parse::and_expr()
ast nasal_parse::and_expr()
{
nasal_ast node=cmp_expr();
ast node=cmp_expr();
while(tokens[ptr].type==tok_and)
{
nasal_ast tmp(tokens[ptr].line,tokens[ptr].col,ast_and);
ast tmp(tokens[ptr].line,tokens[ptr].col,ast_and);
tmp.add(std::move(node));
match(tok_and);
tmp.add(cmp_expr());
@ -554,13 +554,13 @@ nasal_ast nasal_parse::and_expr()
}
return node;
}
nasal_ast nasal_parse::cmp_expr()
ast nasal_parse::cmp_expr()
{
nasal_ast node=additive_expr();
ast node=additive_expr();
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
nasal_ast tmp(tokens[ptr].line,tokens[ptr].col,tokens[ptr].type-tok_cmpeq+ast_cmpeq);
ast tmp(tokens[ptr].line,tokens[ptr].col,tokens[ptr].type-tok_cmpeq+ast_cmpeq);
tmp.add(std::move(node));
match(tokens[ptr].type);
tmp.add(additive_expr());
@ -568,12 +568,12 @@ nasal_ast nasal_parse::cmp_expr()
}
return node;
}
nasal_ast nasal_parse::additive_expr()
ast nasal_parse::additive_expr()
{
nasal_ast node=multive_expr();
ast node=multive_expr();
while(tokens[ptr].type==tok_add || tokens[ptr].type==tok_sub || tokens[ptr].type==tok_link)
{
nasal_ast tmp(tokens[ptr].line,tokens[ptr].col,ast_null);
ast tmp(tokens[ptr].line,tokens[ptr].col,ast_null);
switch(tokens[ptr].type)
{
case tok_add: tmp.set_type(ast_add); break;
@ -587,12 +587,12 @@ nasal_ast nasal_parse::additive_expr()
}
return node;
}
nasal_ast nasal_parse::multive_expr()
ast nasal_parse::multive_expr()
{
nasal_ast node=(tokens[ptr].type==tok_sub || tokens[ptr].type==tok_not)?unary():scalar();
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)
{
nasal_ast tmp(tokens[ptr].line,tokens[ptr].col,tokens[ptr].type-tok_mult+ast_mult);
ast tmp(tokens[ptr].line,tokens[ptr].col,tokens[ptr].type-tok_mult+ast_mult);
tmp.add(std::move(node));
match(tokens[ptr].type);
tmp.add((tokens[ptr].type==tok_sub || tokens[ptr].type==tok_not)?unary():scalar());
@ -600,9 +600,9 @@ nasal_ast nasal_parse::multive_expr()
}
return node;
}
nasal_ast nasal_parse::unary()
ast nasal_parse::unary()
{
nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_null);
ast node(tokens[ptr].line,tokens[ptr].col,ast_null);
switch(tokens[ptr].type)
{
case tok_sub:node.set_type(ast_neg);match(tok_sub);break;
@ -611,9 +611,9 @@ nasal_ast nasal_parse::unary()
node.add((tokens[ptr].type==tok_sub || tokens[ptr].type==tok_not)?unary():scalar());
return node;
}
nasal_ast nasal_parse::scalar()
ast nasal_parse::scalar()
{
nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_null);
ast node(tokens[ptr].line,tokens[ptr].col,ast_null);
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_str) node=str();
@ -643,7 +643,7 @@ nasal_ast nasal_parse::scalar()
// check call and avoid ambiguous syntax
if(is_call(tokens[ptr].type) && !(tokens[ptr].type==tok_lcurve && tokens[ptr+1].type==tok_var))
{
nasal_ast tmp=std::move(node);
ast tmp=std::move(node);
node={tokens[ptr].line,tokens[ptr].col,ast_call};
node.add(std::move(tmp));
while(is_call(tokens[ptr].type))
@ -651,7 +651,7 @@ nasal_ast nasal_parse::scalar()
}
return node;
}
nasal_ast nasal_parse::call_scalar()
ast nasal_parse::call_scalar()
{
switch(tokens[ptr].type)
{
@ -662,15 +662,15 @@ nasal_ast nasal_parse::call_scalar()
// should never run this expression
return {tokens[ptr].line,tokens[ptr].col,ast_nil};
}
nasal_ast nasal_parse::callh()
ast nasal_parse::callh()
{
nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_callh);
ast node(tokens[ptr].line,tokens[ptr].col,ast_callh);
match(tok_dot);
node.set_str(tokens[ptr].str);
match(tok_id,"expected hashmap key"); // get key
return node;
}
nasal_ast nasal_parse::callv()
ast nasal_parse::callv()
{
// panic set for this token is not ','
// this is the FIRST set of subvec
@ -682,7 +682,7 @@ nasal_ast nasal_parse::callv()
tok_lbrace,tok_lbracket,tok_colon,
tok_null
};
nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_callv);
ast node(tokens[ptr].line,tokens[ptr].col,ast_callv);
match(tok_lbracket);
while(tokens[ptr].type!=tok_rbracket)
{
@ -699,7 +699,7 @@ nasal_ast nasal_parse::callv()
match(tok_rbracket,"expected ']' when calling vector");
return node;
}
nasal_ast nasal_parse::callf()
ast nasal_parse::callf()
{
// panic set for this token is not ','
// this is the FIRST set of calculation/hashmember
@ -710,7 +710,7 @@ nasal_ast nasal_parse::callf()
tok_func,tok_var,tok_lcurve,
tok_lbrace,tok_lbracket,tok_null
};
nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_callf);
ast node(tokens[ptr].line,tokens[ptr].col,ast_callf);
bool special_call=check_special_call();
match(tok_lcurve);
while(tokens[ptr].type!=tok_rcurve)
@ -726,12 +726,12 @@ nasal_ast nasal_parse::callf()
match(tok_rcurve,"expected ')' when calling function");
return node;
}
nasal_ast nasal_parse::subvec()
ast nasal_parse::subvec()
{
nasal_ast node=tokens[ptr].type==tok_colon?nil():calc();
ast node=tokens[ptr].type==tok_colon?nil():calc();
if(tokens[ptr].type==tok_colon)
{
nasal_ast tmp(node.line(),node.col(),ast_subvec);
ast tmp(node.line(),node.col(),ast_subvec);
match(tok_colon);
tmp.add(std::move(node));
tmp.add((tokens[ptr].type==tok_comma || tokens[ptr].type==tok_rbracket)?nil():calc());
@ -739,9 +739,9 @@ nasal_ast nasal_parse::subvec()
}
return node;
}
nasal_ast nasal_parse::definition()
ast nasal_parse::definition()
{
nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_def);
ast node(tokens[ptr].line,tokens[ptr].col,ast_def);
if(tokens[ptr].type==tok_var)
{
match(tok_var);
@ -765,24 +765,24 @@ nasal_ast nasal_parse::definition()
die(node[0].line(),"too much or lack values in multi-definition");
return node;
}
nasal_ast nasal_parse::incurve_def()
ast nasal_parse::incurve_def()
{
match(tok_lcurve);
match(tok_var);
nasal_ast node=multi_id();
ast node=multi_id();
match(tok_rcurve);
return node;
}
nasal_ast nasal_parse::outcurve_def()
ast nasal_parse::outcurve_def()
{
match(tok_lcurve);
nasal_ast node=multi_id();
ast node=multi_id();
match(tok_rcurve);
return node;
}
nasal_ast nasal_parse::multi_id()
ast nasal_parse::multi_id()
{
nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_multi_id);
ast node(tokens[ptr].line,tokens[ptr].col,ast_multi_id);
while(tokens[ptr].type!=tok_eof)
{
node.add(id());
@ -800,7 +800,7 @@ nasal_ast nasal_parse::multi_id()
}
return node;
}
nasal_ast nasal_parse::multi_scalar(bool check_call_memory)
ast nasal_parse::multi_scalar(bool check_call_memory)
{
// if check_call_memory is true,we will check if value called here can reach a memory space
const u32 panic_set[]={
@ -809,7 +809,7 @@ nasal_ast nasal_parse::multi_scalar(bool check_call_memory)
tok_func,tok_var,tok_lcurve,
tok_lbrace,tok_lbracket,tok_null
};
nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_multi_scalar);
ast node(tokens[ptr].line,tokens[ptr].col,ast_multi_scalar);
match(tok_lcurve);
while(tokens[ptr].type!=tok_rcurve)
{
@ -826,9 +826,9 @@ nasal_ast nasal_parse::multi_scalar(bool check_call_memory)
match(tok_rcurve,"expected ')' after multi-scalar");
return node;
}
nasal_ast nasal_parse::multi_assgin()
ast nasal_parse::multi_assgin()
{
nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_multi_assign);
ast node(tokens[ptr].line,tokens[ptr].col,ast_multi_assign);
node.add(multi_scalar(true));
match(tok_eq);
if(tokens[ptr].type==tok_eof)
@ -845,10 +845,10 @@ nasal_ast nasal_parse::multi_assgin()
die(node[0].line(),"too much or lack values in multi-assignment");
return node;
}
nasal_ast nasal_parse::loop()
ast nasal_parse::loop()
{
++in_loop;
nasal_ast node(0,0,ast_null);
ast node(0,0,ast_null);
switch(tokens[ptr].type)
{
case tok_while: node=while_loop(); break;
@ -859,9 +859,9 @@ nasal_ast nasal_parse::loop()
--in_loop;
return node;
}
nasal_ast nasal_parse::while_loop()
ast nasal_parse::while_loop()
{
nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_while);
ast node(tokens[ptr].line,tokens[ptr].col,ast_while);
match(tok_while);
match(tok_lcurve);
node.add(calc());
@ -869,9 +869,9 @@ nasal_ast nasal_parse::while_loop()
node.add(exprs());
return node;
}
nasal_ast nasal_parse::for_loop()
ast nasal_parse::for_loop()
{
nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_for);
ast node(tokens[ptr].line,tokens[ptr].col,ast_for);
match(tok_for);
match(tok_lcurve);
// first expression
@ -905,9 +905,9 @@ nasal_ast nasal_parse::for_loop()
node.add(exprs());
return node;
}
nasal_ast nasal_parse::forei_loop()
ast nasal_parse::forei_loop()
{
nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_null);
ast node(tokens[ptr].line,tokens[ptr].col,ast_null);
switch(tokens[ptr].type)
{
case tok_forindex:node.set_type(ast_forindex);match(tok_forindex);break;
@ -927,9 +927,9 @@ nasal_ast nasal_parse::forei_loop()
node.add(exprs());
return node;
}
nasal_ast nasal_parse::iter_gen()
ast nasal_parse::iter_gen()
{
nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_null);
ast node(tokens[ptr].line,tokens[ptr].col,ast_null);
if(tokens[ptr].type==tok_var)
{
match(tok_var);
@ -946,10 +946,10 @@ nasal_ast nasal_parse::iter_gen()
}
return node;
}
nasal_ast nasal_parse::conditional()
ast nasal_parse::conditional()
{
nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_conditional);
nasal_ast ifnode(tokens[ptr].line,tokens[ptr].col,ast_if);
ast node(tokens[ptr].line,tokens[ptr].col,ast_conditional);
ast ifnode(tokens[ptr].line,tokens[ptr].col,ast_if);
match(tok_if);
match(tok_lcurve);
ifnode.add(calc());
@ -958,7 +958,7 @@ nasal_ast nasal_parse::conditional()
node.add(std::move(ifnode));
while(tokens[ptr].type==tok_elsif)
{
nasal_ast elsifnode(tokens[ptr].line,tokens[ptr].col,ast_elsif);
ast elsifnode(tokens[ptr].line,tokens[ptr].col,ast_elsif);
match(tok_elsif);
match(tok_lcurve);
elsifnode.add(calc());
@ -968,28 +968,28 @@ nasal_ast nasal_parse::conditional()
}
if(tokens[ptr].type==tok_else)
{
nasal_ast elsenode(tokens[ptr].line,tokens[ptr].col,ast_else);
ast elsenode(tokens[ptr].line,tokens[ptr].col,ast_else);
match(tok_else);
elsenode.add(exprs());
node.add(std::move(elsenode));
}
return node;
}
nasal_ast nasal_parse::continue_expr()
ast nasal_parse::continue_expr()
{
nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_continue);
ast node(tokens[ptr].line,tokens[ptr].col,ast_continue);
match(tok_continue);
return node;
}
nasal_ast nasal_parse::break_expr()
ast nasal_parse::break_expr()
{
nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_break);
ast node(tokens[ptr].line,tokens[ptr].col,ast_break);
match(tok_break);
return node;
}
nasal_ast nasal_parse::ret_expr()
ast nasal_parse::ret_expr()
{
nasal_ast node(tokens[ptr].line,tokens[ptr].col,ast_ret);
ast node(tokens[ptr].line,tokens[ptr].col,ast_ret);
match(tok_ret);
u32 type=tokens[ptr].type;
if(type==tok_nil || type==tok_num ||

16
stl/csv.nas Normal file
View File

@ -0,0 +1,16 @@
# lib csv.nas
# ValKmjolnir 2022/10/15
var read_csv=func(path,delimeter=",",endline="\n"){
var context=io.fin(path);
context=split(endline,context);
forindex(var i;context){
context[i]=split(delimeter,context[i]);
}
if(size(context)<=1){
die("incorrect csv file <"~path~">: "~size(context)~" line(s).");
}
return {
property:context[0],
data:context[1:]
};
}

View File

@ -13,7 +13,7 @@ var compare=func(){
var total=end-begin;
var timestamp=maketimestamp();
timestamp.stamp();
var bar=process_bar.high_resolution_bar(50);
var bar=process_bar.high_resolution_bar(40);
for(var i=begin;i<end;i+=1){
var s="";
for(var j=0;j<i;j+=1){
@ -24,7 +24,7 @@ var compare=func(){
if(cmp(res,_md5(s))){
die("error: "~str(i));
}
print(" ",bar.bar((i-begin+1)/total)," (",i-begin+1,"/",total,")\t",res," byte: ",byte," elapsed time: ",timestamp.elapsedMSec()," \r");
print(" ",bar.bar((i-begin+1)/total)," (",i-begin+1,"/",total,")\t",res," byte: ",byte," time: ",timestamp.elapsedMSec()," \r");
}
print("\n");
};
@ -77,7 +77,7 @@ var filechecksum=func(){
var total=size(files);
var timestamp=maketimestamp();
timestamp.stamp();
var bar=process_bar.high_resolution_bar(50);
var bar=process_bar.high_resolution_bar(40);
forindex(var i;files){
var f=io.fin(files[i]);
var res=md5(f);
@ -85,7 +85,7 @@ var filechecksum=func(){
if(cmp(res,_md5(f))){
die("error: "~files[i]);
}
print(" ",bar.bar((i+1)/total)," (",i+1,"/",total,")\t",res," byte: ",byte," elapsed time: ",timestamp.elapsedMSec()," \r");
print(" ",bar.bar((i+1)/total)," (",i+1,"/",total,")\t",res," byte: ",byte," time: ",timestamp.elapsedMSec()," \r");
}
print("\n");
}