Skip to content

Commit

Permalink
slight optimizations (x2 parsing speed)
Browse files Browse the repository at this point in the history
  • Loading branch information
Prevter committed Aug 8, 2024
1 parent b262115 commit df91184
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 36 deletions.
12 changes: 6 additions & 6 deletions include/rift.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ namespace rift {
/// @param name The name of the variable.
/// @param value The value of the variable.
template<typename T>
void setVariable(const std::string& name, T value) {
void setVariable(std::string_view name, T value) {
m_variables.insert_or_assign(name, Value::from(value));
}

/// @brief Set a variable in the script.
/// @param name The name of the variable.
/// @param value The value of the variable.
void setVariable(const std::string& name, const Value& value) {
m_variables.insert_or_assign(name, value);
void setVariable(std::string_view name, const Value& value) {
m_variables.insert_or_assign(std::string(name), value);
}

/// @brief Run the script with the specified variables.
Expand All @@ -42,16 +42,16 @@ namespace rift {
std::unordered_map<std::string, Value> m_variables;

friend class Visitor;
friend Result<Script*> compile(const std::string& script);
friend Result<Script*> compile(std::string_view script);
};

/// @brief Compile a script.
/// @param script The script to compile.
Result<Script*> compile(const std::string& script);
Result<Script*> compile(std::string_view script);

/// @brief Compile and run a script.
/// @param script The script to run.
/// @param variables The variables to use in the script.
std::string format(const std::string& script, const std::unordered_map<std::string, Value>& variables = {});
std::string format(std::string_view script, const std::unordered_map<std::string, Value>& variables = {});

}
6 changes: 3 additions & 3 deletions include/rift/lexer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace rift {
public:
/// @brief Construct a lexer.
/// @param script The script to lex.
explicit Lexer(std::string script);
explicit Lexer(std::string_view script);

/// @brief Get the next token.
/// @return The next token.
Expand Down Expand Up @@ -38,10 +38,10 @@ namespace rift {
Token parseIdentifier();

/// @brief Create a token with current index.
Token createToken(TokenType type, std::string value) const;
[[nodiscard]] Token createToken(TokenType type, std::string value) const;

private:
std::string m_script;
std::string_view m_script;
int32_t m_index = 0;
int32_t m_startIndex = 0;
size_t m_expressionDepth = 0;
Expand Down
4 changes: 2 additions & 2 deletions include/rift/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace rift {
public:
/// @brief Construct the parser.
/// @param tokens The tokens to parse.
explicit Parser(Lexer lexer) : m_lexer(std::move(lexer)) {}
explicit Parser(Lexer lexer) : m_lexer(lexer) {}

/// @brief Parse the tokens into an AST.
/// @return Result object containing the root node.
Expand All @@ -67,7 +67,7 @@ namespace rift {
Result<Node*> parseCall();
Result<Node*> parseAtom();

[[nodiscard]] std::string getErrorMessage(const std::string& message) const;
[[nodiscard]] std::string getErrorMessage(std::string_view message) const;

private:
Lexer m_lexer;
Expand Down
8 changes: 4 additions & 4 deletions include/rift/visitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,19 @@ namespace rift {

/// @brief Write a string to the output.
/// @param text The text to write.
void write(const std::string& text);
void write(std::string_view text);

/// @brief Get the output.
/// @return The output.
[[nodiscard]] std::string getOutput() const;
[[nodiscard]] const std::string& getOutput() const;

/// @brief Evaluate the script.
/// @return The result of the script.
[[nodiscard]] std::string evaluate();
[[nodiscard]] const std::string& evaluate();

private:
Script* m_script;
std::stringstream m_output;
std::string m_output;
const std::unordered_map<std::string, Value>* m_variables;
};

Expand Down
14 changes: 7 additions & 7 deletions src/lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

namespace rift {

Lexer::Lexer(std::string script)
: m_script(std::move(script)) {}
Lexer::Lexer(std::string_view script)
: m_script(script) {}

inline bool isWhitespace(char c) {
return c == ' ' || c == '\t' || c == '\n' || c == '\r';
Expand Down Expand Up @@ -166,7 +166,7 @@ namespace rift {
}
}

return createToken(TokenType::Segment, m_script.substr(m_startIndex, m_index - m_startIndex));
return createToken(TokenType::Segment, std::string(m_script.substr(m_startIndex, m_index - m_startIndex)));
}
}
}
Expand All @@ -186,10 +186,10 @@ namespace rift {
}

if (hasDot) {
return createToken(TokenType::FLOAT, m_script.substr(start, m_index - start));
return createToken(TokenType::FLOAT, std::string(m_script.substr(start, m_index - start)));
}

return createToken(TokenType::INTEGER, m_script.substr(start, m_index - start));
return createToken(TokenType::INTEGER, std::string(m_script.substr(start, m_index - start)));
}

Token Lexer::parseString() {
Expand All @@ -203,15 +203,15 @@ namespace rift {
}

advance(); // Consume the closing quote.
return createToken(TokenType::STRING, m_script.substr(start, m_index - start));
return createToken(TokenType::STRING, std::string(m_script.substr(start, m_index - start)));
}

Token Lexer::parseIdentifier() {
size_t start = m_index - 1;

while (isAlphaNumeric(peek()) || peek() == '_') advance();

return createToken(TokenType::IDENTIFIER, m_script.substr(start, m_index - start));
return createToken(TokenType::IDENTIFIER, std::string(m_script.substr(start, m_index - start)));
}

}
2 changes: 1 addition & 1 deletion src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace rift {

std::string Parser::getErrorMessage(const std::string& message) const {
std::string Parser::getErrorMessage(std::string_view message) const {
std::stringstream ss;
ss << message << " at index " << m_index;
return ss.str();
Expand Down
4 changes: 2 additions & 2 deletions src/rift.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace rift {
return visitor.evaluate();
}

Result<Script*> compile(const std::string& script) {
Result<Script*> compile(std::string_view script) {
Lexer lexer(script);
Parser parser(lexer);

Expand All @@ -32,7 +32,7 @@ namespace rift {
return Result<Script*>::success(s);
}

std::string format(const std::string& script, const std::unordered_map<std::string, Value>& variables) {
std::string format(std::string_view script, const std::unordered_map<std::string, Value>& variables) {
auto res = compile(script);
if (!res) return res.getMessage();

Expand Down
12 changes: 6 additions & 6 deletions src/value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,19 @@ namespace rift {
Value Value::operator*(const Value& other) const {
// If first value is a string, repeat it n times where n is the second value.
if (m_type == Type::String && (other.m_type == Type::Integer || other.m_type == Type::Float)) {
std::stringstream result;
std::string result;
auto count = other.m_type == Type::Integer ? other.m_integer : static_cast<int>(other.m_float);
for (int i = 0; i < count; ++i) {
result << m_string;
result += m_string;
}
return Value::string(result.str());
return Value::string(std::move(result));
} else if (other.m_type == Type::String && (m_type == Type::Integer || m_type == Type::Float)) {
std::stringstream result;
std::string result;
auto count = m_type == Type::Integer ? m_integer : static_cast<int>(m_float);
for (int i = 0; i < count; ++i) {
result << other.m_string;
result += other.m_string;
}
return Value::string(result.str());
return Value::string(std::move(result));
} else if (m_type == Type::String || other.m_type == Type::String) {
return Value::string("<error: multiplication of strings>");
}
Expand Down
10 changes: 5 additions & 5 deletions src/visitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

namespace rift {

void Visitor::write(const std::string& text) {
m_output << text;
void Visitor::write(std::string_view text) {
m_output += text;
}

Value Visitor::getVariable(const std::string& name) const {
Expand All @@ -24,8 +24,8 @@ namespace rift {
}
}

std::string Visitor::getOutput() const {
return m_output.str();
const std::string& Visitor::getOutput() const {
return m_output;
}

void Visitor::visit(Node* node) {
Expand Down Expand Up @@ -75,7 +75,7 @@ namespace rift {
write(value.toString());
}

std::string Visitor::evaluate() {
const std::string& Visitor::evaluate() {
for (auto& node : m_script->m_nodes) {
node->accept(this);
}
Expand Down

0 comments on commit df91184

Please sign in to comment.