Skip to content

Commit 1ea91fa

Browse files
committed
parser optimization Three
1 parent 745e0c0 commit 1ea91fa

3 files changed

Lines changed: 68 additions & 84 deletions

File tree

include/chaiscript/language/chaiscript_algebraic.hpp

Lines changed: 35 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#ifndef CHAISCRIPT_ALGEBRAIC_HPP_
88
#define CHAISCRIPT_ALGEBRAIC_HPP_
99

10+
#include "../utility/fnv1a.hpp"
11+
1012
#include <string>
1113

1214
namespace chaiscript
@@ -51,76 +53,39 @@ namespace chaiscript
5153

5254
static Opers to_operator(const std::string &t_str, bool t_is_unary = false)
5355
{
54-
if (t_str == "==")
55-
{
56-
return Opers::equals;
57-
} else if (t_str == "<") {
58-
return Opers::less_than;
59-
} else if (t_str == ">") {
60-
return Opers::greater_than;
61-
} else if (t_str == "<=") {
62-
return Opers::less_than_equal;
63-
} else if (t_str == ">=") {
64-
return Opers::greater_than_equal;
65-
} else if (t_str == "!=") {
66-
return Opers::not_equal;
67-
} else if (t_str == "=") {
68-
return Opers::assign;
69-
} else if (t_str == "++") {
70-
return Opers::pre_increment;
71-
} else if (t_str == "--") {
72-
return Opers::pre_decrement;
73-
} else if (t_str == "*=") {
74-
return Opers::assign_product;
75-
} else if (t_str == "+=") {
76-
return Opers::assign_sum;
77-
} else if (t_str == "-=") {
78-
return Opers::assign_difference;
79-
} else if (t_str == "&=") {
80-
return Opers::assign_bitwise_and;
81-
} else if (t_str == "|=") {
82-
return Opers::assign_bitwise_or;
83-
} else if (t_str == "<<=") {
84-
return Opers::assign_shift_left;
85-
} else if (t_str == ">>=") {
86-
return Opers::assign_shift_right;
87-
} else if (t_str == "%=") {
88-
return Opers::assign_remainder;
89-
} else if (t_str == "^=") {
90-
return Opers::assign_bitwise_xor;
91-
} else if (t_str == "<<") {
92-
return Opers::shift_left;
93-
} else if (t_str == ">>") {
94-
return Opers::shift_right;
95-
} else if (t_str == "%") {
96-
return Opers::remainder;
97-
} else if (t_str == "&") {
98-
return Opers::bitwise_and;
99-
} else if (t_str == "|") {
100-
return Opers::bitwise_or;
101-
} else if (t_str == "^") {
102-
return Opers::bitwise_xor;
103-
} else if (t_str == "~") {
104-
return Opers::bitwise_complement;
105-
} else if (t_str == "+") {
106-
if (t_is_unary) {
107-
return Opers::unary_plus;
108-
} else {
109-
return Opers::sum;
110-
}
111-
} else if (t_str == "-") {
112-
if (t_is_unary) {
113-
return Opers::unary_minus;
114-
} else {
115-
return Opers::difference;
116-
}
117-
} else if (t_str == "/") {
118-
return Opers::quotient;
119-
} else if (t_str == "*") {
120-
return Opers::product;
121-
} else {
122-
return Opers::invalid;
123-
}
56+
const auto op_hash = utility::fnv1a_32(t_str.c_str());
57+
switch (op_hash) {
58+
case utility::fnv1a_32("=="): { return Opers::equals; }
59+
case utility::fnv1a_32("<"): { return Opers::less_than; }
60+
case utility::fnv1a_32(">"): { return Opers::greater_than; }
61+
case utility::fnv1a_32("<="): { return Opers::less_than_equal; }
62+
case utility::fnv1a_32(">="): { return Opers::greater_than_equal; }
63+
case utility::fnv1a_32("!="): { return Opers::not_equal; }
64+
case utility::fnv1a_32("="): { return Opers::assign; }
65+
case utility::fnv1a_32("++"): { return Opers::pre_increment; }
66+
case utility::fnv1a_32("--"): { return Opers::pre_decrement; }
67+
case utility::fnv1a_32("*="): { return Opers::assign_product; }
68+
case utility::fnv1a_32("+="): { return Opers::assign_sum; }
69+
case utility::fnv1a_32("-="): { return Opers::assign_difference; }
70+
case utility::fnv1a_32("&="): { return Opers::assign_bitwise_and; }
71+
case utility::fnv1a_32("|="): { return Opers::assign_bitwise_or; }
72+
case utility::fnv1a_32("<<="): { return Opers::assign_shift_left; }
73+
case utility::fnv1a_32(">>="): { return Opers::assign_shift_right; }
74+
case utility::fnv1a_32("%="): { return Opers::assign_remainder; }
75+
case utility::fnv1a_32("^="): { return Opers::assign_bitwise_xor; }
76+
case utility::fnv1a_32("<<"): { return Opers::shift_left; }
77+
case utility::fnv1a_32(">>"): { return Opers::shift_right; }
78+
case utility::fnv1a_32("%"): { return Opers::remainder; }
79+
case utility::fnv1a_32("&"): { return Opers::bitwise_and; }
80+
case utility::fnv1a_32("|"): { return Opers::bitwise_or; }
81+
case utility::fnv1a_32("^"): { return Opers::bitwise_xor; }
82+
case utility::fnv1a_32("~"): { return Opers::bitwise_complement; }
83+
case utility::fnv1a_32("+"): { return t_is_unary ? Opers::unary_plus : Opers::sum; }
84+
case utility::fnv1a_32("-"): { return t_is_unary ? Opers::unary_minus : Opers::difference; }
85+
case utility::fnv1a_32("/"): { return Opers::quotient; }
86+
case utility::fnv1a_32("*"): { return Opers::product; }
87+
default: { return Opers::invalid; }
88+
}
12489
}
12590

12691
};

include/chaiscript/language/chaiscript_parser.hpp

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "chaiscript_common.hpp"
2424
#include "chaiscript_optimizer.hpp"
2525
#include "chaiscript_tracer.hpp"
26+
#include "../utility/fnv1a.hpp"
2627

2728
#if defined(CHAISCRIPT_UTF16_UTF32)
2829
#include <locale>
@@ -853,36 +854,36 @@ namespace chaiscript
853854
if (Id_()) {
854855

855856
auto text = Position::str(start, m_position);
856-
const auto text_hash = fnv1a_32(text.c_str());
857+
const auto text_hash = utility::fnv1a_32(text.c_str());
857858

858859
if (validate) {
859860
validate_object_name(text);
860861
}
861862

862863
switch (text_hash) {
863-
case fnv1a_32("true"): {
864+
case utility::fnv1a_32("true"): {
864865
m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(std::move(text), start.line, start.col, const_var(true)));
865866
} break;
866-
case fnv1a_32("false"): {
867+
case utility::fnv1a_32("false"): {
867868
m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(std::move(text), start.line, start.col, const_var(false)));
868869
} break;
869-
case fnv1a_32("Infinity"): {
870+
case utility::fnv1a_32("Infinity"): {
870871
m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(std::move(text), start.line, start.col,
871872
const_var(std::numeric_limits<double>::infinity())));
872873
} break;
873-
case fnv1a_32("NaN"): {
874+
case utility::fnv1a_32("NaN"): {
874875
m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(std::move(text), start.line, start.col,
875876
const_var(std::numeric_limits<double>::quiet_NaN())));
876877
} break;
877-
case fnv1a_32("__LINE__"): {
878+
case utility::fnv1a_32("__LINE__"): {
878879
m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(std::move(text), start.line, start.col,
879880
const_var(start.line)));
880881
} break;
881-
case fnv1a_32("__FILE__"): {
882+
case utility::fnv1a_32("__FILE__"): {
882883
m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(std::move(text), start.line, start.col,
883884
const_var(m_filename)));
884885
} break;
885-
case fnv1a_32("__FUNC__"): {
886+
case utility::fnv1a_32("__FUNC__"): {
886887
std::string fun_name = "NOT_IN_FUNCTION";
887888
for (size_t idx = m_match_stack.size() - 1; idx > 0; --idx)
888889
{
@@ -895,7 +896,7 @@ namespace chaiscript
895896
m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(std::move(text), start.line, start.col,
896897
const_var(fun_name)));
897898
} break;
898-
case fnv1a_32("__CLASS__"): {
899+
case utility::fnv1a_32("__CLASS__"): {
899900
std::string fun_name = "NOT_IN_CLASS";
900901
for (size_t idx = m_match_stack.size() - 1; idx > 1; --idx)
901902
{
@@ -909,7 +910,7 @@ namespace chaiscript
909910
m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(std::move(text), start.line, start.col,
910911
const_var(fun_name)));
911912
} break;
912-
case fnv1a_32("_"): {
913+
case utility::fnv1a_32("_"): {
913914
m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(std::move(text), start.line, start.col,
914915
Boxed_Value(std::make_shared<dispatch::Placeholder_Object>())));
915916
} break;
@@ -2525,10 +2526,6 @@ namespace chaiscript
25252526

25262527
return m_match_stack.front();
25272528
}
2528-
private:
2529-
static constexpr std::uint32_t fnv1a_32(const char *s, std::uint32_t h = 0x811c9dc5) {
2530-
return (*s == 0) ? h : fnv1a_32(s+1, ((h ^ (*s)) * 0x01000193));
2531-
}
25322529
};
25332530
template<typename Tracer, typename Optimizer>
25342531
constexpr const char ChaiScript_Parser<Tracer, Optimizer>::m_multiline_comment_begin[];
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// This file is distributed under the BSD License.
2+
// See "license.txt" for details.
3+
// Copyright 2009-2012, Jonathan Turner ([email protected])
4+
// Copyright 2009-2016, Jason Turner ([email protected])
5+
// http://www.chaiscript.com
6+
7+
#ifndef CHAISCRIPT_UTILITY_FNV1A_HPP_
8+
#define CHAISCRIPT_UTILITY_FNV1A_HPP_
9+
10+
#include <cstdint>
11+
12+
namespace chaiscript
13+
{
14+
namespace utility
15+
{
16+
static constexpr std::uint32_t fnv1a_32(const char *s, std::uint32_t h = 0x811c9dc5) {
17+
return (*s == 0) ? h : fnv1a_32(s+1, ((h ^ (*s)) * 0x01000193));
18+
}
19+
}
20+
}
21+
22+
#endif

0 commit comments

Comments
 (0)