📝 code improvement

This commit is contained in:
ValKmjolnir 2024-06-05 23:38:48 +08:00
parent d535c8f7c4
commit f83b693f65
7 changed files with 104 additions and 105 deletions

View File

@ -185,7 +185,7 @@ bool ast_dumper::visit_binary_operator(binary_operator* node) {
case binary_operator::binary_type::leq: std::cout << "<="; break; case binary_operator::binary_type::leq: std::cout << "<="; break;
case binary_operator::binary_type::condition_and: std::cout << "and"; break; case binary_operator::binary_type::condition_and: std::cout << "and"; break;
case binary_operator::binary_type::condition_or: std::cout << "or"; break; case binary_operator::binary_type::condition_or: std::cout << "or"; break;
case binary_operator::binary_type::nullchain: std::cout << "??"; break; case binary_operator::binary_type::null_chain: std::cout << "??"; break;
} }
std::cout << "\"" << format_location(node); std::cout << "\"" << format_location(node);
push_indent(); push_indent();

View File

@ -23,7 +23,7 @@ enum class expr_type {
ast_pair, // pair of key and value in hashmap ast_pair, // pair of key and value in hashmap
ast_call, // mark a sub-tree of calling an identifier ast_call, // mark a sub-tree of calling an identifier
ast_callh, // id.name ast_callh, // id.name
ast_nullaccess, // id?.name ast_null_access, // id?.name
ast_callv, // id[index] ast_callv, // id[index]
ast_callf, // id() ast_callf, // id()
ast_subvec, // id[index:index] ast_subvec, // id[index:index]
@ -304,7 +304,7 @@ public:
bitwise_and, bitwise_and,
condition_and, condition_and,
condition_or, condition_or,
nullchain null_chain
}; };
private: private:
@ -397,7 +397,7 @@ private:
public: public:
null_access(const span& location, const std::string& name): null_access(const span& location, const std::string& name):
call(location, expr_type::ast_nullaccess), call(location, expr_type::ast_null_access),
field(name) {} field(name) {}
~null_access() override = default; ~null_access() override = default;
const std::string& get_field() const {return field;} const std::string& get_field() const {return field;}

View File

@ -339,7 +339,7 @@ void codegen::call_gen(call_expr* node) {
call_vector_gen(reinterpret_cast<call_vector*>(i)); break; call_vector_gen(reinterpret_cast<call_vector*>(i)); break;
case expr_type::ast_callf: case expr_type::ast_callf:
call_func_gen(reinterpret_cast<call_function*>(i)); break; call_func_gen(reinterpret_cast<call_function*>(i)); break;
case expr_type::ast_nullaccess: case expr_type::ast_null_access:
null_access_gen(reinterpret_cast<null_access*>(i)); break; null_access_gen(reinterpret_cast<null_access*>(i)); break;
default: break; default: break;
} }
@ -475,7 +475,7 @@ void codegen::mcall(expr* node) {
call_vector_gen(reinterpret_cast<call_vector*>(tmp)); break; call_vector_gen(reinterpret_cast<call_vector*>(tmp)); break;
case expr_type::ast_callf: case expr_type::ast_callf:
call_func_gen(reinterpret_cast<call_function*>(tmp)); break; call_func_gen(reinterpret_cast<call_function*>(tmp)); break;
case expr_type::ast_nullaccess: case expr_type::ast_null_access:
null_access_gen(reinterpret_cast<null_access*>(tmp)); break; null_access_gen(reinterpret_cast<null_access*>(tmp)); break;
default: break; default: break;
} }
@ -489,7 +489,7 @@ void codegen::mcall(expr* node) {
mcall_vec(reinterpret_cast<call_vector*>(tmp)); break; mcall_vec(reinterpret_cast<call_vector*>(tmp)); break;
case expr_type::ast_callf: case expr_type::ast_callf:
die("bad left-value: function call", tmp->get_location()); break; die("bad left-value: function call", tmp->get_location()); break;
case expr_type::ast_nullaccess: case expr_type::ast_null_access:
die("bad left-value: null access test", tmp->get_location()); break; die("bad left-value: null access test", tmp->get_location()); break;
default: default:
die("bad left-value: unknown call", tmp->get_location()); break; die("bad left-value: unknown call", tmp->get_location()); break;
@ -1083,7 +1083,7 @@ void codegen::binary_gen(binary_operator* node) {
calc_gen(node->get_right()); calc_gen(node->get_right());
emit(op_btand, 0, node->get_location()); emit(op_btand, 0, node->get_location());
return; return;
case binary_operator::binary_type::nullchain: case binary_operator::binary_type::null_chain:
null_chain_gen(node); null_chain_gen(node);
return; return;
default: break; default: break;

View File

@ -46,8 +46,9 @@ void operand_line_counter::dump_operand_count() const {
if (!rate) { if (!rate) {
break; break;
} }
std::clog << " " << operand_name_table[i.first] << " : "; std::clog << " ";
std::clog << i.second << " (" << rate << "%)\n"; std::clog << operand_name_table.at(static_cast<op_code_type>(i.first));
std::clog << " : " << i.second << " (" << rate << "%)\n";
} }
std::clog << " total : " << total << '\n'; std::clog << " total : " << total << '\n';
} }

View File

@ -3,97 +3,6 @@
namespace nasal { namespace nasal {
const char* operand_name_table[] = {
"exit ",
"repl ",
"intl ",
"loadg ",
"loadl ",
"loadu ",
"dup ",
"pnum ",
"pnil ",
"pstr ",
"newv ",
"newh ",
"newf ",
"happ ",
"para ",
"def ",
"dyn ",
"lnot ",
"usub ",
"bitnot",
"bitor ",
"bitxor",
"bitand",
"add ",
"sub ",
"mult ",
"div ",
"lnk ",
"addc ",
"subc ",
"multc ",
"divc ",
"lnkc ",
"addeq ",
"subeq ",
"muleq ",
"diveq ",
"lnkeq ",
"bandeq",
"boreq ",
"bxoreq",
"addeqc",
"subeqc",
"muleqc",
"diveqc",
"lnkeqc",
"addecp",
"subecp",
"mulecp",
"divecp",
"lnkecp",
"meq ",
"eq ",
"neq ",
"less ",
"leq ",
"grt ",
"geq ",
"lessc ",
"leqc ",
"grtc ",
"geqc ",
"pop ",
"jmp ",
"jt ",
"jf ",
"cnt ",
"findx ",
"feach ",
"callg ",
"calll ",
"upval ",
"callv ",
"callvi",
"callh ",
"callfv",
"callfh",
"callb ",
"slcbeg",
"slcend",
"slice ",
"slice2",
"mcallg",
"mcalll",
"mupval",
"mcallv",
"mcallh",
"ret "
};
void codestream::set(const f64* number_list, void codestream::set(const f64* number_list,
const std::string* string_list, const std::string* string_list,
const nasal_builtin_table* native_table, const nasal_builtin_table* native_table,
@ -124,7 +33,7 @@ void codestream::dump(std::ostream& out) const {
} }
// dump operand name // dump operand name
out << " " << operand_name_table[op] << " "; out << " " << operand_name_table.at(static_cast<op_code_type>(op)) << " ";
switch(op) { switch(op) {
case op_addeq: case op_addeq:

View File

@ -98,6 +98,97 @@ enum op_code_type: u8 {
op_ret // return op_ret // return
}; };
static std::unordered_map<op_code_type, std::string> operand_name_table = {
{op_code_type::op_exit, "exit "},
{op_code_type::op_repl, "repl "},
{op_code_type::op_intl, "intl "},
{op_code_type::op_loadg, "loadg "},
{op_code_type::op_loadl, "loadl "},
{op_code_type::op_loadu, "loadu "},
{op_code_type::op_dup, "dup "},
{op_code_type::op_pnum, "pnum "},
{op_code_type::op_pnil, "pnil "},
{op_code_type::op_pstr, "pstr "},
{op_code_type::op_newv, "newv "},
{op_code_type::op_newh, "newh "},
{op_code_type::op_newf, "newf "},
{op_code_type::op_happ, "happ "},
{op_code_type::op_para, "para "},
{op_code_type::op_deft, "def "},
{op_code_type::op_dyn, "dyn "},
{op_code_type::op_lnot, "lnot "},
{op_code_type::op_usub, "usub "},
{op_code_type::op_bnot, "bitnot"},
{op_code_type::op_btor, "bitor "},
{op_code_type::op_btxor, "bitxor"},
{op_code_type::op_btand, "bitand"},
{op_code_type::op_add, "add "},
{op_code_type::op_sub, "sub "},
{op_code_type::op_mul, "mult "},
{op_code_type::op_div, "div "},
{op_code_type::op_lnk, "lnk "},
{op_code_type::op_addc, "addc "},
{op_code_type::op_subc, "subc "},
{op_code_type::op_mulc, "multc "},
{op_code_type::op_divc, "divc "},
{op_code_type::op_lnkc, "lnkc "},
{op_code_type::op_addeq, "addeq "},
{op_code_type::op_subeq, "subeq "},
{op_code_type::op_muleq, "muleq "},
{op_code_type::op_diveq, "diveq "},
{op_code_type::op_lnkeq, "lnkeq "},
{op_code_type::op_btandeq, "bandeq"},
{op_code_type::op_btoreq, "boreq "},
{op_code_type::op_btxoreq, "bxoreq"},
{op_code_type::op_addeqc, "addeqc"},
{op_code_type::op_subeqc, "subeqc"},
{op_code_type::op_muleqc, "muleqc"},
{op_code_type::op_diveqc, "diveqc"},
{op_code_type::op_lnkeqc, "lnkeqc"},
{op_code_type::op_addecp, "addecp"},
{op_code_type::op_subecp, "subecp"},
{op_code_type::op_mulecp, "mulecp"},
{op_code_type::op_divecp, "divecp"},
{op_code_type::op_lnkecp, "lnkecp"},
{op_code_type::op_meq, "meq "},
{op_code_type::op_eq, "eq "},
{op_code_type::op_neq, "neq "},
{op_code_type::op_less, "less "},
{op_code_type::op_leq, "leq "},
{op_code_type::op_grt, "grt "},
{op_code_type::op_geq, "geq "},
{op_code_type::op_lessc, "lessc "},
{op_code_type::op_leqc, "leqc "},
{op_code_type::op_grtc, "grtc "},
{op_code_type::op_geqc, "geqc "},
{op_code_type::op_pop, "pop "},
{op_code_type::op_jmp, "jmp "},
{op_code_type::op_jt, "jt "},
{op_code_type::op_jf, "jf "},
{op_code_type::op_cnt, "cnt "},
{op_code_type::op_findex, "findx "},
{op_code_type::op_feach, "feach "},
{op_code_type::op_callg, "callg "},
{op_code_type::op_calll, "calll "},
{op_code_type::op_upval, "upval "},
{op_code_type::op_callv, "callv "},
{op_code_type::op_callvi, "callvi"},
{op_code_type::op_callh, "callh "},
{op_code_type::op_callfv, "callfv"},
{op_code_type::op_callfh, "callfh"},
{op_code_type::op_callb, "callb "},
{op_code_type::op_slcbeg, "slcbeg"},
{op_code_type::op_slcend, "slcend"},
{op_code_type::op_slc, "slice "},
{op_code_type::op_slc2, "slice2"},
{op_code_type::op_mcallg, "mcallg"},
{op_code_type::op_mcalll, "mcalll"},
{op_code_type::op_mupval, "mupval"},
{op_code_type::op_mcallv, "mcallv"},
{op_code_type::op_mcallh, "mcallh"},
{op_code_type::op_ret, "ret "}
};
struct opcode { struct opcode {
u8 op; // opcode u8 op; // opcode
u16 fidx; // source code file index u16 fidx; // source code file index
@ -128,6 +219,4 @@ public:
std::ostream& operator<<(std::ostream&, const codestream&); std::ostream& operator<<(std::ostream&, const codestream&);
extern const char* operand_name_table[];
} }

View File

@ -595,7 +595,7 @@ expr* parse::null_chain_expr() {
auto node = additive_expr(); auto node = additive_expr();
while(lookahead(tok::tk_quesques)) { while(lookahead(tok::tk_quesques)) {
auto tmp = new binary_operator(toks[ptr].loc); auto tmp = new binary_operator(toks[ptr].loc);
tmp->set_operator_type(binary_operator::binary_type::nullchain); tmp->set_operator_type(binary_operator::binary_type::null_chain);
tmp->set_left(node); tmp->set_left(node);
match(tok::tk_quesques); match(tok::tk_quesques);
tmp->set_right(additive_expr()); tmp->set_right(additive_expr());