1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 *
4 * HDF is dual licensed: you can use it either under the terms of
5 * the GPL, or the BSD license, at your option.
6 * See the LICENSE file in the root of this repository for complete details.
7 */
8
9 #include "ast/ast_expr.h"
10 #include "util/string_builder.h"
11
12 namespace OHOS {
13 namespace HDI {
Dump(const std::string & prefix)14 std::string ASTUnaryExpr::Dump(const std::string &prefix)
15 {
16 StringBuilder sb;
17 sb.Append(prefix);
18 if (isParenExpr) {
19 sb.Append("(");
20 }
21
22 sb.AppendFormat("%s%s", UnaryOpToString(op_).c_str(), expr_->Dump("").c_str());
23
24 if (isParenExpr) {
25 sb.Append(")");
26 }
27
28 return sb.ToString();
29 }
30
UnaryOpToString(UnaryOpKind op) const31 std::string ASTUnaryExpr::UnaryOpToString(UnaryOpKind op) const
32 {
33 switch (op) {
34 case UnaryOpKind::PLUS:
35 return "+";
36 case UnaryOpKind::MINUS:
37 return "-";
38 case UnaryOpKind::TILDE:
39 return "~";
40 default:
41 return "unknown";
42 }
43 }
44
Dump(const std::string & prefix)45 std::string ASTBinaryExpr::Dump(const std::string &prefix)
46 {
47 StringBuilder sb;
48 sb.Append(prefix);
49 if (isParenExpr) {
50 sb.Append("(");
51 }
52
53 sb.AppendFormat("%s %s %s", lExpr_->Dump("").c_str(), BinaryOpToString(op_).c_str(), rExpr_->Dump("").c_str());
54
55 if (isParenExpr) {
56 sb.Append(")");
57 }
58
59 return sb.ToString();
60 }
61
BinaryOpToString(BinaryOpKind op) const62 std::string ASTBinaryExpr::BinaryOpToString(BinaryOpKind op) const
63 {
64 switch (op) {
65 case BinaryOpKind::MUL:
66 return "*";
67 case BinaryOpKind::DIV:
68 return "/";
69 case BinaryOpKind::MOD:
70 return "%";
71 case BinaryOpKind::ADD:
72 return "+";
73 case BinaryOpKind::SUB:
74 return "-";
75 case BinaryOpKind::LSHIFT:
76 return "<<";
77 case BinaryOpKind::RSHIFT:
78 return ">>";
79 case BinaryOpKind::AND:
80 return "&";
81 case BinaryOpKind::XOR:
82 return "^";
83 case BinaryOpKind::OR:
84 return "|";
85 default:
86 return "unknown";
87 }
88 }
89
Dump(const std::string & prefix)90 std::string ASTNumExpr::Dump(const std::string &prefix)
91 {
92 StringBuilder sb;
93 sb.Append(prefix);
94 if (isParenExpr) {
95 sb.Append("(");
96 }
97
98 sb.AppendFormat("%s", value_.c_str());
99
100 if (isParenExpr) {
101 sb.Append(")");
102 }
103
104 return sb.ToString();
105 }
106
Dump(const std::string & prefix)107 std::string ASTEnumExpr::Dump(const std::string &prefix)
108 {
109 StringBuilder sb;
110 sb.Append(prefix);
111 if (isParenExpr) {
112 sb.Append("(");
113 }
114
115 sb.AppendFormat("%s", value_.c_str());
116
117 if (isParenExpr) {
118 sb.Append(")");
119 }
120
121 return sb.ToString();
122 }
123 } // namespace HDI
124 } // namespace OHOS