diff --git a/src/nasal_builtin.cpp b/src/nasal_builtin.cpp index 72743f53..0aec1684 100644 --- a/src/nasal_builtin.cpp +++ b/src/nasal_builtin.cpp @@ -309,8 +309,8 @@ var builtin_substr(context* ctx, gc* ngc) { if (len.type!=vm_num || len.num()<0) { return nas_err("substr", "\"length\" should be number >= 0"); } - usize begin = (usize)beg.num(); - usize length = (usize)len.num(); + auto begin = static_cast(beg.num()); + auto length = static_cast(len.num()); if (begin>=str.str().length()) { 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(ctx->localr[1].num()); if (0<=num && num<128) { - return ngc->newstr((char)num); + return ngc->newstr(static_cast(num)); } else if (128<=num && num<256) { return ngc->newstr(extend[num-128]); } diff --git a/src/nasal_codegen.cpp b/src/nasal_codegen.cpp index 922c532e..7c5a72c7 100644 --- a/src/nasal_codegen.cpp +++ b/src/nasal_codegen.cpp @@ -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)) { return; } @@ -67,7 +67,7 @@ void codegen::regist_num(const f64 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)) { return; } @@ -94,11 +94,11 @@ void codegen::find_symbol(code_block* node) { scope.insert(i.name); } // 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 (global.count(name)) { 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(); - regist_num(num); + regist_number(num); 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(); - regist_str(str); + regist_string(str); emit(op_pstr, const_string_map.at(str), node->get_location()); } void codegen::bool_gen(bool_literal* node) { f64 num = node->get_flag()? 1:0; - regist_num(num); + regist_number(num); 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()) { calc_gen(child); } @@ -180,7 +180,7 @@ void codegen::hash_gen(hash_expr* node) { for(auto child : node->get_members()) { calc_gen(child->get_value()); 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()); } } @@ -243,7 +243,7 @@ void codegen::func_gen(function* node) { tmp->get_location() ); } - regist_str(name); + regist_string(name); switch(tmp->get_parameter_type()) { case parameter::param_type::normal_parameter: 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()); break; } - add_symbol(name); + regist_symbol(name); } code[newf].num = code.size()+1; // entry @@ -282,7 +282,7 @@ void codegen::func_gen(function* node) { while(local_symbol_find(arg)>=0) { arg = "0" + arg; } - add_symbol(arg); + regist_symbol(arg); // generate code block 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(); if (native_function_mapper.count(name)) { emit(op_callb, @@ -357,7 +357,7 @@ void codegen::call_id(identifier* 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()); } @@ -391,7 +391,7 @@ void codegen::call_func_gen(call_function* node) { auto pair_node = reinterpret_cast(child); calc_gen(pair_node->get_value()); 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_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 if (node->get_type()==expr_type::ast_id) { - mcall_id(reinterpret_cast(node)); + mcall_identifier(reinterpret_cast(node)); return; } // generate call expression until the last sub-node @@ -428,17 +428,22 @@ void codegen::mcall(expr* node) { for(usize i = 0; iget_calls().size()-1; ++i) { auto tmp = call_node->get_calls()[i]; switch(tmp->get_type()) { - case expr_type::ast_callh: call_hash_gen((call_hash*)tmp); break; - case expr_type::ast_callv: call_vector_gen((call_vector*)tmp); break; - case expr_type::ast_callf: call_func_gen((call_function*)tmp); break; + case expr_type::ast_callh: + call_hash_gen(reinterpret_cast(tmp)); break; + case expr_type::ast_callv: + call_vector_gen(reinterpret_cast(tmp)); break; + case expr_type::ast_callf: + call_func_gen(reinterpret_cast(tmp)); break; default: break; } } // the last sub-node will be used to generate memory call expression auto tmp = call_node->get_calls().back(); switch(tmp->get_type()) { - case expr_type::ast_callh: mcall_hash((call_hash*)tmp); break; - case expr_type::ast_callv: mcall_vec((call_vector*)tmp); break; + case expr_type::ast_callh: + mcall_hash(reinterpret_cast(tmp)); break; + case expr_type::ast_callv: + mcall_vec(reinterpret_cast(tmp)); break; case expr_type::ast_callf: die("bad left-value: function call", tmp->get_location()); break; 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(); if (native_function_mapper.count(name)) { 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) { - regist_str(node->get_field()); + regist_string(node->get_field()); 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()) { auto& vals = node->get_tuple()->get_elements(); 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() ); return; } else if (identifiers.size()get_tuple()->get_location() ); return; @@ -540,7 +549,7 @@ void codegen::multi_def(definition_expr* node) { 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()) { 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) { emit(op_addeq, 0, node->get_location()); } else { - auto num = ((number_literal*)node->get_right())->get_number(); - regist_num(num); + auto num = reinterpret_cast(node->get_right()) + ->get_number(); + regist_number(num); emit(op_addeqc, const_number_map[num], node->get_location()); } break; @@ -575,8 +585,9 @@ void codegen::assignment_expression(assignment_expr* node) { if (node->get_right()->get_type()!=expr_type::ast_num) { emit(op_subeq, 0, node->get_location()); } else { - auto num = ((number_literal*)node->get_right())->get_number(); - regist_num(num); + auto num = reinterpret_cast(node->get_right()) + ->get_number(); + regist_number(num); emit(op_subeqc, const_number_map[num], node->get_location()); } break; @@ -588,8 +599,9 @@ void codegen::assignment_expression(assignment_expr* node) { if (node->get_right()->get_type()!=expr_type::ast_num) { emit(op_muleq, 0, node->get_location()); } else { - auto num = ((number_literal*)node->get_right())->get_number(); - regist_num(num); + auto num = reinterpret_cast(node->get_right()) + ->get_number(); + regist_number(num); emit(op_muleqc, const_number_map[num], node->get_location()); } break; @@ -601,8 +613,9 @@ void codegen::assignment_expression(assignment_expr* node) { if (node->get_right()->get_type()!=expr_type::ast_num) { emit(op_diveq, 0, node->get_location()); } else { - auto num = ((number_literal*)node->get_right())->get_number(); - regist_num(num); + auto num = reinterpret_cast(node->get_right()) + ->get_number(); + regist_number(num); emit(op_diveqc, const_number_map[num], node->get_location()); } break; @@ -614,8 +627,9 @@ void codegen::assignment_expression(assignment_expr* node) { if (node->get_right()->get_type()!=expr_type::ast_str) { emit(op_lnkeq, 0, node->get_location()); } else { - const auto& str = ((string_literal*)node->get_right())->get_content(); - regist_str(str); + const auto& str = reinterpret_cast( + node->get_right())->get_content(); + regist_string(str); emit(op_lnkeqc, const_string_map[str], node->get_location()); } break; @@ -655,7 +669,7 @@ void codegen::gen_assignment_equal_statement(assignment_expr* node) { // generate symbol load calc_gen(node->get_right()); // get memory space of left identifier - mcall_id(reinterpret_cast(node->get_left())); + mcall_identifier(reinterpret_cast(node->get_left())); // check memory get operand type and replace it with load operand switch(code.back().op) { case op_mcallg: code.back().op = op_loadg; break; @@ -802,9 +816,12 @@ void codegen::loop_gen(expr* node) { break_ptr.push_front({}); switch(node->get_type()) { - case expr_type::ast_while: while_gen((while_expr*)node); break; - case expr_type::ast_for: for_gen((for_expr*)node); break; - case expr_type::ast_forei: forei_gen((forei_expr*)node); break; + case expr_type::ast_while: + while_gen(reinterpret_cast(node)); break; + case expr_type::ast_for: + for_gen(reinterpret_cast(node)); break; + case expr_type::ast_forei: + forei_gen(reinterpret_cast(node)); break; default: break; } } @@ -836,7 +853,7 @@ void codegen::for_gen(for_expr* node) { statement_generation(node->get_initial()); usize jmp_place = code.size(); 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()); } else { calc_gen(node->get_condition()); @@ -900,11 +917,11 @@ void codegen::statement_generation(expr* node) { switch(node->get_type()) { case expr_type::ast_null: break; case expr_type::ast_def: - def_gen((definition_expr*)node); break; + definition_gen(reinterpret_cast(node)); break; case expr_type::ast_multi_assign: - multi_assign_gen((multi_assign*)node); break; + multi_assign_gen(reinterpret_cast(node)); break; case expr_type::ast_assign: - assignment_statement((assignment_expr*)node); break; + assignment_statement(reinterpret_cast(node)); break; case expr_type::ast_nil: case expr_type::ast_num: case expr_type::ast_str: @@ -963,7 +980,7 @@ void codegen::and_gen(binary_operator* node) { void codegen::unary_gen(unary_operator* node) { // generate optimized result if (node->get_optimized_number()) { - num_gen(node->get_optimized_number()); + number_gen(node->get_optimized_number()); return; } @@ -981,11 +998,11 @@ void codegen::unary_gen(unary_operator* node) { void codegen::binary_gen(binary_operator* node) { // generate optimized result if (node->get_optimized_number()) { - num_gen(node->get_optimized_number()); + number_gen(node->get_optimized_number()); return; } if (node->get_optimized_string()) { - str_gen(node->get_optimized_string()); + string_gen(node->get_optimized_string()); return; } @@ -1029,8 +1046,9 @@ void codegen::binary_gen(binary_operator* node) { calc_gen(node->get_right()); emit(op_add, 0, node->get_location()); } else { - auto num = ((number_literal*)node->get_right())->get_number(); - regist_num(num); + auto num = reinterpret_cast(node->get_right()) + ->get_number(); + regist_number(num); emit(op_addc, const_number_map.at(num), node->get_location()); } return; @@ -1040,8 +1058,9 @@ void codegen::binary_gen(binary_operator* node) { calc_gen(node->get_right()); emit(op_sub, 0, node->get_location()); } else { - auto num = ((number_literal*)node->get_right())->get_number(); - regist_num(num); + auto num = reinterpret_cast(node->get_right()) + ->get_number(); + regist_number(num); emit(op_subc, const_number_map.at(num), node->get_location()); } return; @@ -1051,8 +1070,9 @@ void codegen::binary_gen(binary_operator* node) { calc_gen(node->get_right()); emit(op_mul, 0, node->get_location()); } else { - auto num = ((number_literal*)node->get_right())->get_number(); - regist_num(num); + auto num = reinterpret_cast(node->get_right()) + ->get_number(); + regist_number(num); emit(op_mulc, const_number_map.at(num), node->get_location()); } return; @@ -1062,8 +1082,9 @@ void codegen::binary_gen(binary_operator* node) { calc_gen(node->get_right()); emit(op_div, 0, node->get_location()); } else { - auto num = ((number_literal*)node->get_right())->get_number(); - regist_num(num); + auto num = reinterpret_cast(node->get_right()) + ->get_number(); + regist_number(num); emit(op_divc, const_number_map.at(num), node->get_location()); } return; @@ -1073,8 +1094,9 @@ void codegen::binary_gen(binary_operator* node) { calc_gen(node->get_right()); emit(op_lnk, 0, node->get_location()); } else { - const auto& str = ((string_literal*)node->get_right())->get_content(); - regist_str(str); + const auto& str = reinterpret_cast( + node->get_right())->get_content(); + regist_string(str); emit(op_lnkc, const_string_map.at(str), node->get_location()); } break; @@ -1084,8 +1106,9 @@ void codegen::binary_gen(binary_operator* node) { calc_gen(node->get_right()); emit(op_less, 0, node->get_location()); } else { - auto num = ((number_literal*)node->get_right())->get_number(); - regist_num(num); + auto num = reinterpret_cast(node->get_right()) + ->get_number(); + regist_number(num); emit(op_lessc, const_number_map.at(num), node->get_location()); } return; @@ -1095,8 +1118,9 @@ void codegen::binary_gen(binary_operator* node) { calc_gen(node->get_right()); emit(op_leq, 0, node->get_location()); } else { - auto num = ((number_literal*)node->get_right())->get_number(); - regist_num(num); + auto num = reinterpret_cast(node->get_right()) + ->get_number(); + regist_number(num); emit(op_leqc, const_number_map.at(num), node->get_location()); } return; @@ -1106,8 +1130,9 @@ void codegen::binary_gen(binary_operator* node) { calc_gen(node->get_right()); emit(op_grt, 0, node->get_location()); } else { - auto num = ((number_literal*)node->get_right())->get_number(); - regist_num(num); + auto num = reinterpret_cast(node->get_right()) + ->get_number(); + regist_number(num); emit(op_grtc, const_number_map.at(num), node->get_location()); } return; @@ -1117,8 +1142,9 @@ void codegen::binary_gen(binary_operator* node) { calc_gen(node->get_right()); emit(op_geq, 0, node->get_location()); } else { - auto num = ((number_literal*)node->get_right())->get_number(); - regist_num(num); + auto num = reinterpret_cast(node->get_right()) + ->get_number(); + regist_number(num); emit(op_geqc, const_number_map.at(num), node->get_location()); } return; @@ -1143,33 +1169,38 @@ void codegen::calc_gen(expr* node) { case expr_type::ast_nil: emit(op_pnil, 0, node->get_location()); break; case expr_type::ast_num: - num_gen((number_literal*)node); break; + number_gen(reinterpret_cast(node)); break; case expr_type::ast_str: - str_gen((string_literal*)node); break; + string_gen(reinterpret_cast(node)); break; case expr_type::ast_id: - call_id((identifier*)node); break; + call_identifier(reinterpret_cast(node)); break; case expr_type::ast_bool: - bool_gen((bool_literal*)node); break; + bool_gen(reinterpret_cast(node)); break; case expr_type::ast_vec: - vec_gen((vector_expr*)node); break; + vector_gen(reinterpret_cast(node)); break; case expr_type::ast_hash: - hash_gen((hash_expr*)node); break; + hash_gen(reinterpret_cast(node)); break; case expr_type::ast_func: - func_gen((function*)node); break; + func_gen(reinterpret_cast(node)); break; case expr_type::ast_call: - call_gen((call_expr*)node); break; + call_gen(reinterpret_cast(node)); break; case expr_type::ast_assign: - assignment_expression((assignment_expr*)node); break; + assignment_expression( + reinterpret_cast(node) + ); + break; case expr_type::ast_ternary: - trino_gen((ternary_operator*)node); break; + trino_gen(reinterpret_cast(node)); break; case expr_type::ast_unary: - unary_gen((unary_operator*)node); break; + unary_gen(reinterpret_cast(node)); break; case expr_type::ast_binary: - binary_gen((binary_operator*)node); break; + binary_gen(reinterpret_cast(node)); break; case expr_type::ast_def: // definition in calculation only should be single def - single_def((definition_expr*)node); - call_id(((definition_expr*)node)->get_variable_name()); + single_def(reinterpret_cast(node)); + call_identifier( + (reinterpret_cast(node))->get_variable_name() + ); break; default: break; } @@ -1177,11 +1208,16 @@ void codegen::calc_gen(expr* node) { void codegen::repl_mode_info_output_gen(expr* node) { switch(node->get_type()) { - case expr_type::ast_id: call_id((identifier*)node); break; - case expr_type::ast_nil: emit(op_pnil, 0, node->get_location()); break; - case expr_type::ast_num: num_gen((number_literal*)node); break; - case expr_type::ast_str: str_gen((string_literal*)node); break; - case expr_type::ast_bool: bool_gen((bool_literal*)node); break; + case expr_type::ast_id: + call_identifier(reinterpret_cast(node)); break; + case expr_type::ast_nil: + emit(op_pnil, 0, node->get_location()); break; + case expr_type::ast_num: + number_gen(reinterpret_cast(node)); break; + case expr_type::ast_str: + string_gen(reinterpret_cast(node)); break; + case expr_type::ast_bool: + bool_gen(reinterpret_cast(node)); break; default: return; } // 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); // 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_symbol("arg"); + regist_symbol("arg"); // search global symbols first find_symbol(parse.tree()); diff --git a/src/nasal_codegen.h b/src/nasal_codegen.h index 4aaf212c..af1635cb 100644 --- a/src/nasal_codegen.h +++ b/src/nasal_codegen.h @@ -78,34 +78,34 @@ class codegen { err.err("code", loc, info); } - void regist_num(const f64); - void regist_str(const std::string&); + void regist_number(const f64); + void regist_string(const std::string&); 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 global_symbol_find(const std::string&); i32 upvalue_symbol_find(const std::string&); void emit(u8, u32, const span&); - void num_gen(number_literal*); - void str_gen(string_literal*); + void number_gen(number_literal*); + void string_gen(string_literal*); void bool_gen(bool_literal*); - void vec_gen(vector_expr*); + void vector_gen(vector_expr*); void hash_gen(hash_expr*); void func_gen(function*); void call_gen(call_expr*); - void call_id(identifier*); + void call_identifier(identifier*); void call_hash_gen(call_hash*); void call_vector_gen(call_vector*); void call_func_gen(call_function*); void mcall(expr*); - void mcall_id(identifier*); + void mcall_identifier(identifier*); void mcall_vec(call_vector*); void mcall_hash(call_hash*); void multi_def(definition_expr*); void single_def(definition_expr*); - void def_gen(definition_expr*); + void definition_gen(definition_expr*); void assignment_expression(assignment_expr*); void gen_assignment_equal_statement(assignment_expr*); void replace_left_assignment_with_load(const span&); diff --git a/test/md5compare.nas b/test/md5compare.nas index 29cd027a..09a8ef16 100644 --- a/test/md5compare.nas +++ b/test/md5compare.nas @@ -4,95 +4,102 @@ use std.file; srand(); -var compare=func() { - var ch=[ +var compare = func() { + var ch = [ "0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","+", "_","*","/","\'","\"",".",",",";",":","<",">","!","@","#","$","%", "^","&","*","(",")","-","=","\\","|","[","]","{","}","`"," ","\t","?" ]; - return func(begin,end) { - var byte=0; - var total=end-begin; - var timestamp=maketimestamp(); + return func(begin, end) { + var byte = 0; + var total = end-begin; + var timestamp = maketimestamp(); timestamp.stamp(); - var bar=process_bar.high_resolution_bar(40); - for(var i=begin;i