#include "AST/AST.h" #include namespace plsm { namespace ast { static const std::unordered_map binOpToString = { {BinOp::ADD, "+"}, {BinOp::SUB, "-"}, {BinOp::MUL, "*"}, {BinOp::DIV, "/"}, {BinOp::MOD, "%"}, {BinOp::EQ, "=="}, {BinOp::NE, "!="}, {BinOp::LT, "<"}, {BinOp::LE, "<="}, {BinOp::GT, ">"}, {BinOp::GE, ">="}, {BinOp::AND, "&&"}, {BinOp::OR, "||"}, }; static const std::unordered_map stringToBinOp = { {"+", BinOp::ADD}, {"-", BinOp::SUB}, {"*", BinOp::MUL}, {"/", BinOp::DIV}, {"%", BinOp::MOD}, {"==", BinOp::EQ}, {"!=", BinOp::NE}, {"<", BinOp::LT}, {"<=", BinOp::LE}, {">", BinOp::GT}, {">=", BinOp::GE}, {"&&", BinOp::AND}, {"||", BinOp::OR}, }; boost::json::value BinExpr::toJson() { return { {"@type", "BinExpr"}, {"op", binOpToString.at(op)}, {"lhs", lhs->toJson()}, {"rhs", rhs->toJson()}, }; } BinExpr *BinExpr::fromJson(boost::json::value json) { auto opString = getJsonValue(json, "op"); auto op = stringToBinOp.at(opString); auto lhs = fromJsonProperty(json, "lhs"); auto rhs = fromJsonProperty(json, "rhs"); return new BinExpr(SourceRange::json(), op, lhs, rhs); } } // namespace ast } // namespace plsm