#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 file_header; std::vector output_file; std::vector string_table; int error; void init(); void output_int(unsigned int,std::vector&); void output_short(unsigned short,std::vector&); void output_root(nasal_ast&); unsigned int input_int(std::ifstream&); unsigned short input_short(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; string_table.clear(); return; } int nasal_codegen::get_error() { return error; } void nasal_codegen::init() { error=0; string_table.clear(); file_header.clear(); output_file.clear(); return; } void nasal_codegen::output_int(unsigned int num,std::vector& vec) { unsigned int tmp=0xff000000; unsigned int offset=24; for(int i=0;i<4;++i) { vec.push_back((unsigned char)((tmp&num)>>offset)); offset-=8; tmp>>=8; } return; } void nasal_codegen::output_short(unsigned short num,std::vector& vec) { vec.push_back((unsigned char)((num&0xff00)>>8)); vec.push_back((unsigned char)(num&0x00ff)); return; } void nasal_codegen::output_root(nasal_ast& root) { unsigned char type=(unsigned char)root.get_type(); output_file.push_back(type); std::vector& ref_vec=root.get_children(); unsigned short root_children_size=ref_vec.size(); output_short(root_children_size,output_file); if(type==ast_number || type==ast_string || type==ast_identifier || type==ast_dynamic_id || type==ast_call_hash) { std::string tmp=root.get_str(); if(type==ast_number) tmp=trans_number_to_string(root.get_num()); if(std::find(string_table.begin(),string_table.end(),tmp)==string_table.end()) { string_table.push_back(tmp); output_short(string_table.size()-1,output_file); } else { for(int i=0;iinit(); // put header input file_header unsigned char header[4]={0x40,0x56,0x4b,0x21}; for(int i=0;i<4;++i) file_header.push_back(header[i]); // main progress,put codes into output_file output_root(root); // put string table into file_header output_short(string_table.size(),file_header); for(int i=0;i> [code] cannot generate file \""<> [code] cannot open file\""<> [code] \""<