Skip to content

Commit

Permalink
✨ use reinterpret_cast as pointer cast
Browse files Browse the repository at this point in the history
  • Loading branch information
ValKmjolnir committed Nov 4, 2023
1 parent c946e9d commit 8a160dc
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 58 deletions.
22 changes: 15 additions & 7 deletions module/nasocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ var nas_bind(var* args, usize size, gc* ngc) {
server.sin_port = htons(args[2].num());
return var::num(static_cast<double>(bind(
args[0].num(),
(sockaddr*)&server,
reinterpret_cast<sockaddr*>(&server),
sizeof(server)
)));
}
Expand Down Expand Up @@ -110,9 +110,17 @@ var nas_accept(var* args, usize size, gc* ngc) {
sockaddr_in client;
int socklen = sizeof(sockaddr_in);
#ifdef _WIN32
int client_sd = accept(args[0].num(), (sockaddr*)&client, &socklen);
int client_sd = accept(
args[0].num(),
reinterpret_cast<sockaddr*>(&client),
&socklen
);
#else
int client_sd = accept(args[0].num(), (sockaddr*)&client, (socklen_t*)&socklen);
int client_sd = accept(
args[0].num(),
reinterpret_cast<sockaddr*>(&client),
reinterpret_cast<socklen_t*>(&socklen)
);
#endif
var res = ngc->temp = ngc->alloc(vm_hash);
auto& hash = res.hash().elems;
Expand Down Expand Up @@ -159,7 +167,7 @@ var nas_sendto(var* args, usize size, gc* ngc) {
args[3].str().c_str(),
args[3].str().length(),
args[4].num(),
(sockaddr*)&addr,
reinterpret_cast<sockaddr*>(&addr),
sizeof(sockaddr_in)
)));
}
Expand Down Expand Up @@ -205,7 +213,7 @@ var nas_recvfrom(var* args, usize size, gc* ngc) {
buf,
args[1].num(),
args[2].num(),
(sockaddr*)&addr,
reinterpret_cast<sockaddr*>(&addr),
&socklen
);
#else
Expand All @@ -214,8 +222,8 @@ var nas_recvfrom(var* args, usize size, gc* ngc) {
buf,
args[1].num(),
args[2].num(),
(sockaddr*)&addr,
(socklen_t*)&socklen
reinterpret_cast<sockaddr*>(&addr),
reinterpret_cast<socklen_t*>(&socklen)
);
#endif
hash["size"] = var::num(static_cast<double>(recvsize));
Expand Down
2 changes: 1 addition & 1 deletion src/coroutine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var builtin_cocreate(context* ctx, gc* ngc) {
coroutine.ctx.top++;

// store old localr on stack
coroutine.ctx.top[0] = var::addr((var*)nullptr);
coroutine.ctx.top[0] = var::addr(nullptr);
coroutine.ctx.top++;

// store old pc on stack
Expand Down
4 changes: 2 additions & 2 deletions src/dylib_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ var builtin_dlopen(context* ctx, gc* ngc) {

// get "get" function, to get the register table
#ifdef _WIN32
void* register_table_get_function = (void*)GetProcAddress(
void* register_table_get_function = reinterpret_cast<void*>(GetProcAddress(
static_cast<HMODULE>(library_object.ghost().pointer), "get"
);
));
#else
void* register_table_get_function = dlsym(
library_object.ghost().pointer, "get"
Expand Down
6 changes: 3 additions & 3 deletions src/nasal_import.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ bool linker::import_check(expr* node) {
if (call_node->get_calls().size()!=1) {
return false;
}

if (call_node->get_calls()[0]->get_type()!=expr_type::ast_callf) {
auto maybe_func_call = call_node->get_calls()[0];
if (maybe_func_call->get_type()!=expr_type::ast_callf) {
return false;
}
auto func_call = (call_function*)call_node->get_calls()[0];
auto func_call = reinterpret_cast<call_function*>(maybe_func_call);
if (func_call->get_argument().size()!=1) {
return false;
}
Expand Down
8 changes: 6 additions & 2 deletions src/nasal_parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,13 @@ bool parse::check_func_end(expr* node) {
if (type==expr_type::ast_func) {
return true;
} else if (type==expr_type::ast_def) {
return check_func_end(((definition_expr*)node)->get_value());
return check_func_end(
reinterpret_cast<definition_expr*>(node)->get_value()
);
} else if (type==expr_type::ast_assign) {
return check_func_end(((assignment_expr*)node)->get_right());
return check_func_end(
reinterpret_cast<assignment_expr*>(node)->get_right()
);
}
return false;
}
Expand Down
102 changes: 59 additions & 43 deletions src/optimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ void optimizer::const_string(
const auto& left = left_node->get_content();
const auto& right = right_node->get_content();
node->set_optimized_string(
new string_literal(node->get_location(), left+right));
new string_literal(node->get_location(), left+right)
);
}

void optimizer::const_number(
Expand Down Expand Up @@ -43,7 +44,8 @@ void optimizer::const_number(
return;
}
node->set_optimized_number(
new number_literal(node->get_location(), res));
new number_literal(node->get_location(), res)
);
}

void optimizer::const_number(
Expand All @@ -62,46 +64,56 @@ void optimizer::const_number(
return;
}
node->set_optimized_number(
new number_literal(node->get_location(), res));
new number_literal(node->get_location(), res)
);
}

bool optimizer::visit_binary_operator(binary_operator* node) {
node->get_left()->accept(this);
node->get_right()->accept(this);
auto left_node = node->get_left();
auto right_node = node->get_right();
left_node->accept(this);
right_node->accept(this);

number_literal* left_num_node = nullptr;
number_literal* right_num_node = nullptr;
string_literal* left_str_node = nullptr;
string_literal* right_str_node = nullptr;
if (node->get_left()->get_type()==expr_type::ast_num) {
left_num_node = (number_literal*)node->get_left();
} else if (node->get_left()->get_type()==expr_type::ast_binary &&
((binary_operator*)node->get_left())->get_optimized_number()) {
left_num_node = ((binary_operator*)node->get_left())->get_optimized_number();
} else if (node->get_left()->get_type()==expr_type::ast_unary &&
((unary_operator*)node->get_left())->get_optimized_number()) {
left_num_node = ((unary_operator*)node->get_left())->get_optimized_number();
if (left_node->get_type()==expr_type::ast_num) {
left_num_node = reinterpret_cast<number_literal*>(left_node);
} else if (left_node->get_type()==expr_type::ast_binary &&
reinterpret_cast<binary_operator*>(left_node)->get_optimized_number()) {
auto optimized = reinterpret_cast<binary_operator*>(left_node);
left_num_node = optimized->get_optimized_number();
} else if (left_node->get_type()==expr_type::ast_unary &&
reinterpret_cast<unary_operator*>(left_node)->get_optimized_number()) {
auto optimized = reinterpret_cast<unary_operator*>(left_node);
left_num_node = optimized->get_optimized_number();
}
if (node->get_right()->get_type()==expr_type::ast_num) {
right_num_node = (number_literal*)node->get_right();
} else if (node->get_right()->get_type()==expr_type::ast_binary &&
((binary_operator*)node->get_right())->get_optimized_number()) {
right_num_node = ((binary_operator*)node->get_right())->get_optimized_number();
} else if (node->get_right()->get_type()==expr_type::ast_unary &&
((unary_operator*)node->get_right())->get_optimized_number()) {
right_num_node = ((unary_operator*)node->get_right())->get_optimized_number();
if (right_node->get_type()==expr_type::ast_num) {
right_num_node = reinterpret_cast<number_literal*>(right_node);
} else if (right_node->get_type()==expr_type::ast_binary &&
reinterpret_cast<binary_operator*>(right_node)->get_optimized_number()) {
auto optimized = reinterpret_cast<binary_operator*>(right_node);
right_num_node = optimized->get_optimized_number();
} else if (right_node->get_type()==expr_type::ast_unary &&
reinterpret_cast<unary_operator*>(right_node)->get_optimized_number()) {
auto optimized = reinterpret_cast<unary_operator*>(right_node);
right_num_node = optimized->get_optimized_number();
}

if (node->get_left()->get_type()==expr_type::ast_str) {
left_str_node = (string_literal*)node->get_left();
} else if (node->get_left()->get_type()==expr_type::ast_binary &&
((binary_operator*)node->get_left())->get_optimized_string()) {
left_str_node = ((binary_operator*)node->get_left())->get_optimized_string();
if (left_node->get_type()==expr_type::ast_str) {
left_str_node = reinterpret_cast<string_literal*>(left_node);
} else if (left_node->get_type()==expr_type::ast_binary &&
reinterpret_cast<binary_operator*>(left_node)->get_optimized_string()) {
auto optimized = reinterpret_cast<binary_operator*>(left_node);
left_str_node = optimized->get_optimized_string();
}
if (node->get_right()->get_type()==expr_type::ast_str) {
right_str_node = (string_literal*)node->get_right();
} else if (node->get_right()->get_type()==expr_type::ast_binary &&
((binary_operator*)node->get_right())->get_optimized_string()) {
right_str_node = ((binary_operator*)node->get_right())->get_optimized_string();
if (right_node->get_type()==expr_type::ast_str) {
right_str_node = reinterpret_cast<string_literal*>(right_node);
} else if (right_node->get_type()==expr_type::ast_binary &&
reinterpret_cast<binary_operator*>(right_node)->get_optimized_string()) {
auto optimized = reinterpret_cast<binary_operator*>(right_node);
right_str_node = optimized->get_optimized_string();
}
if (left_num_node && right_num_node) {
const_number(node, left_num_node, right_num_node);
Expand All @@ -115,19 +127,23 @@ bool optimizer::visit_binary_operator(binary_operator* node) {
}

bool optimizer::visit_unary_operator(unary_operator* node) {
node->get_value()->accept(this);
number_literal* value_node = nullptr;
if (node->get_value()->get_type()==expr_type::ast_num) {
value_node = (number_literal*)node->get_value();
} else if (node->get_value()->get_type()==expr_type::ast_binary &&
((binary_operator*)node->get_value())->get_optimized_number()) {
value_node = ((binary_operator*)node->get_value())->get_optimized_number();
} else if (node->get_value()->get_type()==expr_type::ast_unary &&
((unary_operator*)node->get_value())->get_optimized_number()) {
value_node = ((unary_operator*)node->get_value())->get_optimized_number();
auto value = node->get_value();
value->accept(this);

number_literal* num_node = nullptr;
if (value->get_type()==expr_type::ast_num) {
num_node = reinterpret_cast<number_literal*>(value);
} else if (value->get_type()==expr_type::ast_binary &&
reinterpret_cast<binary_operator*>(value)->get_optimized_number()) {
auto optimized = reinterpret_cast<binary_operator*>(value);
num_node = optimized->get_optimized_number();
} else if (value->get_type()==expr_type::ast_unary &&
reinterpret_cast<unary_operator*>(value)->get_optimized_number()) {
auto optimized = reinterpret_cast<unary_operator*>(value);
num_node = optimized->get_optimized_number();
}
if (value_node) {
const_number(node, value_node);
if (num_node) {
const_number(node, num_node);
}
return true;
}
Expand Down

0 comments on commit 8a160dc

Please sign in to comment.