Skip to content

Commit 4536e7e

Browse files
committed
unfold RootNode for constants
1 parent 40696bf commit 4536e7e

File tree

5 files changed

+41
-3
lines changed

5 files changed

+41
-3
lines changed

include/rift/nodes/segment.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ namespace rift {
2424
return m_value;
2525
}
2626

27+
[[nodiscard]] std::string& value() noexcept {
28+
return m_value;
29+
}
30+
2731
private:
2832
std::string m_value;
2933
};

include/rift/nodes/unary.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ namespace rift {
2323
return *m_value;
2424
}
2525

26+
[[nodiscard]] std::unique_ptr<Node> const& valuePtr() const noexcept {
27+
return m_value;
28+
}
29+
2630
private:
2731
std::unique_ptr<Node> m_value;
2832
TokenType m_op;

include/rift/script.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ namespace rift {
3131
[[nodiscard]] Object& getVariables() noexcept { return m_variables; }
3232
[[nodiscard]] Object const& getVariables() const noexcept { return m_variables; }
3333

34+
[[nodiscard]] std::unique_ptr<Node> const& getRoot() const noexcept {
35+
return m_root;
36+
}
37+
3438
private:
3539
std::unique_ptr<Node> m_root;
3640
Object m_variables;

src/parser.cpp

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,14 @@ namespace rift {
103103
while (m_currentToken) {
104104
switch (m_currentToken.type) {
105105
case TokenType::SEGMENT: {
106-
nodes.push_back(std::make_unique<SegmentNode>(m_currentToken));
106+
// check if previous token is a segment
107+
if (!nodes.empty() && nodes.back()->type() == Node::Type::Segment) {
108+
// append to the last segment node
109+
auto* segNode = static_cast<SegmentNode*>(nodes.back().get());
110+
segNode->value() += m_currentToken.value;
111+
} else {
112+
nodes.push_back(std::make_unique<SegmentNode>(m_currentToken));
113+
}
107114
UNWRAP_ADVANCE()
108115
} break;
109116
case TokenType::LEFT_BRACE: {
@@ -112,7 +119,26 @@ namespace rift {
112119
if (res.isErr()) {
113120
return res;
114121
}
115-
nodes.push_back(std::move(res.unwrap()));
122+
auto resNode = std::move(res.unwrap());
123+
if (resNode->type() == Node::Type::Value) {
124+
auto* valNode = static_cast<ValueNode*>(resNode.get());
125+
// if it's a string, add the value to previous segment node (if any)
126+
if (valNode->value().type() == Value::Type::String) {
127+
if (!nodes.empty() && nodes.back()->type() == Node::Type::Segment) {
128+
// append to the last segment node
129+
auto* segNode = static_cast<SegmentNode*>(nodes.back().get());
130+
segNode->value() += valNode->value().toString();
131+
} else {
132+
// create a new segment node
133+
nodes.push_back(std::make_unique<SegmentNode>(valNode->value().toString(), valNode->fromIndex(), valNode->toIndex()));
134+
}
135+
136+
// consume the right brace
137+
CONSUME_TOKEN(TokenType::RIGHT_BRACE)
138+
break;
139+
}
140+
}
141+
nodes.push_back(std::move(resNode));
116142
CONSUME_TOKEN(TokenType::RIGHT_BRACE);
117143
} break;
118144
default: {

test/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ void RIFT_TEST(std::string_view code, std::string_view expected, rift::Object co
5555
RIFT_EVAL(code, expected, vars, false);
5656
}
5757

58-
std::string myCustomFunc(std::string name) {
58+
std::string myCustomFunc(std::string const& name) {
5959
return fmt::format("Hello, {}!", name);
6060
}
6161

0 commit comments

Comments
 (0)