mirror of
https://github.com/ValKmjolnir/Nasal-Interpreter.git
synced 2026-05-24 05:25:45 +08:00
variables can be used before definition
change program to command line change trace back info change print function of nasal_vec and nasal_hash
This commit is contained in:
112
main.cpp
112
main.cpp
@@ -1,42 +1,30 @@
|
||||
#include "nasal.h"
|
||||
|
||||
void help_interact()
|
||||
{
|
||||
std::cout
|
||||
<<">> [ ] input a file name to execute. \n"
|
||||
<<">> [help ] show help. \n"
|
||||
<<">> [ast ] view abstract syntax tree. \n"
|
||||
<<">> [code ] view byte code. \n"
|
||||
<<">> [exec ] execute program on bytecode vm.\n"
|
||||
<<">> [logo ] print logo of nasal . \n"
|
||||
<<">> [exit ] quit nasal interpreter. \n";
|
||||
return;
|
||||
}
|
||||
void help_cmd()
|
||||
{
|
||||
std::cout
|
||||
#ifdef _WIN32
|
||||
<<"use command \'chcp 65001\' if want to use unicode.\n"
|
||||
#endif
|
||||
<<"nasal [option]|[file]\n"
|
||||
<<" input 0 argument to use the interactive interpreter.\n"
|
||||
<<"nasal [option]\n"
|
||||
<<"option:\n"
|
||||
<<" -h, --help | get help.\n"
|
||||
<<" -v, --version | get version of nasal interpreter.\n"
|
||||
<<" -v, --version | get version of nasal interpreter.\n\n"
|
||||
<<"nasal [file]\n"
|
||||
<<"file:\n"
|
||||
<<" input file name to execute script file.\n\n"
|
||||
<<"nasal [option] [file]\n"
|
||||
<<"option:\n"
|
||||
<<" --lex | view token info.\n"
|
||||
<<" --ast | view abstract syntax tree.\n"
|
||||
<<" --code | view bytecode.\n"
|
||||
<<" --exec | execute script file.\n"
|
||||
<<" --time | execute and get the running time.\n"
|
||||
<<"file:\n"
|
||||
<<" input file name to execute script file.\n";
|
||||
return;
|
||||
}
|
||||
void info()
|
||||
{
|
||||
std::cout
|
||||
<<">> thanks to https://github.com/andyross/nasal\n"
|
||||
<<">> code: https://github.com/ValKmjolnir/Nasal-Interpreter\n"
|
||||
<<">> code: https://gitee.com/valkmjolnir/Nasal-Interpreter\n"
|
||||
<<">> info: http://wiki.flightgear.org/Nasal_scripting_language\n"
|
||||
<<">> input \"help\" to get help .\n";
|
||||
return;
|
||||
}
|
||||
|
||||
void logo()
|
||||
{
|
||||
std::cout
|
||||
@@ -45,14 +33,21 @@ void logo()
|
||||
<<" / \\/ / _` / __|/ _` | | \n"
|
||||
<<" / /\\ / (_| \\__ \\ (_| | | \n"
|
||||
<<" \\_\\ \\/ \\__,_|___/\\__,_|_|\n"
|
||||
<<" nasal interpreter ver 7.0 \n";
|
||||
<<"nasal interpreter ver 7.0\n"
|
||||
<<"thanks to : https://github.com/andyross/nasal\n"
|
||||
<<"code repo : https://github.com/ValKmjolnir/Nasal-Interpreter\n"
|
||||
<<"code repo : https://gitee.com/valkmjolnir/Nasal-Interpreter\n"
|
||||
<<"lang info : http://wiki.flightgear.org/Nasal_scripting_language\n"
|
||||
<<"input \"nasal -h\" to get help .\n";
|
||||
return;
|
||||
}
|
||||
|
||||
void die(const char* stage,std::string& filename)
|
||||
{
|
||||
std::cout<<">> ["<<stage<<"] in <\""<<filename<<"\">: error(s) occurred,stop.\n";
|
||||
std::cout<<">> ["<<stage<<"] in <"<<filename<<">: error(s) occurred,stop.\n";
|
||||
return;
|
||||
}
|
||||
|
||||
void execute(std::string& file,std::string& command)
|
||||
{
|
||||
nasal_lexer lexer;
|
||||
@@ -67,13 +62,18 @@ void execute(std::string& file,std::string& command)
|
||||
die("lexer",file);
|
||||
return;
|
||||
}
|
||||
if(command=="--lex")
|
||||
{
|
||||
lexer.print_token();
|
||||
return;
|
||||
}
|
||||
parse.main_process(lexer.get_token_list());
|
||||
if(parse.get_error())
|
||||
{
|
||||
die("parse",file);
|
||||
return;
|
||||
}
|
||||
if(command=="ast")
|
||||
if(command=="--ast")
|
||||
{
|
||||
parse.get_root().print_ast(0);
|
||||
return;
|
||||
@@ -91,7 +91,7 @@ void execute(std::string& file,std::string& command)
|
||||
die("codegen",file);
|
||||
return;
|
||||
}
|
||||
if(command=="code")
|
||||
if(command=="--code")
|
||||
{
|
||||
codegen.print_byte_code();
|
||||
return;
|
||||
@@ -101,55 +101,45 @@ void execute(std::string& file,std::string& command)
|
||||
codegen.get_num_table(),
|
||||
import.get_file()
|
||||
);
|
||||
vm.run(codegen.get_exec_code());
|
||||
if(command=="--exec")
|
||||
vm.run(codegen.get_exec_code());
|
||||
else if(command=="--time")
|
||||
{
|
||||
clock_t begin=clock();
|
||||
vm.run(codegen.get_exec_code());
|
||||
std::cout<<"process exited after "<<((double)(clock()-begin))/CLOCKS_PER_SEC<<"s.\n";
|
||||
}
|
||||
vm.clear();
|
||||
return;
|
||||
}
|
||||
void interact()
|
||||
{
|
||||
#ifdef _WIN32
|
||||
// use chcp 65001 to use unicode io
|
||||
system("chcp 65001");
|
||||
#endif
|
||||
std::string command,file="null";
|
||||
logo();
|
||||
info();
|
||||
while(1)
|
||||
{
|
||||
std::cout<<">> ";
|
||||
std::cin>>command;
|
||||
if(command=="help")
|
||||
help_interact();
|
||||
else if(command=="logo")
|
||||
logo();
|
||||
else if(command=="exit")
|
||||
return;
|
||||
else if(command=="ast" || command=="code" || command=="exec")
|
||||
execute(file,command);
|
||||
else
|
||||
file=command;
|
||||
}
|
||||
}
|
||||
int main(int argc,const char* argv[])
|
||||
{
|
||||
std::string command,file="null";
|
||||
if(argc==1)
|
||||
interact();
|
||||
else if(argc==2 && (!strcmp(argv[1],"-v") || !strcmp(argv[1],"--version")))
|
||||
if(argc==2 && (!strcmp(argv[1],"-v") || !strcmp(argv[1],"--version")))
|
||||
logo();
|
||||
else if(argc==2 && (!strcmp(argv[1],"-h") || !strcmp(argv[1],"--help")))
|
||||
help_cmd();
|
||||
else if(argc==2 && argv[1][0]!='-')
|
||||
{
|
||||
file=argv[1];
|
||||
command="exec";
|
||||
command="--exec";
|
||||
execute(file,command);
|
||||
}
|
||||
else if(argc==3 &&
|
||||
(!strcmp(argv[1],"--lex") ||
|
||||
!strcmp(argv[1],"--ast") ||
|
||||
!strcmp(argv[1],"--code") ||
|
||||
!strcmp(argv[1],"--exec") ||
|
||||
!strcmp(argv[1],"--time")))
|
||||
{
|
||||
file=argv[2];
|
||||
command=argv[1];
|
||||
execute(file,command);
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout
|
||||
<<"invalid command.\n"
|
||||
<<"invalid argument(s).\n"
|
||||
<<"use nasal -h to get help.\n";
|
||||
}
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user