🐛 fix formated output

This commit is contained in:
ValKmjolnir 2025-01-13 22:35:56 +08:00
parent 644fbd8647
commit 3752bd8c07
6 changed files with 40 additions and 21 deletions

View File

@ -19,7 +19,6 @@ bool ast_format::visit_use_stmt(use_stmt* node) {
bool ast_format::visit_null_expr(null_expr* node) { bool ast_format::visit_null_expr(null_expr* node) {
dump_formating_node_info(node, "null expression"); dump_formating_node_info(node, "null expression");
out << "null";
return true; return true;
} }
@ -31,7 +30,7 @@ bool ast_format::visit_nil_expr(nil_expr* node) {
bool ast_format::visit_number_literal(number_literal* node) { bool ast_format::visit_number_literal(number_literal* node) {
dump_formating_node_info(node, "number expression"); dump_formating_node_info(node, "number expression");
out << node->get_number(); out << node->get_raw_text();
return true; return true;
} }
@ -81,7 +80,17 @@ bool ast_format::visit_hash_expr(hash_expr* node) {
bool ast_format::visit_hash_pair(hash_pair* node) { bool ast_format::visit_hash_pair(hash_pair* node) {
dump_formating_node_info(node, "hash pair"); 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()) { if (node->get_value()) {
out << " : "; out << " : ";
node->get_value()->accept(this); node->get_value()->accept(this);
@ -99,6 +108,10 @@ bool ast_format::visit_function(function* node) {
} }
} }
out << ") "; out << ") ";
if (node->get_code_block()->get_expressions().empty()) {
out << "{}";
return true;
}
node->get_code_block()->accept(this); node->get_code_block()->accept(this);
return true; return true;
} }
@ -138,11 +151,13 @@ bool ast_format::visit_parameter(parameter* node) {
bool ast_format::visit_ternary_operator(ternary_operator* node) { bool ast_format::visit_ternary_operator(ternary_operator* node) {
dump_formating_node_info(node, "ternary operator"); dump_formating_node_info(node, "ternary operator");
out << "(";
node->get_condition()->accept(this); node->get_condition()->accept(this);
out << " ? "; out << " ? ";
node->get_left()->accept(this); node->get_left()->accept(this);
out << " : "; out << " : ";
node->get_right()->accept(this); node->get_right()->accept(this);
out << ")";
return true; return true;
} }

View File

@ -44,7 +44,11 @@ private:
case expr_type::ast_bool: case expr_type::ast_bool:
case expr_type::ast_vec: case expr_type::ast_vec:
case expr_type::ast_hash: 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: { case expr_type::ast_def: {
auto dn = reinterpret_cast<definition_expr*>(n); auto dn = reinterpret_cast<definition_expr*>(n);
if (dn->get_value() && if (dn->get_value() &&

View File

@ -40,17 +40,14 @@ int main(i32 argc, const char* argv[]) {
const auto config = nasal::cli::parse({argv+1, argv+argc}); const auto config = nasal::cli::parse({argv+1, argv+argc});
// run directly or show help // run directly or show help
if (argc == 2) { if (config.has(nasal::cli::option::cli_help)) {
if (config.has(nasal::cli::option::cli_help)) { std::clog << nasal::cli::nasal_format_help;
std::clog << nasal::cli::nasal_format_help; } else if (config.has(nasal::cli::option::cli_version)) {
} else if (config.has(nasal::cli::option::cli_version)) { std::clog << nasal::cli::nasal_format_version;
std::clog << nasal::cli::nasal_format_version; } else if (config.input_file_path.size()) {
} else if (config.input_file_path.size()) { execute(config);
execute(config); } else {
} else { err();
err();
}
return 0;
} }
return 0; return 0;
} }

View File

@ -120,12 +120,14 @@ public:
class number_literal: public expr { class number_literal: public expr {
private: private:
f64 number; f64 number;
std::string raw_text;
public: public:
number_literal(const span& location, const f64 num): number_literal(const span& location, const f64 num, const std::string& raw):
expr(location, expr_type::ast_num), number(num) {} expr(location, expr_type::ast_num), number(num), raw_text(raw) {}
~number_literal() override = default; ~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; void accept(ast_visitor*) override;
}; };

View File

@ -235,7 +235,8 @@ nil_expr* parse::nil() {
number_literal* parse::num() { number_literal* parse::num() {
auto node = new number_literal( auto node = new number_literal(
toks[ptr].loc, 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); match(tok::tk_num);
return node; return node;

View File

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