diff --git a/main.cpp b/main.cpp index d712896..4179578 100644 --- a/main.cpp +++ b/main.cpp @@ -1,20 +1,18 @@ #include "nasal.h" -nasal_resource resource; nasal_lexer lexer; nasal_parse parse; nasal_import preprocessor; nasal_codegen code_generator; -std::string command; std::string inputfile="null"; nasal_runtime runtime; void help() { std::cout<<">> [\"file\"] input a file name.\n"; + std::cout<<">> [help ] show help.\n"; std::cout<<">> [clear ] clear the screen.\n"; std::cout<<">> [del ] clear the input filename.\n"; - std::cout<<">> [rs ] print source code.\n"; std::cout<<">> [lex ] use lexer to turn code into tokens.\n"; std::cout<<">> [ast ] do parsing and check the abstract syntax tree.\n"; std::cout<<">> [run ] run abstract syntax tree.\n"; @@ -37,7 +35,6 @@ void logo() void del_func() { - resource.clear(); lexer.clear(); parse.clear(); inputfile="null"; @@ -53,12 +50,8 @@ void die(std::string stage,std::string filename) void lex_func() { - if(!resource.input_file(inputfile)) - { - die("resource",inputfile); - return; - } - lexer.scanner(resource.get_file()); + lexer.openfile(inputfile); + lexer.scanner(); if(lexer.get_error()) { die("lexer",inputfile); @@ -70,12 +63,8 @@ void lex_func() void ast_print() { - if(!resource.input_file(inputfile)) - { - die("resource",inputfile); - return; - } - lexer.scanner(resource.get_file()); + lexer.openfile(inputfile); + lexer.scanner(); if(lexer.get_error()) { die("lexer",inputfile); @@ -93,12 +82,8 @@ void ast_print() } void runtime_start() { - if(!resource.input_file(inputfile)) - { - die("resource",inputfile); - return; - } - lexer.scanner(resource.get_file()); + lexer.openfile(inputfile); + lexer.scanner(); if(lexer.get_error()) { die("lexer",inputfile); @@ -124,12 +109,8 @@ void runtime_start() void codegen_start() { - if(!resource.input_file(inputfile)) - { - die("resource",inputfile); - return; - } - lexer.scanner(resource.get_file()); + lexer.openfile(inputfile); + lexer.scanner(); if(lexer.get_error()) { die("lexer",inputfile); @@ -173,6 +154,7 @@ void execution_start() int main() { + std::string command; #ifdef _WIN32 // use chcp 65001 to use unicode io system("chcp 65001"); @@ -213,11 +195,6 @@ int main() } else if(command=="del") del_func(); - else if(command=="rs") - { - if(resource.input_file(inputfile)) - resource.print_file(); - } else if(command=="lex") lex_func(); else if(command=="ast") @@ -233,16 +210,7 @@ int main() else if(command=="exit") break; else - { inputfile=command; - std::ifstream fin(command); - if(fin.fail()) - { - std::cout<<">> [file] cannot open file \""< #include #include -/* if thread is used, don't forget to add -std=c++11 or higher standard before executing */ -// #include +#include #include #include #include @@ -21,7 +20,6 @@ #include "nasal_enum.h" #include "nasal_misc.h" -#include "nasal_resource.h" #include "nasal_lexer.h" #include "nasal_ast.h" #include "nasal_parse.h" diff --git a/nasal_import.h b/nasal_import.h index 2e12b53..efa9aea 100644 --- a/nasal_import.h +++ b/nasal_import.h @@ -4,7 +4,6 @@ class nasal_import { private: - nasal_resource import_src; nasal_lexer import_lex; nasal_parse import_par; nasal_ast import_ast; @@ -26,7 +25,6 @@ public: nasal_import::nasal_import() { - import_src.clear(); import_lex.clear(); import_par.clear(); import_ast.clear(); @@ -43,7 +41,6 @@ void nasal_import::die(std::string filename,std::string error_stage) void nasal_import::init() { - import_src.clear(); import_lex.clear(); import_par.clear(); return; @@ -112,12 +109,8 @@ nasal_ast nasal_import::file_import(nasal_ast& node) return tmp; // start importing... - if(!import_src.input_file(filename)) - { - this->die(filename,"resource"); - return tmp; - } - import_lex.scanner(import_src.get_file()); + import_lex.openfile(filename); + import_lex.scanner(); if(import_lex.get_error()) { this->die(filename,"lexer"); diff --git a/nasal_lexer.h b/nasal_lexer.h index aa9faf9..81a0eaf 100644 --- a/nasal_lexer.h +++ b/nasal_lexer.h @@ -81,14 +81,19 @@ class nasal_lexer { private: int error; + int res_size; + int line; + int ptr; + std::vector res; std::vector token_list; - std::string identifier_gen(std::vector&,int&,int&); + std::string identifier_gen(); void generate_number_error(int,std::string); - std::string number_gen(std::vector&,int&,int&); - std::string string_gen(std::vector&,int&,int&); + std::string number_gen(); + std::string string_gen(); public: void clear(); - void scanner(std::vector&); + void openfile(std::string); + void scanner(); void print_token(); int get_error(); std::vector& get_token_list(); @@ -96,13 +101,41 @@ public: void nasal_lexer::clear() { + error=0; + res_size=0; + line=0; + ptr=0; + res.clear(); token_list.clear(); return; } -std::string nasal_lexer::identifier_gen(std::vector& res,int& ptr,int& line) +void nasal_lexer::openfile(std::string filename) +{ + error=0; + res.clear(); + std::ifstream fin(filename,std::ios::binary); + if(fin.fail()) + { + ++error; + std::cout<<">> [lexer] cannot open file \""<> [lexer] line "<& res,int& ptr,int& line) +std::string nasal_lexer::number_gen() { - int res_size=res.size(); bool scientific_notation=false;// numbers like 1e8 are scientific_notation std::string token_str=""; // generate hex number @@ -203,9 +235,8 @@ std::string nasal_lexer::number_gen(std::vector& res,int& ptr,int& line) return token_str; } -std::string nasal_lexer::string_gen(std::vector& res,int& ptr,int& line) +std::string nasal_lexer::string_gen() { - int res_size=res.size(); std::string token_str=""; char str_begin=res[ptr++]; if(ptr>=res_size) return token_str; @@ -246,11 +277,12 @@ std::string nasal_lexer::string_gen(std::vector& res,int& ptr,int& line) return token_str; } -void nasal_lexer::scanner(std::vector& res) +void nasal_lexer::scanner() { - error=0; token_list.clear(); - int line=1,ptr=0,res_size=res.size(); + line=1; + ptr=0; + std::string token_str; while(ptr& res) if(ptr>=res_size) break; if(IS_IDENTIFIER_HEAD(res[ptr])) { - token_str=identifier_gen(res,ptr,line); + token_str=identifier_gen(); token new_token; new_token.line=line; new_token.str=token_str; @@ -280,7 +312,7 @@ void nasal_lexer::scanner(std::vector& res) } else if(IS_DIGIT(res[ptr])) { - token_str=number_gen(res,ptr,line); + token_str=number_gen(); token new_token; new_token.line=line; new_token.str=token_str; @@ -289,7 +321,7 @@ void nasal_lexer::scanner(std::vector& res) } else if(IS_STRING_HEAD(res[ptr])) { - token_str=string_gen(res,ptr,line); + token_str=string_gen(); token new_token; new_token.line=line; new_token.type=tok_string; diff --git a/nasal_resource.h b/nasal_resource.h deleted file mode 100644 index d65a54d..0000000 --- a/nasal_resource.h +++ /dev/null @@ -1,75 +0,0 @@ -#ifndef __NASAL_RESOURCE_H__ -#define __NASAL_RESOURCE_H__ - -class nasal_resource -{ -private: - std::vector res; -public: - bool input_file(std::string); - void clear(); - void print_file(); - std::vector& get_file(); -}; - -bool nasal_resource::input_file(std::string filename) -{ - res.clear(); - std::ifstream fin(filename,std::ios::binary); - if(fin.fail()) - { - std::cout<<">> [resource] cannot open file \""<=0 && unicode_str.length()) - { - std::cout<& nasal_resource::get_file() -{ - return res; -} - -#endif \ No newline at end of file