diff --git a/include/chaiscript/language/chaiscript_algebraic.hpp b/include/chaiscript/language/chaiscript_algebraic.hpp index 0338a02..36516dd 100644 --- a/include/chaiscript/language/chaiscript_algebraic.hpp +++ b/include/chaiscript/language/chaiscript_algebraic.hpp @@ -7,6 +7,8 @@ #ifndef CHAISCRIPT_ALGEBRAIC_HPP_ #define CHAISCRIPT_ALGEBRAIC_HPP_ +#include "../utility/fnv1a.hpp" + #include namespace chaiscript @@ -51,76 +53,39 @@ namespace chaiscript static Opers to_operator(const std::string &t_str, bool t_is_unary = false) { - if (t_str == "==") - { - return Opers::equals; - } else if (t_str == "<") { - return Opers::less_than; - } else if (t_str == ">") { - return Opers::greater_than; - } else if (t_str == "<=") { - return Opers::less_than_equal; - } else if (t_str == ">=") { - return Opers::greater_than_equal; - } else if (t_str == "!=") { - return Opers::not_equal; - } else if (t_str == "=") { - return Opers::assign; - } else if (t_str == "++") { - return Opers::pre_increment; - } else if (t_str == "--") { - return Opers::pre_decrement; - } else if (t_str == "*=") { - return Opers::assign_product; - } else if (t_str == "+=") { - return Opers::assign_sum; - } else if (t_str == "-=") { - return Opers::assign_difference; - } else if (t_str == "&=") { - return Opers::assign_bitwise_and; - } else if (t_str == "|=") { - return Opers::assign_bitwise_or; - } else if (t_str == "<<=") { - return Opers::assign_shift_left; - } else if (t_str == ">>=") { - return Opers::assign_shift_right; - } else if (t_str == "%=") { - return Opers::assign_remainder; - } else if (t_str == "^=") { - return Opers::assign_bitwise_xor; - } else if (t_str == "<<") { - return Opers::shift_left; - } else if (t_str == ">>") { - return Opers::shift_right; - } else if (t_str == "%") { - return Opers::remainder; - } else if (t_str == "&") { - return Opers::bitwise_and; - } else if (t_str == "|") { - return Opers::bitwise_or; - } else if (t_str == "^") { - return Opers::bitwise_xor; - } else if (t_str == "~") { - return Opers::bitwise_complement; - } else if (t_str == "+") { - if (t_is_unary) { - return Opers::unary_plus; - } else { - return Opers::sum; - } - } else if (t_str == "-") { - if (t_is_unary) { - return Opers::unary_minus; - } else { - return Opers::difference; - } - } else if (t_str == "/") { - return Opers::quotient; - } else if (t_str == "*") { - return Opers::product; - } else { - return Opers::invalid; - } + const auto op_hash = utility::fnv1a_32(t_str.c_str()); + switch (op_hash) { + case utility::fnv1a_32("=="): { return Opers::equals; } + case utility::fnv1a_32("<"): { return Opers::less_than; } + case utility::fnv1a_32(">"): { return Opers::greater_than; } + case utility::fnv1a_32("<="): { return Opers::less_than_equal; } + case utility::fnv1a_32(">="): { return Opers::greater_than_equal; } + case utility::fnv1a_32("!="): { return Opers::not_equal; } + case utility::fnv1a_32("="): { return Opers::assign; } + case utility::fnv1a_32("++"): { return Opers::pre_increment; } + case utility::fnv1a_32("--"): { return Opers::pre_decrement; } + case utility::fnv1a_32("*="): { return Opers::assign_product; } + case utility::fnv1a_32("+="): { return Opers::assign_sum; } + case utility::fnv1a_32("-="): { return Opers::assign_difference; } + case utility::fnv1a_32("&="): { return Opers::assign_bitwise_and; } + case utility::fnv1a_32("|="): { return Opers::assign_bitwise_or; } + case utility::fnv1a_32("<<="): { return Opers::assign_shift_left; } + case utility::fnv1a_32(">>="): { return Opers::assign_shift_right; } + case utility::fnv1a_32("%="): { return Opers::assign_remainder; } + case utility::fnv1a_32("^="): { return Opers::assign_bitwise_xor; } + case utility::fnv1a_32("<<"): { return Opers::shift_left; } + case utility::fnv1a_32(">>"): { return Opers::shift_right; } + case utility::fnv1a_32("%"): { return Opers::remainder; } + case utility::fnv1a_32("&"): { return Opers::bitwise_and; } + case utility::fnv1a_32("|"): { return Opers::bitwise_or; } + case utility::fnv1a_32("^"): { return Opers::bitwise_xor; } + case utility::fnv1a_32("~"): { return Opers::bitwise_complement; } + case utility::fnv1a_32("+"): { return t_is_unary ? Opers::unary_plus : Opers::sum; } + case utility::fnv1a_32("-"): { return t_is_unary ? Opers::unary_minus : Opers::difference; } + case utility::fnv1a_32("/"): { return Opers::quotient; } + case utility::fnv1a_32("*"): { return Opers::product; } + default: { return Opers::invalid; } + } } }; diff --git a/include/chaiscript/language/chaiscript_parser.hpp b/include/chaiscript/language/chaiscript_parser.hpp index 8643094..e693804 100644 --- a/include/chaiscript/language/chaiscript_parser.hpp +++ b/include/chaiscript/language/chaiscript_parser.hpp @@ -23,6 +23,7 @@ #include "chaiscript_common.hpp" #include "chaiscript_optimizer.hpp" #include "chaiscript_tracer.hpp" +#include "../utility/fnv1a.hpp" #if defined(CHAISCRIPT_UTF16_UTF32) #include @@ -853,36 +854,36 @@ namespace chaiscript if (Id_()) { auto text = Position::str(start, m_position); - const auto text_hash = fnv1a_32(text.c_str()); + const auto text_hash = utility::fnv1a_32(text.c_str()); if (validate) { validate_object_name(text); } switch (text_hash) { - case fnv1a_32("true"): { + case utility::fnv1a_32("true"): { m_match_stack.push_back(make_node>(std::move(text), start.line, start.col, const_var(true))); } break; - case fnv1a_32("false"): { + case utility::fnv1a_32("false"): { m_match_stack.push_back(make_node>(std::move(text), start.line, start.col, const_var(false))); } break; - case fnv1a_32("Infinity"): { + case utility::fnv1a_32("Infinity"): { m_match_stack.push_back(make_node>(std::move(text), start.line, start.col, const_var(std::numeric_limits::infinity()))); } break; - case fnv1a_32("NaN"): { + case utility::fnv1a_32("NaN"): { m_match_stack.push_back(make_node>(std::move(text), start.line, start.col, const_var(std::numeric_limits::quiet_NaN()))); } break; - case fnv1a_32("__LINE__"): { + case utility::fnv1a_32("__LINE__"): { m_match_stack.push_back(make_node>(std::move(text), start.line, start.col, const_var(start.line))); } break; - case fnv1a_32("__FILE__"): { + case utility::fnv1a_32("__FILE__"): { m_match_stack.push_back(make_node>(std::move(text), start.line, start.col, const_var(m_filename))); } break; - case fnv1a_32("__FUNC__"): { + case utility::fnv1a_32("__FUNC__"): { std::string fun_name = "NOT_IN_FUNCTION"; for (size_t idx = m_match_stack.size() - 1; idx > 0; --idx) { @@ -895,7 +896,7 @@ namespace chaiscript m_match_stack.push_back(make_node>(std::move(text), start.line, start.col, const_var(fun_name))); } break; - case fnv1a_32("__CLASS__"): { + case utility::fnv1a_32("__CLASS__"): { std::string fun_name = "NOT_IN_CLASS"; for (size_t idx = m_match_stack.size() - 1; idx > 1; --idx) { @@ -909,7 +910,7 @@ namespace chaiscript m_match_stack.push_back(make_node>(std::move(text), start.line, start.col, const_var(fun_name))); } break; - case fnv1a_32("_"): { + case utility::fnv1a_32("_"): { m_match_stack.push_back(make_node>(std::move(text), start.line, start.col, Boxed_Value(std::make_shared()))); } break; @@ -2525,10 +2526,6 @@ namespace chaiscript return m_match_stack.front(); } - private: - static constexpr std::uint32_t fnv1a_32(const char *s, std::uint32_t h = 0x811c9dc5) { - return (*s == 0) ? h : fnv1a_32(s+1, ((h ^ (*s)) * 0x01000193)); - } }; template constexpr const char ChaiScript_Parser::m_multiline_comment_begin[]; diff --git a/include/chaiscript/utility/fnv1a.hpp b/include/chaiscript/utility/fnv1a.hpp new file mode 100644 index 0000000..fcf9b1e --- /dev/null +++ b/include/chaiscript/utility/fnv1a.hpp @@ -0,0 +1,22 @@ +// This file is distributed under the BSD License. +// See "license.txt" for details. +// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) +// Copyright 2009-2016, Jason Turner (jason@emptycrate.com) +// http://www.chaiscript.com + +#ifndef CHAISCRIPT_UTILITY_FNV1A_HPP_ +#define CHAISCRIPT_UTILITY_FNV1A_HPP_ + +#include + +namespace chaiscript +{ + namespace utility + { + static constexpr std::uint32_t fnv1a_32(const char *s, std::uint32_t h = 0x811c9dc5) { + return (*s == 0) ? h : fnv1a_32(s+1, ((h ^ (*s)) * 0x01000193)); + } + } +} + +#endif