#ifndef __NASAL_CODEGEN_H__ #define __NASAL_CODEGEN_H__ // unfinished // now it can output ast but it is not byte code yet // please wait... class nasal_codegen { private: std::vector number_list; std::vector string_list; std::vector symbol_list; int error; void init(); void output_int(unsigned int,std::ofstream&); void output_root(nasal_ast&,std::ofstream&); unsigned int input_int(std::ifstream&); void input_root(nasal_ast&,std::ifstream&); public: nasal_codegen(); int get_error(); void output_exec(std::string,nasal_ast&); void load_exec(std::string,nasal_ast&); }; nasal_codegen::nasal_codegen() { error=0; number_list.clear(); string_list.clear(); symbol_list.clear(); return; } int nasal_codegen::get_error() { return error; } void nasal_codegen::init() { error=0; number_list.clear(); string_list.clear(); symbol_list.clear(); return; } void nasal_codegen::output_int(unsigned int num,std::ofstream& fout) { unsigned int get_byte=0xff000000; int offset=24; for(int i=0;i<4;++i) { fout<<(unsigned char)((get_byte&num)>>offset); get_byte>>=8; offset-=8; } return; } void nasal_codegen::output_root(nasal_ast& root,std::ofstream& fout) { output_int(ast_begin,fout); unsigned int type=root.get_type(); output_int(type,fout); std::vector& ref_vec=root.get_children(); int root_children_size=ref_vec.size(); output_int(root_children_size,fout); if(type==ast_number || type==ast_string || type==ast_identifier || type==ast_dynamic_id || type==ast_call_hash) { std::string tmp=root.get_str(); for(int i=0;iinit(); std::ofstream fout(filename,std::ios::binary); if(fout.fail()) { ++error; std::cout<<">> [code] cannot generate file \""<> [code] file format error: without ast_begin."<> [code] file format error: without ast_end."<> [code] cannot open file\""<> [code] \""<