1/*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include "ast/ast_expr.h"
17#include "util/string_builder.h"
18
19namespace OHOS {
20namespace Idl {
21std::string ASTUnaryExpr::Dump(const std::string &prefix)
22{
23    StringBuilder sb;
24    sb.Append(prefix);
25    if (isParenExpr) {
26        sb.Append("(");
27    }
28
29    sb.AppendFormat("%s%s", UnaryOpToString(op_).c_str(), expr_->Dump("").c_str());
30
31    if (isParenExpr) {
32        sb.Append(")");
33    }
34
35    return sb.ToString();
36}
37
38std::string ASTUnaryExpr::UnaryOpToString(UnaryOpKind op) const
39{
40    switch (op) {
41        case UnaryOpKind::PLUS:
42            return "+";
43        case UnaryOpKind::MINUS:
44            return "-";
45        case UnaryOpKind::TILDE:
46            return "~";
47        default:
48            return "unknown";
49    }
50}
51
52std::string ASTBinaryExpr::Dump(const std::string &prefix)
53{
54    StringBuilder sb;
55    sb.Append(prefix);
56    if (isParenExpr) {
57        sb.Append("(");
58    }
59
60    sb.AppendFormat("%s %s %s", lExpr_->Dump("").c_str(), BinaryOpToString(op_).c_str(), rExpr_->Dump("").c_str());
61
62    if (isParenExpr) {
63        sb.Append(")");
64    }
65
66    return sb.ToString();
67}
68
69std::string ASTBinaryExpr::BinaryOpToString(BinaryOpKind op) const
70{
71    switch (op) {
72        case BinaryOpKind::MUL:
73            return "*";
74        case BinaryOpKind::DIV:
75            return "/";
76        case BinaryOpKind::MOD:
77            return "%";
78        case BinaryOpKind::ADD:
79            return "+";
80        case BinaryOpKind::SUB:
81            return "-";
82        case BinaryOpKind::LSHIFT:
83            return "<<";
84        case BinaryOpKind::RSHIFT:
85            return ">>";
86        case BinaryOpKind::AND:
87            return "&";
88        case BinaryOpKind::XOR:
89            return "^";
90        case BinaryOpKind::OR:
91            return "|";
92        default:
93            return "unknown";
94    }
95}
96
97std::string ASTNumExpr::Dump(const std::string &prefix)
98{
99    StringBuilder sb;
100    sb.Append(prefix);
101    if (isParenExpr) {
102        sb.Append("(");
103    }
104
105    sb.AppendFormat("%s", value_.c_str());
106
107    if (isParenExpr) {
108        sb.Append(")");
109    }
110
111    return sb.ToString();
112}
113
114std::string ASTEnumExpr::Dump(const std::string &prefix)
115{
116    StringBuilder sb;
117    sb.Append(prefix);
118    if (isParenExpr) {
119        sb.Append("(");
120    }
121
122    sb.AppendFormat("%s", value_.c_str());
123
124    if (isParenExpr) {
125        sb.Append(")");
126    }
127
128    return sb.ToString();
129}
130} // namespace Idl
131} // namespace OHOS