Skip to content

Commit 696539e

Browse files
committed
improve moves
1 parent 24e2d49 commit 696539e

File tree

4 files changed

+48
-16
lines changed

4 files changed

+48
-16
lines changed

src/parser.cpp

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <rift/parser.hpp>
22

33
#include <stack>
4+
#include <iterator>
45

56
#include <rift/nodes/accessor.hpp>
67
#include <rift/nodes/assign.hpp>
@@ -123,15 +124,31 @@ namespace rift {
123124
auto resNode = std::move(res.unwrap());
124125
if (resNode->type() == Node::Type::Value) {
125126
auto* valNode = static_cast<ValueNode*>(resNode.get());
126-
// if it's a string, add the value to previous segment node (if any)
127-
if (valNode->value().type() == Value::Type::String || valNode->value().type() == Value::Type::Integer || valNode->value().type() == Value::Type::Boolean) {
127+
auto const& val = valNode->value();
128+
if (
129+
val.type() == Value::Type::String ||
130+
val.type() == Value::Type::Integer ||
131+
val.type() == Value::Type::Boolean
132+
) {
133+
auto appendValue = [](std::string& dest, Value const& value) {
134+
if (value.type() == Value::Type::String) {
135+
dest.append(value.getString());
136+
} else {
137+
fmt::format_to(std::back_inserter(dest), "{}", value);
138+
}
139+
};
140+
128141
if (!nodes.empty() && nodes.back()->type() == Node::Type::Segment) {
129-
// append to the last segment node
130142
auto* segNode = static_cast<SegmentNode*>(nodes.back().get());
131-
segNode->value() += valNode->value().toString();
143+
appendValue(segNode->value(), val);
132144
} else {
133-
// create a new segment node
134-
nodes.push_back(std::make_unique<SegmentNode>(valNode->value().toString(), valNode->fromIndex(), valNode->toIndex()));
145+
std::string segmentValue;
146+
if (val.type() == Value::Type::String) {
147+
segmentValue = val.getString();
148+
} else {
149+
segmentValue = fmt::format("{}", val);
150+
}
151+
nodes.push_back(std::make_unique<SegmentNode>(std::move(segmentValue), valNode->fromIndex(), valNode->toIndex()));
135152
}
136153

137154
// consume the right brace

src/rift.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ namespace rift {
88
Parser parser(Lexer(source, directMode), directMode);
99
auto result = parser.parse();
1010
if (result.isErr()) {
11-
return geode::Err(result.unwrapErr());
11+
return geode::Err(std::move(result).unwrapErr());
1212
}
13-
return geode::Ok(std::make_unique<Script>(std::move(result.unwrap())));
13+
return geode::Ok(std::make_unique<Script>(std::move(result).unwrap()));
1414
}
1515

1616
FormatResult format(std::string_view source, Object const& variables) noexcept {
1717
auto compileResult = compile(source, false);
1818
if (compileResult.isErr()) {
19-
return geode::Err(compileResult.unwrapErr());
19+
return geode::Err(std::move(compileResult).unwrapErr());
2020
}
2121

2222
auto runResult = compileResult.unwrap()->run(variables);
@@ -29,13 +29,13 @@ namespace rift {
2929
));
3030
}
3131

32-
return geode::Ok(runResult.unwrap());
32+
return geode::Ok(std::move(runResult).unwrap());
3333
}
3434

3535
EvaluateResult evaluate(std::string_view source, Object const& variables) noexcept {
3636
auto compileResult = compile(source, true);
3737
if (compileResult.isErr()) {
38-
return geode::Err(compileResult.unwrapErr());
38+
return geode::Err(std::move(compileResult).unwrapErr());
3939
}
4040

4141
auto evalResult = compileResult.unwrap()->eval(variables);
@@ -48,7 +48,7 @@ namespace rift {
4848
));
4949
}
5050

51-
return geode::Ok(evalResult.unwrap());
51+
return geode::Ok(std::move(evalResult).unwrap());
5252
}
5353

5454
}

src/script.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace rift {
66
RunResult Script::run(Object const &variables) noexcept {
77
auto result = eval(variables);
88
if (result.isErr()) {
9-
return geode::Err(std::move(result.unwrapErr()));
9+
return geode::Err(std::move(result).unwrapErr());
1010
}
1111
return geode::Ok(result.unwrap().toString());
1212
}
@@ -15,9 +15,9 @@ namespace rift {
1515
Visitor visitor(variables, m_variables);
1616
auto result = visitor.visit(*m_root);
1717
if (result.isErr()) {
18-
return geode::Err(std::move(result.unwrapErr()));
18+
return geode::Err(std::move(result).unwrapErr());
1919
}
20-
return geode::Ok(std::move(result.unwrap()));
20+
return geode::Ok(std::move(result).unwrap());
2121
}
2222

2323
}

src/value.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <fmt/format.h>
55
#include <algorithm>
6+
#include <iterator>
67

78
namespace rift {
89
#ifdef RIFT_INCLUDE_MATJSON
@@ -172,7 +173,21 @@ namespace rift {
172173

173174
// if either value is a string, concatenate them
174175
if (isString() || other.isString()) {
175-
return geode::Ok(Value(toString() + other.toString()));
176+
std::string result;
177+
auto const stringSize = [](Value const& value) {
178+
return value.isString() ? value.getString().size() : size_t{0};
179+
};
180+
result.reserve(stringSize(*this) + stringSize(other));
181+
auto const appendValueText = [](std::string& dest, Value const& value) {
182+
if (value.isString()) {
183+
dest.append(value.getString());
184+
} else {
185+
fmt::format_to(std::back_inserter(dest), "{}", value);
186+
}
187+
};
188+
appendValueText(result, *this);
189+
appendValueText(result, other);
190+
return geode::Ok(Value(std::move(result)));
176191
}
177192

178193
// if either value is a float, convert both to floats and add

0 commit comments

Comments
 (0)