Skip to content

Commit

Permalink
style: format code
Browse files Browse the repository at this point in the history
  • Loading branch information
JaDogg committed May 19, 2024
1 parent 113990e commit 77faad2
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 47 deletions.
3 changes: 1 addition & 2 deletions compiler/src/ast/ast_printer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ void ast_printer::visit_let_stmt(let_stmt *obj) {
text_ << ")";
}
void ast_printer::visit_assign_expr(assign_expr *obj) {
text_ << "("
<< "assign " << obj->name_->token_ << " ";
text_ << "(" << "assign " << obj->name_->token_ << " ";
obj->right_->accept(this);
text_ << ")";
}
Expand Down
9 changes: 3 additions & 6 deletions compiler/src/ast/ast_vis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,7 @@ void ast_vis::visit_ccode_stmt(ccode_stmt *obj) {
text_ << "<pre class=\"cyan-code\">"
<< ::string_utils::html_escape(
string_utils::unescape(obj->code_str_->token_))
<< "</pre>"
<< "<br />";
<< "</pre>" << "<br />";
end_block();
}
void ast_vis::visit_import_stmt(import_stmt *obj) {
Expand Down Expand Up @@ -348,8 +347,7 @@ void ast_vis::visit_nativeconst_stmt(nativeconst_stmt *obj) {
text_ << "<pre class=\"cyan-code\">"
<< ::string_utils::html_escape(
string_utils::unescape(obj->code_str_->token_))
<< "</pre>"
<< "<br />";
<< "</pre>" << "<br />";
end_block();
}
end_block();
Expand All @@ -359,8 +357,7 @@ void ast_vis::visit_runtimefeature_stmt(runtimefeature_stmt *obj) {
text_ << "<pre class=\"cyan-code\">"
<< ::string_utils::html_escape(
string_utils::unescape(obj->feature_->token_))
<< "</pre>"
<< "<br />";
<< "</pre>" << "<br />";
end_block();
}
void ast_vis::visit_foreach_stmt(foreach_stmt *obj) {
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/builtins/builtins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1268,7 +1268,6 @@ struct builtin_iif : builtin {
const std::string &filepath, slot_matcher *dt_slot_matcher) override {
std::stringstream message{};
auto o = ykobject(dt_pool);

if (args.size() != 3) {
o.string_val_ = "iif() builtin expects 3 arguments";
} else if (!(args[0].datatype_->const_unwrap()->is_bool())) {
Expand All @@ -1287,7 +1286,8 @@ struct builtin_iif : builtin {
o.datatype_ = dt_pool->create("sr");
}
if (args[1].is_a_function()) {
auto datatype_for_function = dt_slot_matcher->function_to_datatype_or_null(args[1]);
auto datatype_for_function =
dt_slot_matcher->function_to_datatype_or_null(args[1]);
if (datatype_for_function != nullptr) {
o.datatype_ = datatype_for_function;
} else {
Expand Down
24 changes: 14 additions & 10 deletions compiler/src/compiler/entry_struct_func_compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@ const std::vector<std::pair<std::string, std::string>> REPLACEMENTS = {
using namespace yaksha;
entry_struct_func_compiler::entry_struct_func_compiler(ykdt_pool *pool)
: pool_(pool), counter_(0), counter_bin_data_(0), autogen_bin_data_(),
bin_data_(), name_improvements_(), errors_(),
structure_pool_(), structures_(),
reverse_name_improvements_() {}
bin_data_(), name_improvements_(), errors_(), structure_pool_(),
structures_(), reverse_name_improvements_() {}
std::string entry_struct_func_compiler::compile(ykdatatype *entry_dt,
datatype_compiler *dtc) {
std::string repr = entry_dt->as_string();
Expand All @@ -69,7 +68,8 @@ std::string entry_struct_func_compiler::compile(ykdatatype *entry_dt,
d->a_ = pool_->create("str");
d->b_ = entry_dt->args_[0];
} else {
errors_.emplace_back("Invalid entry datatype: " + entry_dt->as_string_simplified(), nullptr);
errors_.emplace_back(
"Invalid entry datatype: " + entry_dt->as_string_simplified(), nullptr);
return "<><>";
}
std::stringstream code{};
Expand Down Expand Up @@ -122,15 +122,15 @@ void entry_struct_func_compiler::compile_structures(std::stringstream &target) {
if (!it->permanent_mark_) {
bool cycle = this->visit(it, topo_sorted_types);
if (cycle) {
errors_.emplace_back("Cycle detected in structure: " + it->dt_->as_string_simplified(), nullptr);
errors_.emplace_back("Cycle detected in structure: " +
it->dt_->as_string_simplified(),
nullptr);
return;
}
}
}
// Forward declarations of classes
for (auto *it : sorted) {
target << it->prefixed_full_name_ << ";\n";
}
for (auto *it : sorted) { target << it->prefixed_full_name_ << ";\n"; }
// Topologically sorted structures
for (auto *it : topo_sorted_types) { target << it->code_; }
// Classes
Expand Down Expand Up @@ -212,7 +212,9 @@ entry_struct_func_compiler::compile_function_dt(ykdatatype *function_dt,
code << dtc->convert_dt(d->b_->args_[0], datatype_location::STRUCT, "", "")
<< " ";
} else {
errors_.emplace_back("Invalid function output datatype: " + d->b_->as_string_simplified(), nullptr);
errors_.emplace_back("Invalid function output datatype: " +
d->b_->as_string_simplified(),
nullptr);
code << "<><>";
}
std::string name =
Expand Down Expand Up @@ -320,7 +322,9 @@ entry_struct_func_compiler::compile_fixed_array(ykdatatype *fixed_array_dt,
fixed_array_dt->args_.size() != 2 ||
fixed_array_dt->args_[0]->is_sm_entry() ||
fixed_array_dt->args_[1]->is_m_entry()) {
errors_.emplace_back("Invalid fixed array datatype: " + fixed_array_dt->as_string_simplified(), nullptr);
errors_.emplace_back("Invalid fixed array datatype: " +
fixed_array_dt->as_string_simplified(),
nullptr);
return "<><>";
}
// -- convert
Expand Down
18 changes: 12 additions & 6 deletions compiler/src/compiler/slot_matcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,25 @@ namespace yaksha {
*/
struct slot_matcher : function_datatype_extractor {
~slot_matcher() override = default;

virtual type_match_result slot_match_with_result(ykdatatype *datatype, const ykobject &provided_arg) = 0;
virtual type_match_result rvalue_match(const ykobject& left_side, const ykobject& right_side) = 0;
virtual type_match_result
slot_match_with_result(ykdatatype *datatype,
const ykobject &provided_arg) = 0;
virtual type_match_result rvalue_match(const ykobject &left_side,
const ykobject &right_side) = 0;
/**
* Match given two data types, taking auto-casting into account
* @param required_datatype datatype that we require (LHS)
* @param provided_datatype datatype provided by the user (RHS)
* @param primitive_or_obj true if provided data is a primitive/object, false otherwise
* @return type_match_result struct
*/
virtual type_match_result type_match(ykdatatype* required_datatype, ykdatatype* provided_datatype, bool primitive_or_obj) = 0;
virtual bool is_identical_type(ykdatatype* required_datatype, ykdatatype* provided_datatype) = 0;
virtual bool is_not_identical_type(ykdatatype* required_datatype, ykdatatype* provided_datatype) = 0;
virtual type_match_result type_match(ykdatatype *required_datatype,
ykdatatype *provided_datatype,
bool primitive_or_obj) = 0;
virtual bool is_identical_type(ykdatatype *required_datatype,
ykdatatype *provided_datatype) = 0;
virtual bool is_not_identical_type(ykdatatype *required_datatype,
ykdatatype *provided_datatype) = 0;
};
}// namespace yaksha
#endif
3 changes: 2 additions & 1 deletion compiler/src/compiler/to_c_compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ namespace yaksha {
const std::pair<std::string, ykobject> &rhs,
const ykdatatype *rhs_datatype,
const ykdatatype *lhs_datatype) override;
[[nodiscard]] std::string wrap_in_paren(const std::string &code) const override;
[[nodiscard]] std::string
wrap_in_paren(const std::string &code) const override;
void visit_cfor_stmt(cfor_stmt *obj) override;
void visit_enum_stmt(enum_stmt *obj) override;
void visit_union_stmt(union_stmt *obj) override;
Expand Down
8 changes: 5 additions & 3 deletions compiler/src/compiler/type_checker.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
#include "return_checker.h"
#include "utilities/ykobject.h"
namespace yaksha {

struct type_checker : expr_visitor, stmt_visitor, slot_matcher {
explicit type_checker(std::string filepath, codefiles *cf,
def_class_visitor *dcv, ykdt_pool *pool,
Expand All @@ -61,8 +60,11 @@ namespace yaksha {
type_match_result type_match(ykdatatype *required_datatype,
ykdatatype *provided_datatype,
bool primitive_or_obj) override;
type_match_result slot_match_with_result(ykdatatype *datatype, const ykobject &provided_arg) override;
type_match_result rvalue_match(const ykobject& left_side, const ykobject& right_side) override;
type_match_result
slot_match_with_result(ykdatatype *datatype,
const ykobject &provided_arg) override;
type_match_result rvalue_match(const ykobject &left_side,
const ykobject &right_side) override;
ykdatatype *function_to_datatype_or_null(const ykobject &arg) override;
void check(const std::vector<stmt *> &statements);
void visit_assign_expr(assign_expr *obj) override;
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/utilities/cpp_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ namespace yaksha {
static inline void intentionally_ignored(const T &) {}
// Reference: https://codereview.stackexchange.com/a/238646/47826
static inline std::size_t levenshtein_distance(const std::string &string_a,
const std::string &string_b) {
const std::string &string_b) {
const auto size_a = string_a.size();
const auto size_b = string_b.size();
std::vector<std::size_t> distances(size_b + 1);
Expand Down
22 changes: 14 additions & 8 deletions compiler/src/utilities/error_printer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,27 @@ namespace yaksha::errors {
}
}
}
std::string
error_printer::errors_to_json_lines(const std::vector<parsing_error> &errors) {
std::string error_printer::errors_to_json_lines(
const std::vector<parsing_error> &errors) {
std::stringstream ss{};
for (auto &err : errors) {
ss << "{";
if (err.token_set_) {
ss << "\"file\":\"" << string_utils::escape_json(err.tok_.file_) << "\",";
auto relative_file = std::filesystem::relative(err.tok_.file_, "./").string();
ss << "\"relative_file\":\"" << string_utils::escape_json(relative_file) << "\",";
ss << "\"file\":\"" << string_utils::escape_json(err.tok_.file_)
<< "\",";
auto relative_file =
std::filesystem::relative(err.tok_.file_, "./").string();
ss << "\"relative_file\":\"" << string_utils::escape_json(relative_file)
<< "\",";
ss << "\"line\":" << err.tok_.line_ + 1 << ",";
ss << "\"pos\":" << err.tok_.pos_ << ",";
ss << "\"message\":\"" << string_utils::escape_json(err.message_) << "\",";
ss << "\"token\":\"" << string_utils::escape_json(err.tok_.original_) << "\"";
ss << "\"message\":\"" << string_utils::escape_json(err.message_)
<< "\",";
ss << "\"token\":\"" << string_utils::escape_json(err.tok_.original_)
<< "\"";
} else {
ss << "\"message\":\"" << string_utils::escape_json(err.message_) << "\"";
ss << "\"message\":\"" << string_utils::escape_json(err.message_)
<< "\"";
}
ss << "}\n";
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/utilities/ykdatatype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ bool ykdatatype::is_dimension() const {
return !is_primitive() && builtin_type_ == ykbuiltin::DIMENSION;
}
bool yaksha::internal_is_identical_type(ykdatatype *required_datatype,
ykdatatype *provided_datatype) {
ykdatatype *provided_datatype) {
if (required_datatype != nullptr && provided_datatype != nullptr) {
if (required_datatype->is_primitive() &&
provided_datatype->is_primitive()) {
Expand Down
1 change: 0 additions & 1 deletion compiler/src/utilities/ykdatatype.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ namespace yaksha {
bool lhs_mutates, bool assignment);
[[nodiscard]] std::string as_string() const;
[[nodiscard]] std::string as_string_simplified() const;

// meta type
[[nodiscard]] bool is_c_primitive() const;
[[nodiscard]] bool is_primitive() const;
Expand Down
11 changes: 5 additions & 6 deletions compiler/src/yaksha_lisp/yaksha_lisp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,12 +454,11 @@ yaksha_lisp_value *yaksha_envmap::get(const std::string &symbol) {
#ifdef YAKSHA_LISP_EVAL_DEBUG_IS_PRINTED
auto v = get_internal(symbol);
if (v == nullptr) {
std::cout << indent_ << ">>> "
<< "env(" << this << ")." << symbol << " -> "
std::cout << indent_ << ">>> " << "env(" << this << ")." << symbol << " -> "
<< "NULL" << std::endl;
} else {
std::cout << indent_ << ">>> "
<< "env(" << this << ")." << symbol << " -> " << v << std::endl;
std::cout << indent_ << ">>> " << "env(" << this << ")." << symbol << " -> "
<< v << std::endl;
}
return v;
#else
Expand All @@ -476,8 +475,8 @@ void yaksha_envmap::set(const std::string &symbol, yaksha_lisp_value *value,
}
LOG_COMP("not locked can try setting to this env");
#ifdef YAKSHA_LISP_EVAL_DEBUG_IS_PRINTED
std::cout << indent_ << ">>> "
<< "env(" << this << ")." << symbol << " = " << value << std::endl;
std::cout << indent_ << ">>> " << "env(" << this << ")." << symbol << " = "
<< value << std::endl;
#endif
// Define if not defined
// Do not define in closure, only in current env
Expand Down

0 comments on commit 77faad2

Please sign in to comment.