use reinterpret_cast and static_cast

This commit is contained in:
ValKmjolnir 2023-11-05 21:25:20 +08:00
parent 336139dcae
commit e8c8a6446b
4 changed files with 193 additions and 150 deletions

View File

@ -309,8 +309,8 @@ var builtin_substr(context* ctx, gc* ngc) {
if (len.type!=vm_num || len.num()<0) { if (len.type!=vm_num || len.num()<0) {
return nas_err("substr", "\"length\" should be number >= 0"); return nas_err("substr", "\"length\" should be number >= 0");
} }
usize begin = (usize)beg.num(); auto begin = static_cast<usize>(beg.num());
usize length = (usize)len.num(); auto length = static_cast<usize>(len.num());
if (begin>=str.str().length()) { if (begin>=str.str().length()) {
return nas_err("susbtr", "begin index out of range: "+std::to_string(begin)); return nas_err("susbtr", "begin index out of range: "+std::to_string(begin));
} }
@ -397,7 +397,7 @@ var builtin_chr(context* ctx, gc* ngc) {
}; };
auto num = static_cast<i32>(ctx->localr[1].num()); auto num = static_cast<i32>(ctx->localr[1].num());
if (0<=num && num<128) { if (0<=num && num<128) {
return ngc->newstr((char)num); return ngc->newstr(static_cast<char>(num));
} else if (128<=num && num<256) { } else if (128<=num && num<256) {
return ngc->newstr(extend[num-128]); return ngc->newstr(extend[num-128]);
} }

View File

@ -58,7 +58,7 @@ void codegen::check_id_exist(identifier* node) {
); );
} }
void codegen::regist_num(const f64 num) { void codegen::regist_number(const f64 num) {
if (const_number_map.count(num)) { if (const_number_map.count(num)) {
return; return;
} }
@ -67,7 +67,7 @@ void codegen::regist_num(const f64 num) {
const_number_table.push_back(num); const_number_table.push_back(num);
} }
void codegen::regist_str(const std::string& str) { void codegen::regist_string(const std::string& str) {
if (const_string_map.count(str)) { if (const_string_map.count(str)) {
return; return;
} }
@ -94,11 +94,11 @@ void codegen::find_symbol(code_block* node) {
scope.insert(i.name); scope.insert(i.name);
} }
// add symbol for codegen symbol check // add symbol for codegen symbol check
add_symbol(i.name); regist_symbol(i.name);
} }
} }
void codegen::add_symbol(const std::string& name) { void codegen::regist_symbol(const std::string& name) {
if (local.empty()) { if (local.empty()) {
if (global.count(name)) { if (global.count(name)) {
return; return;
@ -150,25 +150,25 @@ void codegen::emit(u8 operation_code, u32 immediate_num, const span& location) {
}); });
} }
void codegen::num_gen(number_literal* node) { void codegen::number_gen(number_literal* node) {
f64 num = node->get_number(); f64 num = node->get_number();
regist_num(num); regist_number(num);
emit(op_pnum, const_number_map.at(num), node->get_location()); emit(op_pnum, const_number_map.at(num), node->get_location());
} }
void codegen::str_gen(string_literal* node) { void codegen::string_gen(string_literal* node) {
const auto& str = node->get_content(); const auto& str = node->get_content();
regist_str(str); regist_string(str);
emit(op_pstr, const_string_map.at(str), node->get_location()); emit(op_pstr, const_string_map.at(str), node->get_location());
} }
void codegen::bool_gen(bool_literal* node) { void codegen::bool_gen(bool_literal* node) {
f64 num = node->get_flag()? 1:0; f64 num = node->get_flag()? 1:0;
regist_num(num); regist_number(num);
emit(op_pnum, const_number_map.at(num), node->get_location()); emit(op_pnum, const_number_map.at(num), node->get_location());
} }
void codegen::vec_gen(vector_expr* node) { void codegen::vector_gen(vector_expr* node) {
for(auto child : node->get_elements()) { for(auto child : node->get_elements()) {
calc_gen(child); calc_gen(child);
} }
@ -180,7 +180,7 @@ void codegen::hash_gen(hash_expr* node) {
for(auto child : node->get_members()) { for(auto child : node->get_members()) {
calc_gen(child->get_value()); calc_gen(child->get_value());
const auto& field_name = child->get_name(); const auto& field_name = child->get_name();
regist_str(field_name); regist_string(field_name);
emit(op_happ, const_string_map.at(field_name), child->get_location()); emit(op_happ, const_string_map.at(field_name), child->get_location());
} }
} }
@ -243,7 +243,7 @@ void codegen::func_gen(function* node) {
tmp->get_location() tmp->get_location()
); );
} }
regist_str(name); regist_string(name);
switch(tmp->get_parameter_type()) { switch(tmp->get_parameter_type()) {
case parameter::param_type::normal_parameter: case parameter::param_type::normal_parameter:
emit(op_para, const_string_map.at(name), tmp->get_location()); emit(op_para, const_string_map.at(name), tmp->get_location());
@ -256,7 +256,7 @@ void codegen::func_gen(function* node) {
emit(op_dyn, const_string_map.at(name), tmp->get_location()); emit(op_dyn, const_string_map.at(name), tmp->get_location());
break; break;
} }
add_symbol(name); regist_symbol(name);
} }
code[newf].num = code.size()+1; // entry code[newf].num = code.size()+1; // entry
@ -282,7 +282,7 @@ void codegen::func_gen(function* node) {
while(local_symbol_find(arg)>=0) { while(local_symbol_find(arg)>=0) {
arg = "0" + arg; arg = "0" + arg;
} }
add_symbol(arg); regist_symbol(arg);
// generate code block // generate code block
in_foreach_loop_level.push_back(0); in_foreach_loop_level.push_back(0);
@ -323,7 +323,7 @@ void codegen::call_gen(call_expr* node) {
} }
} }
void codegen::call_id(identifier* node) { void codegen::call_identifier(identifier* node) {
const auto& name = node->get_name(); const auto& name = node->get_name();
if (native_function_mapper.count(name)) { if (native_function_mapper.count(name)) {
emit(op_callb, emit(op_callb,
@ -357,7 +357,7 @@ void codegen::call_id(identifier* node) {
} }
void codegen::call_hash_gen(call_hash* node) { void codegen::call_hash_gen(call_hash* node) {
regist_str(node->get_field()); regist_string(node->get_field());
emit(op_callh, const_string_map.at(node->get_field()), node->get_location()); emit(op_callh, const_string_map.at(node->get_field()), node->get_location());
} }
@ -391,7 +391,7 @@ void codegen::call_func_gen(call_function* node) {
auto pair_node = reinterpret_cast<hash_pair*>(child); auto pair_node = reinterpret_cast<hash_pair*>(child);
calc_gen(pair_node->get_value()); calc_gen(pair_node->get_value());
const auto& field_name = pair_node->get_name(); const auto& field_name = pair_node->get_name();
regist_str(field_name); regist_string(field_name);
emit(op_happ, const_string_map.at(field_name), child->get_location()); emit(op_happ, const_string_map.at(field_name), child->get_location());
} }
emit(op_callfh, 0, node->get_location()); emit(op_callfh, 0, node->get_location());
@ -419,7 +419,7 @@ void codegen::mcall(expr* node) {
} }
// generate symbol call if node is just ast_id and return // generate symbol call if node is just ast_id and return
if (node->get_type()==expr_type::ast_id) { if (node->get_type()==expr_type::ast_id) {
mcall_id(reinterpret_cast<identifier*>(node)); mcall_identifier(reinterpret_cast<identifier*>(node));
return; return;
} }
// generate call expression until the last sub-node // generate call expression until the last sub-node
@ -428,17 +428,22 @@ void codegen::mcall(expr* node) {
for(usize i = 0; i<call_node->get_calls().size()-1; ++i) { for(usize i = 0; i<call_node->get_calls().size()-1; ++i) {
auto tmp = call_node->get_calls()[i]; auto tmp = call_node->get_calls()[i];
switch(tmp->get_type()) { switch(tmp->get_type()) {
case expr_type::ast_callh: call_hash_gen((call_hash*)tmp); break; case expr_type::ast_callh:
case expr_type::ast_callv: call_vector_gen((call_vector*)tmp); break; call_hash_gen(reinterpret_cast<call_hash*>(tmp)); break;
case expr_type::ast_callf: call_func_gen((call_function*)tmp); break; case expr_type::ast_callv:
call_vector_gen(reinterpret_cast<call_vector*>(tmp)); break;
case expr_type::ast_callf:
call_func_gen(reinterpret_cast<call_function*>(tmp)); break;
default: break; default: break;
} }
} }
// the last sub-node will be used to generate memory call expression // the last sub-node will be used to generate memory call expression
auto tmp = call_node->get_calls().back(); auto tmp = call_node->get_calls().back();
switch(tmp->get_type()) { switch(tmp->get_type()) {
case expr_type::ast_callh: mcall_hash((call_hash*)tmp); break; case expr_type::ast_callh:
case expr_type::ast_callv: mcall_vec((call_vector*)tmp); break; mcall_hash(reinterpret_cast<call_hash*>(tmp)); break;
case expr_type::ast_callv:
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;
default: default:
@ -446,7 +451,7 @@ void codegen::mcall(expr* node) {
} }
} }
void codegen::mcall_id(identifier* node) { void codegen::mcall_identifier(identifier* node) {
const auto& name = node->get_name(); const auto& name = node->get_name();
if (native_function_mapper.count(name)) { if (native_function_mapper.count(name)) {
die("cannot modify native function", node->get_location()); die("cannot modify native function", node->get_location());
@ -484,7 +489,7 @@ void codegen::mcall_vec(call_vector* node) {
} }
void codegen::mcall_hash(call_hash* node) { void codegen::mcall_hash(call_hash* node) {
regist_str(node->get_field()); regist_string(node->get_field());
emit(op_mcallh, const_string_map.at(node->get_field()), node->get_location()); emit(op_mcallh, const_string_map.at(node->get_field()), node->get_location());
} }
@ -509,12 +514,16 @@ void codegen::multi_def(definition_expr* node) {
if (node->get_tuple()) { if (node->get_tuple()) {
auto& vals = node->get_tuple()->get_elements(); auto& vals = node->get_tuple()->get_elements();
if (identifiers.size()>vals.size()) { if (identifiers.size()>vals.size()) {
die("lack values in multi-definition", die("lack values in multi-definition, expect " +
std::to_string(identifiers.size()) + " but get " +
std::to_string(vals.size()),
node->get_tuple()->get_location() node->get_tuple()->get_location()
); );
return; return;
} else if (identifiers.size()<vals.size()) { } else if (identifiers.size()<vals.size()) {
die("too many values in multi-definition", die("too many values in multi-definition, expect " +
std::to_string(identifiers.size()) + " but get " +
std::to_string(vals.size()),
node->get_tuple()->get_location() node->get_tuple()->get_location()
); );
return; return;
@ -540,7 +549,7 @@ void codegen::multi_def(definition_expr* node) {
emit(op_pop, 0, node->get_location()); emit(op_pop, 0, node->get_location());
} }
void codegen::def_gen(definition_expr* node) { void codegen::definition_gen(definition_expr* node) {
if (node->get_variable_name() && node->get_tuple()) { if (node->get_variable_name() && node->get_tuple()) {
die("cannot accept too many values", node->get_value()->get_location()); die("cannot accept too many values", node->get_value()->get_location());
} }
@ -562,8 +571,9 @@ void codegen::assignment_expression(assignment_expr* node) {
if (node->get_right()->get_type()!=expr_type::ast_num) { if (node->get_right()->get_type()!=expr_type::ast_num) {
emit(op_addeq, 0, node->get_location()); emit(op_addeq, 0, node->get_location());
} else { } else {
auto num = ((number_literal*)node->get_right())->get_number(); auto num = reinterpret_cast<number_literal*>(node->get_right())
regist_num(num); ->get_number();
regist_number(num);
emit(op_addeqc, const_number_map[num], node->get_location()); emit(op_addeqc, const_number_map[num], node->get_location());
} }
break; break;
@ -575,8 +585,9 @@ void codegen::assignment_expression(assignment_expr* node) {
if (node->get_right()->get_type()!=expr_type::ast_num) { if (node->get_right()->get_type()!=expr_type::ast_num) {
emit(op_subeq, 0, node->get_location()); emit(op_subeq, 0, node->get_location());
} else { } else {
auto num = ((number_literal*)node->get_right())->get_number(); auto num = reinterpret_cast<number_literal*>(node->get_right())
regist_num(num); ->get_number();
regist_number(num);
emit(op_subeqc, const_number_map[num], node->get_location()); emit(op_subeqc, const_number_map[num], node->get_location());
} }
break; break;
@ -588,8 +599,9 @@ void codegen::assignment_expression(assignment_expr* node) {
if (node->get_right()->get_type()!=expr_type::ast_num) { if (node->get_right()->get_type()!=expr_type::ast_num) {
emit(op_muleq, 0, node->get_location()); emit(op_muleq, 0, node->get_location());
} else { } else {
auto num = ((number_literal*)node->get_right())->get_number(); auto num = reinterpret_cast<number_literal*>(node->get_right())
regist_num(num); ->get_number();
regist_number(num);
emit(op_muleqc, const_number_map[num], node->get_location()); emit(op_muleqc, const_number_map[num], node->get_location());
} }
break; break;
@ -601,8 +613,9 @@ void codegen::assignment_expression(assignment_expr* node) {
if (node->get_right()->get_type()!=expr_type::ast_num) { if (node->get_right()->get_type()!=expr_type::ast_num) {
emit(op_diveq, 0, node->get_location()); emit(op_diveq, 0, node->get_location());
} else { } else {
auto num = ((number_literal*)node->get_right())->get_number(); auto num = reinterpret_cast<number_literal*>(node->get_right())
regist_num(num); ->get_number();
regist_number(num);
emit(op_diveqc, const_number_map[num], node->get_location()); emit(op_diveqc, const_number_map[num], node->get_location());
} }
break; break;
@ -614,8 +627,9 @@ void codegen::assignment_expression(assignment_expr* node) {
if (node->get_right()->get_type()!=expr_type::ast_str) { if (node->get_right()->get_type()!=expr_type::ast_str) {
emit(op_lnkeq, 0, node->get_location()); emit(op_lnkeq, 0, node->get_location());
} else { } else {
const auto& str = ((string_literal*)node->get_right())->get_content(); const auto& str = reinterpret_cast<string_literal*>(
regist_str(str); node->get_right())->get_content();
regist_string(str);
emit(op_lnkeqc, const_string_map[str], node->get_location()); emit(op_lnkeqc, const_string_map[str], node->get_location());
} }
break; break;
@ -655,7 +669,7 @@ void codegen::gen_assignment_equal_statement(assignment_expr* node) {
// generate symbol load // generate symbol load
calc_gen(node->get_right()); calc_gen(node->get_right());
// get memory space of left identifier // get memory space of left identifier
mcall_id(reinterpret_cast<identifier*>(node->get_left())); mcall_identifier(reinterpret_cast<identifier*>(node->get_left()));
// check memory get operand type and replace it with load operand // check memory get operand type and replace it with load operand
switch(code.back().op) { switch(code.back().op) {
case op_mcallg: code.back().op = op_loadg; break; case op_mcallg: code.back().op = op_loadg; break;
@ -802,9 +816,12 @@ void codegen::loop_gen(expr* node) {
break_ptr.push_front({}); break_ptr.push_front({});
switch(node->get_type()) { switch(node->get_type()) {
case expr_type::ast_while: while_gen((while_expr*)node); break; case expr_type::ast_while:
case expr_type::ast_for: for_gen((for_expr*)node); break; while_gen(reinterpret_cast<while_expr*>(node)); break;
case expr_type::ast_forei: forei_gen((forei_expr*)node); break; case expr_type::ast_for:
for_gen(reinterpret_cast<for_expr*>(node)); break;
case expr_type::ast_forei:
forei_gen(reinterpret_cast<forei_expr*>(node)); break;
default: break; default: break;
} }
} }
@ -836,7 +853,7 @@ void codegen::for_gen(for_expr* node) {
statement_generation(node->get_initial()); statement_generation(node->get_initial());
usize jmp_place = code.size(); usize jmp_place = code.size();
if (node->get_condition()->get_type()==expr_type::ast_null) { if (node->get_condition()->get_type()==expr_type::ast_null) {
regist_num(1); regist_number(1);
emit(op_pnum, const_number_map.at(1), node->get_condition()->get_location()); emit(op_pnum, const_number_map.at(1), node->get_condition()->get_location());
} else { } else {
calc_gen(node->get_condition()); calc_gen(node->get_condition());
@ -900,11 +917,11 @@ void codegen::statement_generation(expr* node) {
switch(node->get_type()) { switch(node->get_type()) {
case expr_type::ast_null: break; case expr_type::ast_null: break;
case expr_type::ast_def: case expr_type::ast_def:
def_gen((definition_expr*)node); break; definition_gen(reinterpret_cast<definition_expr*>(node)); break;
case expr_type::ast_multi_assign: case expr_type::ast_multi_assign:
multi_assign_gen((multi_assign*)node); break; multi_assign_gen(reinterpret_cast<multi_assign*>(node)); break;
case expr_type::ast_assign: case expr_type::ast_assign:
assignment_statement((assignment_expr*)node); break; assignment_statement(reinterpret_cast<assignment_expr*>(node)); break;
case expr_type::ast_nil: case expr_type::ast_nil:
case expr_type::ast_num: case expr_type::ast_num:
case expr_type::ast_str: case expr_type::ast_str:
@ -963,7 +980,7 @@ void codegen::and_gen(binary_operator* node) {
void codegen::unary_gen(unary_operator* node) { void codegen::unary_gen(unary_operator* node) {
// generate optimized result // generate optimized result
if (node->get_optimized_number()) { if (node->get_optimized_number()) {
num_gen(node->get_optimized_number()); number_gen(node->get_optimized_number());
return; return;
} }
@ -981,11 +998,11 @@ void codegen::unary_gen(unary_operator* node) {
void codegen::binary_gen(binary_operator* node) { void codegen::binary_gen(binary_operator* node) {
// generate optimized result // generate optimized result
if (node->get_optimized_number()) { if (node->get_optimized_number()) {
num_gen(node->get_optimized_number()); number_gen(node->get_optimized_number());
return; return;
} }
if (node->get_optimized_string()) { if (node->get_optimized_string()) {
str_gen(node->get_optimized_string()); string_gen(node->get_optimized_string());
return; return;
} }
@ -1029,8 +1046,9 @@ void codegen::binary_gen(binary_operator* node) {
calc_gen(node->get_right()); calc_gen(node->get_right());
emit(op_add, 0, node->get_location()); emit(op_add, 0, node->get_location());
} else { } else {
auto num = ((number_literal*)node->get_right())->get_number(); auto num = reinterpret_cast<number_literal*>(node->get_right())
regist_num(num); ->get_number();
regist_number(num);
emit(op_addc, const_number_map.at(num), node->get_location()); emit(op_addc, const_number_map.at(num), node->get_location());
} }
return; return;
@ -1040,8 +1058,9 @@ void codegen::binary_gen(binary_operator* node) {
calc_gen(node->get_right()); calc_gen(node->get_right());
emit(op_sub, 0, node->get_location()); emit(op_sub, 0, node->get_location());
} else { } else {
auto num = ((number_literal*)node->get_right())->get_number(); auto num = reinterpret_cast<number_literal*>(node->get_right())
regist_num(num); ->get_number();
regist_number(num);
emit(op_subc, const_number_map.at(num), node->get_location()); emit(op_subc, const_number_map.at(num), node->get_location());
} }
return; return;
@ -1051,8 +1070,9 @@ void codegen::binary_gen(binary_operator* node) {
calc_gen(node->get_right()); calc_gen(node->get_right());
emit(op_mul, 0, node->get_location()); emit(op_mul, 0, node->get_location());
} else { } else {
auto num = ((number_literal*)node->get_right())->get_number(); auto num = reinterpret_cast<number_literal*>(node->get_right())
regist_num(num); ->get_number();
regist_number(num);
emit(op_mulc, const_number_map.at(num), node->get_location()); emit(op_mulc, const_number_map.at(num), node->get_location());
} }
return; return;
@ -1062,8 +1082,9 @@ void codegen::binary_gen(binary_operator* node) {
calc_gen(node->get_right()); calc_gen(node->get_right());
emit(op_div, 0, node->get_location()); emit(op_div, 0, node->get_location());
} else { } else {
auto num = ((number_literal*)node->get_right())->get_number(); auto num = reinterpret_cast<number_literal*>(node->get_right())
regist_num(num); ->get_number();
regist_number(num);
emit(op_divc, const_number_map.at(num), node->get_location()); emit(op_divc, const_number_map.at(num), node->get_location());
} }
return; return;
@ -1073,8 +1094,9 @@ void codegen::binary_gen(binary_operator* node) {
calc_gen(node->get_right()); calc_gen(node->get_right());
emit(op_lnk, 0, node->get_location()); emit(op_lnk, 0, node->get_location());
} else { } else {
const auto& str = ((string_literal*)node->get_right())->get_content(); const auto& str = reinterpret_cast<string_literal*>(
regist_str(str); node->get_right())->get_content();
regist_string(str);
emit(op_lnkc, const_string_map.at(str), node->get_location()); emit(op_lnkc, const_string_map.at(str), node->get_location());
} }
break; break;
@ -1084,8 +1106,9 @@ void codegen::binary_gen(binary_operator* node) {
calc_gen(node->get_right()); calc_gen(node->get_right());
emit(op_less, 0, node->get_location()); emit(op_less, 0, node->get_location());
} else { } else {
auto num = ((number_literal*)node->get_right())->get_number(); auto num = reinterpret_cast<number_literal*>(node->get_right())
regist_num(num); ->get_number();
regist_number(num);
emit(op_lessc, const_number_map.at(num), node->get_location()); emit(op_lessc, const_number_map.at(num), node->get_location());
} }
return; return;
@ -1095,8 +1118,9 @@ void codegen::binary_gen(binary_operator* node) {
calc_gen(node->get_right()); calc_gen(node->get_right());
emit(op_leq, 0, node->get_location()); emit(op_leq, 0, node->get_location());
} else { } else {
auto num = ((number_literal*)node->get_right())->get_number(); auto num = reinterpret_cast<number_literal*>(node->get_right())
regist_num(num); ->get_number();
regist_number(num);
emit(op_leqc, const_number_map.at(num), node->get_location()); emit(op_leqc, const_number_map.at(num), node->get_location());
} }
return; return;
@ -1106,8 +1130,9 @@ void codegen::binary_gen(binary_operator* node) {
calc_gen(node->get_right()); calc_gen(node->get_right());
emit(op_grt, 0, node->get_location()); emit(op_grt, 0, node->get_location());
} else { } else {
auto num = ((number_literal*)node->get_right())->get_number(); auto num = reinterpret_cast<number_literal*>(node->get_right())
regist_num(num); ->get_number();
regist_number(num);
emit(op_grtc, const_number_map.at(num), node->get_location()); emit(op_grtc, const_number_map.at(num), node->get_location());
} }
return; return;
@ -1117,8 +1142,9 @@ void codegen::binary_gen(binary_operator* node) {
calc_gen(node->get_right()); calc_gen(node->get_right());
emit(op_geq, 0, node->get_location()); emit(op_geq, 0, node->get_location());
} else { } else {
auto num = ((number_literal*)node->get_right())->get_number(); auto num = reinterpret_cast<number_literal*>(node->get_right())
regist_num(num); ->get_number();
regist_number(num);
emit(op_geqc, const_number_map.at(num), node->get_location()); emit(op_geqc, const_number_map.at(num), node->get_location());
} }
return; return;
@ -1143,33 +1169,38 @@ void codegen::calc_gen(expr* node) {
case expr_type::ast_nil: case expr_type::ast_nil:
emit(op_pnil, 0, node->get_location()); break; emit(op_pnil, 0, node->get_location()); break;
case expr_type::ast_num: case expr_type::ast_num:
num_gen((number_literal*)node); break; number_gen(reinterpret_cast<number_literal*>(node)); break;
case expr_type::ast_str: case expr_type::ast_str:
str_gen((string_literal*)node); break; string_gen(reinterpret_cast<string_literal*>(node)); break;
case expr_type::ast_id: case expr_type::ast_id:
call_id((identifier*)node); break; call_identifier(reinterpret_cast<identifier*>(node)); break;
case expr_type::ast_bool: case expr_type::ast_bool:
bool_gen((bool_literal*)node); break; bool_gen(reinterpret_cast<bool_literal*>(node)); break;
case expr_type::ast_vec: case expr_type::ast_vec:
vec_gen((vector_expr*)node); break; vector_gen(reinterpret_cast<vector_expr*>(node)); break;
case expr_type::ast_hash: case expr_type::ast_hash:
hash_gen((hash_expr*)node); break; hash_gen(reinterpret_cast<hash_expr*>(node)); break;
case expr_type::ast_func: case expr_type::ast_func:
func_gen((function*)node); break; func_gen(reinterpret_cast<function*>(node)); break;
case expr_type::ast_call: case expr_type::ast_call:
call_gen((call_expr*)node); break; call_gen(reinterpret_cast<call_expr*>(node)); break;
case expr_type::ast_assign: case expr_type::ast_assign:
assignment_expression((assignment_expr*)node); break; assignment_expression(
reinterpret_cast<assignment_expr*>(node)
);
break;
case expr_type::ast_ternary: case expr_type::ast_ternary:
trino_gen((ternary_operator*)node); break; trino_gen(reinterpret_cast<ternary_operator*>(node)); break;
case expr_type::ast_unary: case expr_type::ast_unary:
unary_gen((unary_operator*)node); break; unary_gen(reinterpret_cast<unary_operator*>(node)); break;
case expr_type::ast_binary: case expr_type::ast_binary:
binary_gen((binary_operator*)node); break; binary_gen(reinterpret_cast<binary_operator*>(node)); break;
case expr_type::ast_def: case expr_type::ast_def:
// definition in calculation only should be single def // definition in calculation only should be single def
single_def((definition_expr*)node); single_def(reinterpret_cast<definition_expr*>(node));
call_id(((definition_expr*)node)->get_variable_name()); call_identifier(
(reinterpret_cast<definition_expr*>(node))->get_variable_name()
);
break; break;
default: break; default: break;
} }
@ -1177,11 +1208,16 @@ void codegen::calc_gen(expr* node) {
void codegen::repl_mode_info_output_gen(expr* node) { void codegen::repl_mode_info_output_gen(expr* node) {
switch(node->get_type()) { switch(node->get_type()) {
case expr_type::ast_id: call_id((identifier*)node); break; case expr_type::ast_id:
case expr_type::ast_nil: emit(op_pnil, 0, node->get_location()); break; call_identifier(reinterpret_cast<identifier*>(node)); break;
case expr_type::ast_num: num_gen((number_literal*)node); break; case expr_type::ast_nil:
case expr_type::ast_str: str_gen((string_literal*)node); break; emit(op_pnil, 0, node->get_location()); break;
case expr_type::ast_bool: bool_gen((bool_literal*)node); break; case expr_type::ast_num:
number_gen(reinterpret_cast<number_literal*>(node)); break;
case expr_type::ast_str:
string_gen(reinterpret_cast<string_literal*>(node)); break;
case expr_type::ast_bool:
bool_gen(reinterpret_cast<bool_literal*>(node)); break;
default: return; default: return;
} }
// generate repl output operand // generate repl output operand
@ -1273,9 +1309,9 @@ const error& codegen::compile(parse& parse, linker& import, bool repl_flag) {
in_foreach_loop_level.push_back(0); in_foreach_loop_level.push_back(0);
// add special symbol globals, which is a hash stores all global variables // add special symbol globals, which is a hash stores all global variables
add_symbol("globals"); regist_symbol("globals");
// add special symbol arg here, which is used to store command line args // add special symbol arg here, which is used to store command line args
add_symbol("arg"); regist_symbol("arg");
// search global symbols first // search global symbols first
find_symbol(parse.tree()); find_symbol(parse.tree());

View File

@ -78,34 +78,34 @@ private:
err.err("code", loc, info); err.err("code", loc, info);
} }
void regist_num(const f64); void regist_number(const f64);
void regist_str(const std::string&); void regist_string(const std::string&);
void find_symbol(code_block*); void find_symbol(code_block*);
void add_symbol(const std::string&); void regist_symbol(const std::string&);
i32 local_symbol_find(const std::string&); i32 local_symbol_find(const std::string&);
i32 global_symbol_find(const std::string&); i32 global_symbol_find(const std::string&);
i32 upvalue_symbol_find(const std::string&); i32 upvalue_symbol_find(const std::string&);
void emit(u8, u32, const span&); void emit(u8, u32, const span&);
void num_gen(number_literal*); void number_gen(number_literal*);
void str_gen(string_literal*); void string_gen(string_literal*);
void bool_gen(bool_literal*); void bool_gen(bool_literal*);
void vec_gen(vector_expr*); void vector_gen(vector_expr*);
void hash_gen(hash_expr*); void hash_gen(hash_expr*);
void func_gen(function*); void func_gen(function*);
void call_gen(call_expr*); void call_gen(call_expr*);
void call_id(identifier*); void call_identifier(identifier*);
void call_hash_gen(call_hash*); void call_hash_gen(call_hash*);
void call_vector_gen(call_vector*); void call_vector_gen(call_vector*);
void call_func_gen(call_function*); void call_func_gen(call_function*);
void mcall(expr*); void mcall(expr*);
void mcall_id(identifier*); void mcall_identifier(identifier*);
void mcall_vec(call_vector*); void mcall_vec(call_vector*);
void mcall_hash(call_hash*); void mcall_hash(call_hash*);
void multi_def(definition_expr*); void multi_def(definition_expr*);
void single_def(definition_expr*); void single_def(definition_expr*);
void def_gen(definition_expr*); void definition_gen(definition_expr*);
void assignment_expression(assignment_expr*); void assignment_expression(assignment_expr*);
void gen_assignment_equal_statement(assignment_expr*); void gen_assignment_equal_statement(assignment_expr*);
void replace_left_assignment_with_load(const span&); void replace_left_assignment_with_load(const span&);

View File

@ -4,95 +4,102 @@ use std.file;
srand(); srand();
var compare=func() { var compare = func() {
var ch=[ var ch = [
"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","+", "0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","+",
"_","*","/","\'","\"",".",",",";",":","<",">","!","@","#","$","%", "_","*","/","\'","\"",".",",",";",":","<",">","!","@","#","$","%",
"^","&","*","(",")","-","=","\\","|","[","]","{","}","`"," ","\t","?" "^","&","*","(",")","-","=","\\","|","[","]","{","}","`"," ","\t","?"
]; ];
return func(begin,end) { return func(begin, end) {
var byte=0; var byte = 0;
var total=end-begin; var total = end-begin;
var timestamp=maketimestamp(); var timestamp = maketimestamp();
timestamp.stamp(); timestamp.stamp();
var bar=process_bar.high_resolution_bar(40); var bar = process_bar.high_resolution_bar(40);
for(var i=begin;i<end;i+=1) { for(var i = begin; i<end; i += 1) {
var s=""; var s = "";
for(var j=0;j<i;j+=1) { for(var j = 0; j<i; j += 1) {
s~=ch[rand()*size(ch)]; s ~= ch[rand()*size(ch)];
} }
byte+=size(s); byte += size(s);
var res=md5(s); var res = md5(s);
if(cmp(res, md5_self.md5(s))) { if(cmp(res, md5_self.md5(s))) {
die("error: "~str(i)); die("error: "~str(i));
} }
if (i-begin-int((i-begin)/4)*4==0) { if (i-begin-int((i-begin)/4)*4==0) {
print( print(
"\e[1000D ",bar.bar((i-begin+1)/total), "\e[1000D ", bar.bar((i-begin+1)/total),
" (",i-begin+1,"/",total,")\t", " (", i-begin+1, "/", total, ")\t",
res," byte: ",int(byte/1024),"k" res, " byte: ", int(byte/1024), "k"
); );
} }
} }
print( print(
"\e[1000D ",bar.bar((i-begin)/total), "\e[1000D ", bar.bar((i-begin)/total),
" (",i-begin,"/",total,")\t", " (", i-begin, "/", total, ")\t",
res," byte: ",int(byte/1024),"k", res, " byte: ", int(byte/1024), "k",
" time: ",timestamp.elapsedMSec() " time: ", timestamp.elapsedMSec()
); );
print("\n"); print("\n");
}; };
}(); }();
var filechecksum=func(){ var filechecksum = func() {
var files=[]; var files = [];
foreach(var p;file.find_all_files_with_extension("./test","nas")) { foreach(var p; file.find_all_files_with_extension("./test","nas")) {
append(files,"./test/"~p); append(files, "./test/"~p);
} }
foreach(var p;file.find_all_files_with_extension("./std","nas")) { foreach(var p; file.find_all_files_with_extension("./std","nas")) {
append(files,"./std/"~p); append(files, "./std/"~p);
} }
foreach(var p;file.find_all_files_with_extension("./module","nas","cpp")) { foreach(var p; file.find_all_files_with_extension("./module","nas","cpp")) {
append(files,"./module/"~p); append(files, "./module/"~p);
} }
foreach(var p;file.find_all_files_with_extension(".","md")) { foreach(var p; file.find_all_files_with_extension(".","md")) {
append(files,"./"~p); append(files, "./"~p);
} }
foreach(var p;file.find_all_files_with_extension("./src","cpp","h")) { foreach(var p; file.find_all_files_with_extension("./src","cpp","h")) {
append(files,"./src/"~p); append(files, "./src/"~p);
} }
foreach(var p;file.find_all_files_with_extension("./doc","md")) { foreach(var p; file.find_all_files_with_extension("./doc","md")) {
append(files,"./doc/"~p); append(files, "./doc/"~p);
} }
var byte=0; var source = [];
var total=size(files); foreach(var f; files) {
var timestamp=maketimestamp(); append(source, io.readfile(f));
}
var byte = 0;
var total = size(files);
var timestamp = maketimestamp();
timestamp.stamp(); timestamp.stamp();
var bar=process_bar.high_resolution_bar(40); var bar = process_bar.high_resolution_bar(40);
forindex(var i;files){ forindex(var i; files) {
var f=io.readfile(files[i]); var f = source[i];
var res=md5(f); var res = md5(f);
byte+=size(f); byte += size(f);
if(cmp(res, md5_self.md5(f))){ if(cmp(res, md5_self.md5(f))){
die("error: "~files[i]); die("error: "~files[i]);
} }
print( print(
"\e[1000D ",bar.bar((i+1)/total), "\e[1000D ", bar.bar((i+1)/total),
" (",i+1,"/",total,")\t",res, " (", i+1, "/", total, ")\t", res,
" byte: ",int(byte/1024),"k", " byte: ", int(byte/1024), "k",
" time: ",timestamp.elapsedMSec() " time: ", timestamp.elapsedMSec()
); );
} }
print("\n"); print("\n");
} }
var randomchecksum=func(){ var randomchecksum = func() {
for(var i=0;i<2048;i+=256) for(var i = 0; i<2048; i += 256) {
compare(i,i+256); compare(i, i+256);
}
} }
if(os.platform()=="windows") if(os.platform()=="windows") {
system("chcp 65001"); system("chcp 65001");
}
filechecksum(); filechecksum();
randomchecksum(); randomchecksum();