112 lines
3.6 KiB
C++
112 lines
3.6 KiB
C++
export module caioc.expression;
|
|
|
|
import std;
|
|
|
|
namespace caioc {
|
|
|
|
export struct expression {
|
|
virtual ~expression() = default;
|
|
};
|
|
|
|
export struct int_literal_expression : expression {
|
|
std::int64_t value;
|
|
};
|
|
export struct bool_literal_expression : expression {
|
|
bool value;
|
|
};
|
|
export struct string_literal_expression : expression {
|
|
std::string value;
|
|
};
|
|
|
|
export struct identifier_expression : expression {
|
|
std::string name;
|
|
};
|
|
|
|
export struct call_expression : expression {
|
|
std::unique_ptr<expression> callee;
|
|
std::vector<std::unique_ptr<expression>> args;
|
|
};
|
|
|
|
} // namespace caioc
|
|
|
|
export template <>
|
|
struct std::formatter<caioc::int_literal_expression> {
|
|
constexpr auto parse(std::format_parse_context& ctx) { return ctx.begin(); }
|
|
|
|
auto format(const caioc::int_literal_expression& e, std::format_context& ctx) const {
|
|
return std::format_to(ctx.out(), "IntLiteral({})", e.value);
|
|
}
|
|
};
|
|
|
|
export template <>
|
|
struct std::formatter<caioc::bool_literal_expression> {
|
|
constexpr auto parse(std::format_parse_context& ctx) { return ctx.begin(); }
|
|
|
|
auto format(const caioc::bool_literal_expression& e, std::format_context& ctx) const {
|
|
return std::format_to(ctx.out(), "BoolLiteral({})", e.value);
|
|
}
|
|
};
|
|
|
|
export template <>
|
|
struct std::formatter<caioc::string_literal_expression> {
|
|
constexpr auto parse(std::format_parse_context& ctx) { return ctx.begin(); }
|
|
|
|
auto format(const caioc::string_literal_expression& e, std::format_context& ctx) const {
|
|
return std::format_to(ctx.out(), "StringLiteral(\"{}\")", e.value);
|
|
}
|
|
};
|
|
|
|
export template <>
|
|
struct std::formatter<caioc::identifier_expression> {
|
|
constexpr auto parse(std::format_parse_context& ctx) { return ctx.begin(); }
|
|
|
|
auto format(const caioc::identifier_expression& e, std::format_context& ctx) const {
|
|
return std::format_to(ctx.out(), "Identifier({})", e.name);
|
|
}
|
|
};
|
|
|
|
export template <>
|
|
struct std::formatter<caioc::expression> {
|
|
constexpr auto parse(std::format_parse_context& ctx) { return ctx.begin(); }
|
|
|
|
std::format_context::iterator format(const caioc::expression& e, std::format_context& ctx) const;
|
|
};
|
|
|
|
export template <>
|
|
struct std::formatter<caioc::call_expression> {
|
|
constexpr auto parse(std::format_parse_context& ctx) { return ctx.begin(); }
|
|
|
|
std::format_context::iterator format(const caioc::call_expression& e, std::format_context& ctx) const;
|
|
};
|
|
|
|
inline std::format_context::iterator std::formatter<caioc::expression>::format(const caioc::expression& e,
|
|
std::format_context& ctx) const {
|
|
if (auto* p = dynamic_cast<const caioc::int_literal_expression*>(&e)) {
|
|
return std::format_to(ctx.out(), "{}", *p);
|
|
}
|
|
if (auto* p = dynamic_cast<const caioc::bool_literal_expression*>(&e)) {
|
|
return std::format_to(ctx.out(), "{}", *p);
|
|
}
|
|
if (auto* p = dynamic_cast<const caioc::string_literal_expression*>(&e)) {
|
|
return std::format_to(ctx.out(), "{}", *p);
|
|
}
|
|
if (auto* p = dynamic_cast<const caioc::identifier_expression*>(&e)) {
|
|
return std::format_to(ctx.out(), "{}", *p);
|
|
}
|
|
if (auto* p = dynamic_cast<const caioc::call_expression*>(&e)) {
|
|
return std::format_to(ctx.out(), "{}", *p);
|
|
}
|
|
return std::format_to(ctx.out(), "<unknown expression>");
|
|
}
|
|
|
|
inline std::format_context::iterator std::formatter<caioc::call_expression>::format(const caioc::call_expression& e,
|
|
std::format_context& ctx) const {
|
|
auto out = std::format_to(ctx.out(), "Call(");
|
|
out = e.callee ? std::format_to(out, "{}", *e.callee) : std::format_to(out, "<null>");
|
|
for (const auto& arg : e.args) {
|
|
out = std::format_to(out, ", ");
|
|
out = arg ? std::format_to(out, "{}", *arg) : std::format_to(out, "<null>");
|
|
}
|
|
return std::format_to(out, ")");
|
|
}
|