🎨 improve code format

delete option -v, --version, -l, --lex
This commit is contained in:
ValKmjolnir 2022-10-30 01:26:26 +08:00
parent b0f080070a
commit feea901e4d
14 changed files with 381 additions and 324 deletions

View File

@ -15,50 +15,48 @@
using ch_clk=std::chrono::high_resolution_clock; using ch_clk=std::chrono::high_resolution_clock;
const u32 VM_TOKEN =0x01; const u32 VM_AST =0x01;
const u32 VM_AST =0x02; const u32 VM_CODE =0x02;
const u32 VM_CODE =0x04; const u32 VM_TIME =0x04;
const u32 VM_TIME =0x08; const u32 VM_EXEC =0x08;
const u32 VM_EXEC =0x10; const u32 VM_DETAIL=0x10;
const u32 VM_DETAIL=0x20; const u32 VM_DEBUG =0x20;
const u32 VM_DEBUG =0x40; const u32 VM_OPT =0x40;
const u32 VM_OPT =0x80;
void help() std::ostream& help(std::ostream& out)
{ {
std::clog out
<<" ,--#-,\n" <<" ,--#-,\n"
<<"<3 / \\____\\ <3\n" <<"<3 / \\____\\ <3\n"
<<" |_|__A_|\n" <<" |_|__A_|\n"
#ifdef _WIN32 #ifdef _WIN32
<<"use command <chcp 65001> if want to use unicode.\n" <<"use command <chcp 65001> to use unicode.\n"
#endif #endif
<<"nasal <option>\n" <<"\nnasal <option>\n"
<<"option:\n" <<"option:\n"
<<" -h, --help | get this help and exit.\n" <<" -h, --help | get help.\n"
<<" -v, --version | get version of nasal interpreter.\n\n" <<"\nnasal [option] <file> [argv]\n"
<<"nasal [option...] <file> [argv...]\n"
<<"option:\n" <<"option:\n"
<<" -l, --lex | view token info.\n" <<" -a, --ast | view abstract syntax tree.\n"
<<" -a, --ast | view abstract syntax tree.\n" <<" -c, --code | view bytecode.\n"
<<" -c, --code | view bytecode.\n" <<" -e, --exec | execute.\n"
<<" -e, --exec | execute.\n" <<" -t, --time | show execute time.\n"
<<" -t, --time | get the running time.\n" <<" -d, --detail | get detail crash info.\n"
<<" -d, --detail | get detail runtime crash info.\n" <<" | get detail path-not-found info.\n"
<<" | get detail linker path-not-found info.\n" <<" | get detail gc info.\n"
<<" | get garbage collector info if didn't crash.\n" <<" -o, --optimize | use optimizer (beta).\n"
<<" -o, --optimize | use optimizer(beta).\n" <<" | use <-o -e> to run optimized code.\n"
<<" | if want to use -op and run, please use -op -e/-t/-d.\n" <<" -dbg, --debug | debug mode (ignore -t -d).\n"
<<" -dbg, --debug | debug mode (this will ignore -t -d).\n"
<<"file:\n" <<"file:\n"
<<" input file name to execute.\n" <<" <filename> | execute file.\n"
<<"argv:\n" <<"argv:\n"
<<" command line arguments used in program.\n"; <<" <args> | cmd arguments used in program.\n";
return out;
} }
void logo() std::ostream& logo(std::ostream& out)
{ {
std::clog out
<<" __ _\n" <<" __ _\n"
<<" /\\ \\ \\__ _ ___ __ _| |\n" <<" /\\ \\ \\__ _ ___ __ _| |\n"
<<" / \\/ / _` / __|/ _` | |\n" <<" / \\/ / _` / __|/ _` | |\n"
@ -66,11 +64,11 @@ void logo()
<<" \\_\\ \\/ \\__,_|___/\\__,_|_|\n" <<" \\_\\ \\/ \\__,_|___/\\__,_|_|\n"
<<"version : "<<__nasver<<" ("<<__DATE__<<" "<<__TIME__<<")\n" <<"version : "<<__nasver<<" ("<<__DATE__<<" "<<__TIME__<<")\n"
<<"c++ std : "<<__cplusplus<<"\n" <<"c++ std : "<<__cplusplus<<"\n"
<<"thanks to : https://github.com/andyross/nasal\n"
<<"code repo : https://github.com/ValKmjolnir/Nasal-Interpreter\n" <<"code repo : https://github.com/ValKmjolnir/Nasal-Interpreter\n"
<<"code repo : https://gitee.com/valkmjolnir/Nasal-Interpreter\n" <<"code repo : https://gitee.com/valkmjolnir/Nasal-Interpreter\n"
<<"lang info : http://wiki.flightgear.org/Nasal_scripting_language\n" <<"lang wiki : https://wiki.flightgear.org/Nasal_scripting_language\n"
<<"input <nasal -h> to get help .\n"; <<"input <nasal -h> to get help .\n";
return out;
} }
[[noreturn]] [[noreturn]]
@ -93,8 +91,6 @@ void execute(const string& file,const std::vector<string>& argv,const u32 cmd)
// lexer scans file to get tokens // lexer scans file to get tokens
lex.scan(file); lex.scan(file);
if(cmd&VM_TOKEN)
lex.print();
// parser gets lexer's token list to compile // parser gets lexer's token list to compile
parse.compile(lex); parse.compile(lex);
@ -129,16 +125,14 @@ i32 main(i32 argc,const char* argv[])
{ {
if(argc<=1) if(argc<=1)
{ {
logo(); std::clog<<logo;
return 0; return 0;
} }
if(argc==2) if(argc==2)
{ {
string s(argv[1]); string s(argv[1]);
if(s=="-v" || s=="--version") if(s=="-h" || s=="--help")
std::clog<<"nasal "<<__nasver<<" ("<<__DATE__<<" "<<__TIME__<<")\n"; std::clog<<help;
else if(s=="-h" || s=="--help")
help();
else if(s[0]!='-') else if(s[0]!='-')
execute(s,{},VM_EXEC); execute(s,{},VM_EXEC);
else else
@ -146,7 +140,6 @@ i32 main(i32 argc,const char* argv[])
return 0; return 0;
} }
std::unordered_map<string,u32> cmdlst={ std::unordered_map<string,u32> cmdlst={
{"--lex",VM_TOKEN},{"-l",VM_TOKEN},
{"--ast",VM_AST},{"-a",VM_AST}, {"--ast",VM_AST},{"-a",VM_AST},
{"--code",VM_CODE},{"-c",VM_CODE}, {"--code",VM_CODE},{"-c",VM_CODE},
{"--exec",VM_EXEC},{"-e",VM_EXEC}, {"--exec",VM_EXEC},{"-e",VM_EXEC},
@ -156,7 +149,7 @@ i32 main(i32 argc,const char* argv[])
{"--debug",VM_DEBUG},{"-dbg",VM_DEBUG} {"--debug",VM_DEBUG},{"-dbg",VM_DEBUG}
}; };
u32 cmd=0; u32 cmd=0;
string filename; string filename="";
std::vector<string> vm_argv; std::vector<string> vm_argv;
for(i32 i=1;i<argc;++i) for(i32 i=1;i<argc;++i)
{ {

View File

@ -1,4 +1,4 @@
.PHONY:test .PHONY:test clean
SRC=\ SRC=\
main.cpp\ main.cpp\
@ -27,28 +27,33 @@ stable-release:$(SRC)
stable-release-mingw:$(SRC) stable-release-mingw:$(SRC)
$(CXX) -std=c++$(STD) -O2 main.cpp -o nasal.exe -fno-exceptions -Wshadow -Wall -static $(CXX) -std=c++$(STD) -O2 main.cpp -o nasal.exe -fno-exceptions -Wshadow -Wall -static
clean:
-@ rm ./nasal
-@ rm ./nasal.exe
test:nasal test:nasal
@ ./nasal -o -e test/ascii-art.nas @ ./nasal -o -e test/ascii-art.nas
@ ./nasal -o -c test/auto_crash.nas @ ./nasal -o -c test/auto_crash.nas
@ ./nasal -o -a -c test/bf.nas @ ./nasal -o -a -c test/bf.nas
@ ./nasal -o -a -c test/bfconvertor.nas @ ./nasal -o -a -c test/bfconvertor.nas
@ ./nasal -o -e -d test/bfs.nas @ ./nasal -o -d test/bfs.nas
@ ./nasal -o -t test/bigloop.nas @ ./nasal -o -t test/bigloop.nas
@ ./nasal -o -e test/bp.nas @ ./nasal -o -e test/bp.nas
@ ./nasal -o -e -d test/calc.nas @ ./nasal -o -d test/calc.nas
@ ./nasal -o -e test/choice.nas @ ./nasal -o -e test/choice.nas
@ ./nasal -o -e test/class.nas @ ./nasal -o -e test/class.nas
@ ./nasal -o -d test/coroutine.nas @ ./nasal -o -e test/coroutine.nas
@ ./nasal -o -e test/diff.nas @ ./nasal -o -e test/diff.nas
@ ./nasal -o -e test/donuts.nas
-@ ./nasal -o -d test/exception.nas -@ ./nasal -o -d test/exception.nas
@ ./nasal -o -t -d test/fib.nas @ ./nasal -o -t -d test/fib.nas
@ ./nasal -o -e test/filesystem.nas @ ./nasal -o -e test/filesystem.nas
@ ./nasal -o -e -d test/hexdump.nas @ ./nasal -o -d test/hexdump.nas
@ ./nasal -o -c test/httptest.nas @ ./nasal -o -c test/httptest.nas
@ ./nasal -o -e test/json.nas @ ./nasal -o -e test/json.nas
@ ./nasal -o -e test/leetcode1319.nas @ ./nasal -o -e test/leetcode1319.nas
@ ./nasal -o -e -d test/lexer.nas @ ./nasal -o -d test/lexer.nas
@ ./nasal -o -e -d test/life.nas @ ./nasal -o -d test/life.nas
@ ./nasal -o -t test/loop.nas @ ./nasal -o -t test/loop.nas
@ ./nasal -o -t -d test/mandel.nas @ ./nasal -o -t -d test/mandel.nas
@ ./nasal -o -t -d test/mandelbrot.nas @ ./nasal -o -t -d test/mandelbrot.nas

View File

@ -1,5 +1,8 @@
.PHONY=clean all mingw-all .PHONY=clean all mingw-all
dynamic_libs=libfib.so libkey.so libnasock.so
dynamic_libs_dll=libfib.dll libkey.dll libnasock.dll
STD=14 STD=14
libfib.so: fib.cpp libfib.so: fib.cpp
@ -36,7 +39,11 @@ libnasock.dll: nasocket.cpp
@ del nasocket.o @ del nasocket.o
clean: clean:
-@ rm *.so *.dll *.dylib @ echo "[clean] so"
-@ rm $(dynamic_libs)
@ echo "[clean] dll"
-@ rm $(dynamic_libs_dll)
all: libfib.so libkey.so libnasock.so all: libfib.so libkey.so libnasock.so
@ echo "[build] done" @ echo "[build] done"
mingw-all: libfib.dll libkey.dll libnasock.dll mingw-all: libfib.dll libkey.dll libnasock.dll

View File

@ -67,64 +67,21 @@ enum ast_node:u32
const char* ast_name[]= const char* ast_name[]=
{ {
"null", "null", "root", "block", "file",
"root", "nil", "num", "str", "id",
"block", "func", "hash", "vec", "pair",
"file", "call", "callh", "callv", "callf",
"nil", "subvec", "args", "default", "dynamic",
"num", "and", "or", "=", "+=",
"str", "-=", "*=", "/=" "~=",
"id", "==", "!=", "<", "<=",
"func", ">", ">=", "+", "-",
"hash", "*", "/", "~", "neg",
"vec", "!", "trino", "for", "forindex",
"pair", "foreach", "while", "iter", "cond",
"call", "if", "elsif", "else", "multi-id",
"callh", "tuple", "def", "multi-assign", "continue",
"callv", "break", "return"
"callf",
"subvec",
"args",
"default",
"dynamic",
"and",
"or",
"=",
"+=",
"-=",
"*=",
"/=",
"~=",
"==",
"!=",
"<",
"<=",
">",
">=",
"+",
"-",
"*",
"/",
"~",
"neg",
"!",
"trino",
"for",
"forindex",
"foreach",
"while",
"iter",
"cond",
"if",
"elsif",
"else",
"multi-id",
"tuple",
"def",
"multi-assign",
"continue",
"break",
"return"
}; };
class ast class ast

View File

@ -85,7 +85,7 @@ enum op_code_type:u8
op_ret // return op_ret // return
}; };
const char* code_table[]= const char* opname[]=
{ {
"exit ","intg ","intl ","loadg ", "exit ","intg ","intl ","loadg ",
"loadl ","loadu ","pnum ","pnil ", "loadl ","loadu ","pnum ","pnil ",
@ -148,7 +148,7 @@ public:
<<std::setw(2)<<std::setfill('0')<<((num>>16)&0xff)<<" " <<std::setw(2)<<std::setfill('0')<<((num>>16)&0xff)<<" "
<<std::setw(2)<<std::setfill('0')<<((num>>8)&0xff)<<" " <<std::setw(2)<<std::setfill('0')<<((num>>8)&0xff)<<" "
<<std::setw(2)<<std::setfill('0')<<(num&0xff)<<" " <<std::setw(2)<<std::setfill('0')<<(num&0xff)<<" "
<<code_table[op]<<" "<<std::dec; <<opname[op]<<" "<<std::dec;
switch(op) switch(op)
{ {
case op_addeq: case op_subeq: case op_muleq: case op_diveq: case op_addeq: case op_subeq: case op_muleq: case op_diveq:

View File

@ -104,7 +104,7 @@ void debugger::callsort(const u64* arr)
std::clog<<"\n ..."; std::clog<<"\n ...";
break; break;
} }
std::clog<<"\n "<<code_table[i.first] std::clog<<"\n "<<opname[i.first]
<<" : "<<i.second<<" ("<<rate<<"%)"; <<" : "<<i.second<<" ("<<rate<<"%)";
} }
std::clog<<"\n total : "<<total<<'\n'; std::clog<<"\n total : "<<total<<'\n';
@ -136,7 +136,7 @@ void debugger::interact()
if(bytecode[pc].op==op_intg) if(bytecode[pc].op==op_intg)
{ {
std::cout std::cout
<<bold_cyan<<"[debug] "<<reset <<cyan<<"[debug] "<<reset
<<"nasal debug mode\n" <<"nasal debug mode\n"
<<"input \'h\' to get help\n"; <<"input \'h\' to get help\n";
} }
@ -309,7 +309,7 @@ vmexit:
callsort(count); callsort(count);
ngc.clear(); ngc.clear();
imm.clear(); imm.clear();
std::cout<<bold_cyan<<"[debug] "<<reset<<"debugger exited\n"; std::cout<<cyan<<"[debug] "<<reset<<"debugger exited\n";
return; return;
#ifndef _MSC_VER #ifndef _MSC_VER
#define dbg(op,num) {\ #define dbg(op,num) {\

View File

@ -25,7 +25,7 @@ std::ostream& back_white(std::ostream& s)
#endif #endif
return s; return s;
} }
std::ostream& bold_red(std::ostream& s) std::ostream& red(std::ostream& s)
{ {
#ifdef _WIN32 #ifdef _WIN32
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),0x0c); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),0x0c);
@ -34,7 +34,7 @@ std::ostream& bold_red(std::ostream& s)
#endif #endif
return s; return s;
} }
std::ostream& bold_cyan(std::ostream& s) std::ostream& cyan(std::ostream& s)
{ {
#ifdef _WIN32 #ifdef _WIN32
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),0x03); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),0x03);
@ -43,7 +43,7 @@ std::ostream& bold_cyan(std::ostream& s)
#endif #endif
return s; return s;
} }
std::ostream& bold_orange(std::ostream& s) std::ostream& orange(std::ostream& s)
{ {
#ifdef _WIN32 #ifdef _WIN32
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),0x0e); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),0x0e);
@ -52,7 +52,7 @@ std::ostream& bold_orange(std::ostream& s)
#endif #endif
return s; return s;
} }
std::ostream& bold_white(std::ostream& s) std::ostream& white(std::ostream& s)
{ {
#ifdef _WIN32 #ifdef _WIN32
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),0x0f); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),0x0f);
@ -85,7 +85,7 @@ public:
std::ifstream in(f,std::ios::binary); std::ifstream in(f,std::ios::binary);
if(in.fail()) if(in.fail())
{ {
std::cerr<<bold_red<<"src: "<<reset<<"cannot open <"<<f<<">\n"; std::cerr<<red<<"src: "<<reset<<"cannot open <"<<f<<">\n";
std::exit(1); std::exit(1);
} }
string line; string line;
@ -112,49 +112,49 @@ private:
} }
public: public:
error():cnt(0){} error():cnt(0){}
void err(const char* stage,const string& info) void err(const string& stage,const string& info)
{ {
++cnt; ++cnt;
std::cerr<<bold_red<<stage<<": " std::cerr<<red<<stage<<": "
<<bold_white<<info<<reset<<"\n\n"; <<white<<info<<reset<<"\n\n";
} }
void err(const char* stage,u32 line,u32 column,const string& info) void err(const string& stage,u32 line,u32 col,const string& info)
{ {
++cnt; ++cnt;
const string& code=res[line-1]; const string& code=res[line-1];
const string ident=identation(std::to_string(line).length()); const string iden=identation(std::to_string(line).length());
std::cerr<<bold_red<<stage<<": " std::cerr<<red<<stage<<": "
<<bold_white<<info<<reset<<"\n" <<white<<info<<reset<<"\n"
<<bold_cyan<<" --> "<<reset <<cyan<<" --> "<<reset
<<bold_orange<<file<<":"<<line<<":"<<column<<"\n"; <<orange<<file<<":"<<line<<":"<<col<<"\n";
if(!line) if(!line)
{ {
std::cerr<<"\n"; std::cerr<<"\n";
return; return;
} }
std::cerr<<bold_cyan<<ident<<" | "<<reset<<"\n" std::cerr<<cyan<<iden<<" | "<<reset<<"\n"
<<bold_cyan<<line<<" | "<<reset<<code<<"\n" <<cyan<<line<<" | "<<reset<<code<<"\n"
<<bold_cyan<<ident<<" | "<<reset; <<cyan<<iden<<" | "<<reset;
for(i32 i=0;i<(i32)column-1;++i) for(i32 i=0;i<(i32)col-1;++i)
std::cerr<<char(" \t"[code[i]=='\t']); std::cerr<<char(" \t"[code[i]=='\t']);
std::cerr<<bold_red<<"^ "<<info<<reset<<"\n\n"; std::cerr<<red<<"^ "<<info<<reset<<"\n\n";
} }
void err(const char* stage,u32 line,const string& info) void err(const string& stage,u32 line,const string& info)
{ {
++cnt; ++cnt;
const string ident=identation(std::to_string(line).length()); const string iden=identation(std::to_string(line).length());
std::cerr<<bold_red<<stage<<": " std::cerr<<red<<stage<<": "
<<bold_white<<info<<reset<<"\n" <<white<<info<<reset<<"\n"
<<bold_cyan<<" --> "<<reset <<cyan<<" --> "<<reset
<<bold_orange<<file<<":"<<line<<"\n"; <<orange<<file<<":"<<line<<"\n";
if(!line) if(!line)
{ {
std::cerr<<"\n"; std::cerr<<"\n";
return; return;
} }
std::cerr<<bold_cyan<<ident<<" | "<<reset<<"\n" std::cerr<<cyan<<iden<<" | "<<reset<<"\n"
<<bold_cyan<<line<<" | "<<reset<<res[line-1]<<"\n" <<cyan<<line<<" | "<<reset<<res[line-1]<<"\n"
<<bold_cyan<<ident<<" | "<<reset<<"\n\n"; <<cyan<<iden<<" | "<<reset<<"\n\n";
} }
void chkerr(){if(cnt)std::exit(1);} void chkerr() const {if(cnt)std::exit(1);}
}; };

View File

@ -70,16 +70,16 @@ string linker::path(const ast& node)
string linker::findf(const string& fname) string linker::findf(const string& fname)
{ {
std::vector<string> filepath={fname}; std::vector<string> fpath={fname};
for(auto&p:envpath) for(auto&p:envpath)
{ {
#ifdef _WIN32 #ifdef _WIN32
filepath.push_back(p+"\\"+fname); fpath.push_back(p+"\\"+fname);
#else #else
filepath.push_back(p+"/"+fname); fpath.push_back(p+"/"+fname);
#endif #endif
} }
for(auto& i:filepath) for(auto& i:fpath)
if(access(i.c_str(),F_OK)!=-1) if(access(i.c_str(),F_OK)!=-1)
return i; return i;
if(fname=="lib.nas") if(fname=="lib.nas")
@ -94,7 +94,7 @@ string linker::findf(const string& fname)
return ""; return "";
} }
string paths=""; string paths="";
for(auto& i:filepath) for(auto& i:fpath)
paths+=" "+i+"\n"; paths+=" "+i+"\n";
err.err("link","cannot find file <"+fname+"> in these paths:\n"+paths); err.err("link","cannot find file <"+fname+"> in these paths:\n"+paths);
return ""; return "";
@ -192,25 +192,25 @@ ast linker::libimpt()
ast linker::load(ast& root,u16 fileindex) ast linker::load(ast& root,u16 fileindex)
{ {
ast new_root(0,0,ast_root); ast tree(0,0,ast_root);
if(!lib_loaded) if(!lib_loaded)
{ {
link(new_root,libimpt()); link(tree,libimpt());
lib_loaded=true; lib_loaded=true;
} }
for(auto& i:root.child()) for(auto& i:root.child())
{ {
if(imptchk(i)) if(imptchk(i))
link(new_root,fimpt(i)); link(tree,fimpt(i));
else else
break; break;
} }
// add root to the back of new_root // add root to the back of tree
ast file_head(0,0,ast_file); ast file_head(0,0,ast_file);
file_head.set_num(fileindex); file_head.set_num(fileindex);
new_root.add(std::move(file_head)); tree.add(std::move(file_head));
link(new_root,std::move(root)); link(tree,std::move(root));
return new_root; return tree;
} }
void linker::link(parse& parse,const string& self,bool spath=false) void linker::link(parse& parse,const string& self,bool spath=false)

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <sstream> #include <sstream>
#include <unordered_map>
#include <sys/stat.h> #include <sys/stat.h>
#ifdef _MSC_VER #ifdef _MSC_VER
@ -60,58 +61,6 @@ enum tok:u32{
tok_eof // <eof> end of token list tok_eof // <eof> end of token list
}; };
struct{
const char* str;
const u32 type;
}tok_table[]={
{"for" ,tok_for },
{"forindex",tok_forindex },
{"foreach" ,tok_foreach },
{"while" ,tok_while },
{"var" ,tok_var },
{"func" ,tok_func },
{"break" ,tok_break },
{"continue",tok_continue },
{"return" ,tok_ret },
{"if" ,tok_if },
{"elsif" ,tok_elsif },
{"else" ,tok_else },
{"nil" ,tok_nil },
{"(" ,tok_lcurve },
{")" ,tok_rcurve },
{"[" ,tok_lbracket },
{"]" ,tok_rbracket },
{"{" ,tok_lbrace },
{"}" ,tok_rbrace },
{";" ,tok_semi },
{"and" ,tok_and },
{"or" ,tok_or },
{"," ,tok_comma },
{"." ,tok_dot },
{"..." ,tok_ellipsis },
{"?" ,tok_quesmark },
{":" ,tok_colon },
{"+" ,tok_add },
{"-" ,tok_sub },
{"*" ,tok_mult },
{"/" ,tok_div },
{"~" ,tok_link },
{"!" ,tok_not },
{"=" ,tok_eq },
{"+=" ,tok_addeq },
{"-=" ,tok_subeq },
{"*=" ,tok_multeq },
{"/=" ,tok_diveq },
{"~=" ,tok_lnkeq },
{"==" ,tok_cmpeq },
{"!=" ,tok_neq },
{"<" ,tok_less },
{"<=" ,tok_leq },
{">" ,tok_grt },
{">=" ,tok_geq },
{nullptr ,0 }
};
struct token struct token
{ {
u32 line; u32 line;
@ -134,7 +83,54 @@ private:
usize ptr; usize ptr;
string res; string res;
error& err; error& err;
std::vector<token> tokens; std::vector<token> toks;
std::unordered_map<string,u32> typetbl {
{"for" ,tok_for },
{"forindex",tok_forindex },
{"foreach" ,tok_foreach },
{"while" ,tok_while },
{"var" ,tok_var },
{"func" ,tok_func },
{"break" ,tok_break },
{"continue",tok_continue },
{"return" ,tok_ret },
{"if" ,tok_if },
{"elsif" ,tok_elsif },
{"else" ,tok_else },
{"nil" ,tok_nil },
{"(" ,tok_lcurve },
{")" ,tok_rcurve },
{"[" ,tok_lbracket },
{"]" ,tok_rbracket },
{"{" ,tok_lbrace },
{"}" ,tok_rbrace },
{";" ,tok_semi },
{"and" ,tok_and },
{"or" ,tok_or },
{"," ,tok_comma },
{"." ,tok_dot },
{"..." ,tok_ellipsis },
{"?" ,tok_quesmark },
{":" ,tok_colon },
{"+" ,tok_add },
{"-" ,tok_sub },
{"*" ,tok_mult },
{"/" ,tok_div },
{"~" ,tok_link },
{"!" ,tok_not },
{"=" ,tok_eq },
{"+=" ,tok_addeq },
{"-=" ,tok_subeq },
{"*=" ,tok_multeq },
{"/=" ,tok_diveq },
{"~=" ,tok_lnkeq },
{"==" ,tok_cmpeq },
{"!=" ,tok_neq },
{"<" ,tok_less },
{"<=" ,tok_leq },
{">" ,tok_grt },
{">=" ,tok_geq }
};
u32 get_type(const string&); u32 get_type(const string&);
bool is_id(char); bool is_id(char);
@ -157,7 +153,7 @@ public:
err(e){} err(e){}
void scan(const string&); void scan(const string&);
void print(); void print();
const std::vector<token>& result() const {return tokens;} const std::vector<token>& result() const {return toks;}
}; };
bool lexer::is_id(char c) bool lexer::is_id(char c)
@ -221,9 +217,8 @@ void lexer::open(const string& file)
u32 lexer::get_type(const string& str) u32 lexer::get_type(const string& str)
{ {
for(u32 i=0;tok_table[i].str;++i) if(typetbl.count(str))
if(str==tok_table[i].str) return typetbl.at(str);
return tok_table[i].type;
return tok_null; return tok_null;
} }
@ -415,17 +410,17 @@ void lexer::scan(const string& file)
{ {
str=id_gen(); str=id_gen();
u32 type=get_type(str); u32 type=get_type(str);
tokens.push_back({line,column,type?type:tok_id,str}); toks.push_back({line,column,type?type:tok_id,str});
} }
else if(is_dec(res[ptr])) else if(is_dec(res[ptr]))
{ {
str=num_gen(); // make sure column is correct str=num_gen(); // make sure column is correct
tokens.push_back({line,column,tok_num,str}); toks.push_back({line,column,tok_num,str});
} }
else if(is_str(res[ptr])) else if(is_str(res[ptr]))
{ {
str=str_gen(); // make sure column is correct str=str_gen(); // make sure column is correct
tokens.push_back({line,column,tok_str,str}); toks.push_back({line,column,tok_str,str});
} }
else if(is_single_opr(res[ptr])) else if(is_single_opr(res[ptr]))
{ {
@ -434,7 +429,7 @@ void lexer::scan(const string& file)
u32 type=get_type(str); u32 type=get_type(str);
if(!type) if(!type)
die("invalid operator `"+str+"`"); die("invalid operator `"+str+"`");
tokens.push_back({line,column,type,str}); toks.push_back({line,column,type,str});
++ptr; ++ptr;
} }
else if(res[ptr]=='.') else if(res[ptr]=='.')
@ -444,7 +439,7 @@ void lexer::scan(const string& file)
str+=".."; str+="..";
ptr+=str.length(); ptr+=str.length();
column+=str.length(); column+=str.length();
tokens.push_back({line,column,get_type(str),str}); toks.push_back({line,column,get_type(str),str});
} }
else if(is_calc_opr(res[ptr])) else if(is_calc_opr(res[ptr]))
{ {
@ -453,7 +448,7 @@ void lexer::scan(const string& file)
if(ptr<res.size() && res[ptr]=='=') if(ptr<res.size() && res[ptr]=='=')
str+=res[ptr++]; str+=res[ptr++];
column+=str.length(); column+=str.length();
tokens.push_back({line,column,get_type(str),str}); toks.push_back({line,column,get_type(str),str});
} }
else if(res[ptr]=='#')// avoid note, after this process ptr will point to a '\n', so next loop line counter+1 else if(res[ptr]=='#')// avoid note, after this process ptr will point to a '\n', so next loop line counter+1
while(++ptr<res.size() && res[ptr]!='\n'); while(++ptr<res.size() && res[ptr]!='\n');
@ -464,13 +459,13 @@ void lexer::scan(const string& file)
die("invalid character 0x"+chrhex(c)); die("invalid character 0x"+chrhex(c));
} }
} }
tokens.push_back({line,column,tok_eof,"<eof>"}); toks.push_back({line,column,tok_eof,"<eof>"});
res=""; res="";
err.chkerr(); err.chkerr();
} }
void lexer::print() void lexer::print()
{ {
for(auto& tok:tokens) for(auto& tok:toks)
std::cout<<"("<<tok.line<<" | "<<rawstr(tok.str,128)<<")\n"; std::cout<<"("<<tok.line<<" | "<<rawstr(tok.str,32)<<")\n";
} }

View File

@ -39,15 +39,62 @@
class parse class parse
{ {
#define error_line (tokens[ptr].line) #define err_line (toks[ptr].line)
#define is_call(type) ((type)==tok_lcurve || (type)==tok_lbracket || (type)==tok_dot) #define is_call(type) ((type)==tok_lcurve || (type)==tok_lbracket || (type)==tok_dot)
private: private:
u32 ptr; u32 ptr;
u32 in_func; // count function block u32 in_func; // count function block
u32 in_loop; // count loop block u32 in_loop; // count loop block
const token* tokens;// ref from lexer const token* toks;
ast root; ast root;
error& err; error& err;
std::unordered_map<u32,string> tokname {
{tok_for ,"for" },
{tok_forindex,"forindex"},
{tok_foreach ,"foreach" },
{tok_while ,"while" },
{tok_var ,"var" },
{tok_func ,"func" },
{tok_break ,"break" },
{tok_continue,"continue"},
{tok_ret ,"return" },
{tok_if ,"if" },
{tok_elsif ,"elsif" },
{tok_else ,"else" },
{tok_nil ,"nil" },
{tok_lcurve ,"(" },
{tok_rcurve ,")" },
{tok_lbracket,"[" },
{tok_rbracket,"]" },
{tok_lbrace ,"{" },
{tok_rbrace ,"}" },
{tok_semi ,";" },
{tok_and ,"and" },
{tok_or ,"or" },
{tok_comma ,"," },
{tok_dot ,"." },
{tok_ellipsis,"..." },
{tok_quesmark,"?" },
{tok_colon ,":" },
{tok_add ,"+" },
{tok_sub ,"-" },
{tok_mult ,"*" },
{tok_div ,"/" },
{tok_link ,"~" },
{tok_not ,"!" },
{tok_eq ,"=" },
{tok_addeq ,"+=" },
{tok_subeq ,"-=" },
{tok_multeq ,"*=" },
{tok_diveq ,"/=" },
{tok_lnkeq ,"~=" },
{tok_cmpeq ,"==" },
{tok_neq ,"!=" },
{tok_less ,"<" },
{tok_leq ,"<=" },
{tok_grt ,">" },
{tok_geq ,">=" }
};
void die(u32,string,bool); void die(u32,string,bool);
void match(u32 type,const char* info=nullptr); void match(u32 type,const char* info=nullptr);
@ -102,7 +149,7 @@ private:
public: public:
parse(error& e): parse(error& e):
ptr(0),in_func(0),in_loop(0), ptr(0),in_func(0),in_loop(0),
tokens(nullptr),root(0,0,ast_root), toks(nullptr),root(0,0,ast_root),
err(e){} err(e){}
void print(){root.print_tree();} void print(){root.print_tree();}
void compile(const lexer&); void compile(const lexer&);
@ -111,7 +158,7 @@ public:
}; };
void parse::compile(const lexer& lexer) void parse::compile(const lexer& lexer)
{ {
tokens=lexer.result().data(); toks=lexer.result().data();
ptr=in_func=in_loop=0; ptr=in_func=in_loop=0;
root={0,0,ast_root}; root={0,0,ast_root};
@ -122,19 +169,19 @@ void parse::compile(const lexer& lexer)
match(tok_semi); match(tok_semi);
// the last expression can be recognized without semi // the last expression can be recognized without semi
else if(need_semi_check(root.child().back()) && !lookahead(tok_eof)) else if(need_semi_check(root.child().back()) && !lookahead(tok_eof))
die(error_line,"expected \";\"",true); die(err_line,"expected \";\"",true);
} }
err.chkerr(); err.chkerr();
} }
void parse::die(u32 line,string info,bool report_prev=false) void parse::die(u32 line,string info,bool report_prev=false)
{ {
i32 col=(i32)tokens[ptr].col-(lookahead(tok_eof)?0:(i32)tokens[ptr].str.length()); i32 col=(i32)toks[ptr].col-(lookahead(tok_eof)?0:(i32)toks[ptr].str.length());
if(lookahead(tok_str)) if(lookahead(tok_str))
col-=2; // tok_str's str has no \" col-=2; // tok_str's str has no \"
if(report_prev && ptr) // used to report lack of ',' ';' if(report_prev && ptr) // used to report lack of ',' ';'
{ {
line=tokens[ptr-1].line; line=toks[ptr-1].line;
col=tokens[ptr-1].col+1; col=toks[ptr-1].col+1;
} }
err.err("parse",line,col<0?0:col,info); err.err("parse",line,col<0?0:col,info);
} }
@ -144,15 +191,15 @@ void parse::match(u32 type,const char* info)
{ {
if(info) if(info)
{ {
die(error_line,info); die(err_line,info);
return; return;
} }
switch(type) switch(type)
{ {
case tok_num:die(error_line,"expected number"); break; case tok_num:die(err_line,"expected number"); break;
case tok_str:die(error_line,"expected string"); break; case tok_str:die(err_line,"expected string"); break;
case tok_id: die(error_line,"expected identifier");break; case tok_id: die(err_line,"expected identifier");break;
default: die(error_line,"expected '"+string(tok_table[type-tok_for].str)+"'"); break; default: die(err_line,"expected '"+tokname[type]+"'"); break;
} }
return; return;
} }
@ -162,14 +209,14 @@ void parse::match(u32 type,const char* info)
} }
bool parse::lookahead(u32 type) bool parse::lookahead(u32 type)
{ {
return tokens[ptr].type==type; return toks[ptr].type==type;
} }
bool parse::check_comma(const u32* panic_set) bool parse::check_comma(const u32* panic_set)
{ {
for(u32 i=0;panic_set[i];++i) for(u32 i=0;panic_set[i];++i)
if(lookahead(panic_set[i])) if(lookahead(panic_set[i]))
{ {
die(error_line,"expected ',' between scalars",true); die(err_line,"expected ',' between scalars",true);
return true; return true;
} }
return false; return false;
@ -177,9 +224,9 @@ bool parse::check_comma(const u32* panic_set)
bool parse::check_multi_scalar() bool parse::check_multi_scalar()
{ {
u32 check_ptr=ptr,curve=1,bracket=0,brace=0; u32 check_ptr=ptr,curve=1,bracket=0,brace=0;
while(tokens[++check_ptr].type!=tok_eof && curve) while(toks[++check_ptr].type!=tok_eof && curve)
{ {
switch(tokens[check_ptr].type) switch(toks[check_ptr].type)
{ {
case tok_lcurve: ++curve; break; case tok_lcurve: ++curve; break;
case tok_lbracket: ++bracket; break; case tok_lbracket: ++bracket; break;
@ -188,7 +235,7 @@ bool parse::check_multi_scalar()
case tok_rbracket: --bracket; break; case tok_rbracket: --bracket; break;
case tok_rbrace: --brace; break; case tok_rbrace: --brace; break;
} }
if(curve==1 && !bracket && !brace && tokens[check_ptr].type==tok_comma) if(curve==1 && !bracket && !brace && toks[check_ptr].type==tok_comma)
return true; return true;
} }
return false; return false;
@ -221,9 +268,9 @@ bool parse::check_special_call()
{ {
// special call means like this: function_name(a:1,b:2,c:3); // special call means like this: function_name(a:1,b:2,c:3);
u32 check_ptr=ptr,curve=1,bracket=0,brace=0; u32 check_ptr=ptr,curve=1,bracket=0,brace=0;
while(tokens[++check_ptr].type!=tok_eof && curve) while(toks[++check_ptr].type!=tok_eof && curve)
{ {
switch(tokens[check_ptr].type) switch(toks[check_ptr].type)
{ {
case tok_lcurve: ++curve; break; case tok_lcurve: ++curve; break;
case tok_lbracket: ++bracket;break; case tok_lbracket: ++bracket;break;
@ -233,9 +280,9 @@ bool parse::check_special_call()
case tok_rbrace: --brace; break; case tok_rbrace: --brace; break;
} }
// m?1:0 will be recognized as normal parameter // m?1:0 will be recognized as normal parameter
if(curve==1 && !bracket && !brace && tokens[check_ptr].type==tok_quesmark) if(curve==1 && !bracket && !brace && toks[check_ptr].type==tok_quesmark)
return false; return false;
if(curve==1 && !bracket && !brace && tokens[check_ptr].type==tok_colon) if(curve==1 && !bracket && !brace && toks[check_ptr].type==tok_colon)
return true; return true;
} }
return false; return false;
@ -262,30 +309,30 @@ void parse::check_memory_reachable(const ast& node)
} }
ast parse::null() ast parse::null()
{ {
return {tokens[ptr].line,tokens[ptr].col,ast_null}; return {toks[ptr].line,toks[ptr].col,ast_null};
} }
ast parse::nil() ast parse::nil()
{ {
return {tokens[ptr].line,tokens[ptr].col,ast_nil}; return {toks[ptr].line,toks[ptr].col,ast_nil};
} }
ast parse::num() ast parse::num()
{ {
ast node(tokens[ptr].line,tokens[ptr].col,ast_num); ast node(toks[ptr].line,toks[ptr].col,ast_num);
node.set_num(str2num(tokens[ptr].str.c_str())); node.set_num(str2num(toks[ptr].str.c_str()));
match(tok_num); match(tok_num);
return node; return node;
} }
ast parse::str() ast parse::str()
{ {
ast node(tokens[ptr].line,tokens[ptr].col,ast_str); ast node(toks[ptr].line,toks[ptr].col,ast_str);
node.set_str(tokens[ptr].str); node.set_str(toks[ptr].str);
match(tok_str); match(tok_str);
return node; return node;
} }
ast parse::id() ast parse::id()
{ {
ast node(tokens[ptr].line,tokens[ptr].col,ast_id); ast node(toks[ptr].line,toks[ptr].col,ast_id);
node.set_str(tokens[ptr].str); node.set_str(toks[ptr].str);
match(tok_id); match(tok_id);
return node; return node;
} }
@ -300,7 +347,7 @@ ast 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
}; };
ast node(tokens[ptr].line,tokens[ptr].col,ast_vec); ast node(toks[ptr].line,toks[ptr].col,ast_vec);
match(tok_lbracket); match(tok_lbracket);
while(!lookahead(tok_rbracket)) while(!lookahead(tok_rbracket))
{ {
@ -317,7 +364,7 @@ ast parse::vec()
} }
ast parse::hash() ast parse::hash()
{ {
ast node(tokens[ptr].line,tokens[ptr].col,ast_hash); ast node(toks[ptr].line,toks[ptr].col,ast_hash);
match(tok_lbrace); match(tok_lbrace);
while(!lookahead(tok_rbrace)) while(!lookahead(tok_rbrace))
{ {
@ -325,7 +372,7 @@ ast parse::hash()
if(lookahead(tok_comma)) if(lookahead(tok_comma))
match(tok_comma); match(tok_comma);
else if(lookahead(tok_id) || lookahead(tok_str))// first set of hashmember else if(lookahead(tok_id) || lookahead(tok_str))// first set of hashmember
die(error_line,"expected ',' between hash members",true); die(err_line,"expected ',' between hash members",true);
else else
break; break;
} }
@ -334,7 +381,7 @@ ast parse::hash()
} }
ast parse::pair() ast parse::pair()
{ {
ast node(tokens[ptr].line,tokens[ptr].col,ast_pair); ast node(toks[ptr].line,toks[ptr].col,ast_pair);
if(lookahead(tok_id)) if(lookahead(tok_id))
node.add(id()); node.add(id());
else if(lookahead(tok_str)) else if(lookahead(tok_str))
@ -348,7 +395,7 @@ ast parse::pair()
ast parse::func() ast parse::func()
{ {
++in_func; ++in_func;
ast node(tokens[ptr].line,tokens[ptr].col,ast_func); ast node(toks[ptr].line,toks[ptr].col,ast_func);
match(tok_func); match(tok_func);
if(lookahead(tok_lcurve)) if(lookahead(tok_lcurve))
node.add(args()); node.add(args());
@ -360,14 +407,14 @@ ast parse::func()
} }
ast parse::args() ast parse::args()
{ {
ast node(tokens[ptr].line,tokens[ptr].col,ast_args); ast node(toks[ptr].line,toks[ptr].col,ast_args);
match(tok_lcurve); match(tok_lcurve);
while(!lookahead(tok_rcurve)) while(!lookahead(tok_rcurve))
{ {
ast tmp=id(); ast tmp=id();
if(lookahead(tok_eq) || lookahead(tok_ellipsis)) if(lookahead(tok_eq) || lookahead(tok_ellipsis))
{ {
ast special_arg(tokens[ptr].line,tokens[ptr].col,ast_null); ast special_arg(toks[ptr].line,toks[ptr].col,ast_null);
if(lookahead(tok_eq)) if(lookahead(tok_eq))
{ {
match(tok_eq); match(tok_eq);
@ -388,7 +435,7 @@ ast parse::args()
if(lookahead(tok_comma)) if(lookahead(tok_comma))
match(tok_comma); match(tok_comma);
else if(lookahead(tok_id))// first set of identifier else if(lookahead(tok_id))// first set of identifier
die(error_line,"expected ',' between identifiers",true); die(err_line,"expected ',' between identifiers",true);
else else
break; break;
} }
@ -437,17 +484,17 @@ ast parse::args()
} }
ast parse::lcurve_expr() ast parse::lcurve_expr()
{ {
if(tokens[ptr+1].type==tok_var) if(toks[ptr+1].type==tok_var)
return definition(); return definition();
return check_multi_scalar()?multi_assgin():calc(); return check_multi_scalar()?multi_assgin():calc();
} }
ast parse::expr() ast parse::expr()
{ {
u32 type=tokens[ptr].type; u32 type=toks[ptr].type;
if((type==tok_break || type==tok_continue) && !in_loop) if((type==tok_break || type==tok_continue) && !in_loop)
die(error_line,"should use break/continue in loops"); die(err_line,"should use break/continue in loops");
if(type==tok_ret && !in_func) if(type==tok_ret && !in_func)
die(error_line,"should use return in functions"); die(err_line,"should use return in functions");
switch(type) switch(type)
{ {
case tok_nil: case tok_nil:
@ -471,20 +518,20 @@ ast parse::expr()
case tok_ret: return ret_expr(); break; case tok_ret: return ret_expr(); break;
case tok_semi: break; case tok_semi: break;
default: default:
die(error_line,"incorrect token <"+tokens[ptr].str+">"); die(err_line,"incorrect token <"+toks[ptr].str+">");
++ptr; ++ptr;
break; break;
} }
return {tokens[ptr].line,tokens[ptr].col,ast_null}; return {toks[ptr].line,toks[ptr].col,ast_null};
} }
ast parse::exprs() ast parse::exprs()
{ {
if(lookahead(tok_eof)) if(lookahead(tok_eof))
{ {
die(error_line,"expected expression block"); die(err_line,"expected expression block");
return null(); return null();
} }
ast node(tokens[ptr].line,tokens[ptr].col,ast_block); ast node(toks[ptr].line,toks[ptr].col,ast_block);
if(lookahead(tok_lbrace)) if(lookahead(tok_lbrace))
{ {
match(tok_lbrace); match(tok_lbrace);
@ -495,7 +542,7 @@ ast parse::exprs()
match(tok_semi); match(tok_semi);
// the last expression can be recognized without semi // the last expression can be recognized without semi
else if(need_semi_check(node.child().back()) && !lookahead(tok_rbrace)) else if(need_semi_check(node.child().back()) && !lookahead(tok_rbrace))
die(error_line,"expected ';'",true); die(err_line,"expected ';'",true);
} }
match(tok_rbrace,"expected '}' when generating expressions"); match(tok_rbrace,"expected '}' when generating expressions");
} }
@ -513,7 +560,7 @@ ast parse::calc()
if(lookahead(tok_quesmark)) if(lookahead(tok_quesmark))
{ {
// trinocular calculation // trinocular calculation
ast tmp(tokens[ptr].line,tokens[ptr].col,ast_trino); ast tmp(toks[ptr].line,toks[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());
@ -521,13 +568,13 @@ ast parse::calc()
tmp.add(calc()); tmp.add(calc());
node=std::move(tmp); node=std::move(tmp);
} }
else if(tok_eq<=tokens[ptr].type && tokens[ptr].type<=tok_lnkeq) else if(tok_eq<=toks[ptr].type && toks[ptr].type<=tok_lnkeq)
{ {
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
ast tmp(tokens[ptr].line,tokens[ptr].col,tokens[ptr].type-tok_eq+ast_equal); ast tmp(toks[ptr].line,toks[ptr].col,toks[ptr].type-tok_eq+ast_equal);
tmp.add(std::move(node)); tmp.add(std::move(node));
match(tokens[ptr].type); match(toks[ptr].type);
tmp.add(calc()); tmp.add(calc());
node=std::move(tmp); node=std::move(tmp);
} }
@ -538,7 +585,7 @@ ast parse::or_expr()
ast node=and_expr(); ast node=and_expr();
while(lookahead(tok_or)) while(lookahead(tok_or))
{ {
ast tmp(tokens[ptr].line,tokens[ptr].col,ast_or); ast tmp(toks[ptr].line,toks[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());
@ -551,7 +598,7 @@ ast parse::and_expr()
ast node=cmp_expr(); ast node=cmp_expr();
while(lookahead(tok_and)) while(lookahead(tok_and))
{ {
ast tmp(tokens[ptr].line,tokens[ptr].col,ast_and); ast tmp(toks[ptr].line,toks[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());
@ -562,12 +609,12 @@ ast parse::and_expr()
ast parse::cmp_expr() ast parse::cmp_expr()
{ {
ast node=additive_expr(); ast node=additive_expr();
while(tok_cmpeq<=tokens[ptr].type && tokens[ptr].type<=tok_geq) while(tok_cmpeq<=toks[ptr].type && toks[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
ast tmp(tokens[ptr].line,tokens[ptr].col,tokens[ptr].type-tok_cmpeq+ast_cmpeq); ast tmp(toks[ptr].line,toks[ptr].col,toks[ptr].type-tok_cmpeq+ast_cmpeq);
tmp.add(std::move(node)); tmp.add(std::move(node));
match(tokens[ptr].type); match(toks[ptr].type);
tmp.add(additive_expr()); tmp.add(additive_expr());
node=std::move(tmp); node=std::move(tmp);
} }
@ -578,15 +625,15 @@ ast parse::additive_expr()
ast node=multive_expr(); ast node=multive_expr();
while(lookahead(tok_add) || lookahead(tok_sub) || lookahead(tok_link)) while(lookahead(tok_add) || lookahead(tok_sub) || lookahead(tok_link))
{ {
ast tmp(tokens[ptr].line,tokens[ptr].col,ast_null); ast tmp(toks[ptr].line,toks[ptr].col,ast_null);
switch(tokens[ptr].type) switch(toks[ptr].type)
{ {
case tok_add: tmp.set_type(ast_add); break; case tok_add: tmp.set_type(ast_add); break;
case tok_sub: tmp.set_type(ast_sub); break; case tok_sub: tmp.set_type(ast_sub); break;
case tok_link: tmp.set_type(ast_link); break; case tok_link: tmp.set_type(ast_link); break;
} }
tmp.add(std::move(node)); tmp.add(std::move(node));
match(tokens[ptr].type); match(toks[ptr].type);
tmp.add(multive_expr()); tmp.add(multive_expr());
node=std::move(tmp); node=std::move(tmp);
} }
@ -597,9 +644,9 @@ ast parse::multive_expr()
ast node=(lookahead(tok_sub) || lookahead(tok_not))?unary():scalar(); ast node=(lookahead(tok_sub) || lookahead(tok_not))?unary():scalar();
while(lookahead(tok_mult) || lookahead(tok_div)) while(lookahead(tok_mult) || lookahead(tok_div))
{ {
ast tmp(tokens[ptr].line,tokens[ptr].col,tokens[ptr].type-tok_mult+ast_mult); ast tmp(toks[ptr].line,toks[ptr].col,toks[ptr].type-tok_mult+ast_mult);
tmp.add(std::move(node)); tmp.add(std::move(node));
match(tokens[ptr].type); match(toks[ptr].type);
tmp.add((lookahead(tok_sub) || lookahead(tok_not))?unary():scalar()); tmp.add((lookahead(tok_sub) || lookahead(tok_not))?unary():scalar());
node=std::move(tmp); node=std::move(tmp);
} }
@ -607,8 +654,8 @@ ast parse::multive_expr()
} }
ast parse::unary() ast parse::unary()
{ {
ast node(tokens[ptr].line,tokens[ptr].col,ast_null); ast node(toks[ptr].line,toks[ptr].col,ast_null);
switch(tokens[ptr].type) switch(toks[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;
case tok_not:node.set_type(ast_not);match(tok_not);break; case tok_not:node.set_type(ast_not);match(tok_not);break;
@ -618,7 +665,7 @@ ast parse::unary()
} }
ast parse::scalar() ast parse::scalar()
{ {
ast node(tokens[ptr].line,tokens[ptr].col,ast_null); ast node(toks[ptr].line,toks[ptr].col,ast_null);
if(lookahead(tok_nil)) {node=nil();match(tok_nil);} if(lookahead(tok_nil)) {node=nil();match(tok_nil);}
else if(lookahead(tok_num)) node=num(); else if(lookahead(tok_num)) node=num();
else if(lookahead(tok_str)) node=str(); else if(lookahead(tok_str)) node=str();
@ -642,36 +689,36 @@ ast parse::scalar()
} }
else else
{ {
die(error_line,"expected scalar"); die(err_line,"expected scalar");
return node; return node;
} }
// check call and avoid ambiguous syntax // check call and avoid ambiguous syntax
if(is_call(tokens[ptr].type) && !(lookahead(tok_lcurve) && tokens[ptr+1].type==tok_var)) if(is_call(toks[ptr].type) && !(lookahead(tok_lcurve) && toks[ptr+1].type==tok_var))
{ {
ast tmp=std::move(node); ast tmp=std::move(node);
node={tokens[ptr].line,tokens[ptr].col,ast_call}; node={toks[ptr].line,toks[ptr].col,ast_call};
node.add(std::move(tmp)); node.add(std::move(tmp));
while(is_call(tokens[ptr].type)) while(is_call(toks[ptr].type))
node.add(call_scalar()); node.add(call_scalar());
} }
return node; return node;
} }
ast parse::call_scalar() ast parse::call_scalar()
{ {
switch(tokens[ptr].type) switch(toks[ptr].type)
{ {
case tok_lcurve: return callf(); break; case tok_lcurve: return callf(); break;
case tok_lbracket: return callv(); break; case tok_lbracket: return callv(); break;
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,tokens[ptr].col,ast_nil}; return {toks[ptr].line,toks[ptr].col,ast_nil};
} }
ast parse::callh() ast parse::callh()
{ {
ast node(tokens[ptr].line,tokens[ptr].col,ast_callh); ast node(toks[ptr].line,toks[ptr].col,ast_callh);
match(tok_dot); match(tok_dot);
node.set_str(tokens[ptr].str); node.set_str(toks[ptr].str);
match(tok_id,"expected hashmap key"); // get key match(tok_id,"expected hashmap key"); // get key
return node; return node;
} }
@ -687,7 +734,7 @@ ast parse::callv()
tok_lbrace,tok_lbracket,tok_colon, tok_lbrace,tok_lbracket,tok_colon,
tok_null tok_null
}; };
ast node(tokens[ptr].line,tokens[ptr].col,ast_callv); ast node(toks[ptr].line,toks[ptr].col,ast_callv);
match(tok_lbracket); match(tok_lbracket);
while(!lookahead(tok_rbracket)) while(!lookahead(tok_rbracket))
{ {
@ -715,7 +762,7 @@ ast 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
}; };
ast node(tokens[ptr].line,tokens[ptr].col,ast_callf); ast node(toks[ptr].line,toks[ptr].col,ast_callf);
bool special_call=check_special_call(); bool special_call=check_special_call();
match(tok_lcurve); match(tok_lcurve);
while(!lookahead(tok_rcurve)) while(!lookahead(tok_rcurve))
@ -746,15 +793,15 @@ ast parse::subvec()
} }
ast parse::definition() ast parse::definition()
{ {
ast node(tokens[ptr].line,tokens[ptr].col,ast_def); ast node(toks[ptr].line,toks[ptr].col,ast_def);
if(lookahead(tok_var)) if(lookahead(tok_var))
{ {
match(tok_var); match(tok_var);
switch(tokens[ptr].type) switch(toks[ptr].type)
{ {
case tok_id: node.add(id());break; case tok_id: node.add(id());break;
case tok_lcurve: node.add(outcurve_def());break; case tok_lcurve: node.add(outcurve_def());break;
default: die(error_line,"expected identifier");break; default: die(err_line,"expected identifier");break;
} }
} }
else if(lookahead(tok_lcurve)) else if(lookahead(tok_lcurve))
@ -787,19 +834,19 @@ ast parse::outcurve_def()
} }
ast parse::multi_id() ast parse::multi_id()
{ {
ast node(tokens[ptr].line,tokens[ptr].col,ast_multi_id); ast node(toks[ptr].line,toks[ptr].col,ast_multi_id);
while(!lookahead(tok_eof)) while(!lookahead(tok_eof))
{ {
node.add(id()); node.add(id());
if(is_call(tokens[ptr].type)) if(is_call(toks[ptr].type))
{ {
call_scalar();// recognize calls but this is still a syntax error call_scalar();// recognize calls but this is still a syntax error
die(error_line,"cannot call identifier in multi-definition"); die(err_line,"cannot call identifier in multi-definition");
} }
if(lookahead(tok_comma)) if(lookahead(tok_comma))
match(tok_comma); match(tok_comma);
else if(lookahead(tok_id))// first set of identifier else if(lookahead(tok_id))// first set of identifier
die(error_line,"expected ',' between identifiers",true); die(err_line,"expected ',' between identifiers",true);
else else
break; break;
} }
@ -814,7 +861,7 @@ ast 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
}; };
ast node(tokens[ptr].line,tokens[ptr].col,ast_tuple); ast node(toks[ptr].line,toks[ptr].col,ast_tuple);
match(tok_lcurve); match(tok_lcurve);
while(!lookahead(tok_rcurve)) while(!lookahead(tok_rcurve))
{ {
@ -833,12 +880,12 @@ ast parse::multi_scalar(bool check_call_memory)
} }
ast parse::multi_assgin() ast parse::multi_assgin()
{ {
ast node(tokens[ptr].line,tokens[ptr].col,ast_multi_assign); ast node(toks[ptr].line,toks[ptr].col,ast_multi_assign);
node.add(multi_scalar(true)); node.add(multi_scalar(true));
match(tok_eq); match(tok_eq);
if(lookahead(tok_eof)) if(lookahead(tok_eof))
{ {
die(error_line,"expected value list"); die(err_line,"expected value list");
return node; return node;
} }
if(lookahead(tok_lcurve)) if(lookahead(tok_lcurve))
@ -854,7 +901,7 @@ ast parse::loop()
{ {
++in_loop; ++in_loop;
ast node(0,0,ast_null); ast node(0,0,ast_null);
switch(tokens[ptr].type) switch(toks[ptr].type)
{ {
case tok_while: node=while_loop(); break; case tok_while: node=while_loop(); break;
case tok_for: node=for_loop(); break; case tok_for: node=for_loop(); break;
@ -866,7 +913,7 @@ ast parse::loop()
} }
ast parse::while_loop() ast parse::while_loop()
{ {
ast node(tokens[ptr].line,tokens[ptr].col,ast_while); ast node(toks[ptr].line,toks[ptr].col,ast_while);
match(tok_while); match(tok_while);
match(tok_lcurve); match(tok_lcurve);
node.add(calc()); node.add(calc());
@ -876,12 +923,12 @@ ast parse::while_loop()
} }
ast parse::for_loop() ast parse::for_loop()
{ {
ast node(tokens[ptr].line,tokens[ptr].col,ast_for); ast node(toks[ptr].line,toks[ptr].col,ast_for);
match(tok_for); match(tok_for);
match(tok_lcurve); match(tok_lcurve);
// first expression // first expression
if(lookahead(tok_eof)) if(lookahead(tok_eof))
die(error_line,"expected definition"); die(err_line,"expected definition");
if(lookahead(tok_semi)) if(lookahead(tok_semi))
node.add(null()); node.add(null());
else if(lookahead(tok_var)) else if(lookahead(tok_var))
@ -893,7 +940,7 @@ ast parse::for_loop()
match(tok_semi,"expected ';' in for(;;)"); match(tok_semi,"expected ';' in for(;;)");
// conditional expression // conditional expression
if(lookahead(tok_eof)) if(lookahead(tok_eof))
die(error_line,"expected conditional expr"); die(err_line,"expected conditional expr");
if(lookahead(tok_semi)) if(lookahead(tok_semi))
node.add(null()); node.add(null());
else else
@ -901,7 +948,7 @@ ast parse::for_loop()
match(tok_semi,"expected ';' in for(;;)"); match(tok_semi,"expected ';' in for(;;)");
//after loop expression //after loop expression
if(lookahead(tok_eof)) if(lookahead(tok_eof))
die(error_line,"expected calculation"); die(err_line,"expected calculation");
if(lookahead(tok_rcurve)) if(lookahead(tok_rcurve))
node.add(null()); node.add(null());
else else
@ -912,8 +959,8 @@ ast parse::for_loop()
} }
ast parse::forei_loop() ast parse::forei_loop()
{ {
ast node(tokens[ptr].line,tokens[ptr].col,ast_null); ast node(toks[ptr].line,toks[ptr].col,ast_null);
switch(tokens[ptr].type) switch(toks[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;
case tok_foreach: node.set_type(ast_foreach); match(tok_foreach); break; case tok_foreach: node.set_type(ast_foreach); match(tok_foreach); break;
@ -922,11 +969,11 @@ ast parse::forei_loop()
// first expression // first expression
// foreach/forindex must have an iterator to loop through // foreach/forindex must have an iterator to loop through
if(!lookahead(tok_var) && !lookahead(tok_id)) if(!lookahead(tok_var) && !lookahead(tok_id))
die(error_line,"expected iterator"); die(err_line,"expected iterator");
node.add(iter_gen()); node.add(iter_gen());
match(tok_semi,"expected ';' in foreach/forindex(iter;vector)"); match(tok_semi,"expected ';' in foreach/forindex(iter;vector)");
if(lookahead(tok_eof)) if(lookahead(tok_eof))
die(error_line,"expected vector"); die(err_line,"expected vector");
node.add(calc()); node.add(calc());
match(tok_rcurve); match(tok_rcurve);
node.add(exprs()); node.add(exprs());
@ -934,7 +981,7 @@ ast parse::forei_loop()
} }
ast parse::iter_gen() ast parse::iter_gen()
{ {
ast node(tokens[ptr].line,tokens[ptr].col,ast_null); ast node(toks[ptr].line,toks[ptr].col,ast_null);
if(lookahead(tok_var)) if(lookahead(tok_var))
{ {
match(tok_var); match(tok_var);
@ -945,7 +992,7 @@ ast parse::iter_gen()
{ {
node.set_type(ast_call); node.set_type(ast_call);
node.add(id()); node.add(id());
while(is_call(tokens[ptr].type)) while(is_call(toks[ptr].type))
node.add(call_scalar()); node.add(call_scalar());
check_memory_reachable(node); check_memory_reachable(node);
} }
@ -953,8 +1000,8 @@ ast parse::iter_gen()
} }
ast parse::cond() ast parse::cond()
{ {
ast node(tokens[ptr].line,tokens[ptr].col,ast_cond); ast node(toks[ptr].line,toks[ptr].col,ast_cond);
ast ifnode(tokens[ptr].line,tokens[ptr].col,ast_if); ast ifnode(toks[ptr].line,toks[ptr].col,ast_if);
match(tok_if); match(tok_if);
match(tok_lcurve); match(tok_lcurve);
ifnode.add(calc()); ifnode.add(calc());
@ -963,7 +1010,7 @@ ast parse::cond()
node.add(std::move(ifnode)); node.add(std::move(ifnode));
while(lookahead(tok_elsif)) while(lookahead(tok_elsif))
{ {
ast elsifnode(tokens[ptr].line,tokens[ptr].col,ast_elsif); ast elsifnode(toks[ptr].line,toks[ptr].col,ast_elsif);
match(tok_elsif); match(tok_elsif);
match(tok_lcurve); match(tok_lcurve);
elsifnode.add(calc()); elsifnode.add(calc());
@ -973,7 +1020,7 @@ ast parse::cond()
} }
if(lookahead(tok_else)) if(lookahead(tok_else))
{ {
ast elsenode(tokens[ptr].line,tokens[ptr].col,ast_else); ast elsenode(toks[ptr].line,toks[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));
@ -982,21 +1029,21 @@ ast parse::cond()
} }
ast parse::continue_expr() ast parse::continue_expr()
{ {
ast node(tokens[ptr].line,tokens[ptr].col,ast_continue); ast node(toks[ptr].line,toks[ptr].col,ast_continue);
match(tok_continue); match(tok_continue);
return node; return node;
} }
ast parse::break_expr() ast parse::break_expr()
{ {
ast node(tokens[ptr].line,tokens[ptr].col,ast_break); ast node(toks[ptr].line,toks[ptr].col,ast_break);
match(tok_break); match(tok_break);
return node; return node;
} }
ast parse::ret_expr() ast parse::ret_expr()
{ {
ast node(tokens[ptr].line,tokens[ptr].col,ast_ret); ast node(toks[ptr].line,toks[ptr].col,ast_ret);
match(tok_ret); match(tok_ret);
u32 type=tokens[ptr].type; u32 type=toks[ptr].type;
if(type==tok_nil || type==tok_num || if(type==tok_nil || type==tok_num ||
type==tok_str || type==tok_id || type==tok_str || type==tok_id ||
type==tok_func || type==tok_sub || type==tok_func || type==tok_sub ||

View File

@ -46,6 +46,7 @@ var testfile=[
"class.nas", "class.nas",
"coroutine.nas", "coroutine.nas",
"diff.nas", "diff.nas",
"donuts.nas",
"exception.nas", "exception.nas",
"fib.nas", "fib.nas",
"filesystem.nas", "filesystem.nas",

51
test/donuts.nas Normal file
View File

@ -0,0 +1,51 @@
var mod=func(n,a){
return n-int(n/a)*a;
}
var main=func(run_time){
var (sin,cos)=(math.sin,math.cos);
var (A,B)=(0,0);
var (z,b)=([],[]);
setsize(z,1760);
setsize(b,1760);
print("\e[2J");
for(var run=0;run<run_time;run+=1){
forindex(var i;b)
b[i]=32;
forindex(var i;z)
z[i]=0;
for(var j=0;j<6.28;j+=0.07){
for(var i=0;i<6.28;i+=0.02){
var (c,d,e,f,g)=(sin(i),cos(j),sin(A),sin(j),cos(A));
var h=d+2;
var D=1/(c*h*e+f*g+5);
var (l,m,n)=(cos(i),cos(B),sin(B));
var t=c*h*g-f*e;
var x=int(40+30*D*(l*h*m-t*n));
var y=int(12+15*D*(l*h*n+t*m));
var o=int(x+80*y);
var N=int(8*((f*e-c*d*g)*m-c*d*e-f*g-l*d*n));
if(22>y and y>0 and x>0 and 80>x and D>z[o]){
z[o]=D;
b[o]=".,-~:;=!*#$@"[N>0?N:0];
}
}
}
print("\e[H");
var screen="";
for(var k=0;k<1761;k+=1) {
screen~=(mod(k,80)?chr(b[k]):chr(10));
A+=0.00004;
B+=0.00002;
}
print(screen);
}
}
var frames=5e2;
var ts=maketimestamp();
ts.stamp();
main(frames);
println(int(frames/(ts.elapsedMSec()/1000))," fps");

View File

@ -46,6 +46,7 @@ var filechecksum=func(){
"./test/bp.nas", "./test/calc.nas", "./test/bp.nas", "./test/calc.nas",
"./test/choice.nas", "./test/class.nas", "./test/choice.nas", "./test/class.nas",
"./test/coroutine.nas", "./test/diff.nas", "./test/coroutine.nas", "./test/diff.nas",
"./test/donuts.nas",
"./test/exception.nas", "./test/fib.nas", "./test/exception.nas", "./test/fib.nas",
"./test/filesystem.nas", "./test/hexdump.nas", "./test/filesystem.nas", "./test/hexdump.nas",
"./test/httptest.nas", "./test/json.nas", "./test/httptest.nas", "./test/json.nas",

View File

@ -1,7 +1,7 @@
# wave collapse function 2022/4/10 # wave collapse function 2022/4/10
# by ValKmjolnir # by ValKmjolnir
srand(); srand();
var interval=1/120; var interval=1/160;
var table=[ var table=[
# c ,w,a,s,d # c ,w,a,s,d
["═",0,1,0,1], ["═",0,1,0,1],