Skip to content

Commit

Permalink
🐛 fix formated output
Browse files Browse the repository at this point in the history
  • Loading branch information
ValKmjolnir committed Jan 13, 2025
1 parent 644fbd8 commit 3752bd8
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 21 deletions.
21 changes: 18 additions & 3 deletions src/ast_format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ bool ast_format::visit_use_stmt(use_stmt* node) {

bool ast_format::visit_null_expr(null_expr* node) {
dump_formating_node_info(node, "null expression");
out << "null";
return true;
}

Expand All @@ -31,7 +30,7 @@ bool ast_format::visit_nil_expr(nil_expr* node) {

bool ast_format::visit_number_literal(number_literal* node) {
dump_formating_node_info(node, "number expression");
out << node->get_number();
out << node->get_raw_text();
return true;
}

Expand Down Expand Up @@ -81,7 +80,17 @@ bool ast_format::visit_hash_expr(hash_expr* node) {

bool ast_format::visit_hash_pair(hash_pair* node) {
dump_formating_node_info(node, "hash pair");
out << node->get_name();
bool contain_not_identifier = false;
for (auto c : node->get_name()) {
if (!(std::isdigit(c) || std::isalpha(c) || c == '_')) {
contain_not_identifier = true;
}
}
if (contain_not_identifier) {
out << "\"" << node->get_name() << "\"";
} else {
out << node->get_name();
}
if (node->get_value()) {
out << " : ";
node->get_value()->accept(this);
Expand All @@ -99,6 +108,10 @@ bool ast_format::visit_function(function* node) {
}
}
out << ") ";
if (node->get_code_block()->get_expressions().empty()) {
out << "{}";
return true;
}
node->get_code_block()->accept(this);
return true;
}
Expand Down Expand Up @@ -138,11 +151,13 @@ bool ast_format::visit_parameter(parameter* node) {

bool ast_format::visit_ternary_operator(ternary_operator* node) {
dump_formating_node_info(node, "ternary operator");
out << "(";
node->get_condition()->accept(this);
out << " ? ";
node->get_left()->accept(this);
out << " : ";
node->get_right()->accept(this);
out << ")";
return true;
}

Expand Down
6 changes: 5 additions & 1 deletion src/ast_format.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ class ast_format: public ast_visitor {
case expr_type::ast_bool:
case expr_type::ast_vec:
case expr_type::ast_hash:
case expr_type::ast_call: return true;
case expr_type::ast_call:
case expr_type::ast_multi_assign:
case expr_type::ast_unary:
case expr_type::ast_binary:
case expr_type::ast_ternary: return true;
case expr_type::ast_def: {
auto dn = reinterpret_cast<definition_expr*>(n);
if (dn->get_value() &&
Expand Down
19 changes: 8 additions & 11 deletions src/format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,14 @@ int main(i32 argc, const char* argv[]) {
const auto config = nasal::cli::parse({argv+1, argv+argc});

// run directly or show help
if (argc == 2) {
if (config.has(nasal::cli::option::cli_help)) {
std::clog << nasal::cli::nasal_format_help;
} else if (config.has(nasal::cli::option::cli_version)) {
std::clog << nasal::cli::nasal_format_version;
} else if (config.input_file_path.size()) {
execute(config);
} else {
err();
}
return 0;
if (config.has(nasal::cli::option::cli_help)) {
std::clog << nasal::cli::nasal_format_help;
} else if (config.has(nasal::cli::option::cli_version)) {
std::clog << nasal::cli::nasal_format_version;
} else if (config.input_file_path.size()) {
execute(config);
} else {
err();
}
return 0;
}
8 changes: 5 additions & 3 deletions src/nasal_ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,14 @@ class nil_expr: public expr {
class number_literal: public expr {
private:
f64 number;
std::string raw_text;

public:
number_literal(const span& location, const f64 num):
expr(location, expr_type::ast_num), number(num) {}
number_literal(const span& location, const f64 num, const std::string& raw):
expr(location, expr_type::ast_num), number(num), raw_text(raw) {}
~number_literal() override = default;
f64 get_number() const {return number;}
f64 get_number() const { return number; }
const std::string& get_raw_text() const { return raw_text; }
void accept(ast_visitor*) override;
};

Expand Down
3 changes: 2 additions & 1 deletion src/nasal_parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,8 @@ nil_expr* parse::nil() {
number_literal* parse::num() {
auto node = new number_literal(
toks[ptr].loc,
util::str_to_num(toks[ptr].str.c_str())
util::str_to_num(toks[ptr].str.c_str()),
toks[ptr].str
);
match(tok::tk_num);
return node;
Expand Down
4 changes: 2 additions & 2 deletions src/optimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void optimizer::const_number(
return;
}
node->set_optimized_number(
new number_literal(node->get_location(), res)
new number_literal(node->get_location(), res, "")
);
}

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

Expand Down

0 comments on commit 3752bd8

Please sign in to comment.