1
2
3
4
5
6 #include <sngcpp/ast/Literal.hpp>
7 #include <sngcpp/ast/Visitor.hpp>
8 #include <sngcpp/ast/Writer.hpp>
9 #include <sngcpp/ast/Reader.hpp>
10
11 namespace sngcpp { namespace ast {
12
13 std::u32string ToString(Suffix suffix)
14 {
15 std::u32string s;
16 if ((suffix & Suffix::u) != Suffix::none)
17 {
18 s.append(U"u");
19 }
20 if ((suffix & Suffix::f) != Suffix::none)
21 {
22 s.append(U"f");
23 }
24 if ((suffix & Suffix::l) != Suffix::none)
25 {
26 s.append(U"l");
27 }
28 if ((suffix & Suffix::ll) != Suffix::none)
29 {
30 s.append(U"ll");
31 }
32 return s;
33 }
34
35 LiteralNode::LiteralNode(NodeType nodeType_) : Node(nodeType_)
36 {
37 }
38
39 LiteralNode::LiteralNode(NodeType nodeType_, const Span& span_, const std::u32string& rep_) : Node(nodeType_, span_), rep(rep_)
40 {
41 }
42
43 void LiteralNode::Write(Writer& writer)
44 {
45 Node::Write(writer);
46 writer.GetBinaryWriter().Write(rep);
47 }
48
49 void LiteralNode::Read(Reader& reader)
50 {
51 Node::Read(reader);
52 rep = reader.GetBinaryReader().ReadUtf32String();
53 }
54
55 FloatingLiteralNode::FloatingLiteralNode() : LiteralNode(NodeType::floatingLiteralNode)
56 {
57 }
58
59 FloatingLiteralNode::FloatingLiteralNode(const Span& span_, double value_, Suffix suffix_, const std::u32string& rep_) : LiteralNode(NodeType::floatingLiteralNode, span_, rep_), value(value_), suffix(suffix_)
60 {
61 }
62
63 void FloatingLiteralNode::Accept(Visitor& visitor)
64 {
65 visitor.Visit(*this);
66 }
67
68 void FloatingLiteralNode::Write(Writer& writer)
69 {
70 LiteralNode::Write(writer);
71 writer.GetBinaryWriter().Write(value);
72 writer.Write(suffix);
73 }
74
75 void FloatingLiteralNode::Read(Reader& reader)
76 {
77 LiteralNode::Read(reader);
78 value = reader.GetBinaryReader().ReadDouble();
79 suffix = reader.ReadSuffix();
80 }
81
82 IntegerLiteralNode::IntegerLiteralNode() : LiteralNode(NodeType::integerLiteralNode)
83 {
84 }
85
86 IntegerLiteralNode::IntegerLiteralNode(const Span& span_, uint64_t value_, Suffix suffix_, Base base_, const std::u32string& rep_) :
87 LiteralNode(NodeType::integerLiteralNode, span_, rep_), value(value_), suffix(suffix_), base(base_)
88 {
89 }
90
91 void IntegerLiteralNode::Accept(Visitor& visitor)
92 {
93 visitor.Visit(*this);
94 }
95
96 void IntegerLiteralNode::Write(Writer& writer)
97 {
98 LiteralNode::Write(writer);
99 writer.GetBinaryWriter().Write(value);
100 writer.Write(suffix);
101 writer.Write(base);
102 }
103
104 void IntegerLiteralNode::Read(Reader& reader)
105 {
106 LiteralNode::Read(reader);
107 value = reader.GetBinaryReader().ReadULong();
108 suffix = reader.ReadSuffix();
109 base = reader.ReadBase();
110 }
111
112 CharacterLiteralNode::CharacterLiteralNode() : LiteralNode(NodeType::characterLiteralNode)
113 {
114 }
115
116 CharacterLiteralNode::CharacterLiteralNode(const Span& span_, char32_t prefix_, char32_t chr_, const std::u32string& rep_) :
117 LiteralNode(NodeType::characterLiteralNode, span_, rep_), prefix(prefix_), chr(chr_)
118 {
119 }
120
121 void CharacterLiteralNode::Accept(Visitor& visitor)
122 {
123 visitor.Visit(*this);
124 }
125
126 void CharacterLiteralNode::Write(Writer& writer)
127 {
128 LiteralNode::Write(writer);
129 writer.GetBinaryWriter().Write(prefix);
130 writer.GetBinaryWriter().Write(chr);
131 }
132
133 void CharacterLiteralNode::Read(Reader& reader)
134 {
135 LiteralNode::Read(reader);
136 prefix = reader.GetBinaryReader().ReadUChar();
137 chr = reader.GetBinaryReader().ReadUChar();
138 }
139
140 StringLiteralNode::StringLiteralNode() : LiteralNode(NodeType::stringLiteralNode)
141 {
142 }
143
144 StringLiteralNode::StringLiteralNode(const Span& span_, const std::u32string& encodingPrefix_, const std::u32string& chars_, const std::u32string& rep_) :
145 LiteralNode(NodeType::stringLiteralNode, span_, rep_), encodingPrefix(encodingPrefix_), chars(chars_)
146 {
147 }
148
149 void StringLiteralNode::Accept(Visitor& visitor)
150 {
151 visitor.Visit(*this);
152 }
153
154 void StringLiteralNode::Write(Writer& writer)
155 {
156 LiteralNode::Write(writer);
157 writer.GetBinaryWriter().Write(encodingPrefix);
158 writer.GetBinaryWriter().Write(chars);
159 }
160
161 void StringLiteralNode::Read(Reader& reader)
162 {
163 LiteralNode::Read(reader);
164 encodingPrefix = reader.GetBinaryReader().ReadUtf32String();
165 chars = reader.GetBinaryReader().ReadUtf32String();
166 }
167
168 BooleanLiteralNode::BooleanLiteralNode() : LiteralNode(NodeType::booleanLiteralNode)
169 {
170 }
171
172 BooleanLiteralNode::BooleanLiteralNode(const Span& span_, bool value_, const std::u32string& rep_) : LiteralNode(NodeType::booleanLiteralNode, span_, rep_), value(value_)
173 {
174 }
175
176 void BooleanLiteralNode::Accept(Visitor& visitor)
177 {
178 visitor.Visit(*this);
179 }
180
181 void BooleanLiteralNode::Write(Writer& writer)
182 {
183 LiteralNode::Write(writer);
184 writer.GetBinaryWriter().Write(value);
185 }
186
187 void BooleanLiteralNode::Read(Reader& reader)
188 {
189 LiteralNode::Read(reader);
190 value = reader.GetBinaryReader().ReadBool();
191 }
192
193 NullPtrLiteralNode::NullPtrLiteralNode() : LiteralNode(NodeType::nullPtrLiteralNode)
194 {
195 }
196
197 NullPtrLiteralNode::NullPtrLiteralNode(const Span& span_, const std::u32string& rep_) : LiteralNode(NodeType::nullPtrLiteralNode, span_, rep_)
198 {
199 }
200
201 void NullPtrLiteralNode::Accept(Visitor& visitor)
202 {
203 visitor.Visit(*this);
204 }
205
206 } }